Skip to content

Vulkan base: runtime and the Q4 projection registered in the kernel contract (opt-in) - #1526

Merged
ashhart merged 7 commits into
ashhart:mainfrom
joshuaswarren:vulkan-base-pr
Oct 29, 2026
Merged

ashhart merged 7 commits into
ashhart:mainfrom
joshuaswarren:vulkan-base-pr

Conversation

@joshuaswarren

@joshuaswarren joshuaswarren commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

First of the stacked Vulkan PRs for #1420, smallest first as the maintainers asked: an opt-in Vulkan compute runtime, and one kernel, the affine Q4 group-64 projection, registered in the kernel contract. Nothing else of the Vulkan backend is in this PR; the 2B engine, submit batching and penalties follow as separate PRs.

  • Runtime (zig/src/vulkan/): loads the Vulkan loader at run time, picks a device, owns buffers, a command buffer, a fence and the staged upload and readback paths, and dispatches compute pipelines over checked SPIR-V, bindings and limits. Every submit has a finite fence wait; a timeout or an in-flight failure ends the process before anything is freed. Bindings are checked against the device's storage-buffer range and offset alignment.
  • Kernel (zig/kernels/vulkan/qmm.comp, compiled as qmm_bf16): the Q4 affine projection, bf16 in and out, fp32 sums.
  • Bring-up probe (zig/tests/vulkan/probe.zig, affine_fixture.zig, zig/src/vulkan/gpu_tests.zig): the first device checks (a real device, staged buffer round trips and the projection against its float64 bound); host-checkable parts run in vulkan-host-test, the device parts only through explicit vulkan-gpu-test and vulkan-probe steps.
  • Contract runner (zig/tests/vulkan/contract_*.zig, zig/kernels/vulkan/contract_controls.comp, zig/src/core/contract/vulkan_records.zig): the Vulkan counterpart of HIP's. The kernel fills the shared contract.spec; the shared gates run its float64 reference. Five planted controls, one per gate, each built to break its gate, plus two that write to the weights and to the biases after the dispatch and must fail gate 2. Gate 5 per-chip limits are measured, not assumed.
  • Build: everything is behind -Dvulkan=true (default off). With it, zig build tf-vulkan-kernel-contract builds the runner and zig build vulkan-kernel-contract runs it; vulkan-host-test joins test. Without the option no Vulkan step exists and no other backend's code changes. Outside the new files the diff touches build.zig (the option and call), THIRD_PARTY_NOTICES.md and LICENSES/mlx-omarchy-MIT.txt (the MIT notice for shader code adapted from mlx-omarchy), and zig/src/core/contract.zig plus zig/src/core/contract/limits.zig (additive Vulkan launch and limit types).

Part of #1420; the stacking order is this PR, then the 2B engine, then submit batching, then penalties.

Receipt (kernel contract)

Gate 5 limits, measured on the devices (Mesa 26.3.0-devel Honeykrisp, Vulkan, all three chips reported the same values): maxComputeWorkGroupInvocations 1024, maxComputeWorkGroupSize 1024,1024,64, maxComputeSharedMemorySize 32768, maxStorageBufferRange 2147483647, minStorageBufferOffsetAlignment 16, subgroup size 32. The kernel and every control launch at 256x1x1 with no shared memory; the over-limit control declares 2048x1x1 and is rejected.

Served shapes: the ones the Qwen3.5-2B engine calls the projection at (K 2048 and 6144, N from 16 to the 248320-row head, one row for decode, 32 for a prefill chunk) plus small fixture shapes.

SUMMARY lines from the runner built at this PR's head 17fa2cd (tf-vulkan-kernel-contract, ReleaseSafe aarch64, 4573232 bytes, sha256 0cfd4fe10761973a3ea08453a5a7e46d640f84c1885b17a4544b57b7e99fa4ea; zig 0.17.0, -Dvulkan=true -Dtarget=aarch64-linux-gnu -Doptimize=ReleaseSafe):

SUMMARY Apple M1 Max (G13C C0) 1 kernels: 1 pass gates 1-4, 0 fail, 0 known findings; 8 controls: 8 caught, 0 missed; 0 limit failures; gates 11048 ms
SUMMARY Apple M2 Max (G14C B1) 1 kernels: 1 pass gates 1-4, 0 fail, 0 known findings; 8 controls: 8 caught, 0 missed; 0 limit failures; gates 11722 ms
SUMMARY Apple M1 (G13G B1) 1 kernels: 1 pass gates 1-4, 0 fail, 0 known findings; 8 controls: 8 caught, 0 missed; 0 limit failures; gates 10941 ms

