Repository navigation
perf(glm): bound prefill indexer logits workspace #1686
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
Open
shihaobai
wants to merge
1
commit into
main
Choose a base branch
from
codex/glm-prefill-logits-budget
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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,94 @@ | ||
| import weakref | ||
| from types import SimpleNamespace | ||
|
|
||
| import pytest | ||
| import torch | ||
|
|
||
| from lightllm.models.glm5_next.indexer import Glm5NextNsaInfer, get_prefill_logits_chunk_size | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("budget_mib,expected", [(64, 64), (128, 128), (256, 256), (512, 512)]) | ||
| def test_one_million_context_query_parallelism(budget_mib, expected): | ||
| assert get_prefill_logits_chunk_size(2048, 262144, budget_mib) == expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("num_q_tokens,max_pools", [(1, 262144), (8, 8192), (2048, 640), (8192, 262144), (0, 262144)]) | ||
| def test_default_matches_previous_bound(num_q_tokens, max_pools): | ||
| assert get_prefill_logits_chunk_size(num_q_tokens, max_pools, 64) == max( | ||
| 1, min(num_q_tokens, 16 * 1024 * 1024 // max_pools) | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("budget_mib", [0, -1, 15, 1025]) | ||
| def test_reject_unbounded_workspace(budget_mib): | ||
| with pytest.raises(ValueError, match="budget"): | ||
| get_prefill_logits_chunk_size(2048, 262144, budget_mib) | ||
|
|
||
|
|
||
| def test_environment_controls_each_indexer(monkeypatch): | ||
| monkeypatch.setenv("LIGHTLLM_GLM53_PREFILL_LOGITS_MIB", "256") | ||
| indexer = Glm5NextNsaInfer( | ||
| 0, {"index_kpool": 4, "index_topk": 2048, "index_n_heads": 32, "index_head_dim": 128, "rms_norm_eps": 1e-5}, 1 | ||
| ) | ||
| assert indexer.prefill_logits_budget_mib == 256 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("max_pools", [0, -1]) | ||
| def test_reject_invalid_pool_capacity(max_pools): | ||
| with pytest.raises(ValueError, match="max_pools"): | ||
| get_prefill_logits_chunk_size(128, max_pools, 64) | ||
|
|
||
|
|
||
| @pytest.mark.skipif(not torch.cuda.is_available(), reason="requires the GLM GPU indexer kernels") | ||
| def test_workspace_preserves_mixed_request_pool_selection(monkeypatch): | ||
| """Exercise an unaligned request boundary and a full 1M key range.""" | ||
| if torch.cuda.mem_get_info()[0] < 1536 * 1024 ** 2: | ||
| pytest.skip("requires at least 1.5 GiB of unused GPU memory") | ||
| import deep_gemm | ||
|
|
||
| fp8_mqa_logits = deep_gemm.fp8_mqa_logits | ||
| previous_logits = None | ||
|
|
||
| def track_logits_lifetime(*args, **kwargs): | ||
| nonlocal previous_logits | ||
| if previous_logits is not None: | ||
| assert previous_logits() is None, "the previous logits chunk is still alive" | ||
| logits = fp8_mqa_logits(*args, **kwargs) | ||
| previous_logits = weakref.ref(logits) | ||
| return logits | ||
|
|
||
| monkeypatch.setattr(deep_gemm, "fp8_mqa_logits", track_logits_lifetime) | ||
| monkeypatch.setenv("LIGHTLLM_GLM53_PREFILL_LOGITS_MIB", "64") | ||
| torch.manual_seed(20261029) | ||
| max_pools = 262144 | ||
| packed_buffer = torch.empty((max_pools * 2, 1, 132), dtype=torch.uint8, device="cuda") | ||
| packed_buffer[:, 0, :128] = torch.randn(max_pools * 2, 128, device="cuda").to(torch.float8_e4m3fn).view(torch.uint8) | ||
| packed_buffer[:, 0, 128:] = torch.ones(max_pools * 2, device="cuda").view(torch.uint8).view(-1, 4) | ||
| req_to_token_indexs = torch.full((2, 1048576), -1, dtype=torch.int32, device="cuda") | ||
| for batch in range(2): | ||
| req_to_token_indexs[batch, 3::4] = torch.arange( | ||
| batch * max_pools, (batch + 1) * max_pools, dtype=torch.int32, device="cuda" | ||
| ) | ||
| q_fp8 = torch.randn(128, 32, 128, device="cuda").to(torch.float8_e4m3fn) | ||
| weights = torch.randn(128, 32, device="cuda") * 0.05 | ||
| lengths = torch.cat( | ||
| [torch.arange(65536 - 30, 65537, device="cuda"), torch.arange(1048576 - 96, 1048577, device="cuda")] | ||
| ).to(torch.int32) | ||
| infer_state = SimpleNamespace( | ||
| b_req_idx=torch.tensor([0, 1], dtype=torch.int32, device="cuda"), | ||
| b_seq_len=torch.tensor([65536, 1048576], dtype=torch.int32, device="cuda"), | ||
| b1_cu_q_seq_len=torch.tensor([0, 31, 128], dtype=torch.int32, device="cuda"), | ||
| max_q_seq_len=97, | ||
| req_manager=SimpleNamespace(req_to_token_indexs=req_to_token_indexs), | ||
| ) | ||
| att_state = SimpleNamespace(lengths=lengths) | ||
| indexer = Glm5NextNsaInfer( | ||
| 0, {"index_kpool": 4, "index_topk": 2048, "index_n_heads": 32, "index_head_dim": 128, "rms_norm_eps": 1e-5}, 1 | ||
| ) | ||
| expected = indexer._get_prefill_indices(q_fp8, weights, packed_buffer, infer_state, att_state, max_pools) | ||
| expected = expected.sort(dim=1).values | ||
| for budget in [256, 512]: | ||
| indexer.prefill_logits_budget_mib = budget | ||
| actual = indexer._get_prefill_indices(q_fp8, weights, packed_buffer, infer_state, att_state, max_pools) | ||
| torch.testing.assert_close(actual.sort(dim=1).values, expected, rtol=0, atol=0) | ||
| assert ((actual >= 0) & (actual < (lengths // 4)[:, None])).all().item() |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Size chunks for the allocated logits tensor.
When
max_poolsis 640, this calculation budgets 640 FP32 entries per query. The pinned DeepGEMM revision allocates 768 entries per query because it rounds the pool dimension up to 256; it also rounds the query count up to a query block. A large prefill can therefore allocate about 19.2 MiB of logits with a 16 MiB setting, or 76.8 MiB with the 64 MiB default. Use the padded dimensions to calculate the chunk size, and update the bound test to cover an unaligned pool count. (github.com)