Skip to content

perf(hip): Inkling 2 Mini's loop kernels (cross-pass message and message gate) under named HIP baselines - #5012

Draft
AbrahamCete wants to merge 4 commits into
ashhart:mainfrom
AbrahamCete:perf/hip-inkling2-loop
Draft

AbrahamCete wants to merge 4 commits into
ashhart:mainfrom
AbrahamCete:perf/hip-inkling2-loop

Conversation

@AbrahamCete

@AbrahamCete AbrahamCete commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

TensorFold has no HIP kernels for Inkling 2 Mini's loop: the cross-pass message and the message gate. This adds them as
a new inkling2_loop HIP kernel group with its launcher and a kernel-contract entry at Mini's shapes:
tf_ink2_loop_messages, and the gate's two halves tf_ink2_loop_gate_down and tf_ink2_loop_gate_up. It is HIP4 of
the HIP column on #4274. Kernels only: no engine, glue or attention residual.

This head replaces the earlier one (9c61b6e), which was stacked on #4994 and held every output to
loop_model.zig's serial order. Since your correction on #4274 (issuecomment-7166823149), the bar is the one main
applies to CUDA and Metal, including "Named HIP baselines: one per operation, with every faster kernel byte-equal to its
baseline." So the group now sits on main alone and defines its own baselines, chosen for speed, and the contract
compares every output's bits with host models of them.

Named HIP baselines

Baseline Kernels Order Rounding points and math
HIP-RMS-W32 rho, in the messages (bf16 banks) and in gate down (f32 h and m) element e goes to lane (e div 8) mod 32; each lane's chain is ascending from +0; xor butterfly at 1, 2, 4, 8, 16 fmaf(x, x) each step; mean = s / 2,304; inv = 1 / sqrtf(mean + eps), a correctly rounded square root, then a division; x · inv rounded to f32
HIP-RETRIEVE-W32 the message logits as HIP-RMS-W32, over q · b fmaf; logit · binary32(1/48); softmax max from -inf, zexp, den = (0 + e0) + e1; out = (0 + w0 · b0) + w1 · b1, unfused
HIP-ZETA-W32 gate down as HIP-RMS-W32, over the 4,608 joined inputs, h's half first joined = bf16(x · inv); fmaf; bf16(zeta)
HIP-GATE-UP-R64 gate up 64 terms ascending, unfused ztanh; (1 - F) · h + G · m in four fp32 steps

The group builds with -ffp-contract=off, so nothing beyond the fmaf steps above is fused. zexp, ztanh, zexpm1
and zldexp are Zig 0.17's f32 functions ported operation for operation (inkling2/zmath.hpp, its own first commit,
which #4994's rebuild will share). They cost no measurable time here, and they keep HIP's math equal to the host's.

Where they sit. In passes 2 and 3 of the core layers (visits 18 to 41), a layer's messages come from the previous
pass's banks: B_12 and P_18, bf16, copied at the end of passes 1 and 2. The q, k and v gates mix each role's raw
attention-residual output with its message. After the MLP's attention residual, the mlp gate does the same. A token
has 96 messages and 96 gates. tf_ink2_loop_messages takes 1 to 12 core layers a call, so a visit's 4 messages (as
CUDA's engine computes them) or all 48 at a pass reset (as Metal's does) both work. One gate launch takes 1 to 3
gates, so a visit's q, k and v gates can share one. A gate may write over its own h, as CUDA's engine does.

Where CUDA, Metal and these baselines differ:

  • The inverse square root: CUDA's rsqrtf, Metal's precise::rsqrt, here a correctly rounded 1 / sqrtf.
  • rho's sum of squares: CUDA runs 256 lane chains strided with FMA and a tree; Metal runs strided unfused lane sums,
    simd_sum, then 8 partials in order; here 32 lane chains of 8-element chunks with fmaf and a butterfly.
  • The message dot: CUDA uses FMA and a tree, Metal strided unfused lanes and simd_sum, here HIP-RETRIEVE-W32.
  • zeta: CUDA sums with a tree; Metal sums in input order; here HIP-ZETA-W32.
  • exp and tanh: CUDA's expf and tanhf, Metal's precise::exp and precise::tanh, here the Zig ports.
  • The message output: CUDA and Metal write m0 + m1, which is -0 where both shares are -0; here the sum starts from +0
    and gives +0 there. A contract control holds that difference.

