Repository navigation
gpu: make the tiled and single-dispatch decode paths byte-identical in f32 (stacked on #3431) - #3640
joshuaswarren wants to merge 62 commits into
Conversation
|
[Joshua's agent] Cross-model review pass at head 00f8467 (reviewer model moonshotai/kimi-k3.1-pro, independent of the implementing model):
Reviewer summary: "numerically sound and appropriately scoped ... aligns the single-dispatch decode path with the tiled kernels on the one rounding site that actually mattered." Supporting measurements in the PR description: attention-contract passed=47 total=47 on both the RTX 5060 Ti (gpu-timeouts delta 0) and the 13-inch M1, probability arrays byte-identical at 7200, 7201 and 7205 keys. |
3f95be3 to
0727e05
Compare
0727e05 to
3f95be3
Compare
|
[Joshua's agent] A note on the two force-pushes above from this morning (11:20 and 11:24 UTC). A rebase attempt on our side went wrong, and for four minutes this branch pointed at a commit that would have undone unrelated files on main. I have put the branch back to |
|
Thanks @joshuaswarren for isolating the f32 divergence to P·V accumulation and running the 47/47 attention contract on both the RTX 5060 Ti and the 13-inch M1. The explicit-fma changes are narrowly scoped, and the probability-array comparison separates scores from PV as intended. Two exactness issues remain before this stacked branch can land. First, the unresolved #3431 ask still stands: its default 256-key token parity has not been closed by this separate f32 fix. Second, this PR deliberately changes NVIDIA f32 accumulator bits by one to two ulp; please add served greedy token IDs from the same rebased parent and head for the tiled and single-dispatch routes, including the 511/513/1025-key boundary cases. Kernel output parity alone does not show whether served tokens stay unchanged. The supplied diff also conflicts with current main in
|
|
Verified on a different card than the author's runs: jw16 (16-inch), NVIDIA GeForce RTX 5060 Ti (0x2d04), driver 615.78.08, head
The f32 divergence this branch fixes (plain multiply-add in the P·V accumulators vs the decode path's explicit FMA) is resolved on Blackwell sm_120 as well as the author's card. +1 from a third-party repro. — lane w7S |
|
@joshuaswarren Thanks for pinning both P·V accumulation sites to explicit FMA and for the independent 47/47 contract results. Three small items remain:
|
…erved tokens asserted per path ashhart's three ashhart#3640 review items: - sdpa_decode_native.comp's adaptation count covers the two FMA pins (3 lines adapted); - the opening comments of sdpa_scores_tile.comp and sdpa_softmax_rows.comp wrap below 120 columns; - probe_keys gains the default 256-key decode and the 511/513/1025 routing boundaries, and probsGate derives each path's served greedy token from its output row (largest wins, ties to the lowest index, non-finite refuses) and fails unless tiled and single-dispatch serve the same token; the MILESTONE line prints tokens single=<id> tiled=<id>.
|
All three items are in at
The contract binary compiles clean on the Linux host ( |
|
Both landing requirements are met. Commit 9e5b3db on w7p/sdpa-fma-consistency:
Head: 9e5b3db |
|
Note 🤖 GPT-6.2 Sol responding on behalf of joshuaswarren The previous reply called the new probe IDs served tokens. The probe takes the argmax of kernel output rows. The server comparison is still open: matched parent and head, tiled and single-dispatch execution, at 256, 511, 513, and 1025 keys. I will keep the kernel and server results separate. |
|
Thanks for staying on this, and for correcting the record on the probe in your last comment. Where this stands against main: main's Vulkan decode path is Your last comment is the right split: the probe takes the argmax of kernel output rows, and the server comparison (matched parent and head, tiled and single-dispatch execution, at 256, 511, 513 and 1025 keys) is still open. Keep that separation; the served-token assertion is the part that matters to us, because a 1-2 ulp f32 difference between the paths can flip a greedy tie. What we need from you, and only you can supply it:
The shader notice and copyright fix in Status: waiting on #5215 (and on your re-verified runs), then ready for our review. No device run of ours is possible for non-Apple Vulkan; the Apple-side check we can run is the M-class Vulkan gate on our Macs against your posted NVIDIA output.
|
2b690e3 to
9e5b3db
Compare
…m kernels Header becomes Copyright 2026 Joshua Warren; MIT on the omarchy-mlx-derived shader files; the licence file and the credits lines stay. Kernel comments now describe our own push-constant layout and macro names instead of the other backend's names. ASH_QWEN35, ash_gate and gated_delta_prefill_ash.comp become ORACLE_QWEN35, oracle_gate and gated_delta_prefill_oracle.comp. SPIR-V check: 25 server shader images compiled at the base and the modified tree with the shipped glslang flags. 24 of 25 are byte-identical. gdn_split_bf16 differs only by the embedded oracle_gate identifier; with debug names stripped both objects hash the same (01d232c2cdc2).
The choices field showed every backend name on every build; the served set was already computed for the honoured values. Listing what the binary does not serve invited --backend vulkan attempts on builds that refuse it.
…e gated refusal Choices equal to the served set made an unserved pick fail the choice check first, whose message main's flag-deps pin does not expect; the pin expects the gated refusal for an unserved value. Take main's choices form so the choice passes and the native gate speaks: main's zig job passes this test at 6e9af93 and fde40f6, and the failure showed only on this stack's merge (run 42815854414, flag_deps_test.zig:45).
…y-mlx shaders The Vulkan build's own choices gate already carried vulkan, but the flag's static choice list did not, so --backend vulkan died as an invalid choice before the served check could refuse it on other builds. The cli test now covers both builds: it parses on the Vulkan build and gets the gated refusal elsewhere. The credits line said all of the engine's compute shaders were new work; fifteen of them and omarchy_trig.h come from omarchy-mlx unchanged, and the list now says so. Proved: zig build test-flag-deps-host failed first on the unfixed choice list, with the invalid-choice refusal instead of the gated one, then passed 22 of 23 with one skip without Vulkan and succeeded with -Dvulkan=true.
The first figures (40% to 52%, against ashhart#1544's head) came from builds before the float32 fix, on an older kernel and Mesa build. On the fixed head, on a 13-inch M1 with Linux 7.3.0-2-20261110.1-lab-ARCH, the three cases took 34.8%, 41.0% and 43.0% less time than the same head with the scalar kernel selected (three pairs for the first two, two pairs for the 1,024-token document, one logits digest per case). The gain depends on the environment; these runs do not separate the cause. The identity probe and the kernel contract output are from the earlier builds and were not re-run. Signed-off-by: joshuaswarren <816217+joshuaswarren@users.noreply.github.com>
…-inch M1 The receipts folder holds the identity probe (21 of 21, the tiled image byte-identical on all 12 projection cases), the contract runner's output (3 kernels pass gates 1 to 4, 8 of 8 controls caught), the pre-registration with its addendum, a summary generated from the raw run files, and the SHA-256 of each raw file. Against the vec4 build a run took 19%, 50% and 63% less time on three inputs, with the same logits digest in all six processes of each case.
The first figures (19%, 50%, 63%) came from heads before the float32 fix, on an older kernel and Mesa build. On the fixed heads, on a 13-inch M1 with Linux 7.3.0-2-20261110.1-lab-ARCH, the three cases took 10.9%, 35.6% and 44.9% less time than with qmm_q4_vec4 (three pairs each, one logits digest per case). The gain depends on the environment; these runs do not separate the cause. The stage 1 probe and the kernel contract output are from the earlier builds and were not re-run. Signed-off-by: joshuaswarren <816217+joshuaswarren@users.noreply.github.com>
…y-mlx shaders The Vulkan build's own choices gate already carried vulkan, but the flag's static choice list did not, so --backend vulkan died as an invalid choice before the served check could refuse it on other builds. The cli test now covers both builds: it parses on the Vulkan build and gets the gated refusal elsewhere. The credits line said all of the engine's compute shaders were new work; fifteen of them and omarchy_trig.h come from omarchy-mlx unchanged, and the list now says so. Proved: zig build test-flag-deps-host failed first on the unfixed choice list, with the invalid-choice refusal instead of the gated one, then passed 22 of 23 with one skip without Vulkan and succeeded with -Dvulkan=true.
…-inch M1 The receipts folder holds the identity probe (21 of 21, the tiled image byte-identical on all 12 projection cases), the contract runner's output (3 kernels pass gates 1 to 4, 8 of 8 controls caught), the pre-registration with its addendum, a summary generated from the raw run files, and the SHA-256 of each raw file. Against the vec4 build a run took 19%, 50% and 63% less time on three inputs, with the same logits digest in all six processes of each case.
…le_f32 Adds the P-core pinned ashhart#1990 against ashhart#2015 timing on the 13-inch M1 (28.6%, 54.4%, 66.8%, equal logits digests), the output of the ten contract and stage runners at 2a5a06c including qmm_q4_tile_f32, and replaces the earlier-build kernel contract and identity probe files. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
Fix the stale shader filename and separate unchanged from adapted sources. Record source commit, full ELF hashes, and sizes from retained receipts. Verified the JSON summary parses; historical build commands are absent.
The bf16 SDPA kernel stages f32 scores in shared memory: 7168 keys with the query row and reduction tree fill the 30720 B budget, and Apple's Vulkan limit is 32768 B, so 8192 keys cannot sit in shared memory. Add a -DSTREAM_GLOBAL twin of the kernel that stages scores in a coherent f32 storage buffer (one total-keys region per workgroup, memoryBarrierBuffer at the two write-to-read points). Same arithmetic and order as the shared arm. attend() picks the twin only when total keys exceed 7168; forward.zig allocates the stream from scratch only then. The shared-memory images below 7168 keys are byte-identical to the previous SPIR-V. max_visible_keys and the engine capacity become 8192; the scratch arena grows to 32 MiB. Contract cases cover 7169, 8192 decode and 64 rows ending at 8192. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
The PV tile zeroed a future key's probability but still multiplied it by that key's value, and 0 * NaN is NaN, so a non-finite value sitting in the cache's future rows poisoned every earlier row's output. Each thread now computes its row's visible bound once and accumulates only keys below it; the per-output order over visible keys is unchanged. Proved on a 16-inch M1 Max: with the contract's new planted poison (following commit) the tiled output diverges from per-row decode at element 0 before this change and is byte-identical after it, and a 6173-token served prompt through 97 tiled chunks produces the same greedy tokens and byte-identical logits on both driver stacks. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
…tiled prefill Three tiled cases (rows 2, 17, 64) plant NaN, +inf and -inf over keys past row 0's bound but inside the loaded chunk, and past the served keys into spare capacity. The rows 2 case is the review's repro: zero Q/K, V0 = 1, V1 = NaN, so row 0 must come out exactly 1.0. Each case runs the tiled path and the replaced per-row decode kernel and requires byte-identical complete outputs, finite outputs for the rows whose windows hold only finite values, and non-finite outputs where the plants are genuinely visible, on the bf16 and f32 paths. On the pre-fix shader all three cases fail at element 0 on a 16-inch M1 Max; with the causal-bound guard all six case runs pass and the whole gate reads passed=21 total=21. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
CI fmt job flagged contract_attention_main.zig. Whitespace only.
qmm_q4_tile and qmm_q4_tile_f32 ran one thread per output column with a serial walk over k, so a 2048-column projection filled 8 workgroups. They now run one workgroup per (row, 32-column tile) with 8 slices of k and add the slice sums in slice order. The f32 sum order changes; the dequantisation of each weight does not. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
M2 Max and M1 decode A/B on clean-tree builds with identical greedy token hashes, and the QMM kernel contract for base and head. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
…thin 120 columns Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
The runner compiles the pre-k-split qmm_q4_tile as a test-only reference image (byte-identical SPIR-V to the oracle tree's build) and compares its complete output bytes against the current image in one process: Qwen3.5-2B's real decode and prefill shapes, n tails, k 64/128/192/2048/6144, stress fills, six seeds, both activation widths, and an explicit finite-BF16 case whose serial sum is 0. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
The k split kept the occupancy win but summed k in eight slice pieces and added the partials afterwards, so the logits were not byte-identical. The workgroup now dequantizes each two-group chunk of weights into 16 KiB of shared memory with all 256 lanes, and only the 32 slice-0 lanes walk k over that chunk in the old ascending order with the old single accumulator, the old x reads and the old dequant expression, so the compiler contracts the same multiplies and adds in both images. Chunks, grid, tails, rows and the block loop keep the previous semantics; the 16 KiB both tile variants use is declared in the launch records, which the k split never did for its 1024 B of partials. On-device exactness: the runner from the parent commit compares every output byte against the pre-k-split reference image. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
A group has one scale, so nibble-1 terms inside the scaled group came out at 2^21 and the planted example no longer showed the maintainer's clean serial 0 against a slice reorder at 64. The pattern now fills whole groups: groups 0 and 8 are nibble 8 at scale plus and minus 2^21, group 1 is nibble 1 at scale 1, everything else is nibble 0, so the serial sum is exactly 0.0 and any slice reordering is not. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
… plant the cancel fill only for the cancel cases Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
…ow over 511 to 2048 keys The attention gate compared tiled output against attend() at one row as the old path, but attend() now routes one row over 512 or more keys through the tiled path, so that reference is the tiled path itself there. The decode cases check against an f64 reference inside a bound, not byte for byte, and the tile cases use dyadic data whose f32 sums are exact, so a different summation order never shows. attendOn(path, ...) forces the single or the tiled dispatch; attend() is attendOn(.auto, ...) with the same routing. routeGate decodes one row both ways for 511, 512, 513, 1024, 1025 and 2048 keys, bf16 and f32 queries, dyadic and uniform data, and prints the mismatch count and max abs difference. Built for aarch64 with stage3-gpu-build; not yet run on a device. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
The single-dispatch decode arm computes every product with explicit fma; the tile kernels used plain mul-add and left contraction to the driver. Apple contracts both the same way, so the gate was byte-identical there; the NVIDIA driver does not contract the plain adds, and the f32 tile path then diverges from the f32 decode path by one to two ulp whenever a product is not exact: the attention gate failed 3 of 24 route cases (f32, uniform data, visible keys 511, 513, 1025) and the f32 poison tile cases on the RTX 5060 Ti while the 13-inch M1 passed 33 of 33. Dyadic data hides it because every product is exact. Explicit fma pins the same rounding on both paths. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
496 and 512 are 16-aligned controls; 508, 510, 511, 513, 514, 1025 are partial-tile cases. On the RTX 5060 Ti the f32 tile path mismatched only at partial key tiles (511, 513, 1025); the sweep tests whether the boundary is exactly keys mod 16. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
…stream probsGate runs the single dispatch through the long (stream) image and the tiled path at 7200/7201/7205 keys, then byte-compares the probability arrays themselves: the single probs from the f32 stream, the tiled probs from the score scratch (probsScratch accessor, Attention.tile_rows exposes the head stride). A prob mismatch convicts scores/softmax; equal probs with differing outputs convict PV. The RTX 5060 Ti sweep pinned the f32 divergence to partial key tiles; this splits which stage rounds differently. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
…udget The first probe fetched the whole 4M-word score scratch through the gate's 1 MiB staging buffer and corrupted the heap (glibc double-free abort on the M1 before any probe line). Fetch each head strip with a source offset instead: 256 KiB per head, well inside staging. The unused probsScratch accessor goes with the fix. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
The chase commits left comment paragraphs past the house lean rules (past 120 columns, consecutive comment lines, a 669-line test file). Merge each pair into a single line under the cap, move the tiled prefill gate into contract_attention_tile.zig, and zig fmt the qmm gate file. tf-attention-contract cross-compiles clean after the split. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
…erved tokens asserted per path ashhart's three ashhart#3640 review items: - sdpa_decode_native.comp's adaptation count covers the two FMA pins (3 lines adapted); - the opening comments of sdpa_scores_tile.comp and sdpa_softmax_rows.comp wrap below 120 columns; - probe_keys gains the default 256-key decode and the 511/513/1025 routing boundaries, and probsGate derives each path's served greedy token from its output row (largest wins, ties to the lowest index, non-finite refuses) and fails unless tiled and single-dispatch serve the same token; the MILESTONE line prints tokens single=<id> tiled=<id>.
Three-way merge against the shared attention commit restores the stack's tile machinery (scores/softmax/PV pipelines, STREAM_GLOBAL long arm, score scratch) alongside upstream's quantTiled rewrite, gdn_split contract source, hybrid module imports, and qwen4-ultra/tfQ6 build steps. Generated with [Devin](https://devin.ai)
9e5b3db to
273a643
Compare
The reconcile merge interleaved a pre-cli_flag cli.zig with upstream's extraction (unclosed Flag struct, duplicated --tp and Test tail) and dropped ComposedParams, ComposedSdpa, sdpaF32Gate, poison32, unchanged32 and plantRefuseF32 out of contract_attention_main.zig while keeping their callers, plus the composed parameter on sdpaGate. - cli.zig restored to upstream; the stack's backend-choices work is already upstreamed (full choices, gated native_values) - composed baseline decls restored; sdpaGate takes composed again; sdpaF32Gate restored with the branch's f32 stream plumbing ported - the duplicated tile_cases loop and inline sdpaTileGate removed; the tile module stays the single copy - route/probs routing-boundary gates moved to contract_attention_route.zig: the file was over the 600-line lean bound, matching the tile split's shape - lean: one 120-column comment shortened, a three-line comment merged Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
What
Routes 1-row Vulkan decode over 512 or more keys to the tiled SDPA path and pins both tiled and single-dispatch score/PV accumulators to explicit
fma()so f32 outputs stay byte-identical across Apple and NVIDIA drivers.#5215:#5215does not includedecode_tile_keys/attendOn()1-row decode routing (>=512keys), explicitfma()pinning in SDPA score/PV accumulators, or therouteGate/probsGatef32 cross-vendor byte-parity contract checks.fma(): tiled score and PV accumulators (sdpa_scores_tile.comp,sdpa_pv_tile.comp) and single-dispatch decode PV accumulation (sdpa_decode_native.comp).routeGate(dense sweep around 512 keys) andprobsGate(shared-probs byte comparison and served greedy-token assertion per path).Tested
attention-contractafter both FMA fixes:passed=47 total=47on RTX 5060 Ti (0 GPU timeouts) andpassed=47 total=47on 13-inch M1.located=8head strips, 12/12 datasets on 13-inch M1, 0 differing probability words at 7200/7201/7205 keys).#5215/main(waits on#5215due to file overlap, and#5193onmainrewrotesdpa_decode_native.comp, so single-vs-tiled byte-identity must be re-verified on GPU after rebasing onto#5215).Risk
#5193rewrotesdpa_decode_native.compwith 16-key sub-block accumulation and end-of-loop division bydenom; reconciling exact bit-identity against the tiled path needs fresh GPU verification on both Apple and NVIDIA after#5215lands.attention.zig, and attention-contract files touched by#5215.Closes #3846.