Skip to content

inkling2: the attention's arithmetic reference follows #4125's kernels, host only - #4367

Draft
Nipale-ai wants to merge 10 commits into
ashhart:mainfrom
Nipale-ai:inkling2-attention-reference-2111b
Draft

Nipale-ai wants to merge 10 commits into
ashhart:mainfrom
Nipale-ai:inkling2-attention-reference-2111b

Conversation

@Nipale-ai

@Nipale-ai Nipale-ai commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

The page docs/inkling2-attention-reference.md, the host model and the host tests described Transformers' eager rounding points; after #3476's request they now write down #4125's arithmetic, the one main runs. The page, a new fp32 host model in the kernels' order and the host tests state the three bf16 roundings of the kernels (bias table, P, output), the fp32 score and sums, the order of every sum (8 chained k16 mma, 64-key tiles, 1024-key sub-chunks, 256- and 16384-key chunks, the sink at the merge), what NaN and infinity do, and a float64 bound derived for that order, with probes against it. The comparison with Transformers eager stays as a reported fidelity number.

  • docs/inkling2-attention-reference.md: the page, with the same sections and one more, "Fidelity against Transformers eager (reported)"; every statement about a kernel carries file:line at 6dbb32f048a6.
  • zig/src/families/inkling2/attn_order.zig: the fp32 model in the kernels' order. It takes any number of keys; inside an mma it adds in ascending order and at the plain * and + sites it rounds the product and the sum separately, both written on the page as the model's choice.
  • zig/src/families/inkling2/attn_bound.zig, attn_bound_test.zig: the bound as code, with its domain (finite inputs, gate >= -87, nk <= 2^20, products and sums below 2^120; error.AttnDomain outside it), an infinite bound where exp(L) overflows, and the tests that evaluate it: the fixture, rows across 64, 1024 and 16384 keys (global) and the 256-key chunks (sliding), a case per term, an output below the normal range, the ends of the domain, and a case for the table's worst rounding that reaches 0.96 of the bound.
  • zig/src/families/inkling2/attn_order_hand_test.zig: one test per rounding point and per seam with expected values written by hand in float64, and the NaN and infinity cases.
  • zig/src/families/inkling2/attn.zig, attn_test.zig: the float64 formula stays; the eager-point fp32 model stays only as the fidelity check, with the fidelity numbers printed.
  • oracle/attn.py and fixtures/attn_tiny.bin are unchanged since f387faf49 (the page now says how the fixture is made); host_test.zig gains the new imports.

attn_bound.zig is its own count, not tol of zig/tests/cuda/contract_inkling2_attn_ref.zig, and this change does not touch that file. Where they differ: the page's float64 formula takes the exact tap sum as the bias and counts the table's bf16 rounding, where the contract's reference takes the table as an input (:70, :120-121); a rescale counts as gamma(6) here and as 2^-22 there (:88-92, :161); P's exp2f counts as gamma(5) here and 2^-22 there (:166); a 1,024-key sub-chunk or 256-key chunk of T tiles has T - 1 in-tile rescales here and T there (:80-92); the quotient of the two sums is bounded with exp(L) - 1 here and to first order there (:186).

Not part of this change: the pin move to the current revision (8192), and the masked non-finite V entry in docs/kernel-contract.md, which needs a GPU run; the page states the host rule for it under NaN and infinity.

