Skip to content

[Engine] HIP: v_perm_b32 for the 16-value codebook lookups on every AMD card - #237

Draft
okada-shogo wants to merge 8 commits into
simplexai-labs:mainfrom
okada-shogo:port/amd-vperm
Draft

okada-shogo wants to merge 8 commits into
simplexai-labs:mainfrom
okada-shogo:port/amd-vperm

Conversation

@okada-shogo

@okada-shogo okada-shogo commented Nov 22, 2026 •

Copy link
Copy Markdown

Status: tests pending. This draft is open for early review. Its tests are queued and will run on: Salad RX 9070 XT (gfx1201) / RX 7900 XT (gfx1100). Results will be added here; if it doesn't help, it will be closed.

Problem and result

For AMD cards (HIP), with IQ4_NL, IQ4_XS or MXFP4 weights. These formats decode each weight through a 16-value
table (table16_lookup()). This PR switches every HIP build to the faster of two instruction sequences for that
lookup. Same bytes. Not measured yet on this branch. CUDA is unchanged (the code is under __HIPCC__).

What it changes, in plain words: there are two ways to do the lookup: llama.cpp's v_perm_b32 sequence and a
__byte_perm one. Both give the same bytes. Main uses v_perm_b32 only on gfx906 and __byte_perm on every other AMD
card. On RDNA4, v_perm_b32 was faster in our earlier engine, so every HIP build now uses it.

Related discussion

Split out of #200, as the maintainer asked there:
#200 (comment)

Stacked on #217 (AMD support, not yet working end to end: the prompt hang is open). Speed numbers come after #217 runs. This branch carries #217's first commit (b681c69, the DSpark HIP build fix), so its diff here shows that commit too. It will be rebased as #217 moves.

Approach

  • Both sequences return the same bytes, so the output bits do not change.
  • Expected gain, from our first Inkling engine on the 9070 XT (not this branch): 8 cached IQ4_NL experts 337 -> 411 GB/s (2,304 values), 244 -> 349 (1,152);
    IQ4_NL decode 43.2 -> 45.6 tok/s.

Validation

Passed:

Failed: none observed.

Not run:

  • Logits with the experts frozen, base vs this branch (expected the same): queued (Salad RX 9070 XT / RX 7900 XT).
  • Decode tok/s, IQ4_NL: queued.
  • Effect on RDNA3 (gfx1100): queued on the RX 7900 XT.

Limits

🤖 Generated with Claude Code

okada-shogo and others added 5 commits November 23, 2026 04:48
The HIP build of strata-poly stopped at inkling2.cu since 8ddd416:
the screen kernels (scr_base_kernel, scr_step_kernel, scr_final_kernel)
call __ldcs and __ldcg on uint4, uint32_t, float and float2, and HIP
has those two only for halves ("no matching function for call to
'__ldcs'", ten errors, ROCm 7.2.4, gfx1201).  On HIP they are now plain
loads: the screen uses them as cache hints, on a copy's words read once
and on candidates that earlier launches wrote.  CUDA builds compile the
same code as before (the definitions are under __HIPCC__).

Class: same bits on CUDA; HIP did not build before.  Inkling 2 is
VecSzn's engine.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Erqa3PhC87SPTfVSKZ3Ri5
docs/models/inkling.md said "Not run yet: an AMD card".  It now says
what was seen on the first AMD runs of this engine: it builds for
gfx1100, gfx1200 and gfx1201, main's HIP tests pass 15 of 16 on an
RX 7900 XT and 11 of 16 on an RX 9070 XT (ROCm 7.2.4, a container that
reaches the card through librocdxg), a prompt with experts in RAM can
hang, and on the RX 9070 XT the prompt products' library gave a NaN and
run-to-run differences in its test.  Setup's family line (and the MCP
server's copy of it) says AMD cards are not ready.  No behaviour change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ROCm 7.2.4 under WSL2 (librocdxg 1.2.2, an RX 9070 XT) refuses
hipMemSetAccess (hipErrorInvalidValue) on a piece mapped after a larger
one in the same reservation: 64 MiB then 1 MiB, 2 then 1, 8 then 4,
64 then 4 all fail, while pieces of one size always pass (64 + 64,
8 + 8, 1 + 1).  vmm_map() maps a reservation in pieces of the size it
is asked for and a shorter last one, so the expert cache lost its last
piece (3,058 of Inkling 2 Mini UD-Q2_K_XL's 3,072 experts in VRAM,
whatever the VRAM, reserve or context), and an attention cache grown to
the end of its reservation failed ("no VRAM for the attention cache
(1 MiB more)": an 8,192-token prompt at --max-context 8704).

On a Linux HIP build under WSL2 (/dev/dxg and no /dev/kfd: the test of
setup's and the launcher's WSL note, wsl_amd_note()) every piece of a
reservation is the size of its first: a reservation is a whole number
of 2 MiB units, a first map's piece the most units that divide it and
are no more than the piece asked for and the map's bytes, and a map
covers whole pieces.  The expert cache reserves a whole number of its
64 MiB pieces and an attention cache a whole number of cover()'s, so
they keep those sizes; cover() counts the rows' VRAM as the pieces
vmm_map() maps.  STRATA_VMM_EQUAL_PIECES=1/0 turns it on or off on any
Linux HIP build.  Native Linux and Windows HIP, the pool (--vram-pool,
already one size) and CUDA are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ROCm keeps a file descriptor open for each VMM handle (its dmabuf;
librocdxg under WSL2), so the usual soft limit of 1,024 caps the
handles a process can map: with --vram-pool's 2 MiB handles an RX 9070
XT stopped at 774 of Inkling 2 Mini UD-Q2_K_XL's experts ("fail to
import fd, ret 11", and the map fails as a card without VRAM does),
where a raised limit maps 6,000 handles (12 GB).  A HIP build on Linux
now raises its soft RLIMIT_NOFILE towards the hard limit (at most
2^20) at load, as any process may, and never lowers it.  CUDA builds
are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Mmq::mul()'s BLAS path reads a launch's bounds on the host, where
Mmq::mirror() says they are, as Engine::alloc() sets it up for tab_.
poly_mmq_tiles_test never called mirror(), so wherever the BLAS path
takes its matrices (every HIP build: gemm_iq_f16_grouped and the int8
tiles are CUDA-only) it aborted at its first case with "the matrices'
bounds are not where mirror() says" (RX 7900 XT gfx1100 and RX 9070 XT
gfx1201, ROCm 7.2.4).  The test now mirrors its reference bounds and
its class bounds before the launches that read them, as the engine
does.  Only the test changes.  With it the test passes (48 cases) on the RX 7900
XT (Salad container, ROCm 7.2.4).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
okada-shogo and others added 3 commits November 23, 2026 08:04
ink_clips_kernel and ink_qkv_kernel never finished on AMD cards: an
RX 9070 XT (gfx1201) under WSL2 and RX 7900 XT / RX 9070 XT
containers, ROCm 7.2.4.  The first prompt of Inkling 2 Mini stopped in
the first layer's ink_clips launch (with AMD_SERIALIZE_KERNEL=3 the
launch never returned; on Windows the driver reset the card), with or
without experts on the card, and poly_inkling2_test hung in its clips
and qkv cases while clip and head_rms passed.  The same source built
with any change to ink_clips_kernel finished, so it is the compiler's
code for these two kernels, not their arithmetic.

ink_clips() is ink_clip() and ink_head_rms() of its four jobs with the
same bits, and ink_qkv() ink_conv() of K and V and then the same, by
their own contract.  On HIP they are now those launches, with the K and
V rows stored by store_f16(), store_f16_at() (a recorded launch's
positions) or ink_store_ring() (a window's ring), the cells the fused
kernels wrote.  More launches a layer on AMD; CUDA builds compile the
same code as before (the change is under __HIPCC__).

Class: same bits by the kernels' contract (poly_inkling2_test's clips
and qkv cases, simplexai-labs#222, compare them); HIP did not run before.  The kernels
are VecSzn's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…inux)

Between prompts the expert cache takes the VRAM of the buffers only a
prompt batch uses (lend_prompt()), and a prompt maps them again.  ROCm
on Linux gives VRAM back only with its addresses (vmm_unmap() frees and
reserves them again), which the staging buffer's widening already stays
away from (Engine::alloc(), `lend`).  With the buffers lent strata-poly
crashed in 15 of 16 runs of Inkling 2 Mini IQ4_NL on an RX 9070 XT with
2,443 of its 3,072 experts in VRAM (ROCm 7.2.4 container): SIGSEGV
inside hipLaunchKernel, the dequantize launch of a prompt's dense
product (Mmq::mul's BLAS path) into the lent work memory.  With every
expert in VRAM nothing is lent, and it ran.  With this change 8 of 8 runs on that
card finished (2,424 of 3,072 experts in VRAM, 16 tokens written, the same first
tokens as on an RX 7900 XT).

Where STRATA_VMM_RELEASE_WITH_ADDRESSES is defined (HIP on Linux) the
buffers are now allocated as they are without VMM (our engine in simplexai-labs#200, which
ran this case, never lent them).  CUDA and Windows HIP are unchanged (the
preprocessed CUDA engine.cpp is identical).

Class: placement only (the buffers' VRAM stays theirs; the expert cache
does not grow into it between prompts).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…P card

table16_lookup() (IQ4_NL, IQ4_XS, MXFP4, the formats of 16 values) took llama.cpp's v_perm_b32 sequence on gfx906
only, and the __byte_perm one elsewhere.  Both give the same bytes; on RDNA4 the v_perm_b32 one is faster.  In our
first Inkling engine (PR simplexai-labs#200) on an RX 9070 XT (gfx1201, ROCm 7.2.4), 8 cached IQ4_NL experts' products read at
337 -> 411 GB/s (2,304 values) and 244 -> 349 (1,152), and Inkling 2 Mini IQ4_NL wrote 256 tokens after 302 at
43.2 -> 45.6 tok/s, medians of 3.  So every HIP build takes it.  CUDA builds compile the same code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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