Skip to content

[Engine] Inkling 2 on AMD: GPU doorbell for experts in RAM - #306

Open
okada-shogo wants to merge 2 commits into
simplexai-labs:mainfrom
okada-shogo:port/hip-doorbell
Open

okada-shogo wants to merge 2 commits into
simplexai-labs:mainfrom
okada-shogo:port/hip-doorbell

Conversation

@okada-shogo

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

Copy link
Copy Markdown
Contributor

Problem and result

On CUDA, main has a "doorbell" for decode (STRATA_POLY_BELL): the GPU routes the token itself and asks the CPU workers for the experts it lacks by setting a flag in pinned memory, so there is no host round trip. AMD (HIP) did not have it. When some routed experts were outside VRAM, an Inkling 2 decode token waited for the host at each of its 48 layer visits: the router's scores went to the host, the host waited on an event, and the expert picks came back. Under WSL2 (dxg), one such round trip costs 30-90 µs.

This PR turns on main's doorbell for HIP. The card routes the token, signals the host through pinned memory for the experts it lacks, and goes on with the experts it has. The host's CPU workers answer the same way. The token's layers run as one recorded graph, with no host round trip inside it. On an RX 9070 XT, IQ4_NL decode is 39.4% faster after a short prompt and 14.8% faster after 8,192 tokens.

Decode tok/s, RX 9070 XT 16 GB (WSL2, ROCm 7.2.4), mean of 3 runs (warm-up request, rotated order, free placement):

Card Model Measure main this PR alone Change
RX 9070 XT 16 GB IQ4_NL decode after 302 (tok/s) 43.9 61.2 +39.4%
RX 9070 XT 16 GB IQ4_NL decode after 8,192 (tok/s) 34.5 39.6 +14.8%
RX 9070 XT 16 GB UD-Q2_K_XL (loop) decode after 302 (tok/s) 54.3 55.0 +1.3%
RX 9070 XT 16 GB UD-Q2_K_XL (loop) decode after 8,192 (tok/s) 52.1 52.2 +0.2%

Native Linux, the same RX 9070 XT (Fedora, in-tree amdgpu, ROCm 7.2.4; a host round trip costs 18 µs there against 41-97 µs under WSL2), mean of 3 runs, --threads 11 --vram-reserve-mib 700:

Card Model Measure main this PR alone Change
RX 9070 XT 16 GB (native Linux) IQ4_NL decode after 302 (tok/s) 63.4 67.2 +6.0%
RX 9070 XT 16 GB (native Linux) IQ4_NL decode after 8,192 (tok/s) 58.2 61.2 +5.0%
RX 9070 XT 16 GB (native Linux) UD-Q2_K_XL (loop) decode after 302 (tok/s) 65.6 65.4 -0.2%
RX 9070 XT 16 GB (native Linux) UD-Q2_K_XL (loop) decode after 8,192 (tok/s) 61.6 61.3 -0.5%

On native Linux UD-Q2_K_XL holds 3,058 of 3,072 experts, so it rarely rings; its rows are within noise.

UD-Q2_K_XL fits the card whole (3,072 of 3,072 experts in VRAM), so it never uses the doorbell: its rows are a control (no change expected). Prompt speed was not A/B-timed: a prompt's calls have more than one row, so they never use the doorbell.

Accuracy class: routing rounding, not the same bits. The card's routing is the same as CUDA's doorbell, and the same as HIP's own routing when every expert is in VRAM (route_rows(), the GPU's expf). HIP main routed these tokens on the host (route_by(), std::exp). The two round the router's sigmoid a little differently. On CUDA, main's doorbell also differs from STRATA_POLY_BELL=0 in the same way. The KL check is within the noise of a placement control: top-1 is the same at 30-31 of 32 steps, as for the control (31-32 of 32). STRATA_POLY_BELL=0 turns the doorbell off and gives old main's bits.

Accuracy: KL against main, per text and placement (RX 9070 XT, IQ4_NL)

KL(main ‖ this PR) over main's top 20 at each step, RX 9070 XT, IQ4_NL, 2,048-token prompts + 32 written. This PR is
fed main's tokens. The control is main again, with 10 fewer experts in VRAM (frozen) or in a second process (free).
Texts a and b are GPL text the model knows by heart; text c is random synthetic sentences (nothing memorized):

