Skip to content

core: exact row top-k, sparse index scores and block summaries for sparse indexers - #5051

Merged
ashhart merged 9 commits into
ashhart:mainfrom
jschmied:kolibri11-indexer-pr
Nov 26, 2026
Merged

ashhart merged 9 commits into
ashhart:mainfrom
jschmied:kolibri11-indexer-pr

Conversation

@jschmied

@jschmied jschmied commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Refs #4846

Head: a2e25b9812. The evidence below ran on b3e89e5c81 unless a line names another sha; the follow-ups since are listed
under "Since b3e89e5" with the runs on their own heads.

Slice B of Kolibri 1.1 (after slice A, #4941): the core pieces a sparse indexer needs, generic and in core, with
CUDA kernels and their contracts. Nothing here is wired into a family yet; no family's behaviour changes. Against main: no served tokens or bytes can change (no family calls these yet), and GLM's select kernels compile to identical SASS; there is no served or seeded equality claim, so no parity-controls run applies. GLM's
select kernels are untouched (their SASS is identical, below); moving GLM onto the shared header is a later,
byte-identical PR of its own.

What changes

Piece Where What it does
exact row top-k kernels/cuda/common/topk_rows.cuh, topk_rows.cu, cuda/topk_rows.zig each row's top k of n scores, positions bit-equal to core/sparse_index.zig select; 128 threads, and 1,024 threads when rows <= 48 and n >= 4,096 (the same switch GLM uses)
index scores kernels/cuda/sparse_index.cu tf_index_scores per query row, per key: head reduction (weighted sum or max) of phi(q_h . k_g), fp32 sums, times a configurable scale (default 1); writes -inf from each row's count to ld
block summaries tf_index_blocks, tf_index_open_blocks mean / max / first / last of each complete block (optionally of unit-RMS keys); each row's open block from its own end (t + 1), so a prompt chunk's rows never see a later row's keys
core reference core/sparse_index.zig the f64 reference every kernel is checked against: one score order (NaN above +inf, -0 equal to +0, ties to the lower position), budget, flips(); dense is a policy rule (off by default; equal to every stage only where the open block is scored, a closed one shown to differ); the pool shortcut tested equal to every stage; separate NVFP4 options for token keys, queries and block summaries, and summary_input (summaries from the original or the stored keys); a pool bound to its sequence, layer and position
layer roles core/sparse_topology.zig per layer: KV owner, selection owner, pool owner, index-key owner, from explicit lists (an index cache apart from the KV cache is named in index_cache); a sparse layer left out of the selection list is refused (UnsettledSelectOwner), not guessed
NVFP4 store core/nvfp4_store.zig E2M1 values with E4M3 scales per 16; infinities saturate at 6 * 448, a NaN refuses the row (or stores 0 when the format says so), a scale floor

Refused by name before any launch (cuda/sparse_index.zig scoresFor / blocksFor): IndexHeadDim, IndexHeads,
IndexKeyHeads, IndexPhi, IndexReduce, IndexSummary, IndexStreams, IndexPagedKeys, and IndexPrecision
(NVFP4 keys, queries or summaries: the kernels read bf16). A follower given a pool from another owner, position or
sequence gets error.ForeignPool.

Where each rule comes from

Rule Source
top 2,048 keys plus the 128 most recent blog kolibri-1-1 (D1): "allows each token to attend to the 2,048 most relevant tokens, plus the 128 most recent ones."
index_topk 2048, local 128, sink 8, which layers are indexed, which layers share a KV cache config.json at e9515166 (index_*, kv_source_layers)
indexer cache stored as NVFP4 blog (D2): "(4-bit NVFP4 for the main and indexer caches, BF16 for the sliding-window layers)"
at most 2,048 + 128 + 8 = 2,184 keys a query arithmetic on D1 and the config
sinks additive, top-k excluding the local and sink ranges ASSUMED (not settled; a policy parameter)
query quantization, keys dequantized before the multiply ASSUMED (not settled; queries_nvfp4 / keys_nvfp4 are options)
whether block summaries are stored NVFP4, and whether they average original or stored keys UNSETTLED (summary_nvfp4, summary_input are parameters; no source decides it)
every key attended while they fit the budget (dense) a policy rule a model states; not claimed for Kolibri here
index score scale ASSUMED (configurable, default 1; a positive constant does not change the top-k)
tie rule (lower position first), NaN order ours, the same as core bf16_topk
dense / pool shortcuts generic (exactness of the shortcut), not a Kolibri claim

The publisher's package was used only as a black box for outputs. No engine's source was read for these rules.

Evidence

Kernel contract (tf-cuda-test kernel-contract 'topk rows' 'sparse index' 'glm select' 'glm decode select', GB10,
nvcc 13.0): SUMMARY 15 kernels: 15 pass gates 1-4, 0 fail, 0 known findings; 5 controls: 5 red as planted, 0 missed; 0 limit problems.

  • topk rows (tf_topk_rows, tf_topk_rows_wide): positions equal to sparse_index.select on every row, over 19 shapes,
    including 1/4/16 x 16,384 keys top-2,048, 24 x 4,096, 6 x 65,536, 2 x 131,072 and 60 x 8,192; the same with every
    score tied, and with subnormal scores (lost under flush-to-zero; the images are built --ftz=false). Counts out of
    range (-3, n + 7) are planted and clamped.
  • index scores: flat (ReLU, weighted sum, one key head) and GQA (identity, max, 48 heads over 4 key heads), within
    the float64 bound; ld = n + 37 checks the -inf fill; a count-0 row and an oversized count. Sums gate: exact at
    k 64, where fp16 sums would miss by 3,495,253x and bf16 by 5,592,405x.
  • block summaries: mean, mean with the open block left out, mean of unit-RMS keys, max, first, last; open blocks per row.
    Sums gate: exact at k 128 (fp16 would miss by 1,792x with the open block left out).
  • Non-finite inputs (NaN, +inf, -inf) land in the float64 classes on every kernel; five runs byte-identical.

Planted controls, each red as planted:

  • zig/tests/cuda/contract_topk_rows.zig:32: told each row one score fewer than it has.
  • zig/tests/cuda/contract_sparse_index.zig:37: scores told each row one key fewer.
  • zig/tests/cuda/contract_sparse_index.zig:249: block means told one key fewer.
  • zig/tests/cuda/contract_sparse_index.zig:413: open blocks told each row's end one key later.
  • zig/tests/cuda/contract_sparse_index.zig:414: open blocks told one key fewer than the cache holds (the kernel's
    bound on ends; the normal entry plants ends of -5 and n + 9, clamped).

Register / shared-memory records for sm_80/86/89/120/121 in core/contract/sparse_index_records.zig; the live limits
check passes for all five launches.

GLM unchanged: glm_glue.cu SASS identical between main 47a605aa12 (the base) and b3e89e5c81 on sm_80, sm_86, sm_89,
sm_120 and sm_121 (nvcc 13.0 -O3, cuobjdump). No GLM timing run: its machine code did not change.

Host tests (core, Zig): every selection is causal, ascending, without repeats and within budget (a planted future key,
a repeat, a missing sink key and one key over budget are each caught); dense counter-case (ten keys under a budget of
16 are all attended); block boundaries; NaN order; key heads and scale; shortcuts equal to every stage scored; NVFP4
flips across the top-k boundary; foreign pool refused (planted: it was accepted); topology roles for layers 14, 24 and
49; planted bad lists refused by name; NVFP4 saturation, NaN rule and floor (each planted).

Since b3e89e5

  • 87617d97a0 (tests) + ed1270a72d (fix): every NaN of either sign or payload maps to one key above +inf (PTX does
    not state NaN encodings); the padding loop steps in 64 bits and the host refuses k above maxInt(i32) - 1024. The
    nonfinite rows now count NaN of both signs and with payloads (row 1 was told -3 before, so its NaN was never ranked).
    Test first: the k host test fails on 87617d97a0; the NaN rows pass on the old kernel too (GB10 returns one NaN).
  • d0048e586d (tests): a row whose k cuts through 2,560 mixed NaN (lower index first), and a stress row over every
    head whose head sum is simulated in the accumulator (an fp16 or bf16 head sum misses it).
  • 4d70a5559b (test) + e87d88d74a (fix): contract_family_sets.zig sets the top-k and sparse-index sets up as a
    pair (contract_set_pair.zig): a failing sparse-index setup releases the top-k set and its module. Host test
    test-contract-set-pair-host (in test-cuda-host): leaks on 4d70a5559b, passes on e87d88d74a.
  • Kernel contract on d0048e586d (topk rows, sparse index, glm select, glm decode select): 15 kernels pass
    gates 1-4, 0 fail; 5 controls red as planted. Full CI set on d0048e586d and on e87d88d74a: ci, cuda-host,
    pr-check, fmt, diff-check and lean all rc=0 (X86-ALL-GREEN), lean 0 new. GLM: common/topk_rows.cuh is included
    by topk_rows.cu alone, so no GLM image changes.
  • a2e25b9812: the gradient-serving pins of cuda/kernels.zig and cuda/root.zig (raw) refreshed for the new
    images and exports; zig build test-gradient-serving passes (it failed CI on e87d88d74a).

Full CI set (x86 lane, kol11-b3-final-x86) on b3e89e5c81: ci, cuda-host, pr-check, fmt, diff-check and lean all rc=0
(X86-ALL-GREEN); lean: 0 new problems (164 on the base, 164 on the head).

Shapes: the shapes gate runs every served shape above plus each dimension's edges (the tile less a step, the tile,
the tile plus a step, the least and the largest size; contract.zig:57). The full hazard row list (1, 2, 3, 4, 7, 8, 13,
16, 31, 32, 65) is not run one by one: the kernels give each row its own block, so rows have no tile tail; key-count
tails are covered by the edges and by ld = n + 37.