Speed, measured on gfx1151. A small harness loads this group's code object, as the build makes it, and launches it
as the launcher does. Times are rocprofv3 kernel-trace medians of the last 60 of 65 queued launches a cell, in the order
9c61b6e, this head, this head, 9c61b6e, each a fresh process, in two rounds. A cell counts as faster only when this
head beats 9c61b6e in both rounds by more than that round's gap between the two 9c61b6e runs. Message inputs rotate
through two 268 MB rings, so at least 250 MB of reads pass between two reads of the same byte, and the gates' weights
through 226.5 MB of controller copies, both well past the 32 MB MALL; the gates' h and m do not rotate. A 20 us probe kernel before and after each cell read the GPU clock at 2,850-2,926 MHz for
9c61b6e and about 2,900 MHz for this head, apart from one stretch noted below. Every cell's output hashes match between
the two runs of each arm. The gfx1151 code objects' sha256 prefixes are 7673ac4a (9c61b6e) and 650fc01b (this head).

Cell 9c61b6e, us This head, us
messages, 1 row, 4 a launch 15.4 3.2-3.4
messages, 1 row, 48 a launch 17.4-17.5 5.1-5.2
messages, 512 rows, 4 a launch 326.5-328.2 72.2-72.8
messages, 2,048 rows, 4 a launch 1,230-1,234 669-671
gate down, 1 row, 1 gate 22.2-22.3 4.9-5.0
gate down, 1 row, 3 gates 23.8 10.0-10.1
gate down, 512 rows, 1 gate 1,317-1,320 156-161
gate down, 2,048 rows, 1 gate 5,060-5,170 1,105-1,142

All 14 message cells and all 14 gate-down cells (1, 4, 8, 16 and 64 rows; 4 and 48 messages; 1 and 3 gates; 512 and
2,048 rows; one row with the same weights every launch) are faster: messages 1.84x to 6.14x, gate down 2.37x to 8.7x.
Gate up is the same machine code in both arms. Timed alone it is within noise in all 10 cells; inside a down and up pair
it reads up to 1.21x faster at 16 and 64 rows, because 9c61b6e's gate down slows the gate up after it, and 0.93x at
512 rows, where this head's clock probes read 2,424-2,794 MHz. Neither is a result for gate up. These are kernel times
in a harness, not a decode measurement.

Questions for you; say which you want changed:

  1. Engine placement, for HIP5: should the messages be computed at the pass resets, as Metal does, or 4 a visit, as
    CUDA does? And may HIP5 pass a visit's q, k and v gates in one launch, as the launcher allows?
  2. HIP2's attention residual uses the same rms and retrieve as these kernels. May it follow HIP-RMS-W32 and
    HIP-RETRIEVE-W32, so each stays one operation?
  3. The probe tf_ink2_loop_math and the ten control kernels ship in the inkling2_loop image. The served kernels
    contain no switch; each control is its own compile-time instantiation. Would you rather have them in the controls'
    code object (contract_controls.hip)?
  4. Notices: zmath.hpp ports Zig 0.17's f32 tanh, expm1, exp and ldexp (MIT). Should THIRD_PARTY_NOTICES.md get an
    entry for it?

Checks on this head

  • Head commit: 6547aff, on main b92aebe.
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 164 and 164, 0 new.
  • Tests run, with the command and pass, fail and skip counts:
    • zig build test-inkling2-loop-hip-host -Dhip=true: 23 of 23 passed.
    • zig build test-inkling2-host: 46 of 50 passed, 4 skipped.
    • zig build test on a clean tree of this head: 13,218 of 13,311 passed, 92 skipped, 1 failed. The failure is
      stage_hip_test "every staged call on HIP memory equals the reference, whole and in parts", which fails on main
      on this machine as well.
    • zig build test -Dhip=true on the same tree: 1,543 of the 1,565 tests it ran passed, 21 skipped, and the same
      stage_hip_test failed.
    • zig fmt --check build.zig zig and git diff --check: clean.
    • A code-object comparison of main and this head on gfx1151: 621 kernels identical, the 14 new ones added, no spill
      and no scratch in any of them. The tool reports FAIL for three affine_dot2_block<DotBF16, N> instantiations in
      tiles/dot2.hip (N = 3, 5 and 6), which this branch does not touch; two builds of the same sources differ in the
      same function (N = 2, 3, 5 and 6).
  • The new tests fail before their change: 9c61b6e's message and gate-down kernels, run against the new host models,
    are red (0xbff6c967 where the baseline gives 0xbff6c94c; 0xbda2a62e where the baseline gives 0xbda2a62b); the
    math probe with the device's rsqrtf is red at nonfinite and shapes (46,664 of 986,721 outputs differ).
  • CI: green on this head (10 checks pass, 2 skipped).
  • Not run, and why:
    • gfx1030, gfx1100 and gfx1201 on a device: I have only gfx1151. All three build, and their records match the code
      objects.
    • Same-engine exactness, teacher-forced fidelity, a served trace and the decode speed bar: no engine calls these
      kernels yet.
    • A DRAM counter run: the speedups' cause (fewer waves, less LDS, the W32 chains) is read from the code objects,
      not measured.
    • Timing on other chips, and with h and m rotated in the gate cells.

Receipt

n/a: these kernels do not reach inference yet; no engine or server calls them. The kernel contract below is the
evidence, and the timing above is a kernel-level measurement, not a served receipt.

Kernel contract, only when a GPU kernel is new or changed
  • Kernels registered with the contract (tf-hip-kernel-contract image inkling2_loop); none replaces another kernel.
    Each output's bits are compared with the baseline host models (Zig's, for the probe), NaN as a class:

    • tf_ink2_loop_math: tanh, exp and 1 / sqrt at 328,907 inputs, 986,721 outputs: every branch edge of the ported
      tanhf, expf and expm1f one ulp either side and both signs, zeros, infinities, NaNs, subnormals, the largest finite,
      exp's subnormal outputs, all 65,536 bf16 patterns and 262,144 random f32.
    • tf_ink2_loop_messages: 1 row at one and all 12 core layers; 4 and 8 rows at 12 layers; prompt calls of 16 rows at
      one layer, 64 at 12, and 2,048 at one. Order cells: a query of +1, +2^24, +1, -2^24 at elements 0, 8, 16 and 24 against banks of ±1,024 gives
      1 under HIP-RETRIEVE-W32, 0 serially and 2 with the butterfly reversed; a bank row of 2^12 and then ones sums its
      squares to 2^24 + 2,232 under HIP-RMS-W32 and to 2^24 serially.
    • tf_ink2_loop_gate_down with tf_ink2_loop_gate_up: 1 row with one and three gates; 4 and 8 rows with three;
      prompt calls of 16 rows with one, 64 with three, and 2,048 with one. A rho order cell puts its readouts on a bf16 midpoint, so a serial or
      rounded-square inverse moves the joined value; a zeta cell gives 2^-24, 0 and 2^-23 for three orders.
    • Gate 1 plants NaN and infinities in banks, queries, h, m and the weights. Gate 4's rows sum exactly in fp32 in any
      order.
  • A planted control for each new kernel, each its own kernel, red at shapes:

    • the probe with the device's own tanhf, expf and rsqrtf;
    • messages with rsqrt, with the second share fused into an fma, without the +0 start, with a serial rho, and with the
      butterfly reversed;
    • gate down with a serial rho, with the butterfly reversed, and with rounded squares;
    • gate up with the device's own tanhf, and with (1 - F) · h + G · m fused into one fma.
  • Each backend's SUMMARY line and exit status (gfx1151, ROCm 10.1's hipcc, HIP 7.16), on a clean tree of this
    head's code:

    SUMMARY inkling2_loop HIP full: 3 kernels: 3 pass gates 1-4, 0 fail, 0 known findings; 11 controls: 11 red as planted, 0 missed; 0 limit problems; gates 721 ms, limits 8 ms (exit 0)

    SUMMARY inkling2_loop HIP smoke: 3 kernels: 3 pass gates 1-4, 0 fail, 0 known findings; 11 controls: 11 red as planted, 0 missed; 0 limit problems; gates 502 ms, limits 7 ms (exit 0)

    zig build hip-kernel-contract-limits: all 56 inkling2_loop records PASS on the four chips. The step exits 1 with
    11 problems, the same 11 as on main with this hipcc, all in other groups: tf_moe_combine_gate (gfx1030, gfx1100,
    gfx1151), tf_vl_resize (all four chips), and the 6- and 8-bit relu2 stream tiles 2x8 and 4x4 on gfx1100.

    The 2^32 proofs through this head's tf_ink2_loop_math: ztanh, zexp and 1 / sqrtf give Zig's bits on all
    4,294,967,296 inputs; the device's tanhf, expf and rsqrtf differ on 109,637,798, 41,203,423 and 597,936,208.

    Records (hip_records_inkling2_loop.zig), VGPRs on gfx1030 / gfx1100 / gfx1151 / gfx1201; no scratch on any chip:

    Launch Threads VGPRs LDS bytes
    tf_ink2_loop_messages 64 155 / 156 / 156 / 152 18,440
    tf_ink2_loop_gate_down 256 149 / 149 / 149 / 149 9,224
    tf_ink2_loop_gate_up 256 112 / 112 / 112 / 111 1,024
    tf_ink2_loop_math 256 15 / 15 / 15 / 15 0
  • Chips I could not run: gfx1030, gfx1100 and gfx1201 (limits only), and every CUDA SM and Metal chip.

Checklist

  • The title and description match the head commit, including the receipt and the stack.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency.
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.
  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off. (n/a: no engine)
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (n/a: no engine)
  • No precision traded for speed. If the bits change, the description says which sums change and why. (n/a: new kernels; the baselines above name every sum)
  • Decode and prompt processing are no slower than main at one stream and the widest admitted; with --drafter, no slower than without it, greedy and sampled. (n/a: no engine)

Inkling 2's HIP loop kernels and routed experts both need Zig 0.17's
f32 tanh, exp, expm1 and ldexp, ported operation for operation, and
bf16_round's round to nearest even. This adds them as one header,
inkling2/zmath.hpp, with no other change, so both kernel groups can
include the same bytes (blob f5672f7).

Nothing includes it yet; the loop group that does follows.
…to Zig's bits

Inkling 2 Mini's loop kernels (the cross-pass message and the message
gate) get named HIP baselines whose math is Zig 0.17's f32 tanh and
exp, ported operation for operation in inkling2/zmath.hpp, and 1 /
sqrt as a correctly rounded sqrt, then a division (not rsqrt). This
adds the inkling2_loop kernel group, built with the strict flags, and
its first launch: tf_ink2_loop_math, a probe that runs those three
functions as the kernels run them over given inputs.

The contract checks the probe against Zig at every branch edge of the
ported functions, all 65,536 bf16 patterns and 262,144 random f32: on
gfx1151 every output has Zig's bits. A control runs the device's own
tanhf, expf and rsqrtf instead and goes red at `shapes` (58,315 of
986,721 outputs differ). With rsqrtf planted in the probe's 1 / sqrt,
the probe itself fails `nonfinite` and `shapes` (46,664 differ).

The group plumbing (build group, kernels.zig, contract entry, record
families, a host step `test-inkling2-loop-hip-host`) follows the
inkling2 attention group's.
… HIP-RETRIEVE-W32

tf_ink2_loop_messages computes the cross-pass message for every row,
core layer and role (q, k, v, mlp): rho of the bf16 banks B_12 and
P_18, each query's dot with both, the two-way softmax, and the
weighted sum, into f32 [message][rows][2,304], CUDA's layout.

Its sums follow two named HIP baselines. In a W32 sum, element e goes
to lane (e / 8) % 32 of one wave, each lane's chain runs ascending
from +0 through fmaf, and the 32 partials join by an xor butterfly at
1, 2, 4, 8 and 16. HIP-RMS-W32 is rho (the W32 sum of fmaf(x, x),
mean = s / 2,304, inv = 1 / sqrtf(mean + eps) as a correctly rounded
sqrt and a division, then x * inv); HIP-RETRIEVE-W32 is the logits
(the W32 sum of q * b, times binary32(1/48)). The softmax runs Zig's
f32 exp (zmath.hpp), and each output sums its two shares from +0. A
64-thread block holds one row of one message: wave k reduces bank k
from registers, so no lane runs a 2,304-step chain.

The host model in contract_inkling2_loop_ref.zig restates the order on
its own (w32Sum) and the contract compares every output's bits with
it at 1, 4 and 8 rows for one and all 12 core layers, and prompt calls
of 16, 64 and 2,048 rows. Each call carries two order cells. A dot
cell (a query of +1, +2^24, +1, -2^24 at elements 0, 8, 16 and 24
against banks of +-1,024) gives a logit sum of 1 under W32, 0 as one
serial chain and 2 with the butterfly reversed. A rho cell (2^12 and
then 2,303 ones) sums its squares to 2^24 + 2,232 under W32 and to
2^24 serially, which moves every f32 output. A host test holds the
W32 sums within fp32's bound for depth n / 32 + 5 against float64.

Five controls go red at `shapes`: rsqrt, the second share fused, no
+0 start, rho's squares in one serial chain, and the butterfly
reversed. With the earlier serial kernel in its place, the served
messages fail `nonfinite` and `shapes` (0xbff6c967 where the
baseline gives 0xbff6c94c).
…-W32 and HIP-GATE-UP-R64

tf_ink2_loop_gate_down and tf_ink2_loop_gate_up compute the message
gate for one to three gates a launch, so the q, k and v gates of a
visit share one. A gate may write over its own h, as the engine's
gates do.

Down: a block of 8 waves a row and 8 ranks. Waves 0 and 1 find rho of
h and m under HIP-RMS-W32 (the W32 sum of fmaf(x, x) from f32 rows,
then 1 / sqrtf(mean + eps) as a sqrt and a division), every thread
rounds the joined norm to bf16, and each wave runs one zeta under
HIP-ZETA-W32: the W32 sum of the 4,608 bf16 products through fmaf,
h's half first, rounded to bf16. Each lane holds 18 chunks of 8 of
its rank's weights, loaded before rho so they arrive during it, so no
lane runs the 6,912-step serial chain the earlier kernel did. Up,
HIP-GATE-UP-R64, is kept: 64 terms a chain in rank order, unfused,
through Zig's f32 tanh, then (1 - F) * h + G * m as four fp32 steps.

The contract compares every output's bits with the host model at 1,
4 and 8 rows for one and three gates (the second and third in place),
an up block's row edges, and prompt calls of 16, 64 and 2,048 rows.
Gate 0 carries order cells: ranks 0-3 give zeta 2^-24 under W32, 0
serially and 2^-23 with the butterfly reversed; its last row holds an
h of 2^12 and ones, and an m near 1, each with a readout whose bf16
joined value differs between the W32 inverse and the serial (or
rounded-square) one; columns 0-2 read those ranks alone, so a changed
zeta reaches an f32 output. The earlier cell, h of 2^12 and ones read
through the sum of all joined values, gave the same bf16 zeta under
both orders; on gfx1151 each order control now differs from the
served bits at its own readout column of the cell row.

Five controls go red at `shapes`: the device's tanhf, the last two
steps fused, rho's squares in one serial chain, the butterfly
reversed, and each square rounded before its add. With the earlier
serial gate down in its place, the served gates fail `nonfinite` and
`shapes` (0xbda2a62e where the baseline gives 0xbda2a62b).
@AbrahamCete
AbrahamCete force-pushed the perf/hip-inkling2-loop branch from 9c61b6e to 6547aff Compare November 25, 2026 19:33
@AbrahamCete AbrahamCete changed the title feat(hip): Inkling 2 Mini's loop kernels (cross-pass message and message gate) under the kernel contract, bit-equal to loop_model perf(hip): Inkling 2 Mini's loop kernels (cross-pass message and message gate) under named HIP baselines Nov 25, 2026

This branch has not been deployed

No deployments
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.

1 participant