HyTopK: Exact Deterministic Top-k Selection
- HyTopK is an exact, deterministic, on-the-fly Top-$k$ selection operator for learned sparse attention, reducing memory and computational overhead by avoiding dense score-table materialization and separate Top-$k$ selection passes.
- HyTopK implements a hybrid algorithm that combines query-local strided sampling, fused score computation, and candidate filtering, with GPU-side recovery to ensure exact selection, achieving up to 1.71x speedup in specific long-context training settings.
- HyTopK is crucial for large-scale sparse attention models like those used in the HyScale system, enabling efficient handling of million-token contexts with exact Top-$k$ selection and deterministic output order, despite potential limitations and overheads in certain failure scenarios.
HyTopK is an exact, deterministic, on-the-fly Top- selection operator for learned sparse attention, introduced as a component of the HyScale long-context training system. It selects the highest-scoring eligible tokens or blocks for each query while avoiding materialization of the dense score table. HyTopK combines query-local strided sampling, fused score computation and candidate filtering, bounded candidate buffers, exact GPU-side recovery, and deterministic final selection. Its principal systems objective is to replace the conventional pipeline of dense score computation, score-table storage, score-table rereading, and separate Top- selection with a streaming GPU operation (Deng et al., 23 Oct 2026).
1. Terminology and scope
The name HyTopK is used formally in “HyScale: Scaling Long-Context Training for Learned Sparse Attention” (Deng et al., 23 Oct 2026). It denotes the Top- selection component of HyScale rather than a general differentiable Top- relaxation. The operator is designed for learned sparse attention at million-token context lengths, including token-level dynamic sparse attention (DSA) and block-level methods such as context sparse attention (CSA) and query sparse attention (QSA).
The term should be distinguished from several superficially related uses of “hybrid” or “HyTopK”:
- “SOFT top-” is an entropy-regularized optimal-transport relaxation for differentiable end-to-end learning; the associated paper does not define or cite HyTopK (Xie et al., 2020).
- “HPC-Ops Top-K” is a separate sample-guided exact selector for ragged sparse-attention score rows. It was described in supplied material as a possible inferred shorthand related to Tencent’s Hy4-Preview, but its formal name is not HyTopK (Liu et al., 8 Sep 2026).
- Size-aware hypergraph motif retrieval is also referred to as HyTopK in supplied material, but it solves a different combinatorial hyperedge-triplet retrieval problem (Niu et al., 2023).
- Higher-order topological point states and hierarchical topological clustering are unrelated uses of similar abbreviations or terminology (Li et al., 2023, Carpio et al., 31 Dec 2025).
HyTopK addresses a systems problem rather than changing the mathematical Top- objective. For query , let be the eligible set of tokens or blocks, let be their indexer scores, and let 0 be the number of entries retained:
1
Sparse attention subsequently reads only the selected entries. HyTopK preserves this exact selection semantics while changing how scores are computed, filtered, stored, and reduced.
2. Motivation in learned sparse attention
Learned sparse attention reduces the attention-read cost by allowing each query to attend to only 2 selected entries. The indexer nevertheless scores every eligible entry. For DSA, the score is
3
where 4 is the number of indexer heads and 5 is the indexer-key dimension. The resulting score vector has one value for every eligible token or block.
In the conventional implementation described for Megatron-LM, selection consists of two dense operations:
- a scoring kernel computes all query–key scores and writes an 6 score table to GPU memory;
- a Top-7 kernel reads that table and selects 8 entries per query.
At a sequence length of 9M tokens, with 0K queries and 1, the score table for one layer is approximately 2 GiB per GPU, while selected indices occupy 3 MiB. Only 4 of the scores survive selection. Full activation recomputation further repeats dense scoring and selection. In the reported GLM-5.1 profile at one million tokens, selection consumed 5 of a training step and was 6 more expensive than sparse attention (Deng et al., 23 Oct 2026).
HyTopK targets the intermediate representation rather than the score arithmetic itself. The full eligible context still generally has to be scored, so the method does not eliminate the fundamental 7 scoring requirement. Its contribution is to avoid writing and rereading the dense 8 score table, to avoid a separate Top-9 pass over that table, and to retain only a bounded candidate set and selected metadata.
This distinction is central: HyTopK is primarily a memory-traffic and execution-organization optimization. It does not, by itself, turn dense score computation into 0 computation.
3. Algorithmic design
For a query with eligible-context length 1, HyTopK first forms a query-local sample. It samples
2
evenly spaced keys from the query’s own context. The sampled scores are used to estimate an optimistic threshold corresponding approximately to the 3-th best full-context entry:
4
best sampled score. The factor of three intentionally provides slack, so the expected candidate count is approximately 5kk$6 result.
The implementation reports a default candidate buffer of $k$7K slots per query in the sampling experiments. Candidate collection uses fast atomic appends, which can make candidate order nondeterministic. Deterministic postprocessing is therefore applied after the selected set has been established.
A query is marked as failed if too many candidates pass and the buffer overflows, or if fewer than $k$8 candidates survive. An overflow indicates that the threshold was too low to control candidate volume; an underfilled candidate set indicates that the threshold may have excluded a true Top-$k$9 entry. Both conditions trigger exact recovery.
Recovery is performed without host synchronization. A check kernel identifies failed queries, recovery kernels rescore each failed query over its entire eligible context, and a conventional exact Top-$k$0 operation replaces the provisional result. Recovery handles up to $k$1 failed queries; if more than $k$2 queries fail, HyTopK falls back to rescoring every query and selecting its Top-$k$3. The reported training runs encountered no more than $k$4 failures.
The resulting control flow is therefore hybrid in an algorithmic sense:
$k$5
The sampling stage controls common-path work but never determines correctness.
4. Exactness and deterministic selection
HyTopK is an exact Top-$k$6 method, not an approximate selector. Exactness follows from the candidate-count checks and recovery protocol.
If every true Top-$k$7 entry passes the threshold and the candidate buffer does not overflow, exact selection over the candidate set returns the global Top-$k$8. If the threshold is too high and fewer than $k$9 candidates survive, the query is rescored over the full eligible context. If the threshold is too low and the candidate buffer overflows, the query is also recovered by full-context exact selection.
The sample therefore affects performance-related quantities:
- candidate-buffer occupancy;
- the amount of candidate refinement;
- the frequency of recovery;
- common-path memory traffic;
- the number of queries requiring fallback.
It does not affect the final answer. Correctness depends on the full-context scan and exact recovery rather than on the statistical quality of the sample.
HyTopK also defines deterministic behavior for ties and output ordering. If multiple candidates share the $k$0-th score, radix selection over their positions chooses the smallest positions when a deterministic tie rule is required. After the selected set has been determined, the $k$1 selected entries are radix-sorted by score, with position used to break ties. This fixes the order in which sparse attention accumulates selected entries and avoids floating-point summation differences caused by nondeterministic candidate order.
The exactness contract is consequently based on score order followed by position order. Sampling can fail as a performance heuristic, but such failure is detected before an incorrect result is committed.
HyTopK is also distinct from selection replay. HyTopK accelerates one execution of selection by fusing scoring and filtering. Selection replay avoids repeating selection during activation recomputation by saving the selected indices $k$2 from the forward pass and rescoring only those selected entries during recomputation. The two mechanisms address different costs: HyTopK removes dense score-table materialization, whereas selection replay removes repeated full-context selection.
For DSA, the indexer loss uses selected scores,
$k$3
so the indexer backward pass requires selected-entry information rather than the scores of all unselected entries. HyScale uses index replay by default, saving approximately $k$4 GiB per GPU compared with score replay while adding at most $k$5 of a training step in rescoring in the reported configuration (Deng et al., 23 Oct 2026).
5. GPU implementation and systems integration
HyTopK is implemented as a GPU pipeline using CUDA, Triton, and CuTe kernels. The score computation is tiled, and threshold sampling for groups of queries can use matrix multiplication over combined sampled contexts. During the full scan, query and key blocks are loaded cooperatively, scores are computed, and threshold filtering occurs immediately.
The principal execution paths are:
- Fused common path: scores are computed, filtered, and appended to candidate buffers without materializing the dense score table.
- Exact recovery path: failed queries are rescored over their complete eligible contexts and processed by exact Top-$k$6 selection.
- Deterministic postprocessing: selected candidates and relevant ties are radix-ordered.
- Selection replay path: during recomputation, saved indices are loaded and only selected entries are rescored.
HyTopK is compatible with context parallelism (CP). Each GPU owns a subset of queries but may access indexer keys from the full causal context. Sampling is query-local and does not depend on selections from neighboring queries or another CP rank. This avoids synchronization that would otherwise undermine scalability.
The broader HyScale system adds several optimizations that are separate from the Top-$k$7 operator itself. These include decoupled CP overlap, separate gathering of indexer keys and sparse-attention key/value states, overlap of communication with projections and selection, indexer-gradient communication overlap, locality-aware sparse kernels, elementwise fusion, and avoidance of unnecessary key/value permutations.
HyTopK’s common-path score work for $k$8 local queries and eligible-context length $k$9 is approximately
$k$0
for score arithmetic, with threshold sampling adding approximately
$k$1
where $k$2. Candidate writes and candidate selection scale with the number of passing candidates, typically a small multiple of $k$3. Selected-index storage is $k$4, while candidate-buffer storage is $k$5 for buffer capacity $k$6. Dense score-table storage of $k$7 is eliminated.
The method therefore retains the mathematical full-context scan but changes memory behavior from dense intermediate storage to streaming candidate maintenance. Its practical advantage grows when the dense score table becomes too large for efficient GPU residency and when query chunking or repeated global-memory traffic becomes a dominant cost.
6. Evaluation, limitations, and significance
The reported experiments use NVIDIA H20 GPUs with $k$8 GiB memory and NVIDIA H800 GPUs with $k$9 GiB memory. The main GLM-5.1 experiments use BF16 training, $t$0, long-context sequences, and full activation recomputation.
For GLM-5.1 on one H20 GPU with $t$1K queries, HyTopK’s selection memory was reported as $t$2–$t$3 GiB per layer and nearly flat as context length increased. Relative to the default Megatron-LM baseline, HyTopK achieved speedups of $t$4, $t$5, and $t$6 at context lengths of $t$7K, $t$8K, and $t$9M tokens, respectively. At one million tokens, forward selection decreased from $U_t$0 seconds to $U_t$1 seconds with HyTopK, while recomputed selection decreased from $U_t$2 seconds to $U_t$3 second when selection replay was added. The latter improvement is primarily attributable to replay rather than HyTopK alone.
At one million tokens, deterministic sorting added $U_t$4 to base HyTopK selection time. One failed query increased time by $U_t$5, while $U_t$6 failed queries increased it by $U_t$7. In the reported training experiments, no selection encountered more than $U_t$8 failed queries.
In a sampling comparison involving $U_t$9 million queries from a GLM-5.1 training step, the optimistic strided sampling strategy produced more than $I_t$0 fewer failures than LiteTopK and DoSA with fixed thresholds in the trained setting. With randomly initialized indexers, HyTopK produced no failures at the reported buffer size. These comparisons concern failure frequency and candidate-buffer behavior, not a change to the exactness contract.
The principal limitations are structural:
- Full score computation remains: HyTopK generally still evaluates every eligible token or block, so its asymptotic score arithmetic remains $I_t$1 per query.
- Distribution-dependent performance: poorly calibrated thresholds can produce large candidate sets, buffer overflows, or frequent recovery.
- Fixed candidate capacity: the common path depends on bounded per-query buffers, although overflow is handled exactly.
- Recovery cost: pathological failure rates can trigger dense exact fallback.
- Sampling overhead: threshold estimation and checking introduce additional kernels and score work.
- Deterministic ordering cost: radix operations are required for deterministic tie handling and selected-entry ordering.
- Masking requirements: the sampled and scanned eligible set must exactly match the model’s causal or block-level eligibility rule.
- Replay assumptions: selection replay is valid for the described learned sparse-attention formulation, in which the backward computation requires only selected entries.
HyTopK’s significance lies in treating Top-$I_t$2 selection as a streaming discrete GPU operation whose persistent output is an index set rather than a dense activation. It preserves exactness through full-context validation and recovery while avoiding the dense score-table pipeline that causes learned sparse attention to retain substantial dense-system costs at long context lengths. Within HyScale, HyTopK addresses forward selection, selection replay addresses recomputation, and the remaining system optimizations address communication, fusion, memory layout, and sparse-attention execution (Deng et al., 23 Oct 2026).