Not here (later PRs): wiring into the Kolibri 1.1 forward, tensor-core scoring, a multi-CTA top-k, incremental
summaries, and GLM's migration to the shared header.

Seeded, resume, concurrency and against-main replies: not applicable here; no family calls these kernels yet, so no
served path changes (GLM's machine code is identical, above). They come with the Kolibri 1.1 forward PR.

Proposed CHANGELOG line: core: exact row top-k, sparse index scores and block summaries (CUDA, with contracts), layer roles for sparse indexers, NVFP4 store.

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you @jschmied. The top-k comparisons, causal open-block ranges and explicit precision/layout refusals are in place, and these helpers remain separate from serving.

One cleanup remains in the parent: after top-k setup succeeds, a failure while setting up the sparse-index set leaks the top-k allocation and module. Please add its error cleanup before the next initialization and a regression for that failure. The existing numerical results can keep their scope; this guard alone needs no repeat run.

  • Synthena

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you, @jschmied. The explicit NaN key, padding bound and head-sum stress improve the numerical coverage. The current head merges cleanly.

The earlier cleanup ask remains at zig/tests/cuda/contract_family_sets.zig:90: top-k setup succeeds before sparse-index setup, with no top-k error cleanup between them. If the second setup fails, its predecessor's set and module leak. Please add the immediate error guard and the focused failure regression from the last review. The existing numerical receipts keep their tested scope.

  • Synthena

@jschmied

Copy link
Copy Markdown
Contributor Author

Thanks @ashhart. The cleanup is in e87d88d74a (answers both your comments on it):

  • zig/tests/cuda/contract_family_sets.zig:89 now sets the top-k and sparse-index sets up through pair() in zig/tests/cuda/contract_set_pair.zig: the top-k set has its error cleanup right after it succeeds, so a failing sparse-index setup releases the top-k set and its module before returning.
  • Regression: contract_set_pair.zig's host test (test-contract-set-pair-host, also in test-cuda-host) sets up a pair whose second set fails, under the testing allocator. It reports a leak on 4d70a5559b (the pair without the cleanup) and passes on e87d88d74a.
  • Full CI set on e87d88d74a: ci, cuda-host, pr-check, fmt, diff-check and lean all rc=0, lean 0 new. No numerical rerun, as you said.

The body's "Since b3e89e5" section lists the earlier follow-ups (NaN key, padding bound, NaN-cut and multi-head stress rows) with their runs.

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you, @jschmied. The pair helper closes the cleanup ask: it arms top-k destruction before sparse-index setup can fail. The focused allocator test uses that same helper, covers failure and success, and is unchanged across the reported failing/passing guard comparison. The actual destructor unloads the module and frees the set.

No further source change is requested. The kernels and numerical fixtures are unchanged from the previous review, and this head merges cleanly. The cleanup alone needs no numerical rerun. Keep existing results tied to their tested images; any still-unfilled mixed-NaN/padding and head-sum stress cells remain suitable for focused synthetic GB10 qualification without a checkpoint.

  • Synthena

@ashhart

ashhart commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thank you, @jschmied. The GB10 kernel contract for this head passed: all 12 kernels through gates 1-4, all 5 planted controls red, and the topk rows and sparse index live launches within their recorded limits. Against main, the only new launch-limit finding was tf_topk_rows_wide at sm_86 and sm_89, which nvcc 13.0.88 builds with 56 registers where the record says 57. We added a one-line commit setting those two entries to 56. This goes into a landing train next.

  • Synthena

jschmied and others added 9 commits November 26, 2026 05:41
…arse indexers

CUDA kernels tf_topk_rows / tf_topk_rows_wide, tf_index_scores, tf_index_blocks and tf_index_open_blocks, each with a
kernel contract and a planted control; core/sparse_index.zig as their float64 reference (one score order, budget, the
dense rule as a policy field, the pool shortcut, NVFP4 options for token keys, queries and block summaries and the
summaries' input, pools bound to their sequence); core/sparse_topology.zig for per-layer KV, selection, pool and
index-key owners from explicit lists; core/nvfp4_store.zig (E2M1 with E4M3 scales, saturation, NaN rule, floor).
GLM's select kernels are untouched (SASS identical).
…nonfinite rows; a k the padding loop cannot step is refused
…ps in 64 bits and k leaves room for a block's stride
…a stress row over every head, its head sum simulated in the accumulator
@ashhart
ashhart force-pushed the kolibri11-indexer-pr branch from abd048c to f8c1085 Compare November 26, 2026 05:41
@ashhart
ashhart merged commit 67ae36c into ashhart:main Nov 26, 2026
2 of 12 checks passed
@ashhart

ashhart commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thank you @jschmied. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants