Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Understanding 1MB per token calculation #25

Open
hlamba-dm opened this issue Sep 21, 2023 · 2 comments
Open

Understanding 1MB per token calculation #25

hlamba-dm opened this issue Sep 21, 2023 · 2 comments

Comments

@hlamba-dm
Copy link

I am finding the 1MB GPU ram usage per token while inferencing calculation a bit hard to understand --- also not what I am seeing in practice.

Any insights on how this number was computed ?

@zhuangxy
Copy link

zhuangxy commented Oct 25, 2023

I think this is a very rough estimation , the actual value should depend on batch size, token length and the embedding size(or hidden layer dimension).
For example a 13B model, has 40 layers and the token length is 4096 , embedding size is 8192, if using batchsize 1, it needs 1 (batchsize) * 8192 (embedding size) * 2 (byets, FP16) * 4096 (token length) * 40 (layer) ~= 2560M, about 0.625 M per token

@ftdzh1994
Copy link

ftdzh1994 commented Dec 17, 2024

I think the number here is about memory for KV Cache instead of the memory for activation.
the total number of parameters needed to stored in KV Cache is:
$N = 2 * B * L * l * n_{head} * dim_{head} $

e.g. with llama-13B, which has 40 layers, 5120 hidden size, 40 attention heads(and hence 128 head dim) and loaded with FP16, inferencing one batch size and one token(L = 1, B = 1), the consumed memory is:
$M = (2 * 1 * 1 * 40 * 40 * 128) * 2 bytes= 0.8M \approx 1M$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants