Skip to content

HIP: model-free Zig runtime, caps table and one exact affine matmul - #492

Closed
BlivionIaG wants to merge 7 commits into
ashhart:mainfrom
BlivionIaG:hip-runtime-v2
Closed

BlivionIaG wants to merge 7 commits into
ashhart:mainfrom
BlivionIaG:hip-runtime-v2

Conversation

@BlivionIaG

Copy link
Copy Markdown
Contributor

Draft, for the structure discussion in #491. This is the model-free part of #490 cut to #463's scope, so the two runtimes can be compared side by side. It is not meant to replace #463.

What this changes

A model-free HIP runtime for the Zig engine in its own zig/src/hip, beside zig/src/cuda:

  • Runtime: libamdhip64 through dlopen, admitted only when every entry point resolves; device and pinned / mapped host memory with range-checked copies and fills (stream-less ones finish before returning); streams and events; modules; launches with packed arguments and cooperative grids.
  • Caps table keyed by the device's gfx name: RDNA2 gfx1030-1036, RDNA3 gfx1100-1103, RDNA3.5 gfx1150-1153, RDNA4 gfx1200-1201. Unknown GPUs are refused.
  • Build: -Dhipcc compiles the kernels into one offload bundle for -Dgfx, which also takes rdna2, rdna3 and rdna3.5.
  • One exact matmul: MLX affine 4-bit (bf16 x, scales, biases; fp32 sums), with tests that fail when its bits move.

No model, family, serving or tensor-parallel code. CUDA and Metal are untouched; build.zig gains one import and two calls. The argument-pack and grid refusals and the async new-bytes round trip follow #463 and credit @ThinkOffApp.

Receipt

  • Environment: base main at f8fe17d, head 5f6550a. Zig 0.17.0. HIP 7.14.60850, Linux 7.1.7. GPUs: Radeon PRO W7800 (gfx1100) and Radeon PRO V620 (gfx1030). Host tests on an Apple Silicon Mac. No checkpoint involved.
  • Exactness: on both GPUs, all 10,304 matmul outputs (1, 3 and 16 rows; groups 32, 64 and 128) equal the host's fp32 recipe bit for bit, within 0.01 of the float64 bound, and reproduce the pinned digest.
  • Decode and prompt speed: not applicable.
  • Tests run:
    • zig build test on macOS: 105/105 steps, 397 passed, 2 skipped. The HIP part is 7 runtime unit tests, 4 mock-library tests and 2 reference tests.
    • tf-hip-test all on gfx1100 and gfx1030: 11/11 each (copies, fills, mapped memory, launches, module globals, wave32, argument packing, refusals, cooperative grid, image refusal, affine).
    • Code objects for -Dgfx=rdna2,rdna3,rdna3.5 (15 targets) build.
    • Fail before: every test is new; the pinned-digest test fails when the kernel's last two fmas are swapped.
  • Not run, and why: CUDA and Metal device runs (sources untouched); gfx1151, gfx1150 and gfx1201 hardware (none here).

Checklist

  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off. (No decode path touched.)
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (No model path touched.)
  • No precision traded for speed. If the bits change, the description says which sums change and why.
  • Prompt processing is no slower than the last release. (No prefill path touched.)
  • The description says which platforms I ran, Metal M1 to M5 and CUDA, and which I could not.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency. Host and mock tests run anywhere; tf-hip-test is a separate program that needs a GPU.
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths. No AI attribution lines.

BlivionIaG and others added 7 commits October 8, 2026 01:23
zig/src/hip mirrors zig/src/cuda: the entry points declared by hand from HIP's ABI, a driver that admits the
library only when every one resolves, the current device, device and pinned host memory with range-checked
copies and fills, streams and events, modules and launches with packed arguments. Stream-less fills and device
copies finish before they return. zig build test runs its host unit tests; nothing else changes.
…he table refused

zig/src/hip/caps.zig lists RDNA2 (gfx1030-gfx1036), RDNA3 (gfx1100-gfx1103), RDNA3.5 (gfx1150-gfx1153) and
RDNA4 (gfx1200, gfx1201) with the wave width, dot and matrix instructions each has. Context reads the device's
gcnArchName and refuses a GPU the table does not list, naming it, instead of picking a nearby generation.
…ds are refused in every build mode

The pack's bounds were a debug assert, gone in release builds; a full pack now marks itself and the launch
refuses it. Both checks follow the argument and grid validation in ashhart#463.

Co-authored-by: ThinkOffApp <[email protected]>
…a real GPU

zig/build/hip.zig builds zig/kernels/hip/probe.hip with -Dhipcc into one offload bundle for every -Dgfx target
(default gfx1030,gfx1100,gfx1151; the family names rdna2, rdna3 and rdna3.5 stand for every part the caps
table lists), and HIP loads the code object built for the device. Without -Dhipcc the program builds with no
kernels. tf-hip-test info, smoke, cooperative and image mirror zig/tests/cuda/runtime_tests.zig: blocking and
async copies through pinned memory, fills, mapped host memory, launches, argument packing, module globals, the
wave width, refusals, a cooperative grid and refused images. No model, family or serving code is involved.
… so a copy that does nothing fails

The first round trip writes a fresh buffer and reads into zeroes, which a stale allocation could satisfy. The
second inverts the bytes over the same buffers, as ashhart#463's pinned-memory round trip does.

Co-authored-by: ThinkOffApp <[email protected]>
…ld test, on any host

A mock library in host memory, built three ways: complete, missing one entry point, and an unknown GPU. The
tests check admission, copies, fills, out-of-range refusals, the GPU table, and that a launch hands HIP its
geometry and packed arguments while an overfull pack or a grid past 2^32 threads never reaches it. A refused
library or GPU logs a warning; the caller reports the error.
…hen its bits move

zig/kernels/hip/affine.hip computes y = sum over groups of scale * (x . codes) + bias * sum(x) with bf16 x,
scales and biases and fp32 sums: one thread an output, an ordered fma of x and code and a plain sum of x a
group, then two fmas into the output. zig/src/hip/affine.zig launches it and refuses group sizes MLX does not
use. zig/tests/hip/affine_reference.zig is the same recipe on the host, a float64 reference and the fp32
bound between them; zig build test checks the recipe against that bound and pins its output digest.
tf-hip-test affine checks every GPU output against the host recipe bit for bit at 1, 3 and 16 rows over every
group size, each against float64 within the bound, and the pinned digest.
@ThinkOffApp

Copy link
Copy Markdown
Contributor

gfx1151 receipt (Strix Halo, Radeon 8060S), #492 has no RDNA3.5 hardware run yet. At head 5f6550a with Zig 0.17.0, the build passes 7/7, tf-hip-test all exits 0, and all 10,304 affine outputs are bit-exact. Run on 8 Oct, first reported in #463. #463 at 3270cec passes on the same GPU (affine 22/22, runtime 16/16). A side-by-side run of both, together with #490's first gfx1151 run, will follow here.

@ashhart

ashhart commented Oct 10, 2026

Copy link
Copy Markdown
Owner

Thank you, @BlivionIaG. Your gfx1100 and gfx1030 runs, 10,304 affine outputs bit-exact, and @ThinkOffApp's gfx1151 rerun show this runtime works. We're keeping one HIP runtime, the one #463 landed, so we'll close this as superseded by it. Two pieces #463 doesn't have yet are welcome as small PRs on top of it, each with its exactness check: the gfx caps table from RDNA2 to RDNA4, and the -Dgfx=rdna2,rdna3,rdna3.5 builds.

@ashhart ashhart closed this Oct 10, 2026
@BlivionIaG

Copy link
Copy Markdown
Contributor Author

Hi yeah I have the next PRs that is additive on top of it as planned and in testing. Will push when it's done.

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.

3 participants