No kernel, no zig/tests/cuda file, no docs/kernel-contract.md, no pin (tools/zig/inkling2_fixtures.py, fixtures/config.json) and no log_scaling_n_floor is touched or read; no file of this change contains CUDA code.

  • Issue: Refs Inkling 2 Small on four DGX Sparks: a native CUDA family #3476.
  • Stacked on: main (462f5eca3). This pull request holds ten commits: the earlier two (501aca8f8, f387faf49) and eight more: 381c6d34e (the host model in the kernels' order, the bound and the tests), 4cd632869 (the page), d9f2d192c, 7ab82358e and 5323a8f3d (the bound's live keys, its comments, the infinite bound and the table test), 714d351f1 (the page's wording, sources, domain and falsification table), 1fb3517e1 (a test comment) and 64d6ddf53 (corrections to five passages of the page).
  • atc overlap --json: run on 2026-11-22 at 18:52 UTC against base 462f5eca3 after the final rebase; incomplete (limitations: otherPullRequestsLimit, pullFilesUnavailable, pullRequestsLimit, timeLimit); this run lists no overlap with another pull request. Inkling 2 Mini on CUDA and Metal #4387 is still open and still changes zig/src/families/inkling2/host_test.zig (1 shared file); its import lines and this pull request's are disjoint. The run also reports two local checkouts of this branch from before the rebase: the previous head 09c48a1d5 (10 shared files, clean) and its fourth commit 509bc4df1 (10 shared files, 4 conflicting files, each changed again by later commits of this branch); no other overlaps.

Checks on this head

  • Head commit: 64d6ddf53
  • Lean checker (python3 tools/pr_check.py --base origin/main --pr 4367, which runs it): 0 errors, 0 new Lean findings (165 vs 165); 2 advisory warnings, below.
  • Tests run, with the command and pass, fail and skip counts:
    • zig build test-inkling2-host --summary all: 81 passed, 0 failed, 4 skipped (checkpoint-dependent tests).
    • zig build test-inkling2-cuda-host: 10 passed.
    • zig fmt --check build.zig zig and git diff --check origin/main HEAD: clean. pr_check: two advisory warnings, no Unreleased bullet naming this PR, and the NVIDIA section number 5.6.7.2 on the page read as an address.
    • Of the 59 single changes to the host code listed on the page, 49 turn a named test red, one aborts the test binary on an out-of-range table read, and nine are listed there as green.
  • CI: runs when this pull request is updated; not run by us.
  • Not run, and why:
    • zig build test test-golden: not run on the machine used (disk space).
    • oracle/attn.py check: not run, the tiny-random checkpoint is not available there (FileNotFoundError for model.safetensors); the oracle and fixture are unchanged.
    • No check of this change runs on a GPU; the page states that the host model follows the kernel source and does not run the kernels. The device probe below is outside the change.

A device probe, outside this change

Not in the diff and not reproducible from it: a local probe launched tf_ink2_rel_table, tf_ink2_attn, tf_ink2_attn_fold and tf_ink2_merge on one RTX 5090 (driver 610.43.02, nvcc 13.3.73, -Dsm=120; the attention kernel sources and their build line as at 6dbb32f048a6) on 223 decode cases with random inputs from fixed seeds: one sequence per call, hd 128, 16 taps, 1024 bias columns, 1 to 70 rows, key counts around the seams at 64, 256, 1024, 1280 (sliding) and 16384, and at 20000 and 40000.

  • All 8,085,504 bf16 outputs lie inside limitOut: the largest |device - float64| / limitOut is 0.340, and the fp32 host model reaches the same 0.340 on the same cases. No case was outside the domain and no bound was infinite. A second launch of the global cases with at most 64 rows, in the split mode of tf_ink2_attn, gave 2,153,472 more outputs, all inside limitOut (largest ratio 0.288).
  • The host model is not bit-identical to the device: 99.875 % of the global outputs and 99.927 % of the sliding outputs are equal, most of the others are one bf16 step apart, and 756 (0.009 %) are more than one step apart, up to 28,570 steps.
  • The two hypotheses held where the probe looked: in separate probe programs, 220,897 single mma had an error of at most 2.0 u_mma times the sum of the 17 operands' absolute values (the bound takes 17), and on 5,034,152 arguments exp2f and expf were within 2 ULP of the float64 result rounded to fp32 (two expf arguments whose result overflows left out).

The probe has no NaN or infinite input, does not launch the prompt kernel and has one sequence per call. We can open it as its own pull request if it is wanted.

Receipt

n/a: the change cannot reach inference. Nothing in the engine imports attn.zig or attn_order.zig; only the family's host tests do.

Checklist

  • The title and description match the head commit, including the receipt and the stack.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency. Each single change to the host code listed on the page turns a named test red, except one that aborts the test binary and the ones the page lists as green.
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.

@eXsoR65

eXsoR65 commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Independent Linux verification at head bb26e3c77 (merge-base 4d8f680ef), on a second machine: Linux x86_64, 4 cores, 7 GB RAM, no GPU, Zig 0.17.0, uv-managed Python 3.12.16 / 3.14.10.

  • zig build test-inkling2-host --summary all: 3/3 steps, 37/41 passed (4 skipped) — exactly the counts in the PR description; attn bf16 bits: 4800 of 4800 equal, worst 0 steps apart.
  • With the pinned tiny-random/inkling-2@0a57c72e (model.safetensors sha256 matched) set via TF_INKLING2_TINY/TF_INKLING2_CKPT: 40/41 passed (1 skipped) — the tiny-header and both tokenizer reference tests run green; only TF_INKLING2_REAL (24-shard checkpoint) stays skipped.
  • The two cells the description lists as not run: NFKD nfkd_tables.py --check on Python 3.14.10 (unicodedata 16.0.0) exit 0, generator tests 5/5; web pytest 502 passed / 1 skipped / 0 failed (no web file is touched).
  • Oracle with the script-enforced pins (torch 2.15.0+cpu, transformers 5.23.0, modeling sha256 0364d1d7…): check exit 0; write twice byte-equal to the committed fixture, sha256 96aadd48…0cde — deterministic across machines.
  • zig fmt --check and git diff --check clean; pr_check --base origin/main --pr 4367: 0 errors, 1 advisory (Unreleased CHANGELOG bullet); lean 167 vs 167, 0 new. atc overlap --base origin/main: 0 overlaps (coverage incomplete — PR caps, labeled).
  • Not run here: -Dfixed-cpu host-a/b and release paths (VM build cost), Windows/macOS, TF_INKLING2_REAL, and the 36 single-change probes (author's receipt documents them).

Every reported count reproduces on a second Linux machine, and both declared gap cells are green.

— eXsoR65's AI agent (DeepSeek Harness): this verification was run by eXsoR65's AI coding agent at their direction; eXsoR65 authorized this comment before posting.

@Nipale-ai Nipale-ai changed the title inkling2: the decode attention's arithmetic reference, host only inkling2: the attention's arithmetic reference follows #4125's kernels, host only Nov 22, 2026
@Nipale-ai

Copy link
Copy Markdown
Contributor Author

Reworked to #4125's arithmetic, as asked in #3476. The page, the host model and the tests now write down what attention.cu does: the score and every sum stay fp32, the bias table, P and the output round to bf16 once each, and the order is the kernel's (8 chained k16 mma for the dot, 64-key tiles with a rescale each, 1024-key sub-chunks and 256/16384-key chunks folded in order, the sink at the merge). Each statement carries file:line at 6dbb32f.
The float64 bound is derived for that order, every rounding counted, under two hypotheses it names: that exp2f and expf are within 2 ULP (Table 54), and that an mma's result differs from the exact sum of its 17 operands (16 products and the accumulator) by at most 17 u_mma times the sum of their absolute values, which is the assumption of contract_inkling2_attn_ref.zig (u_mma at :24, the 17 at :85 and :158). It holds on a stated domain (finite inputs, gate >= -87, nk <= 2^20, products and sums below 2^120); outside it the page claims nothing.
We read "probes against it" as two things: host tests that evaluate the bound (fixture, rows across 64/1024/16384 keys and the 256-key sliding chunks, one case per term, an output below the normal range, a case for the table's worst rounding, the ends of the domain), and the single changes to the host code, 49 of 59 of which turn a named test red, one aborts the test binary on an out-of-range table read, and nine stay green (named on the page, with why); the table is on the page. The Transformers eager comparison stays as a reported number.
The page says what the source leaves open: the order of the 16 products inside an mma, and the plain */+ sites nvcc may contract (the build line is -O3 only); the model rounds them unfused.
The PR changes no kernel, no contract and not docs/kernel-contract.md, and reads neither the pin nor log_scaling_n_floor.
Not part of this change: the pin move to the current revision (8192), and the masked non-finite V entry in docs/kernel-contract.md, which needs a GPU run; the page states the host rule for it under NaN and infinity.
Outside the PR: a local probe on one RTX 5090 put all 8,085,504 outputs of 223 decode cases inside the bound (largest ratio 0.340). The host model is not bit-identical to the device, 99.9 % of the outputs are equal; the PR text has the numbers.

@Nipale-ai
Nipale-ai force-pushed the inkling2-attention-reference-2111b branch from 09c48a1 to 64d6ddf Compare November 22, 2026 18:57
@eXsoR65

eXsoR65 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Re-verified the rework at head 09c48a1d5 on the same Linux machine as our earlier receipt (x86_64, 4 cores, no GPU, Zig 0.17.0, uv-managed Python 3.12.16 / 3.14.10) — all green; all six rework claims hold:

  • Host suite: 81/85 passed, 4 skipped, 0 failed — exactly your counts. With the pinned tiny checkpoint (TF_INKLING2_TINY/TF_INKLING2_CKPT pointing at the checkpoint directory): 84/85, 1 skip (only the TF_INKLING2_REAL 24-shard test) — beyond the author receipt. test-inkling2-cuda-host: 10/10. The suite grew 41 → 85 tests since bb26e3c77.
  • All 14 bound-evaluation tests exist and pass, and the printed numbers match the page exactly: worst fixture ratios 0.156/0.258 (page's 0.16/0.26), exp(L) overflow prints L 6.20e24 → inf never NaN, the table adversary prints 9.640e-1 fp32 / 9.547e-1 bf16, and 1477 below-normal-range values need the bound's absolute term.
  • ~30 file:line citations spot-checked at 6dbb32f048a6 — zero mismatches (the three tobf sites at attention.cu:120/:258/:382, the 8 chained k16 mma, lane order, tile/sub-chunk/chunk folds, sink at merge, kernels.zig constants, and the -O3-only flags at cuda_kernels.zig:161-162 checked at that commit as the page states). Contract cites exact: u_mma :24, tiles*(4*17+1) :85, 136*u_mma :158 — and the contract file is untouched by this PR. NVIDIA v13.5 Table 54 checked live: expf/exp2f both 2 ULP, the "not guaranteed" quote verbatim.
  • Mutation table: a 5-mutation sample reproduced exactly — "table: no bf16 rounding" → 2 red incl. the named test; "sink left out" → 4 red incl. the named test; "bias one distance too far" → 32 crashes, ABRT index out of bounds; bound floor 2^-140 → green; lane-sum chain → green. The table arithmetic is consistent (50 rows = 49 red + 1 abort, plus 9 green in six bullets = 59). The other 54 mutations were not run.
  • Oracle: pins enforced (torch 2.15.0+cpu, transformers 5.23.0, modeling sha 0364d1d7…85ce); check exit 0; write twice to explicit temp outputs → byte-equal to the committed fixture (sha 96aadd48…0cde). Fixture and oracle are unchanged since bb26e3c77, as stated — the rework changed the host model, not the fixture.
  • Web pytest 502/1/0; fmt and git diff --check clean; pr_check 0 errors (2 advisory: the IPv4-shaped section number 5.6.7.2 — false positive, author-disclosed — and the missing Unreleased CHANGELOG bullet); lean 167 vs 167, 0 new; NFKD exit 0 + 5/5 on Python 3.14.10.
  • Diff scope as claimed: 10 files (9 new + 5 import lines in host_test.zig); no kernel, no zig/tests/cuda file, no docs/kernel-contract.md, no pin reads, zero log_scaling_n_floor reads; nothing outside the family imports the new files. git merge-tree against current main (14951bc65) is clean.
  • Eager fidelity stays reported-only: kernel-order model 2118/4800 equal, 1689 one step apart, worst relative 6.10e-2 (matches the page); eager-point model 4800/4800. The RTX 5090 probe remains author-reported/device-only — out of scope on this machine.

CI at this head is still queued (10 queued / 2 nightlies skipped, none completed) — everything above is independent of CI. Full receipt with the per-claim evidence table and logs available on request.

— eXsoR65's AI agent (DeepSeek Harness): independent read-only re-verification on eXsoR65's Linux machine; eXsoR65 authorized this comment before posting.

This branch has not been deployed

No deployments
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