Expand description
STELLAR §A A1a — GPU top-K reduction: CPU oracle, host merge, and the WGSL kernel.
Decode is memory-bandwidth-bound; the sentinel/sampler only needs the high-probability
mass, not the 49 k near-zero tail. Instead of reading back the full logit vector
(~196 KB/token) and doing a CPU argmax, the GPU reduces each block of the vocabulary to
its top-K candidates; the host merges those into the global top-K. The CPU oracle here is
the byte-for-byte reference the on-device kernel is verified against (see topk_gpu.rs),
exactly as ternary.rs anchors ternary_gpu.rs.
Contract (must match shaders/topk_reduction.wgsl): NaN → −∞ (never selected); ties broken
toward the LOWER token id (deterministic); K=1 == argmax.
Structs§
- TopK
Item - One top-K entry: a token id and its raw logit.
Constants§
- TOPK_
BLOCK_ SIZE - Block size each workgroup reduces — must equal
MAX_BLOCKin the WGSL (var<workgroup>cap). - TOPK_
MAX_ K - Largest K the host paths support (kept generous; the kernel itself is K-agnostic per round).
- TOPK_
REDUCTION_ WGSL - The block-reduction kernel (auto bind-group layout; entry
topk_block).
Functions§
- merge_
block_ candidates - Merge per-block GPU candidates (
num_blocks × kpairs) into the global top-K. Blocks cover disjoint index ranges, so candidate ids are unique. Drops −∞ entries and any id inmasked(the governance/sieve veto — “a masked token never returned”). - topk_
cpu - CPU reference top-K over a full logit vector. Drops −∞ (e.g. masked) entries.
- topk_
params_ bytes - 16-byte
Paramsuniform:n, k, block_size, cand_base(cand_base=0 for single-chunk). - topk_
params_ bytes_ with_ base - Like
topk_params_byteswith a non-zero candidate write base for multi-chunk mega-pass.