-
Notifications
You must be signed in to change notification settings - Fork 358
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
Reduce the memory usage of logits from O(context_length) to O(1) #4688
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/4688
Note: Links to docs will display an error until the docs builds have been completed. ❌ 1 Cancelled JobAs of commit be438eb with merge base d7c069f (): CANCELLED JOB - The following job was cancelled. Please retry:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
277310e
to
1e0ff89
Compare
1e0ff89
to
b5bc307
Compare
@iseeyuan has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
Summary: The logits size is big, with size [context_length x vocab_size]. But we always use the last (new) logits, because the model generates one new token in each Transformer inference. This PR changes the transformer to return the logits of the last token only. In the runner code, we don't have to fetch the logits for the last token specifically, but directly use the output . Test command: ``` python -m examples.models.llama2.export_llama --checkpoint /Users/myuan/data/llama/story110m/checkpoint.pt --params /Users/myuan/data/llama/story110m/params.json -kv --use_sdpa_with_kv_cache -X -qmode 8da4w --group_size 128 -d fp32 --max_seq_length 1024 --profile_memory ``` Before: 284 MB activation, with 262 MB on logits After: 162 MB activation, with 0.128 MB on logits Verified with llamma_runner, before and after it generates the same text with temperature=0. Now the dominant memory usage would be KV cache. TODO: - Improve KV cache memory usage using pf16 or quantization. - This PR only fixes logits. Further activation memory optimization with one token output. Differential Revision: D61246566
b5bc307
to
4e1f741
Compare
This pull request was exported from Phabricator. Differential Revision: D61246566 |
Can you fix llava CI job? |
Summary: The logits size is big, with size [context_length x vocab_size]. But we always use the last (new) logits, because the model generates one new token in each Transformer inference. This PR changes the transformer to return the logits of the last token only. In the runner code, we don't have to fetch the logits for the last token specifically, but directly use the output . Test command: ``` python -m examples.models.llama2.export_llama --checkpoint /Users/myuan/data/llama/story110m/checkpoint.pt --params /Users/myuan/data/llama/story110m/params.json -kv --use_sdpa_with_kv_cache -X -qmode 8da4w --group_size 128 -d fp32 --max_seq_length 1024 --profile_memory ``` Before: 284 MB activation, with 262 MB on logits After: 162 MB activation, with 0.128 MB on logits Verified with llamma_runner, before and after it generates the same text with temperature=0. Now the dominant memory usage would be KV cache. TODO: - Improve KV cache memory usage using pf16 or quantization. - This PR only fixes logits. Further activation memory optimization with one token output. Differential Revision: D61246566 Pulled By: iseeyuan
4e1f741
to
45d639e
Compare
This pull request was exported from Phabricator. Differential Revision: D61246566 |
Summary: The logits size is big, with size [context_length x vocab_size]. But we always use the last (new) logits, because the model generates one new token in each Transformer inference. This PR changes the transformer to return the logits of the last token only. In the runner code, we don't have to fetch the logits for the last token specifically, but directly use the output . Test command: ``` python -m examples.models.llama2.export_llama --checkpoint /Users/myuan/data/llama/story110m/checkpoint.pt --params /Users/myuan/data/llama/story110m/params.json -kv --use_sdpa_with_kv_cache -X -qmode 8da4w --group_size 128 -d fp32 --max_seq_length 1024 --profile_memory ``` Before: 284 MB activation, with 262 MB on logits After: 162 MB activation, with 0.128 MB on logits Verified with llamma_runner, before and after it generates the same text with temperature=0. Now the dominant memory usage would be KV cache. TODO: - Improve KV cache memory usage using pf16 or quantization. - This PR only fixes logits. Further activation memory optimization with one token output. Differential Revision: D61246566 Pulled By: iseeyuan
45d639e
to
6ef8bef
Compare
This pull request was exported from Phabricator. Differential Revision: D61246566 |
Summary: The logits size is big, with size [context_length x vocab_size]. But we always use the last (new) logits, because the model generates one new token in each Transformer inference. This PR changes the transformer to return the logits of the last token only. In the runner code, we don't have to fetch the logits for the last token specifically, but directly use the output . Test command: ``` python -m examples.models.llama2.export_llama --checkpoint /Users/myuan/data/llama/story110m/checkpoint.pt --params /Users/myuan/data/llama/story110m/params.json -kv --use_sdpa_with_kv_cache -X -qmode 8da4w --group_size 128 -d fp32 --max_seq_length 1024 --profile_memory ``` Before: 284 MB activation, with 262 MB on logits After: 162 MB activation, with 0.128 MB on logits Verified with llamma_runner, before and after it generates the same text with temperature=0. Now the dominant memory usage would be KV cache. TODO: - Improve KV cache memory usage using pf16 or quantization. - This PR only fixes logits. Further activation memory optimization with one token output. Differential Revision: D61246566 Pulled By: iseeyuan
6ef8bef
to
9f105a2
Compare
This pull request was exported from Phabricator. Differential Revision: D61246566 |
Summary: The logits size is big, with size [context_length x vocab_size]. But we always use the last (new) logits, because the model generates one new token in each Transformer inference. This PR changes the transformer to return the logits of the last token only. In the runner code, we don't have to fetch the logits for the last token specifically, but directly use the output . Test command: ``` python -m examples.models.llama2.export_llama --checkpoint /Users/myuan/data/llama/story110m/checkpoint.pt --params /Users/myuan/data/llama/story110m/params.json -kv --use_sdpa_with_kv_cache -X -qmode 8da4w --group_size 128 -d fp32 --max_seq_length 1024 --profile_memory ``` Before: 284 MB activation, with 262 MB on logits After: 162 MB activation, with 0.128 MB on logits Verified with llamma_runner, before and after it generates the same text with temperature=0. Now the dominant memory usage would be KV cache. TODO: - Improve KV cache memory usage using pf16 or quantization. - This PR only fixes logits. Further activation memory optimization with one token output. Differential Revision: D61246566 Pulled By: iseeyuan
9f105a2
to
296fd23
Compare
This pull request was exported from Phabricator. Differential Revision: D61246566 |
Summary: The logits size is big, with size [context_length x vocab_size]. But we always use the last (new) logits, because the model generates one new token in each Transformer inference. This PR changes the transformer to return the logits of the last token only. In the runner code, we don't have to fetch the logits for the last token specifically, but directly use the output . Test command: ``` python -m examples.models.llama2.export_llama --checkpoint /Users/myuan/data/llama/story110m/checkpoint.pt --params /Users/myuan/data/llama/story110m/params.json -kv --use_sdpa_with_kv_cache -X -qmode 8da4w --group_size 128 -d fp32 --max_seq_length 1024 --profile_memory ``` Before: 284 MB activation, with 262 MB on logits After: 162 MB activation, with 0.128 MB on logits Verified with llamma_runner, before and after it generates the same text with temperature=0. Now the dominant memory usage would be KV cache. TODO: - Improve KV cache memory usage using pf16 or quantization. - This PR only fixes logits. Further activation memory optimization with one token output. Differential Revision: D61246566 Pulled By: iseeyuan
296fd23
to
4d7963f
Compare
This pull request was exported from Phabricator. Differential Revision: D61246566 |
Summary: The logits size is big, with size [context_length x vocab_size]. But we always use the last (new) logits, because the model generates one new token in each Transformer inference. This PR changes the transformer to return the logits of the last token only. In the runner code, we don't have to fetch the logits for the last token specifically, but directly use the output . Test command: ``` python -m examples.models.llama2.export_llama --checkpoint /Users/myuan/data/llama/story110m/checkpoint.pt --params /Users/myuan/data/llama/story110m/params.json -kv --use_sdpa_with_kv_cache -X -qmode 8da4w --group_size 128 -d fp32 --max_seq_length 1024 --profile_memory ``` Before: 284 MB activation, with 262 MB on logits After: 162 MB activation, with 0.128 MB on logits Verified with llamma_runner, before and after it generates the same text with temperature=0. Now the dominant memory usage would be KV cache. TODO: - Improve KV cache memory usage using pf16 or quantization. - This PR only fixes logits. Further activation memory optimization with one token output. Differential Revision: D61246566 Pulled By: iseeyuan
4d7963f
to
3101b7a
Compare
This pull request was exported from Phabricator. Differential Revision: D61246566 |
Summary: The logits size is big, with size [context_length x vocab_size]. But we always use the last (new) logits, because the model generates one new token in each Transformer inference. This PR changes the transformer to return the logits of the last token only. In the runner code, we don't have to fetch the logits for the last token specifically, but directly use the output . Test command: ``` python -m examples.models.llama2.export_llama --checkpoint /Users/myuan/data/llama/story110m/checkpoint.pt --params /Users/myuan/data/llama/story110m/params.json -kv --use_sdpa_with_kv_cache -X -qmode 8da4w --group_size 128 -d fp32 --max_seq_length 1024 --profile_memory ``` Before: 284 MB activation, with 262 MB on logits After: 162 MB activation, with 0.128 MB on logits Verified with llamma_runner, before and after it generates the same text with temperature=0. Now the dominant memory usage would be KV cache. TODO: - Improve KV cache memory usage using pf16 or quantization. - This PR only fixes logits. Further activation memory optimization with one token output. Reviewed By: larryliu0820 Differential Revision: D61246566 Pulled By: iseeyuan
3101b7a
to
2bca64c
Compare
This pull request was exported from Phabricator. Differential Revision: D61246566 |
Summary: The logits size is big, with size [context_length x vocab_size]. But we always use the last (new) logits, because the model generates one new token in each Transformer inference. This PR changes the transformer to return the logits of the last token only. In the runner code, we don't have to fetch the logits for the last token specifically, but directly use the output . Test command: ``` python -m examples.models.llama2.export_llama --checkpoint /Users/myuan/data/llama/story110m/checkpoint.pt --params /Users/myuan/data/llama/story110m/params.json -kv --use_sdpa_with_kv_cache -X -qmode 8da4w --group_size 128 -d fp32 --max_seq_length 1024 --profile_memory ``` Before: 284 MB activation, with 262 MB on logits After: 162 MB activation, with 0.128 MB on logits Verified with llamma_runner, before and after it generates the same text with temperature=0. Now the dominant memory usage would be KV cache. TODO: - Improve KV cache memory usage using pf16 or quantization. - This PR only fixes logits. Further activation memory optimization with one token output. Reviewed By: larryliu0820 Differential Revision: D61246566 Pulled By: iseeyuan
2bca64c
to
5e15211
Compare
This pull request was exported from Phabricator. Differential Revision: D61246566 |
1 similar comment
This pull request was exported from Phabricator. Differential Revision: D61246566 |
Summary: The logits size is big, with size [context_length x vocab_size]. But we always use the last (new) logits, because the model generates one new token in each Transformer inference. This PR changes the transformer to return the logits of the last token only. In the runner code, we don't have to fetch the logits for the last token specifically, but directly use the output . Test command: ``` python -m examples.models.llama2.export_llama --checkpoint /Users/myuan/data/llama/story110m/checkpoint.pt --params /Users/myuan/data/llama/story110m/params.json -kv --use_sdpa_with_kv_cache -X -qmode 8da4w --group_size 128 -d fp32 --max_seq_length 1024 --profile_memory ``` Before: 284 MB activation, with 262 MB on logits After: 162 MB activation, with 0.128 MB on logits Verified with llamma_runner, before and after it generates the same text with temperature=0. Now the dominant memory usage would be KV cache. TODO: - Improve KV cache memory usage using pf16 or quantization. - This PR only fixes logits. Further activation memory optimization with one token output. Pull Request resolved: #4688 Reviewed By: larryliu0820 Differential Revision: D61246566 Pulled By: iseeyuan
5e15211
to
7384903
Compare
Summary: The logits size is big, with size [context_length x vocab_size]. But we always use the last (new) logits, because the model generates one new token in each Transformer inference. This PR changes the transformer to return the logits of the last token only. In the runner code, we don't have to fetch the logits for the last token specifically, but directly use the output . Test command: ``` python -m examples.models.llama2.export_llama --checkpoint /Users/myuan/data/llama/story110m/checkpoint.pt --params /Users/myuan/data/llama/story110m/params.json -kv --use_sdpa_with_kv_cache -X -qmode 8da4w --group_size 128 -d fp32 --max_seq_length 1024 --profile_memory ``` Before: 284 MB activation, with 262 MB on logits After: 162 MB activation, with 0.128 MB on logits Verified with llamma_runner, before and after it generates the same text with temperature=0. Now the dominant memory usage would be KV cache. TODO: - Improve KV cache memory usage using pf16 or quantization. - This PR only fixes logits. Further activation memory optimization with one token output. Reviewed By: larryliu0820 Differential Revision: D61246566 Pulled By: iseeyuan
7384903
to
be438eb
Compare
This pull request was exported from Phabricator. Differential Revision: D61246566 |
The logits size is big, with size [context_length x vocab_size]. But we always use the last (new) logits, because the model generates one new token in each Transformer inference.
This PR changes the transformer to return the logits of the last token only. In the runner code, we don't have to fetch the logits for the last token specifically, but directly use the output .
Test command:
Before: 284 MB activation, with 262 MB on logits
After: 162 MB activation, with 0.128 MB on logits
Verified with llamma_runner, before and after it generates the same text with temperature=0.
Now the dominant memory usage would be KV cache.
TODO:
Additional tests:
llava