SHA-256 of the full stderr of each run: M1 Max ee54483cab907f645800bb64056f05df0527efd3821b8581c86976dc8e42fb41, M2 Max ae9ee961dc894bf8f80fd9e8156e6af4d9f8051ca4e67866ea9f604ab280a04b, M1 f9ee23180d8968b04d5f7dc5fd42e03f8a808570095a42e782feb8003e1031ff.

  • Gate 1 (non-finite classes), gate 2 (five byte-identical runs, inputs and guards untouched, no overlap), gate 3 (every served shape and the tile edges against the float64 bound; on every chip 15 shapes within the bound with the worst at 0.990 of the bound, 0 edges refused) and gate 4 (exact fp32 sums; fp16 would miss by 5268x, bf16 by 256x) pass on the real kernel. All 8 controls are caught on every chip run so far, including the two new ones that write the weights and the biases (both red at gate 2, the repeat gate).
  • Two bugs found by the first device runs are fixed in this PR: the runner's own harness table pointed at a stack temporary (a crash at the first gate), and a host allocation cap refused the 248320x2048 head shape as a served shape. Neither is in the kernel.

What this does not claim

  • No model and no fidelity gate here: this PR holds one kernel and its contract. The fidelity gate (the v2 margins, run on fixtures not used to choose them, with the v1.1 failures kept in the description) goes into the 2B-engine PR.
  • Contract runs on a real device are Apple M1-family GPUs under Mesa Honeykrisp only; AMD and Intel Vulkan are not run. Gate 5 is declared for those three chips; other devices need their own measured rows.
  • Tests run: zig build vulkan-host-test -Dvulkan=true (26 of 26 host tests pass on head 17fa2cd, in three test binaries of 18, 6 and 2, run fresh), and the runner and the probe cross-build for aarch64-linux-gnu with ReleaseSafe. Not run: CUDA, HIP, XPU and Metal (their code is unchanged).

@joshuaswarren

joshuaswarren commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor Author

This is the smallest first PR, as you asked, now ready for review at head 17fa2cd. Every kernel in it is registered in your contract, and the contract runner passes gates 1 to 4 on M1, M1 Max and M2 Max with all 8 planted controls caught. The SUMMARY lines and the stderr sha256 of each run are in the description. PR-B (#1544) builds on this one and stays a draft until you are happy with this one.

[Joshua agent]

joshuaswarren and others added 7 commits October 29, 2026 23:44
The Vulkan backend comes in on the same terms as HIP: every kernel registers
with the kernel contract. This registers the one kernel the base PR holds,
the affine Q4 group-64 projection (qmm_bf16: bf16 in and out, fp32 sums).

contract_qmm.zig fills the shared contract.spec for it and gives the shared
gates a Vulkan fixture (real dispatch through the runtime, staged upload and
readback, poisoned guard words). Served shapes are the ones the Qwen3.5-2B
engine calls it at: K 2048 and 6144, N from 16 up to the 248320-row head, one
row for decode and 32 for a prefill chunk, plus small fixture shapes. Gate 4
keeps the harness default (fp16 and bf16 sums must miss by 100x).

contract_controls.comp holds the planted controls, one per gate, each a
kernel built to break its gate: a projection that drops NaN, an
order-dependent atomic sum, a sum that writes into its input, a projection
that loses its K tail, and one that sums in fp16. A run where a control
passes is a failed run.

Gate 5 needs per-chip limits: vulkan_records.zig declares each launch's
workgroup size and shared bytes, limits.zig adds the Vulkan launch and limit
types, and a host test refuses any chip whose limits are not measured, so no
value is assumed. The runner prints every limit it reads from the live device
(LIMITS lines) and checks the declared launches against them.

tf-vulkan-kernel-contract builds the runner and the controls without running
them; vulkan-kernel-contract runs them on the device. Both exist only with
-Dvulkan=true.

Checked: the host tests and the aarch64 build of the runner pass. Not yet
run: the runner on a device (gates 1 to 4 and the live limits check), so no
SUMMARY line exists yet, and the per-chip limit table is still unmeasured.
…wo input-write controls

Review found gate 2 (repeat, aliasing, guards) vacuous for qmm_bf16: Fixture.run read back only the output buffer and the rig's inputs stayed the host snapshot taken before upload, so 'inputs untouched' could not fail for the real kernel. (The scribble control was caught only because Control.run downloads its x buffer.) Fixture.run now downloads all four input buffers (x, w, scales, biases) into the snapshot after each run, so the before/after comparison sees what the device holds. Two planted controls prove it: the same fixture with a write to its weights and to its biases (the last input, so a readback that stops one short misses it) after the dispatch must fail gate 2 and are counted as controls (8 controls now). The records comment no longer cites runner stderr hashes (they were from older runs, and a file cannot carry the hash of a run of itself); the description carries the hashes of the runs on this head.

Proved by: vulkan-host-test 26 of 26 (18, 6 and 2 in three binaries) and the aarch64 ReleaseSafe runner build; not device-run yet.
Each multi-line or over-120-column comment in vulkan_records.zig,
contract_main.zig and contract_qmm.zig becomes one line saying the same
thing; no code changes.
@ashhart

ashhart commented Oct 31, 2026

Copy link
Copy Markdown
Owner

Thank you @joshuaswarren. This is in TensorFold 1.2.0, released today: https://github.com/ashhart/TensorFold/releases/tag/v1.2.0

It landed with your commits under your own name, so you're listed as a TensorFold contributor, and it's credited to you in the release notes and the changelog.

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