[Feature] Add optional TileLang Top-K selector - #2096
Open
qiyueliuhuo23 wants to merge 2 commits into
Open
qiyueliuhuo23 wants to merge 2 commits into
qiyueliuhuo23 wants to merge 2 commits into
Conversation
added 2 commits
September 18, 2026 17:20
- Add a range-aware TileLang radix Top-K implementation.\n- Add dense-prefix fallback for concentrated score distributions.\n- Keep PyTorch top-k as the default and expose USE_TILELANG_TOPK switch.\n- Add bitwise correctness tests for packed, causal, concentrated, and indexer paths.
Adapt the optional TileLang Top-K integration to the relocated GLM-5.2 attention module and the independent indexer backend introduced upstream.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This PR adds an optional TileLang radix Top-K implementation for the GLM-5.2 DSA indexer path.
PyTorch topk remains the default behavior. The new implementation is enabled only when:
Background
The GLM-5.2 DSA indexer first produces an FP32 logits matrix and then selects the Top-K KV positions using torch.topk.
For packed causal sequences, each query only has a valid [start, end) KV range. However, the original PyTorch path still scans the complete logits row, including invalid positions that have already been masked to -inf.
The TileLang implementation uses these valid ranges directly to avoid unnecessary processing outside the causal interval.
Main Changes
1. Add TileLang radix Top-K
New file:
The implementation:
2. Add an independent runtime switch
Default behavior remains PyTorch Top-K:
Enable TileLang Top-K explicitly:
The switch is exposed through:
It only changes the Top-K selection stage. It does not change:
3. Add correctness tests
New test file:
The tests cover:
The tests use unique FP32 values so that the comparison is not affected by PyTorch's unspecified ordering for tied scores.
Standalone Performance
The measurement includes the complete selector path:
The main benefit comes from skipping invalid causal ranges. For long single-segment inputs or highly concentrated distributions, the dense rescan fallback can reduce or eliminate the benefit.
End-to-End Training Performance
Configuration:
The official CI checker passed:
Expected End-to-End Benefit
The current Top-K path accounts for approximately 0.38% of the total step time.
Using the standalone speedup range of 1.47x–1.75x:
Expected step-time improvement
= 0.38% × (1 - 1 / speedup)
≈ 0.121%–0.163%
The measured average step-time improvement was 0.1687%, and TGS improved by 0.1690%, which is consistent with the theoretical estimate.
Compatibility
Validation
To use the default PyTorch implementation: