Repository navigation
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
Draft
AbrahamCete wants to merge 4 commits into
AbrahamCete wants to merge 4 commits into
Conversation
This was referenced Nov 25, 2026
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
force-pushed
the
perf/hip-inkling2-loop
branch
from
November 25, 2026 19:33
9c61b6e to
6547aff
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_loopHIP kernel group with its launcher and a kernel-contract entry at Mini's shapes:tf_ink2_loop_messages, and the gate's two halvestf_ink2_loop_gate_downandtf_ink2_loop_gate_up. It is HIP4 ofthe 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 mainapplies 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
mainalone and defines its own baselines, chosen for speed, and the contractcompares every output's bits with host models of them.
Named HIP baselines
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 f32fmaf; logit · binary32(1/48); softmax max from -inf,zexp, den = (0 + e0) + e1; out = (0 + w0 · b0) + w1 · b1, unfusedfmaf; bf16(zeta)ztanh; (1 - F) · h + G · m in four fp32 stepsThe group builds with
-ffp-contract=off, so nothing beyond thefmafsteps above is fused.zexp,ztanh,zexpm1and
zldexpare 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_messagestakes 1 to 12 core layers a call, so a visit's 4 messages (asCUDA'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:
rsqrtf, Metal'sprecise::rsqrt, here a correctly rounded1 / sqrtf.simd_sum, then 8 partials in order; here 32 lane chains of 8-element chunks withfmafand a butterfly.simd_sum, here HIP-RETRIEVE-W32.expfandtanhf, Metal'sprecise::expandprecise::tanh, here the Zig ports.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).
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.
the column remain.
mainb92aebe. Main has moved since; of this branch's files it changed onlydocs/kernel-contract.md,and
git merge-treewith main is clean. Commits:atc overlap --json(ATC 0.9.6, 2026-11-25 19:26Z, againstmain): 15 overlaps. Four report a conflict in one fileeach: idol: BiRefNet background removal and preprocessing on Metal, so tf-idol-generate takes a raw photo (stacked on #1780) #1787, hip: ModelOpt NVFP4 and FP8 checkpoints keep their codes and decode in flight #2490, ricursive: serve Ripple-65B on two GB10s with up to 1M tokens of context #2522 and ricursive: speed up Ripple-65B on two GB10s #2523, the same four as before, at the one-line append points this group shares with
them; whichever lands second rebases. Eleven report unknown because the run is incomplete (limits on pull requests,
their files and time).
Questions for you; say which you want changed:
CUDA does? And may HIP5 pass a visit's q, k and v gates in one launch, as the launcher allows?
HIP-RETRIEVE-W32, so each stays one operation?
tf_ink2_loop_mathand the ten control kernels ship in theinkling2_loopimage. The served kernelscontain no switch; each control is its own compile-time instantiation. Would you rather have them in the controls'
code object (
contract_controls.hip)?zmath.hppports Zig 0.17's f32 tanh, expm1, exp and ldexp (MIT). ShouldTHIRD_PARTY_NOTICES.mdget anentry for it?
Checks on this head
mainb92aebe.python3 tools/zig/lean_check.py, on the base and on this head: 164 and 164, 0 new.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 teston a clean tree of this head: 13,218 of 13,311 passed, 92 skipped, 1 failed. The failure isstage_hip_test"every staged call on HIP memory equals the reference, whole and in parts", which fails onmainon this machine as well.
zig build test -Dhip=trueon the same tree: 1,543 of the 1,565 tests it ran passed, 21 skipped, and the samestage_hip_testfailed.zig fmt --check build.zig zigandgit diff --check: clean.mainand this head on gfx1151: 621 kernels identical, the 14 new ones added, no spilland no scratch in any of them. The tool reports FAIL for three
affine_dot2_block<DotBF16, N>instantiations intiles/dot2.hip(N = 3, 5 and 6), which this branch does not touch; two builds of the same sources differ in thesame function (N = 2, 3, 5 and 6).
are red (
0xbff6c967 where the baseline gives 0xbff6c94c;0xbda2a62e where the baseline gives 0xbda2a62b); themath probe with the device's rsqrtf is red at
nonfiniteandshapes(46,664 of 986,721 outputs differ).objects.
kernels yet.
not measured.
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 portedtanhf, 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 atone 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_downwithtf_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.
order.
A planted control for each new kernel, each its own kernel, red at
shapes:butterfly reversed;
Each backend's
SUMMARYline and exit status (gfx1151, ROCm 10.1's hipcc, HIP 7.16), on a clean tree of thishead'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 56inkling2_looprecords PASS on the four chips. The step exits 1 with11 problems, the same 11 as on
mainwith 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,zexpand1 / sqrtfgive Zig's bits on all4,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:tf_ink2_loop_messagestf_ink2_loop_gate_downtf_ink2_loop_gate_uptf_ink2_loop_mathChips I could not run: gfx1030, gfx1100 and gfx1201 (limits only), and every CUDA SM and Metal chip.
Checklist
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (n/a: no engine)--drafter, no slower than without it, greedy and sampled. (n/a: no engine)