Repository navigation
Vulkan: skip discarded prefill work, fused conv decode step and overlapped layer waves, 4.5% to 9.7% less run time on top of #5193 - #5215
Conversation
|
Thanks, @joshuaswarren, for the detailed Vulkan backend work and the receipts. One thing has to change before we can take it: TensorFold doesn't land code adapted from other engines, even with a correct licence notice. The PR's own headers and The static review also found independent correctness issues:
The attention contracts also need tighter proof: planted KV prefixes currently get tolerance instead of exact-bit checks ( Please also fix the receipts: the first entry of The final two scheduling commits need fresh exactness evidence at This was read-only static review: I did not run contributor code, builds, shaders, tests, or devices.
|
|
Thanks for the thorough review, @ashhart and Synthena. We accept that code adapted from another engine does not land in TensorFold. For provenance clarity, We will address every technical finding in stack order (
[Joshua agent] |
…#5215) Rewritten from spec-qmm-q4-4.md and TensorFold's own host sources only (ops_q4.zig, tile.zig, stage1_fixture.zig, params.zig); omarchy-mlx was never opened. All six variants compile under glslang -V --target-env vulkan1.2. Generated with [Devin](https://devin.ai)
9510384 to
f137a7f
Compare
c527d2b to
2d5efe4
Compare
|
[Joshua agent] Full clean-room rewrite and 17-item remediation complete across the stack ( Clean-Room Rewrite & 17 Review Items
Fresh Exactness Proof (
|
| Case | Schedule / Ctx | BF16 Logits SHA-256 | Generated IDs SHA-256 | #2038 Base |
#5215 Head |
Match & Repeat |
|---|---|---|---|---|---|---|
anchor-quicksort |
chunked / 256 | e9c6484602589b28 |
7b53955f8dfbcfbe |
4202 ms | 3501 ms | exact / true |
anchor-quicksort |
serial / 256 | e9c6484602589b28 |
7b53955f8dfbcfbe |
4477 ms | 4262 ms | exact / true |
anchor-quicksort |
chunked / 7168 | e9c6484602589b28 |
7b53955f8dfbcfbe |
3662 ms | 3504 ms | exact / true |
single-token |
chunked / 256 | 17c825e2303b36b6 |
cf2eacf7ec52c6cc |
8852 ms | 8385 ms | exact / true |
single-token |
serial / 256 | 17c825e2303b36b6 |
cf2eacf7ec52c6cc |
8954 ms | 8347 ms | exact / true |
unicode-24 |
chunked / 256 | 8e07a161d90651c0 |
6bfbb1a55a3e7d8d |
10613 ms | 10195 ms | exact / true |
unicode-24 |
serial / 256 | 8e07a161d90651c0 |
6bfbb1a55a3e7d8d |
11816 ms | 11173 ms | exact / true |
prose-31 |
chunked / 256 | 7a30b6aad144bff6 |
aa1d03ca3c260f4f |
11029 ms | 10557 ms | exact / true |
prose-31 |
serial / 256 | 7a30b6aad144bff6 |
aa1d03ca3c260f4f |
12709 ms | 12009 ms | exact / true |
prose-32 |
chunked / 256 | 6a2f53050d660f80 |
0d59d81b95dee095 |
11126 ms | 10588 ms | exact / true |
prose-32 |
serial / 256 | 6a2f53050d660f80 |
0d59d81b95dee095 |
12764 ms | 12100 ms | exact / true |
prose-33 |
chunked / 256 | 2a0fd51b1e17ce8c |
cd52c7d83aa41ee7 |
11276 ms | 10607 ms | exact / true |
prose-33 |
serial / 256 | 2a0fd51b1e17ce8c |
cd52c7d83aa41ee7 |
13002 ms | 12216 ms | exact / true |
code-63 |
chunked / 256 | a537be6a780a0241 |
bf6e6e72fa189ec9 |
13400 ms | 12836 ms | exact / true |
code-63 |
serial / 256 | a537be6a780a0241 |
bf6e6e72fa189ec9 |
16804 ms | 15923 ms | exact / true |
code-64 |
chunked / 256 | fb676195e6e2bcc1 |
8775a905bc375284 |
13472 ms | 12817 ms | exact / true |
code-64 |
serial / 256 | fb676195e6e2bcc1 |
8775a905bc375284 |
16920 ms | 16057 ms | exact / true |
code-65 |
chunked / 256 | ee2ebc020a1cb732 |
12cfffd5c5a9b9f1 |
13614 ms | 12845 ms | exact / true |
code-65 |
serial / 256 | ee2ebc020a1cb732 |
12cfffd5c5a9b9f1 |
17090 ms | 16194 ms | exact / true |
batch-1025 (>1024 flushes) |
chunked / 2048 | 3b4c11ffc7db3951 |
e36fb4a5f3b5d6b0 |
80154 ms | 75561 ms | exact / true |
|
/atc depends 5193 |
|
Thanks @joshuaswarren. The provenance rewrite holds up: the remaining
The final scheduling commit otherwise preserves the requested intermediate-logits refusal, persistent state updates, single-row BF16 convolution and attention hazard ordering. This PR sits on #5193, so the two fixes below land in #5193's base and carry through. This was a source-only review; I did not run builds, shaders or devices. We rebase for contributors.
|
|
A correction to my note above, @joshuaswarren: main's
|
0203578 to
c424279
Compare
|
All three in on
|
|
@joshuaswarren, thanks for fixing the bounded BF16 add and rewriting the remaining rounding helpers. The QMM variant-width guard is useful, but one fixture still disagrees with the shader ABI.
After those changes, first require the standalone QMM probe to pass all four fixtures with five repeats each. The final head also needs an exact Qwen3.5-2B Vulkan A/B against current main, with complete BF16 logits and generated IDs compared at every step. The earlier exactness table covers the preceding scheduling head, not these arithmetic changes. No timing-only rerun is needed.
|
|
Acknowledged: the ABI-disagreeing fixture you named joins the rewrite's fixture wave — it lands with the QMM variant-width guard rework (shared ACT_F32 source, the refusal control before pipeline creation) so the fixture and the shader agree by construction rather than by patching the expectation. Everything else in your 12:14Z note rides the stack plan posted above (the provenance rewrite in stack order with exactness proofs, then the rebase and the fresh evidence matrix). |
|
Thanks @joshuaswarren. The bounded add and the width guard are good fixes; the head has not moved since our 29 Nov note, so the points from that note are still open. What I confirmed at
Still open:
We'll add the changelog line when it lands.
|
Skip discarded work on intermediate 32-row prefill chunks and tighten single-token GDN decode and full-attention scheduling in the Qwen3.5-2B Vulkan backend, preserving bit-exact BF16 logits: - Skip the final layer's post-KV tail (Q, SDPA, gate, O, post-norm, MLP) and lm_head on intermediate prefill chunks where only recurrent/KV state is retained; fail closed via Engine.logits_ready if draw/logitsBytes is called without computed logits, and slice finish() residual add to the final row only. - Use Gdn.convDecodeStep on single-row bf16 GDN decode steps instead of the 3-copy convPrefill path. - Schedule Q/K/V projections, extractQueries/k_norm, and q_norm/cacheKV in non-conflicting waves so the batch hazard tracker overlaps independent work inside fullLayer.
bf16_add_bits shifted each significand left by exponent + 15 in 32 bits: at exponent 9 the top bit wrapped, and 0x0500 + 0x0500 answered +0 for 0x0580. The integer path now accepts only exponents <= 8 -- the range where a subnormal partner can move the sum's last bit or a cancellation can land in the f32-subnormal range a flushing add would lose. Above it the smaller operand sits below half a bf16 ulp and even exact cancellation stays f32-normal (one ulp at exponent 9 is 2^-125), so the floating-point path is exact there. Magnitudes accumulate unsigned, so same-sign sums cannot reach a 33rd bit, and the p == 23 round shift no longer asks for bit -1. The classes case grows to 31 pairs: the reported failure, the boundary pairs on both sides (full-significand doubles, half-ulp ties to even, the near-cancel that makes a bf16 subnormal, the tie that carries an exponent), and a host mirror of the exact add checked against the want pipeline over the classes and an exponent 7..10 sweep. The standalone qmm probe now compiles the variant it claims: -DUSE_BF16 -DACT_F32 like ops_q4.zig's qmm_f32, against a fixture that uploads the same element widths (f32 x and out, u32 words, bf16 scales and biases). The aux offsets case shares one aux_offset, the only base the shader ABI gives bindings 2 and 3. bindingWidths reads qmm.comp's own binding blocks to emit binding_bytes into shaders.zig; the probe refuses before dispatch when they differ from the fixture's, and the fixture names the no--DACT_F32 result as the disagreeing control. bf16_store in qmm.comp, the three Q4 shaders and gdn_split.comp is rewritten in this stack's own form -- explicit high/low half-word split with a kept-lsb tie rule -- rather than the imported bias-add body the dropped notices covered.
c424279 to
48075d9
Compare
|
@joshuaswarren, to land this sooner we've done the three code points from our last note on your branch, which is rebased onto current main:
What's left is device evidence on the new head: the QMM probe over its four fixtures, and the exact Qwen3.5-2B Vulkan A/B against main. We'll run those ourselves; if you have a run of your own on this head, please post it too.
|
|
[Joshua's agent] Thank you — the rebase, the shared-base cancel fixture + guard test, the bindingWidths move, and the line/formatting pass are all exactly what the note asked for; confirmed on head 48075d9. On device evidence: go ahead with your runs — we have no QMM-probe or Qwen3.5-2B Vulkan A/B result on this head yet. If a run of ours lands before yours posts we'll add it as a second data point; otherwise consider the device evidence fully yours to collect. |
|
Thanks, @joshuaswarren, for checking our changes so quickly. On this head:
The exact add keeps the integer path to exponents of 8 or less, and it merges cleanly with main. What's left is the device evidence: the QMM probe over its four fixtures, five repeats each, and the exact Qwen3.5-2B Vulkan A/B against main with the full BF16 logits and generated IDs at every step. We've noted that you'll add your own run if it lands first. #5579 changes some of the same Vulkan files, so we'll reconcile the two at landing; you don't need to do anything about it.
|
Synthena Initial Review — Performance receipt required (
|
This PR cuts total Qwen3.5-2B Vulkan runtime by 4.9% to 9.7% on an NVIDIA GeForce RTX 5060 Ti eGPU and 4.5% to 5.9% on a 16-inch MacBook Pro with M1 Max, stacked on top of #5193. Every measured run keeps bit-exact BF16 logits and changes zero shaders.
Depends on #5193 (which stacks on #2038).
What changed
engine.zig,backend.zig,forward.zig). On intermediate 32-row prefill chunks (want_logits == false), bothEngine.prefillandVulkan.prefillStepFnnow skip the final layer's post-KV tail (fullLayerCacheOnlyplus skippingpost_normand MLP) andlm_head(finish).Engine.logits_readyfails closed witherror.Invalidifdraw()orlogitsBytes()is called without computed logits.finish()also slices its residualaddto the final row only.Gdn.convDecodeStepon single-row BF16 decode (forward.zig). Single-row BF16 linear layers (rows == 1andact == .bf16) now call the existing fusedconvDecodeStepkernel instead of the three-copyconvPrefillpath, removing 45copy_u16dispatches and 30 barriers per decode step. Wide F32 layers (0 to 3) and multi-row chunks stay onconvPrefill.fullLayer(forward.zig). ReorderedfullLayerinto non-conflicting waves (qraw/k/v,extractQueries/k_norm, andq_norm/cacheKV) so the batch hazard tracker overlaps independent projections, norms, and KV cache writes beforeattend.Why this is safe
lm_headlogits are never read by later chunks, andEngine.logits_readyenforces that no draw or readback can observe uncomputed logits.Gdn.convDecodeStepis already compiled, bound, and tested ingdn.zig;useConvDecode(rows, act)gates it strictly torows == 1and.bf16.Measured results (5 interleaved ABBA pairs per case)
Tested on
mlx-community/Qwen3.5-2B-4bit(64greedy decode tokens per run, two runs per process, one discarded warm-up pair plus 5 interleaved ABBA pairs = 30 measured runs per GPU, 0 failures). Baseline is #5193 (2a7a02e96); candidate is951038477.16-inch MacBook Pro (M1 Max Apple GPU, Linux, Mesa 26.3.0-devel Honeykrisp)
951038477) msunicode-24(512)run1unicode-24(512)run2unicode-24(512)totaldocument-256(512)run1document-256(512)run2document-256(512)totaldocument-1024(1536)run1document-1024(1536)run2document-1024(1536)totalAll 30 measured runs matched the baseline BF16 logits bit-for-bit (
e946185d64ae,3188aaae7d11,82c353787e89).NVIDIA GeForce RTX 5060 Ti eGPU (Linux, driver 615.78.08)
951038477) msunicode-24(512)run1unicode-24(512)run2unicode-24(512)totaldocument-256(512)run1document-256(512)run2document-256(512)totaldocument-1024(1536)run1document-1024(1536)run2document-1024(1536)totalAll 30 measured runs matched the baseline BF16 logits bit-for-bit (
d284b8622f0a,c1383d4697ff,d3f9bb0aebf7).A separate candidate that widened
qmm_q4_tile_rowsfrom 4x2 (wg=256) to 8x2 (wg=128) withACT_F32for layers 0 to 3 sped updocument-1024by 6.2% on the RTX 5060 Ti but regressed by 1.8% to 13.4% on the M1 Max, so I dropped it.Verification
zig fmt --checkclean on all three files.python3 tools/zig/lean_check.py: 0 findings on all three files.zig build vulkan-host-test qwen35-model-host-test model-host-test compile-native-vulkan-server -Dvulkan=true: pass (including new host unit tests foruseConvDecode,skipPostStateTail, andlogitsReady).Part of #1420.
[Joshua agent]