Repository navigation
feat(hip): Inkling 2 attention kernels under the kernel contract - #4633
Merged
Merged
Conversation
This was referenced Nov 22, 2026
This was referenced Nov 23, 2026
ashhart
force-pushed
the
perf/hip-inkling2-attention
branch
from
November 24, 2026 02:42
e90a42e to
f3f9d4d
Compare
Move ashhart#4125's float64 attention reference from zig/tests/cuda into core.contract.inkling2_attn_ref so the HIP contract can hold its kernels to the same rounding points. A Call now names its query heads, key heads and fp32 chains (Chain.cuda by default, Chain.hip for scalar IEEE sums), and tauAt takes the model's n_floor (Mini's is 8,192). CUDA's defaults are unchanged: the CUDA tests only change their import, and a pinned test holds row() at CUDA's defaults to the bits the old reference gave (checked bit for bit against it on sliding and global rows before the move). Checks: zig test of core.contract (17 pass); zig build test-inkling2-cuda-host (10/10) and compile-cuda-tests on the tree.
A new inkling2 kernel group (strict IEEE build) with ashhart#4125's rounding points on HIP: tf_ink2_prep (q/k L2 clips, tau on global layers, the gamma-free v norm, k and v into each segment's ring), tf_ink2_rel_table, chunked attention for sliding (tf_ink2_attn) and global layers (tf_ink2_attn_fold, a split form a block a 1,024-key sub-chunk, and tf_ink2_attn_prompt), and the ordered merge that takes the sink and the gate raw. One wave holds a (row, head) pair over 64-key K/V tiles in LDS with scalar fp32 sums; every path runs the same per-pair steps, so split and prompt are bit-equal to the fold. The prompt kernel shares each tile among 8 pairs in one 37 KiB LDS stage while the next tile loads into registers. The kernels build for gfx1030, gfx1100, gfx1151 and gfx1201 (no scratch; records in hip_records_inkling2.zig). The launcher (families/inkling2/hip_attention.zig) keeps heads, key heads, floor and clips as runtime arguments and per-segment cache offsets, as ashhart#4125's; whole-stream rings versus 64-position pages for global KV stays open with the maintainer. The contract entry (tf-hip-kernel-contract image inkling2) holds them at Mini's 24/8 heads and n_floor 8,192 to core.contract's FP64 reference with HIP's chains, at served shapes to 1M keys on a ring that wraps; three streams in one launch are bit-equal to each stream alone (prep, sliding, fold, split). Eight controls go red at shapes, among them a window one key long, the sink and gate given after their sigmoid (ashhart#4387's glue) and a prep writing one stream's keys into another's. Checks on gfx1151 with ROCm 10.1's hipcc: image inkling2 smoke and full, 11 kernels pass gates 1-4, 8 controls red, 0 limit problems; hip-kernel-contract-limits passes all 24 inkling2 records (its 11 remaining problems are other groups' records); test-inkling2-hip-host, check-hip-kernel-contract-host; lean 165 on base and head, 0 new.
The verifier's pins for core.contract.inkling2_attn_ref at CUDA's defaults: keys past the bias extent, a full sliding window, the exact (gate 4) branch, a NaN q and a +inf sink, gate 4's replay in fp32 and bf16, and tau at Small's floor. The bits come from the base file (5c1fea6:zig/tests/cuda/contract_inkling2_attn_ref.zig). Checks: zig test of core.contract, 18 pass.
The verifier's edge cases for the HIP contract: global (fold) rows at positions 0 to 1,024 held to float64 rather than only bit-compared, a lone sliding row at 0, prep at pos + 1 = 8,193 and 8,194 just past Mini's floor, and a NaN sink and a NaN gate planted beside gate 1's infinities.
attend() now refuses a layer whose ring is not whole 64-key tiles (error.Inkling2RingTiles: a tile's load never wraps), or is shorter than its window plus the call's rows less one (error.Inkling2RingShort: the rows' new keys would overwrite keys an earlier row still reads), before any launch. The contract's rings (3,072 sliding, 4,096 global) and Layer.sliding/global already meet both. Checks: test-inkling2-hip-host 8/8 (two new tests, red before the check), check-hip-kernel-contract-host.
The float64 bound allows P's bf16 rounding either way, so a kernel that kept P unrounded stayed green. Two registrations (sliding, fold) now make every output exact: rows below position 64 (one tile), q 0, no sink, a gate whose sigmoid is 1, integer values in [-16, 16], bias 2 at distance 0 and the rest drawn so each bf16(exp2f) sits over 2^-21 from a tie. Every P is then at least 2^-5, both of its sums are exact in fp32, and each output must equal bf16(fp32(sum P v / sum P)) bit for bit. A host test shows an unrounded P moves 19,232 of 49,152 outputs. docs/kernel-contract.md states the cases and the ring refusals, and gives the prompt kernel's LDS as 37,120 bytes (36.25 KiB). Checks on gfx1151: image inkling2 full, 13 kernels pass gates 1-4, 8 controls red, 0 limit problems. With the verifier's m1_p_unrounded mutation, smoke goes red on exactly these two cases (11 of 13 pass). test-inkling2-hip-host, check-hip-kernel-contract-host, test-inkling2-cuda-host, compile-cuda-tests; lean 165, 0 new.
… stated Rows.streams has no default any more: a caller states how many segments `segs` holds, so a multi-stream call that forgets it no longer compiles into the one-stream prompt kernel, which reads only segs[0] and ignores the tiles. A call with no streams is refused (error.Inkling2Streams). `slots` is now the partials the buffers hold, and attend() refuses fewer than rows x heads x stride before any launch (error.Inkling2PartialRoom); a split still needs 16 times that. The contract's calls pass their real room, so their paths are unchanged. A global position past the ring is not refused: the contract's 4,096-slot ring over 1M keys wraps on purpose. Layer.global and the contract docs say the engine glue keeps a global layer's positions inside its ring. Checks: test-inkling2-hip-host 11/11 (two new tests, red before the change); test-inkling2-cuda-host and compile-cuda-tests; on gfx1151, image inkling2 full: 13 kernels pass gates 1-4, 8 controls red, 0 limit problems. Lean 165 on base and head, 0 new.
ashhart
force-pushed
the
perf/hip-inkling2-attention
branch
from
November 24, 2026 05:38
f3f9d4d to
713d33b
Compare
Owner
|
Thank you @AbrahamCete. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes. |
This was referenced Nov 24, 2026
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's attention. This adds them: an
inkling2HIP kernel group with the prep(
tf_ink2_prep), the relative-bias table (tf_ink2_rel_table), chunked attention for sliding layers (tf_ink2_attn)and global ones (
tf_ink2_attn_fold, a split form a block a 1,024-key sub-chunk, andtf_ink2_attn_prompt), and theordered merge (
tf_ink2_merge), with their launcher and their kernel-contract entry at Mini's shapes. It is the HIPattention slice of #4274's plan, the HIP counterpart of M1, started on #4274 (issuecomment-7077604368). Kernels only:
no engine, loop, experts or glue.
The gate, as ashhart set it on #4265 (issuecomment-7077460180):
This pull request covers the first part, the kernel contract on gfx1151 with planted controls. Same-engine exactness and
teacher-forced fidelity need an engine on #4387's host, which is not here.
Arithmetic. The kernels keep #4125's rounding points, as #4387's Metal attention does, and follow its structure
(prep, table, chunk attention, ordered merge):
fminf(thr / (norm + 1e-6), 1); on global layers q and rel scaled bytau = 1 + alpha ln(max((pos + 1) / floor, 1)) after a first bf16 rounding; a gamma-free RMS norm on v; k and v into
the segment's cache at slot
pos % ring;bf16(sum_j rel[j] proj[j][d]), fp32 in tap order, for distances below 1,024;layers and every key on global ones;
rounds once to bf16.
attend()says so: glue must not apply either sigmoid (Inkling 2 Mini on CUDA and Metal #4387's double-sigmoid defect).Heads, key heads, n_floor and the clips are runtime arguments, as in #4125's launcher. Mini is 24 query and 8 key heads
with n_floor 8,192. The KV layout is #4125's and #4387's, [kv][ring][128] bf16 per segment, at per-segment offsets.
Design.
builds for gfx1030, gfx1100, gfx1151 and gfx1201 with no chip-specific path and no trap body. WMMA is not used.
dword, so each lane reads its own bank). Each lane scores two keys with a 128-term fp32 dot and owns 4 of the 128
output dims.
the fold by construction; the contract checks it on the device.
tile into registers meanwhile. CUDA's 96 KiB three-stage pipeline does not fit RDNA's 64 KiB a workgroup.
-O2 -fno-fast-math -fdenormal-fp-math=ieee,-ffp-contract=off).The reference. #4125's float64 reference moves from
zig/tests/cudatocore.contract.inkling2_attn_ref, so bothbackends hold their kernels to the same rounding points. A
Callnow names its query heads, key heads and fp32 chains(
Chain.cudaby default,Chain.hipfor scalar IEEE adds), andtauAttakes the model's floor. CUDA's tests changeonly their import. An independent check compared the old and new reference bit for bit: all 18 CUDA served shapes,
random, non-finite and stress fills, 2 seeds; 163,440 (row, head) pairs, 41.8M want and tolerance values, 2,016 gate-4
replays, and tau over 2^21 positions, with no difference. Pinned tests in the moved file hold those bits.
Beyond the plan, or where it was silent; say which you want changed:
attend()refuses, before any launch, a ring that is not whole 64-key tiles or is shorter than the window plus thecall's rows less one, a call with no streams, and partial buffers smaller than rows x heads x stride.
Rows.streamshas no default, so a multi-stream caller cannot fall into the one-stream prompt kernel by omission.The engine's glue keeps a global layer's positions inside its ring.
global layers instead? The tile load reads each segment's ring as one contiguous block, so pages would need a page
table there.
zig/tests/cuda/contract_inkling2_attn_prep.zig) narrows the tau range for a negative value thatstraddles a bf16 boundary (it scales the more negative end by 1 - 8u). The HIP copy widens each end by its own sign. I
did not change CUDA's cases; it was already on the base.
Size: +2,177/-31 in 19 files, seven commits.
loop remain.
main14951bc. Commits:atc overlap --json(2026-11-22 14:03 UTC, on e90a42e,main14951bc): incomplete (limitationsotherPullRequestsLimit,pullFilesUnavailable,pullRequestsLimit,timeLimit). Of the 21 open pull requests it read that share a file with this branch, 10 merge clean and for 11 it could not tell; it reports no conflict with a pull request, and I have no other open pull request on these files. It read 20 of the local worktrees; the five conflicts it reports there are with my own local branches, on older bases. Bygit merge-tree, my local branch for HIP: every row of a decode round re-reads the whole K/V cache on gfx1151; reading it once for a stream's rows makes a 2-4 row round's attention walk 16-30% shorter at 131K-262K, same bits #3811's score kernel, which it did not read, conflicts inzig/build/hip.zigandzig/src/hip/kernels.zig: both add a kernel group at the end of the same lists; whichever lands first, I rebase the other.Checks on this head
main14951bc, built from agit archivetree.python3 tools/zig/lean_check.py, on the base and on this head: 165 and 165, 0 new.python3 tools/pr_check.py --base origin/main: 0 errors, 1 advisory warning (the changelog line needs this pull request's number).zig build test-inkling2-hip-host test-inkling2-cuda-host compile-cuda-tests -Dhip=trueon this head: 21 of 21 testspassed, all steps succeeded.
zig build check-hip-kernel-contract-host -Dhip=true: builds.zig build test -Dhip=trueon this head: 12,788 of 12,883 passed, 94 skipped, 1 failed. The failure is on the basetoo and is not in files this changes:
stage_hip_test"every staged call on HIP memory equals the reference, wholeand in parts" fails on
main5c1fea6 as well, run alone on a quiet GPU, andmainhas not changedzig/src/fabric/tp/since.zig fmt --checkon the changed files andgit diff --check: clean.tauAtundeclared);expected error..., found voidbefore its check;const float pa = exp2f(sa - mu), ...), smoke goes red onexactly those two registrations (
13 kernels: 11 pass gates 1-4, 2 fail), while every other gate stays green.release-paths (ubuntu-24.04), benchmark-clients (ubuntu, windows) and
comfyui-moss-installer pass; host-nightly-linux and host-nightly-macos skipped.
objects.
arguments. An independent run of this contract at 4 and at 24 key heads, on an earlier head with Mini's floor,
passed every registration but the sliding one, which stopped at a probe shape the test refuses (3 rows at 2 keys).
Receipt
n/a: these kernels do not reach inference yet; no engine or server calls them. The kernel contract below is the
evidence. Same-engine exactness, teacher-forced fidelity against huggingface/transformers#51050 and the decode speed bar
come with the engine on #4387's host.
Kernel contract, only when a GPU kernel is new or changed
Kernels registered with the contract, and the kernel each one replaces (
tf-hip-kernel-contract image inkling2, atMini's 24/8 heads, served shapes to 1M keys on a 4,096-slot global ring that wraps):
tf_ink2_prepsliding and global: 8 shapes each, each output inside its range at the device's rounding points,tau at n_floor 8,192 to 1M positions, including pos + 1 = 8,193 and 8,194.
tf_ink2_rel_table: 4 shapes within the float64 bound, worst 0.996 of it; gate 4's sums exact, bf16 would miss 1,792x.tf_ink2_attn+tf_ink2_merge(sliding): 17 shapes within the bound, worst 0.779.tf_ink2_attn_fold+tf_ink2_merge(global): 18 shapes within the bound, worst 0.726, including rows atpositions 0 to 1,024 against float64.
sliding, fold and split (12 shapes each). The second and third streams sit near half and a third of the context,
far from the call's last position.
values, and biases drawn so each bf16(exp2f) is more than 2^-21 from a rounding tie. Every sum is then exact in
fp32, and each output must equal bf16(fp32(sum P v / sum P)) bit for bit (7 shapes each). The float64 bound alone
allows P's bf16 rounding either way, so this is what pins it.
state that 64 keys prove neither.
A planted control for each new kernel, and each one fails as planted (through the device's inputs or launch; the
served kernels run unchanged), all red at
shapes:Each backend's
SUMMARYline and exit status (gfx1151, ROCm 10.1's hipcc, HIP 7.16):SUMMARY inkling2 HIP full: 13 kernels: 13 pass gates 1-4, 0 fail, 0 known findings; 8 controls: 8 red as planted, 0 missed; 0 limit problems; gates 155893 ms, limits 4 ms(exit 0)zig build hip-kernel-contract-limits: everyinkling2record PASS on all four chips. The step exits 1 with 11problems, the same 11 as on
main5c1fea6 with this hipcc (14951bc changes no HIP record, kernel orzig/build/hip.zig), 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.Records (
hip_records_inkling2.zig), VGPRs on gfx1030 / gfx1100 / gfx1151 / gfx1201; no scratch on any chip:tf_ink2_preptf_ink2_rel_tabletf_ink2_attntf_ink2_attn_foldtf_ink2_attn_prompttf_ink2_mergeChips I could not run: gfx1030, gfx1100 and gfx1201 (limits only), and every CUDA SM.
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)