Skip to content

cuda: sparse attention contract adds the focused case from #5166 (one head a key head, negative count, duplicate across a tile edge, unnamed entries in a nonfinite replay) - #5242

Merged
ashhart merged 2 commits into
ashhart:mainfrom
jschmied:sparse-attn-focused-contract
Nov 26, 2026

Conversation

@jschmied

Copy link
Copy Markdown
Contributor

Follow-up to #5166 (review comment 7187194600).

Head: de04fcb555, on main c21d201714. Test only: no kernel or wrapper change.

What changes

zig/tests/cuda/contract_sparse_attn.zig gains a third sparse attention case next to the 48-heads-over-4 case and its
planted control. It runs with the block attention cells as before. The case:

  • one query head a key head (4 over 4), so the fold sees G == 1;
  • row 0, key head 0 told -3 keys: a negative count, taken as none (NaN, as an empty list);
  • row 0, key head 1 repeats list entry 31 at slot 32, a duplicate across a 32-key tile edge;
  • row 0, key head 1 names two decoy cache rows past keys at slots 2 and 3. The nonfinite fill makes that head's values
    infinite, so the kernel replays it. The decoys are real memory holding +inf at value element 11, so a replay that
    reads an unnamed entry gives +inf where float64 gives a finite value.

Shape: 3 rows, 4 heads, 70 listed keys (three 32-key tiles). The 48-head case and its served shapes are unchanged.

Evidence

Kernel contract on de04fcb555 (tf-cuda-test kernel-contract 'sparse attention' 'block attention', GB10, nvcc 13.0), exit code 0:

PASS nonfinite cuda sparse attention tf_sparse_attention (4 heads over 4: negative count, duplicate, replay): the stated rule holds: outputs equal the float64 reference's bytes
PASS repeat cuda sparse attention tf_sparse_attention (4 heads over 4: negative count, duplicate, replay): five runs byte-identical (3072 bytes), inputs and guards untouched, no overlap
PASS shapes cuda sparse attention tf_sparse_attention (4 heads over 4: negative count, duplicate, replay): 5 shapes exact by its own check (worst 0.000 of the bound), 0 edges refused by the kernel
PASS sums cuda sparse attention tf_sparse_attention (4 heads over 4: negative count, duplicate, replay): exact sums at k 70, where fp16 sums would miss by 3840x and bf16 by 33023x
FAIL shapes cuda sparse attention tf_sparse_attention, control: the kernel told one key fewer: at 4x48x700: head 4 element 0 is -3.881836e-2 where float64 gives -3.091259e-2: 1.27 of attention_ref's bound
(filtered) SUMMARY 4 kernels: 4 pass gates 1-4, 0 fail, 0 known findings; 1 controls: 1 red as planted, 0 missed; 0 limit problems; gates 12125 ms, limits 0 ms

Planted break, red as intended: the same contract with the replay's key filter dropping its named-entry check
(seen in sparse_attention.cu without named(key)) fails only the new case; the 48-head case passes with the break:

PASS nonfinite cuda sparse attention tf_sparse_attention (Kolibri 1.1, 48 heads over 4): the stated rule holds: outputs equal the float64 reference's bytes
FAIL nonfinite cuda sparse attention tf_sparse_attention (4 heads over 4: negative count, duplicate, replay): head 1 element 11: pos_inf where float64 gives finite
(filtered) SUMMARY 2 kernels: 1 pass gates 1-4, 1 fail, 0 known findings; 1 controls: 1 red as planted, 0 missed; 0 limit problems; gates 1054 ms, limits 0 ms

A planted break in the negative-count clamp cannot be caught by any input: with n < 0 the tile count is 0 or 1 and no key
passes k < n, so the output equals a count of 0 either way. The case still pins that a negative count gives NaN.

Full CI set (x86 lane) on de04fcb555: ci, cuda-host, pr-check, fmt, diff-check and lean all rc 0. Lean check: 0 new.

Seeded, resume, concurrency: not applicable, a contract test with fixed inputs and no server. Against main: on
c21d201714; main has moved 19 commits since, none touching contract_sparse_attn.zig or sparse_attention.cu, and the
branch merges cleanly.

Proposed CHANGELOG line: cuda: sparse attention contract adds a focused case (one head a key head, a negative count, a duplicate across a tile edge, unnamed entries in a nonfinite replay).

jschmied and others added 2 commits November 26, 2026 23:13
…head, a negative count, a duplicate across a tile edge, unnamed entries in a nonfinite replay
@ashhart
ashhart force-pushed the sparse-attn-focused-contract branch from de04fcb to ecd5da8 Compare November 26, 2026 23:13
@ashhart
ashhart merged commit bc0eab7 into ashhart:main Nov 26, 2026
3 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.

ashhart added a commit to gilby/TensorFold that referenced this pull request Nov 26, 2026
ashhart added a commit that referenced this pull request Nov 26, 2026
ashhart added a commit to jschmied/TensorFold that referenced this pull request Nov 26, 2026
ashhart added a commit that referenced this pull request Nov 26, 2026
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