-
Couldn't load subscription status.
- Fork 560
Add block size table for ragged_paged_attention #8942
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits Select commit Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import torch_xla | ||
| | ||
| | ||
| def _next_power_of_2_bit_manipulation(x): | ||
| """ | ||
| Finds the smallest power of 2 >= x using bit manipulation. | ||
| Assumes x is an integer. | ||
| | ||
| Args: | ||
| x: The input number (should be an integer). | ||
| | ||
| Returns: | ||
| The smallest integer power of 2 that is >= x. | ||
| Returns 1 if x <= 0. | ||
| """ | ||
| if x <= 0: | ||
| return 1 | ||
| if x == 1: | ||
| return 1 | ||
| return 1 << (x - 1).bit_length() | ||
| | ||
| | ||
| # ragged_paged_attention | ||
| # key: (q_head_num, kv_head_num, token_num, max_model_len) | ||
| # value: (num_kv_pages_per_block, num_queries_per_block) | ||
| | ||
| | ||
| def _simplify_key_ragged_paged_attention(q_head_num, kv_head_num, token_num, | ||
| max_model_len): | ||
| token_num = _next_power_of_2_bit_manipulation(token_num) | ||
| max_model_len = _next_power_of_2_bit_manipulation(max_model_len) | ||
| return q_head_num, kv_head_num, token_num, max_model_len | ||
| | ||
| | ||
| # TODO: add more tuned block sizes in the table | ||
| _ragged_attention_table = { | ||
| (32, 8, 4096, 2048): (128, 64), | ||
| (4, 1, 4096, 2048): (128, 128), | ||
| (32, 8, 2048, 2048): (128, 32), | ||
| (4, 1, 2048, 2048): (128, 64), | ||
| (32, 8, 1024, 2048): (64, 32), | ||
| (1, 1, 1024, 2048): (64, 32), | ||
| (32, 8, 4096, 4096): (128, 64), | ||
| (4, 1, 4096, 4096): (128, 128), | ||
| (32, 8, 2048, 4096): (128, 32), | ||
| (4, 1, 2048, 4096): (128, 64), | ||
| (32, 8, 1024, 4096): (64, 32), | ||
| (1, 1, 1024, 4096): (64, 32), | ||
| (32, 8, 4096, 64): (32, 32), | ||
| (4, 1, 4096, 64): (32, 32), | ||
| (32, 8, 2048, 64): (32, 32), | ||
| (4, 1, 2048, 64): (32, 32), | ||
| (32, 8, 1024, 64): (32, 32), | ||
| (1, 1, 1024, 64): (32, 32), | ||
| (32, 8, 4096, 128): (32, 32), | ||
| (4, 1, 4096, 128): (32, 32), | ||
| (32, 8, 2048, 128): (32, 32), | ||
yaochengji marked this conversation as resolved. Show resolved Hide resolved | ||
| (4, 1, 2048, 128): (32, 32), | ||
| (32, 8, 1024, 128): (32, 32), | ||
| (1, 1, 1024, 128): (32, 32), | ||
| } | ||
| | ||
| | ||
| def get_ragged_attention_tuned_block_size(q_head_num, kv_head_num, token_num, | ||
| max_model_len): | ||
| tpu_version = torch_xla.tpu.version() | ||
| if tpu_version < 4: | ||
| raise NotImplementedError("TPU version must be 4 or higher.") | ||
| if tpu_version == 4: | ||
| # This default block size is not tuned, only make sure there's no | ||
| # OOM in vmem | ||
| num_kv_pages_per_block = 16 | ||
| num_queries_per_block = 128 | ||
| return num_kv_pages_per_block, num_queries_per_block | ||
| | ||
| key = _simplify_key_ragged_paged_attention(q_head_num, kv_head_num, token_num, | ||
| max_model_len) | ||
| block_sizes = _ragged_attention_table.get(key, (128, 32)) | ||
| return block_sizes | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.