Skip to content

[Engine] Inkling 2 on AMD: load hipBLASLt's prompt kernels at start, not in the first prompt - #257

Draft
okada-shogo wants to merge 14 commits into
simplexai-labs:mainfrom
okada-shogo:poly/hip-blas-warmup
Draft

okada-shogo wants to merge 14 commits into
simplexai-labs:mainfrom
okada-shogo:poly/hip-blas-warmup

Conversation

@okada-shogo

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

Copy link
Copy Markdown

Problem and result

Stacked on #217 (Inkling 2 on AMD). Review only the last commit.

On Windows with an RX 9070 XT, the first prompt of every strata-poly process waited 4 s with the GPU idle. hipBLASLt
loads a product type's kernels at its first call, and the first single-precision product (gemm_f32's cublasSgemm)
comes in the middle of the first prompt. This PR makes one small product of each kind the prompt uses while the
model loads, so the wait moves into the load. Nothing reads those products.

Windows 11, RX 9070 XT 16 GB, Inkling 2 Mini IQ4_NL, --vram-reserve-mib 3072, a fresh process each run, the
first request after start, mean of 3 runs.

Prompt speed, tokens per second (tok/s), higher is better:

Card Model Measure #217 this PR alone Change
RX 9070 XT 16 GB IQ4_NL first prompt 302 60.0 223.6 +273%
RX 9070 XT 16 GB IQ4_NL first prompt 2,048 284.7 605.5 +113%

Decode speed, tokens per second (tok/s), higher is better:

Card Model Measure #217 this PR alone Change
RX 9070 XT 16 GB IQ4_NL decode after 302 40.8 41.2 +1.1%

Load time, seconds (s), lower is better:

Card Model Measure #217 this PR alone Change
RX 9070 XT 16 GB IQ4_NL load 8.07 11.63 +44%

The total is about the same (load + first 302-token prompt: 13.1 s → 13.0 s). What changes is the wait after the
server says it is ready. STRATA_POLY_WARMUP=0 turns it off.

Each run

Prompt tok/s, higher is better, each run:

Card Model Measure Build Run 1 Run 2 Run 3
RX 9070 XT 16 GB IQ4_NL first prompt 302 #217 60.6 60.8 58.6
RX 9070 XT 16 GB IQ4_NL first prompt 302 this PR alone 220.8 225.3 224.8
RX 9070 XT 16 GB IQ4_NL first prompt 2,048 #217 278.5 286.8 288.7
RX 9070 XT 16 GB IQ4_NL first prompt 2,048 this PR alone 593.8 615.0 607.7

Decode tok/s, higher is better, each run:

Card Model Measure Build Run 1 Run 2 Run 3
RX 9070 XT 16 GB IQ4_NL decode after 302 #217 40.3 40.9 41.1
RX 9070 XT 16 GB IQ4_NL decode after 302 this PR alone 41.7 40.7 41.3

Load time in seconds, lower is better, each run:

Card Model Measure Build Run 1 Run 2 Run 3
RX 9070 XT 16 GB IQ4_NL load #217 8.1 8.1 8.0
RX 9070 XT 16 GB IQ4_NL load this PR alone 11.7 11.6 11.6

"#217" is this build with STRATA_POLY_WARMUP=0, which skips exactly the added code. On and off alternated.

How the wait was found
  • Windows' counters during the first prompt: 3-4 s with the engine at 100% of one CPU core and every GPU engine idle.
    A second prompt in the same process has no such gap (302 tokens: 0.83 s).
  • ROCBLAS_LAYER=1, HIPBLASLT_LOG_LEVEL=2, AMD_LOG_LEVEL=3: the gap starts at the prompt's first rocblas_sgemm
    (T,N, 2304 × 301 × 64) and ends with a code-object load and hipBLASLt looking up its Cijk_..._S_B_... kernels.
    The f16 products before it took 0.45 s to load in all.
  • ROCBLAS_USE_HIPBLASLT=0 removes the gap (first prompt 2.2 s), but rocBLAS's own kernels make later prompts slower.
  • The prompt's products, from the trace: rocblas_gemm_ex f16→f32 (6,880 calls), rocblas_sgemm (192),
    rocblas_gemm_strided_batched_ex (97). blas_create() already made the last kind once at load.

Related discussion

none (a fix for #217's AMD path, kept as its own PR).

Approach

blas_create() already makes a 16 × 16 batched product of halves at load, because cuBLAS took 75 ms over the first
one. On HIP builds it now also makes one cublasGemmEx (halves into floats, T,N) and one cublasSgemm (floats, T,N)
on the same zeroed scratch. Their results are not read. CUDA builds are unchanged (#if defined(STRATA_USE_HIP)).

Class: same. No product of the model changes. Only the time of the library's load moves.

STRATA_POLY_WARMUP is in the settings register (rollback, hip, poly), docs/SETTINGS.md and docs/AMD_HIP.md.

Validation

Passed:

  • Build, Windows 11, ROCm 10.2.0a20260930 (TheRock wheels), VS 2022 Build Tools 17.14, gfx1201: strata-poly, rc=0.
  • Build, Linux, ROCm 7.2.4, gfx1201;gfx1100: strata-poly, rc=0. CUDA 12.9, sm_75: strata-poly, rc=0.
  • python3 tools/settings_register.py --check: rc=0. python3 -m unittest tools/test_settings_register.py: OK.
  • Identity, this build with the warm-up on vs STRATA_POLY_WARMUP=0, RX 9070 XT, Windows, IQ4_NL, --top-probs 20,
    token ids and the top-20 lines, 4 runs per case (on, off, off, on), with the expert split pinned
    (STRATA_FREEZE_EXPERTS=1 STRATA_POLY_AHEAD=0 --expert-swaps 0). Every "off" run matched the other "off" run, and
    every case has an "on" run identical to them. Two of the eight "on" runs differ (see Limits):
  • Speed: the table above.
Case Runs Ids Top-20
302 + 64 on, off, off, on SAME SAME
302 + 64, --no-gpu-experts on, off, off, on SAME 3 of 4 the same; the first "on" run differs
2,048 + 16 on, off, off, on 3 of 4 the same; the second "on" run differs 3 of 4 the same
8,192 + 16 on, off, off, on SAME SAME

Failed: none observed.

Not run:

  • Speed and identity on Linux HIP: the stall was measured on Windows only. The products are tiny, so the cost on
    Linux should be small, but it was not measured.
  • RDNA3 (gfx1100): built only.

Limits

  • On this card the engine does not give the same answer twice unless the expert split is pinned. The prompt path's
    staging is chosen from measured times, and so is the cache when it moves during a prompt. With the default settings,
    two runs with the warm-up off already differed (302 + 64: the same ids with other probabilities; with
    --expert-swaps 0 alone, other ids). The identity above therefore pins the split, and even then 2 of 8 "on" runs
    differed once each, in cases where other "on" runs matched. Answers here depend on timing. The warm-up changes
    timing (the first prompt no longer waits 4 s) and never the products: their results are not read. With
    --no-gpu-experts the ids were the same in all 26 runs, on or off.
  • On a Linux RX 9070 XT, [Engine] Inkling 2 on AMD (RDNA3/RDNA4): HIP support #217 alone was not repeatable either at 2,048 tokens, capped and frozen. hipBLASLt choosing
    its algorithms per process is the suspect; an RX 7900 XTX, which does not use hipBLASLt, gave the same answer each time.
  • The load takes 3.6 s longer, so the total time to the first answer does not change. Making the products on
    another thread while the experts load could hide it. Not done here.
  • The first prompt is still about 0.4 s slower than a second one in the same process.

🤖 Generated with Claude Code

okada-shogo and others added 14 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>
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>
The setting the WSL2 VRAM-piece change reads (engine.cpp) was missing
from terminal/data/settings-register.json, which settings_register.py
--check and tools/test_settings_register.py require.  It is there now
beside STRATA_VMM_SETTLE (research, poly), and docs/AMD_HIP.md says what
it does next to STRATA_VMM_SETTLE.  No code change.

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

With one piece size a reservation (STRATA_VMM_EQUAL_PIECES), a first map
that asks for any piece size (SIZE_MAX: the staging buffer, a read's
rows) took the most units that divide the reservation and are no more
than the map's bytes, and then covers whole pieces.  That can map up to
about twice the bytes: a reservation of 100 units and a first map of
51 gave pieces of 50 and mapped all 100, so the staging buffer's narrow
size gave the expert cache nothing back.  Such a map now takes the
divisor that maps the fewest units past its bytes, of 16 MiB or more
where one maps at most half again as much, the larger on a tie (100 and
51: pieces of 20 units, 60 mapped).  Maps that name a piece (the expert
cache, the attention caches) are as before.  HIP on Linux only; the
preprocessed CUDA engine.cpp is identical.

Class: placement only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ink_qkv()'s HIP path refused FP8 caches only after it had launched the
two convolutions; it refuses them first now.  And ink_qkv() and
ink_clips() take the fused kernels in an #else, so on HIP the templates
that never finished there are not instantiated at all.  CUDA compiles
the same code (inkling2.cu's SASS, PTX and host code have the same md5).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
docs/models/inkling.md said a prompt with experts in RAM can hang and
quoted the first runs' test counts.  It now says what runs: the HIP
tests 16 of 16 and the Inkling 2 kernel test on an RX 7900 XT and an RX
9070 XT (ROCm 7.2.4, librocdxg containers), the two failures fixed, and
the RX 7900 XT's answers against an RTX 2070 Super's; still not ready,
with the RX 9070 XT's 2,048-token difference open.  The "Not run yet"
paragraph is wrapped again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The "still open" line about the RX 9070 XT's 2,048-token answers now
says what it was: rocBLAS hands the FP16 GEMMs to hipBLASLt on gfx12,
which rounds them differently, and on that prompt (one every engine and
prompt batch splits on) ROCBLAS_USE_HIPBLASLT=0 brings the KL against
CUDA from 0.215 to 0.059, the RX 7900 XT's 0.056; after 302 and 8,192
tokens the RX 9070 XT matches CUDA (KL 0.0014 and 0.00001).  No code
change.

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

alloc() decided two_scratch_ before prompt_lends_, and never looked at it. Under
STRATA_VMM_RELEASE_WITH_ADDRESSES nothing is lent, yet a model with two scratch
layouts (Qwen3.6) kept both: after the first prompt take_prompt() switched to the
batch layout and lend_prompt() never switched back, so graph replay stayed off for
every later decode step. Decide whether the buffers are lent first, and keep two
layouts only where they are. Everywhere else the condition is the one before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
hipBLASLt loads a product type's kernels at its first call. On Windows
(RX 9070 XT, ROCm 10.2.0a20260930) the first single-precision product of
a prompt (gemm_f32's cublasSgemm) took 4 s on one CPU thread with the
card idle, inside the first prompt of every strata-poly process. A
prompt makes three kinds of product: halves into floats (cublasGemmEx),
floats (cublasSgemm) and the batched halves that blas_create() already
makes once.

blas_create() now also makes one small product of each of the other two
kinds on its zeroed scratch, on HIP builds, so the load happens while
the model loads. Nothing reads their results. Class: same (no product
of the model changes; the load moves from the first prompt to the
model's load). STRATA_POLY_WARMUP=0 leaves the two products out
(settings register, docs/AMD_HIP.md, docs/SETTINGS.md).

Measured on that card, Inkling 2 Mini IQ4_NL, --vram-reserve-mib 3072,
a fresh process each time, 3 runs each: the first 302-token prompt
1.35 s instead of 5.0 s, the first 2,048-token prompt 3.4 s instead of
7.2 s, the load 11.6 s instead of 8.1 s.

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