Placement Text Pair top-1 same KL median KL mean KL mean without the worst step JS mean
frozen (1,600) a this PR 31/32 0.00011 0.165 0.065 0.027
frozen (1,600) a control 32/32 0.00058 0.044 0.029 0.009
frozen (1,600) b this PR 31/32 0.00002 0.047 0.005 0.013
frozen (1,600) b control 31/32 0.00036 0.086 0.015 0.019
frozen (1,600) c this PR 31/32 0.00051 0.0017 0.0011 0.00007
frozen (1,600) c control 32/32 0.00000 0.0022 0.0013 0.00013
free a this PR 31/32 0.0026 0.073 0.058 0.014
free a control 31/32 0.0029 0.059 0.042 0.011
free b this PR 30/32 0.00087 0.034 0.019 0.009
free b control 31/32 0.0011 0.081 0.045 0.015
free c this PR 31/32 0.00001 0.00002 -0.0003 0.00004
free c control 32/32 0.00000 0.00007 -0.0002 0.00001
  • The worst steps (text a step 29 frozen, step 20 free; text b steps 2, 13, 22) are steps where the control itself
    moves by more than 0.5 nat (reference-unstable).
  • No step has the mode-flip or top-1 swap shape.
  • Step 0 (right after the prompt) has the same top-1 on every text, KL ≤ 0.001.
Host round trips: bench report diagnosis (RX 9070 XT, IQ4_NL)

strata-poly --bench-report (branch port/bench-roofline: #285 and its report), quick tier, decoding one token per call. Left: that build. Right: the same build merged with this PR. One run each. The bench text finds ~100% of its experts in VRAM, so CPU experts are almost absent here.

Measure bench-roofline + this PR
Token (ms) 18.4 14.7
Decode, quick runs (tok/s) 53.6 65.0
Host round trips a token 48 0
Card idle during host round trips (ms a token) 4.75 -
Launches a token (direct / replayed) 242 / 1,246 2 / 1,582
  • The report's "most to recover" list no longer has host round trips. Its top item is now launches (~6.3 ms at the probed floor).
  • Its "GPU waits for CPU experts" row (4.55 ms) comes from the tokens it times layer by layer, which it launches kernel by kernel. There a doorbell request waits for the host's experts without the overlap a whole-token graph has. The whole-token figure (14.7 ms) has no such wait.
  • Probes on this box: a host round trip (kernel, event, wait) 97.3 µs, a small kernel 2.3 µs.
Each run (decode tok/s)
Card Model Measure Build Run 1 Run 2 Run 3
RX 9070 XT 16 GB IQ4_NL decode after 302 main 37.2 49.9 44.5
RX 9070 XT 16 GB IQ4_NL decode after 302 this PR alone 59.4 60.5 63.7
RX 9070 XT 16 GB IQ4_NL decode after 8,192 main 35.4 34.4 33.7
RX 9070 XT 16 GB IQ4_NL decode after 8,192 this PR alone 38.0 40.1 40.6
RX 9070 XT 16 GB UD-Q2_K_XL (loop) decode after 302 main 53.4 56.2 53.3
RX 9070 XT 16 GB UD-Q2_K_XL (loop) decode after 302 this PR alone 55.1 55.0 55.0
RX 9070 XT 16 GB UD-Q2_K_XL (loop) decode after 8,192 main 52.2 51.5 52.7
RX 9070 XT 16 GB UD-Q2_K_XL (loop) decode after 8,192 this PR alone 53.1 51.6 52.0

main held 2,388 of 3,072 IQ4_NL experts in VRAM, this PR 2,385-2,386.

Related discussion

#200, comment #200 (comment) (AMD as separate PRs, one
optimization per PR).

Approach

  • engine/src/poly/bell.cuh (new): the card's side of the doorbell.
    • On HIP: system-scope atomic loads of ack/abort, a system-scope release store of ring, an acquire fence
      after the wait, and s_sleep between polls.
    • On CUDA: the same volatile accesses and __nanosleep(500) as before, so the generated code is unchanged.
  • bell_ring_kernel and bell_add_kernel use it. Nothing else in the kernels changes.
  • Pinned memory: Engine::hmalloc(..., live) makes the bell, the token's input (h_x_) and the host's answer
    (h_cpu_) hipHostMallocMapped | hipHostMallocCoherent on HIP. hipHostMalloc's default memory is not coherent, so
    a running kernel could read it from a cache.
  • bell_serve() on HIP makes one hipStreamQuery after the token's launches. Under WSL2, a launch followed only by polling host memory was sometimes not submitted for over 2 s (32 of 40 tries in a probe on an RX 9070 XT); one query after the launch fixed it. After that, the serve loop's event queries keep making HIP calls.
  • Abort and timeout:
    • bell_serve() now gives up after 60 s with neither a request from the card nor the end of the card's work. It ends the card's waits (abort), syncs and throws, where the process would otherwise hang.
    • An exception in run()/step() after a doorbell kernel was launched ends the card's waits too (bell_unwind()).
  • STRATA_POLY_BELL=0 is unchanged: no doorbell, and the host routes as in old main. It is documented in
    docs/SETTINGS.md and docs/models/inkling.md.
  • Guesses (--window N, the drafter): a window's tokens are now routed on the card too, where a single token is,
    with the step()-rows path main already has for --batch (1d4d9e2). Without it, a window's rows were routed on
    the host (std::exp) and a single token on the card (expf), so guessed and unguessed answers could part at near
    ties. Same commit as the CUDA fix in PR (port/window-card-picks); whichever merges second drops it.
  • The doorbell is VecSzn's design (after Strata v0.2.19's Inkling hybrid mode). This PR only ports it to HIP.

With other open PRs:

  • [Engine] Inkling 2 on AMD under WSL2: simpler decode graph #293 (HIP: layer recordings only for a whole pass) needs a one-line change with this PR. Without it the two together are slower than [Engine] Inkling 2 on AMD under WSL2: simpler decode graph #293 alone. Native Linux, the AMD combined branch (both PRs and others), decode tok/s, mean of 3 runs:

    Model Measure without this PR with this PR, no fix with this PR + fix
    IQ4_NL after 302 70.2 66.4 76.5
    IQ4_NL after 8,192 63.8 60.6 69.1
    UD-Q2_K_XL (loop) after 302 75.3 70.8 83.9
    UD-Q2_K_XL (loop) after 8,192 70.0 66.0 77.2

    ("without this PR" is the "no fix" binary run with STRATA_POLY_BELL=0.) [Engine] Inkling 2 on AMD under WSL2: simpler decode graph #293 merges without conflict, but its new line
    if (replay && n == 1 && !layer_records() && !card_routes()) replay = false; also turns off the whole-token
    graph on the doorbell path. Whichever merges second must add && !bell_on(n). Checked by trial merge:
    identity is the same as this PR's and decode works (39.5 tok/s in the frozen identity run against this PR's 39.5).
    It can't be in this PR, because layer_records() isn't on main yet.

  • [Engine] Inkling 2: merge small decode kernels for CPU experts #288 (CUDA: fewer decode launches on the doorbell path, with route_ring and merged kernels at the end of a layer): one comment conflict in kernels.cu. Its ink_moe_bell_conv_add
    kernel should call bell_wait() from bell.cuh in place of its own loop. With that, the merge builds for HIP
    and poly_inkling2_test passes on the RX 9070 XT.

  • [Engine] Retry instead of failing when a CUDA graph is out of VRAM #301 (graph out of VRAM): merges without conflict. Its drop_recordings() comment says HIP has no doorbell, which
    is no longer true with this PR. The cudaStreamSynchronize there is still safe: nothing is recorded between a
    doorbell request and its answer (checked in run(), step() and the drafter). Whichever lands second only updates
    the comment.

Validation

