Repository navigation
gpu: split k across 8 slices in the decode q4 tile matmul - #3435
joshuaswarren wants to merge 448 commits into
Conversation
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>
The f32 layers kept row weights because qmm_q4_tile only read bf16 x. ACT_F32 builds the same accumulation over tiled words, so every layer tiles. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
A planted-input fixture runs qmm_q4_tile_f32 through gates 1-4 with f32 activations: x is the bf16 values widened exactly, and the output is f32 with the same guard words. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
The zig lean check allows one-line comments within 120 columns only. Comment change, no code change. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
…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>
…ce per batch Every Pipeline.run was its own submission with a fence wait. On a 13-inch M1 the 31,104 dispatches of a 64-step Qwen3.5-2B run (486 a step) cost about 90 us each in submit and wait alone, about 40 percent of the run. Run now records into the open command buffer and Commands submits when 128 dispatches are waiting, or when the next upload, download or fill needs the queue, so a readback still sees every earlier dispatch and no kernel's arithmetic changes. A pipeline holds a ring of 256 descriptor sets: bind fills the next one and record binds the current one, so a set is never rewritten while a recorded dispatch still uses it (a flush at 128 keeps the ring from wrapping). Commands.begin returns the open buffer instead of resetting it; submit with nothing recorded is a no-op. Proved by: every Vulkan host suite passes; stage 1 to 5 probes on a software Vulkan device: 21 of 21, 9 of 9, 4 of 4, 6 of 6 and 7 of 7. Not measured on a GPU yet.
…on a 13-inch M1 The receipts folder holds the stage 1 probe on the GPU (21 of 21, the tiled image equal to the default image 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, a summary generated from the raw run files, and the SHA-256 of each raw file. Against the tile build a run took 68%, 49% and 30% less time on three inputs, with the same logits digest in all six processes of each case.
…nd that the fence timeout covers a batch
…ontracts gates.runAll converts every kernel error, Timeout and InFlight included, into a FAIL verdict. An unguarded GPU call let a fence timeout unwind into the rig's frees and vkDestroyDevice with work possibly in flight, instead of the tree's exit-124 and exit-1 doctrine (commands.zig:104,122). Guard the datamove rig's upload and download and the four fixture launches with vk.orExit. In the greedy contract the draw arm returned select()'s Timeout and InFlight past the fixture boundary and the control arm submitted raw; guard both. A NonFinite refusal still reaches the refused marker. Host tests lock the swallowing channel and the refusal.
…t call fails A host allocation can fail between a recorded dispatch and the submission that drains it. gates.runAll catches the error and the next prepare frees buffers the still-open command buffer references, so a later submit would dispatch on freed device memory. Commands.discard resets the open command buffer and clears the batch state. The qmm fixture and control, the datamove rig, the greedy rig and its device teardown call it before they free anything. Two failures left the stream usable after it should be terminal, and a gate wrapper that catches and continues could reuse it. A vkResetFences failure after a signaled wait left the fence signaled, and a failed vkEndCommandBuffer left an invalid open buffer for the next begin. Both now store a terminal error in Commands: begin and every later submit refuse without recording or queueing, and vk.orExit ends the process. Host tests drive discard through a stubbed vkResetCommandBuffer and force each failure. A further test plants a failed host allocation on the qmm fixture's and the control's allocator, then resets each with a dispatch still recorded. Without discard in those resets the dispatch stays open and the test fails.
Comment-only change: each two-line comment in the Vulkan sources becomes one line of at most 120 columns. Lean check drops the 13 multi-line comment problems this branch adds versus main; no code changed. Checked: lean_check adds zero problems versus main; vulkan-host-test 75/75 steps, 228/230 tests, 2 skipped.
The first figures (68%, 49%, 30%) 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 57.8%, 34.1% and 16.6% less time (three pairs each, one logits digest per case). Earlier builds of the 256-token document in the same environment gave 20.5%, so the gain depends on the kernel and Mesa build; 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>
…rted preservation
…rows Brings main 41a72fd through ashhart#2016's merge. No conflicts. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
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>
Shorten the comments that the 8192 change and the chain left past 120 columns or on two lines, so tools/zig/lean_new.py reports 0 new problems against main. Comment-only change. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
…rows Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
…zards Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
TF_VK_DECODE_BENCH=N prefills the prompt, runs 8 warm-up tokens and N greedy tokens with readback, and prints ms per token, tokens per second and an FNV hash of the token ids. Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
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>
Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> # Conflicts: # CHANGELOG.md
…-ksplit 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>
Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> # Conflicts: # CHANGELOG.md
|
[Joshua's agent] Main moved again (5c82ef2) and its new CHANGELOG.md entries collided with this branch's, so I merged main once more (head b3f7f14, merge commit, no force-push) and moved this branch's own entries to the bottom of the Unreleased list, where main's top inserts no longer touch them. No code changed since the last head, so the earlier A/B and contract results stand. |
|
[Joshua's agent] Independent review of the kernel and dispatch commits ( Non-blocking notes from the review:
|
…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>
|
[Joshua's agent] The lean check failed on this head: one comment in ops_q4.zig went past 120 columns, and zig/tests/vulkan/qwen35_model_probe.zig was 621 lines (limit 600). New head: 6852ec6. The decode bench moved from the probe into zig/tests/vulkan/qwen35_decode_bench.zig, which leaves the probe at 593 lines. Same bench, same DECODE-BENCH output line, no kernel change. Pushed as plain commits with no rebase. |
|
[Joshua's agent] 13-inch M1 decode A/B at the 6173-token depth, which the earlier M1 runs on this PR did not cover. Clean-tree builds, Qwen3.5-2B, 128 tokens, greedy, 3 alternating rounds per case, run records Median ms per token:
Some runs in the k-split-only and attention-only rows came in at 43 to 61 ms per token, in both arms and at every depth, next to ordinary runs of the same arm (for example u24 ckb 51.4 next to 28.4 and 28.4). I excluded those runs from the k-split-only column; the base and combined columns come from the combined row, whose three rounds agree to 0.1 ms. I do not know the cause [INFERENCE: other work on the GPU during those rounds]. One d6173 base run in each row lost its result line to a kernel-log echo and was dropped, so d6173 base has n=2. |
|
Thanks @joshuaswarren. The receipts show a substantial speed gain, but we cannot accept this replacement as submitted: it changes the sum order and logits. Three matching greedy traces do not establish the required equality. A finite BF16 example gives old sum 0 and new sum 64, so this needs a result-preserving design. Please also declare the 1024 bytes of shared memory for both tile variants and test the actual production grid and tails. Retain complete timing samples; the truncated and excluded rows cannot establish the stated full sample counts. The new shader comment also needs the one-line/120-column cleanup. A revised implementation needs exact output/token evidence and green zig, zig-windows, pytest and fmt jobs at its final head.
|
|
[Joshua's agent] Read, thanks. You are right that the 8-slice split changes the summation order, and matching greedy traces do not prove equal logits. I will make the kernel order-preserving (same per-output accumulation order as the current kernel) or close this PR if I cannot keep the speed gain that way. A revision will declare the 1024 bytes of shared memory for both variants, test the production grid and tails, keep complete timing samples, and carry exact output and token evidence. The speed numbers above should be read as belonging to the order-changing version only. |
|
[Joshua's agent] Status, not a ready note: CI is green at the current head 6852ec6 (8 success, 2 skipped), but that head is the order-changing k-split you rejected, and the numbers I posted belong to it. The order-preserving rewrite is still being built and measured; it has not landed on this branch. I will post the exactness contract result, the maintainer's finite-BF16 example, token hashes vs the old kernel, and complete timing samples when the rewrite is pushed — or close this PR with the data if no bit-exact design keeps the gain. Do not take the green CI as readiness. |
|
[Joshua's agent] Closing this one. Two attempts at this split produced no passing exactness evidence: the order-changing kernel mismatches 55/140 byte-exact cases, and the order-preserving rewrite mismatches 61/140 with a 5x slowdown against base on the 13-inch M1 (the gain came from parallelizing the chain that bit-exactness needs serial). No replacement PR for this split. #3436 stays open pending a different split. |
|
@joshuaswarren, the speed gain is real, but the kernel as submitted changes the summation order and the When the rewrite exists, post it with the 1024 bytes of shared memory declared for both tile variants, the
|
[Joshua's agent] Decode with Qwen3.5-2B 4-bit on Vulkan was GPU-bound in the one-row q4 matmul (
qmm_q4_tile). It ran one thread per output column and walked all of k, so a 2048-column projection was 8 workgroups and a 6144-column one 24. This PR splits k across 8 slices inside one 256-lane workgroup: each workgroup computes one row and a 32-column tile, each lane sums one eighth of k, and the first 32 lanes add the 8 partial sums in slice order.Stack. This PR is stacked on #3417 (head
f57eab37f). New here, after that head:80b2c090f(TF_VK_DECODE_BENCH=Nmode in the Qwen3.5 model probe),8613ae9aa(the kernel and its dispatch size inops_q4.zig), a merge of #3417's new head, and a docs commit (changelog and measurements). The diff against main shows the stack until it lands.Numbers. Clean-tree builds of
80b2c090f(base, kernel unchanged) and8613ae9aa(head), run alternately, 128 greedy tokens per run with readback, medians, no GPU timeouts:Correctness. The f32 sums are added in a different order, so the logits are not bit-identical to the base. The gate is the greedy token hash: it is equal between base and head in every run on both machines (
fc93d24775b1d909,3efb3759184db87e,fc0ededa74f07707). The QMM kernel contract (tf-vulkan-kernel-contract) gives the same result for base and head on both machines: 5 kernels pass gates 1 to 4, 0 fail, 9 planted controls caught, 0 missed, 0 limit failures. The contract'slocal_sizechecks are unchanged because the workgroup stays 256 lanes (k-splits of 4 and 16 were also tried on the M2 Max; 8 keeps the 256-lane size, 16 was about 2% faster again at 24 tokens).One note on the measurement files: the wrapper script that ran the contract printed
VERDICT ... FAILbecause it counted the planted-controlFAILlines; theSUMMARYline is the contract's own verdict, and it passes. The decode benchmark output and the contract output are indocs/receipts/vulkan-qmm-ksplit/.Not covered here: attention at depth and the server path, which are separate PRs.