Passed:

  • poly_bell_test (new), RX 9070 XT (gfx1201, WSL2, ROCm 7.2.4) and RTX 2070 (sm_75): 16 checks, 0 failed. Cases:
    • no miss (no ring);
    • one layer's request sent and answered while the card waits, 2,000 rounds with new numbers each, some answered late;
    • 48 layers in one recorded graph;
    • a token-sized pass: 1,536 kernels with large arguments in two recorded graphs, answered as they ask, no deadlock;
    • an answer 3 s late (the card waits in one kernel that long; no GPU reset under WSL2);
    • abort, and the bell working again after it.
  • poly_inkling2_test: RX 9070 XT 336 checks, 0 failed for this PR and for this PR merged with [Engine] Inkling 2: merge small decode kernels for CPU experts #288; RTX 2070
    425 checks, 0 failed.
  • Frozen identity, strata-poly --tokens @ids302 --max-new 64 --top-probs 20, STRATA_FREEZE_EXPERTS=1:
    • CUDA, RTX 2070 8 GB, STRATA_GPU_EXPERTS=600, IQ4_NL and UD-Q2_K_XL loop, graphs and --no-graphs: this PR
      against main 36e654b SAME in all 4.
    • HIP, RX 9070 XT, STRATA_GPU_EXPERTS=1600, IQ4_NL: STRATA_POLY_BELL=0 against main SAME. The doorbell
      against main: DIFF (routing class, above). Same tokens, top-1 the same at 64 of 64 steps. Router wait 2.8 → 0.3
      ms/token. Graphs and --no-graphs give the same bits.
  • Guess identity, RX 9070 XT native Linux, IQ4_NL, STRATA_FREEZE_EXPERTS=1 STRATA_SPEC_FIXED=1, 128 tokens of code 2048 and chat/prose 8192, each mode's --vram-reserve-mib set so all hold the same 2,284 experts: --window 4 and the DSpark drafter against no guesses differed in 6 of 6 without the window commit, 0 of 6 with it.
  • KL check (table above, in the accuracy details): kl.sh, cmp47.py --ref2 with the safeguards (median, mean without the top step, JS,
    mode-flip tags, a placement control).
  • Decode e2e: strata-poly --serve --prompt-cache 0 --conversation-cache-mib 0 --max-context 32768 --kv fp16, a
    16-token warm-up request, then 256 tokens after 302 or 8,192, free placement, 3 rounds with the order rotated.
    Machine: Salad RX 9070 XT + Ryzen 7 9850X3D, 23 GB RAM, WSL2. Both builds came from the same build run (rbuild, ROCm 7.2.4 container, gfx1201, AVX2).
  • Native Linux (RX 9070 XT, Fedora, in-tree amdgpu, ROCm 7.2.4 container, Ryzen 7 7800X3D): the decode A/B above, main 36e654b against this PR and against this PR with STRATA_POLY_BELL=0, 3 rounds rotated, --serve as below with --threads 11 --vram-reserve-mib 700. The written tokens with STRATA_POLY_BELL=0 equal main's in every case where main's own runs agree with each other (IQ4_NL after 8,192, UD-Q2_K_XL after 302 and 8,192). With the doorbell on, IQ4_NL after 8,192 is the same too, and UD-Q2_K_XL differs (routing class).
  • Builds: HIP gfx1201 (strata-poly, poly_bell_test, poly_inkling2_test), CUDA sm_75 and 75;86;120.

Failed:

  • none observed

Not run:

  • Windows HIP.
  • Frozen identity (STRATA_GPU_EXPERTS) on native Linux: main itself refuses every cap there ("the card has N MiB for the expert cache, the first N experts take a few MiB more"), so only free-placement runs were compared.
  • RDNA3 (gfx1100/1101).
  • Speed on CUDA: the CUDA code is unchanged.

Limits

  • The output is not bit-identical to old HIP main on these tokens (routing rounding, as on CUDA). STRATA_POLY_BELL=0
    is the opt-out.
  • While the host computes a miss, the card waits inside one kernel. A very slow host (a cold first token reading its
    experts from disk) keeps that kernel running. 3 s was fine under WSL2. The 60 s timeout ends anything longer.
  • The gain is smaller after long prompts (+15% after 8,192), where attention and misses weigh more than the round
    trips did.
  • On native Linux a host round trip is cheap (18 µs), so the gain there is smaller: +5-6% on IQ4_NL. It still pays,
    so the doorbell stays on by default on every AMD system (no gate by OS); STRATA_POLY_BELL=0 turns it off.
  • With [Engine] Inkling 2 on AMD under WSL2: simpler decode graph #293 the one-line change above is needed, or the pair is slower than [Engine] Inkling 2 on AMD under WSL2: simpler decode graph #293 alone.

🤖 Generated with Claude Code

…VRAM routes on the card, with no host round trip a layer

Until now HIP had no doorbell, so an Inkling 2 token with some routed experts outside VRAM waited for the host
at every layer (the router's scores to the host, an event wait, the picks back): 48 host round trips a token,
30-90 us each under WSL2 (dxg). With the doorbell the card routes, rings the host for the experts it lacks and
goes on; the host's CPU workers answer through pinned memory, and the whole token is one recording.

- bell.cuh: the card's side of the bell. On HIP: system-scope atomic loads and a release store for the bell's
  words, an acquire fence after the wait, s_sleep between looks. CUDA keeps its volatile accesses and
  __nanosleep(500) (the same code as before).
- Engine::hmalloc(..., live): the bell, the token's input (h_x_) and the host's answer (h_cpu_) are
  hipHostMallocCoherent on HIP (hipHostMalloc's default memory is not coherent). CUDA unchanged.
- bell_serve(): on HIP one hipStreamQuery after the token's launches. Under dxg a launch followed only by a look
  at host memory was not submitted within 2 s in 32 of 40 tries on an RX 9070 XT; one query fixed it.
- bell_serve() gives up after 60 s with neither a ring nor the card's end (the card's waits end, the call
  throws), and run()/step() end the card's waits when a throw comes after a ring was launched (bell_unwind()).
- poly_bell_test: ring and answer while the card waits, a whole pass in one recording, a token-sized pass
  (1,536 kernels in two recordings), an answer 3 s late, an abort.

Routing on the card is the doorbell's own (route_rows(), as on CUDA and as HIP already routes with every
expert in VRAM). It rounds the router's sigmoid differently from the host's route_by(), so the result is not
bit-identical to HIP main's host-routed path (on CUDA main the doorbell differs from STRATA_POLY_BELL=0 in the
same way). STRATA_POLY_BELL=0 gives main's bits.

The doorbell is VecSzn's design (after Strata v0.2.19's Inkling hybrid mode); this ports it to HIP.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
okada-shogo added a commit to okada-shogo/PolyStrata that referenced this pull request Nov 24, 2026
With the HIP doorbell (simplexai-labs#306) a token routes on the card, so the per-layer rule
for partial caches must not turn the token's recording off. bell_on() is false
on HIP without simplexai-labs#306, so this changes nothing on its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@okada-shogo okada-shogo changed the title [Engine] Inkling 2 on AMD: the doorbell, so a decode token with experts in RAM routes on the card [Engine] Inkling 2 on AMD: route CPU experts through a GPU doorbell instead of host round trips Nov 24, 2026
@okada-shogo okada-shogo changed the title [Engine] Inkling 2 on AMD: route CPU experts through a GPU doorbell instead of host round trips [Engine] Inkling 2 on AMD: bring main's CUDA doorbell to HIP, so the GPU asks the CPU for missing experts without a host round trip (+39% IQ4_NL decode) Nov 24, 2026
@okada-shogo okada-shogo changed the title [Engine] Inkling 2 on AMD: bring main's CUDA doorbell to HIP, so the GPU asks the CPU for missing experts without a host round trip (+39% IQ4_NL decode) [Engine] Inkling 2 on AMD: GPU-to-CPU doorbell for experts in RAM (+39% IQ4_NL decode) Nov 24, 2026
@okada-shogo okada-shogo changed the title [Engine] Inkling 2 on AMD: GPU-to-CPU doorbell for experts in RAM (+39% IQ4_NL decode) [Engine] Inkling 2 on AMD: GPU doorbell for experts in RAM Nov 24, 2026
With the doorbell (bell_on(): Inkling 2 with experts outside VRAM, now on
HIP as well) a token alone is routed on the card: route_card(), whose gate
sigmoid uses the card's expf.  A window of several tokens (--window N
lookup guesses, the drafter's verify) went to moe(), which routed each row
on the host: route(), the same formula with the host's std::exp.  The two
need not round alike in the last bit, so a guessed token's expert weights
(and, at a near tie, an expert) could differ from the ones it got alone,
and greedy answers with guesses parted from the answers without them.

run() now does for a window what step() does for its rows since
1d4d9e2: where a token alone routes on the card, up() routes the
window's rows there before the router's event, with the picks and
weights written straight to pinned memory (h_ids_, h_wts_, now also allocated for bells()
with a window over 1), and moe() takes them (step_ids_).  step()'s tests
move into picks_like_alone() and picks_like_alone(L), shared by both;
step() behaves as before.  Not in a window's halves (they need
reads_by_layer(), which no family with the doorbell has), not with
--cache-bias, and not on Metal (no doorbell there).

Class: same bits as one token at a time.  RX 9070 XT, IQ4_NL, frozen
experts at equal placement, STRATA_SPEC_FIXED=1, 128 tokens of code 2048
and chat/prose 8192: --window 4 and the DSpark drafter differed from no
guesses in 6 of 6 before, 0 of 6 after.

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