Skip to content

[Engine] AMD on Linux: size the expert staging buffer to the misses - #322

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

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

Conversation

@okada-shogo

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

Copy link
Copy Markdown
Contributor

Problem and result

On ROCm on Linux the expert staging buffer is never lent back to the expert cache (vmm_unmap() frees VRAM there only
together with its addresses), so it stays one whole layer for the whole run. For Inkling 2 Mini IQ4_NL that is
1,033 MiB: its four Q8_0 layers have 128 experts of 8.1 MiB. A prompt call only copies the experts the cache lacks
through it.

This PR cuts the buffer, once the cache is filled, to what a layer's call can copy (plus a margin), and gives the rest
to the expert cache. On an RX 9070 XT at a 32K context the buffer goes from 1,033 to 384 MiB and the cache holds 131
more experts. Same bits for the same cached set.

Mean of 3 runs each, --prompt-batch 1024, --max-context 32768, a warm 302-token prompt first in every engine.

Prompt (tok/s)

Card Model Measure main this PR alone Change
RX 9070 XT, native Fedora Inkling 2 Mini IQ4_NL prompt 302 673.1 682.0 +1.3%
RX 9070 XT, native Fedora Inkling 2 Mini IQ4_NL prompt 2,048 1,267.4 1,263.1 -0.3%
RX 9070 XT, native Fedora Inkling 2 Mini IQ4_NL prompt 8,192 1,291.9 1,312.8 +1.6%

Decode (tok/s)

Card Model Measure main this PR alone Change Experts in VRAM (main → PR)
RX 9070 XT, native Fedora Inkling 2 Mini IQ4_NL decode after 302 64.1 64.8 +1.1% 2,363 → 2,494
RX 9070 XT, native Fedora Inkling 2 Mini IQ4_NL decode after 2,048 55.8 58.0 +3.9% 2,363 → 2,494
RX 9070 XT, native Fedora Inkling 2 Mini IQ4_NL decode after 8,192 59.0 59.7 +1.2% 2,347 → 2,486
Each run (tok/s)
Card Model Measure Build Run 1 Run 2 Run 3
RX 9070 XT, native Fedora IQ4_NL prompt 302 main 673.5 672.8 673.0
RX 9070 XT, native Fedora IQ4_NL prompt 302 this PR 681.3 685.6 679.1
RX 9070 XT, native Fedora IQ4_NL prompt 2,048 main 1,271.3 1,263.4 1,267.4
RX 9070 XT, native Fedora IQ4_NL prompt 2,048 this PR 1,266.5 1,262.8 1,260.1
RX 9070 XT, native Fedora IQ4_NL prompt 8,192 main 1,293.4 1,293.5 1,288.8
RX 9070 XT, native Fedora IQ4_NL prompt 8,192 this PR 1,306.5 1,311.0 1,320.9
RX 9070 XT, native Fedora IQ4_NL decode after 302 main 64.1 64.1 64.0
RX 9070 XT, native Fedora IQ4_NL decode after 302 this PR 64.7 64.9 64.8
RX 9070 XT, native Fedora IQ4_NL decode after 2,048 main 56.0 55.6 55.8
RX 9070 XT, native Fedora IQ4_NL decode after 2,048 this PR 58.5 57.7 57.7
RX 9070 XT, native Fedora IQ4_NL decode after 8,192 main 59.0 59.0 58.9
RX 9070 XT, native Fedora IQ4_NL decode after 8,192 this PR 59.6 59.7 59.7
Speed: Salad RX 9070 XT container (noisy node, not used above)

A rented RX 9070 XT (Salad, 12 vCPUs, ROCm 7.2.4 container), same commands and commit, 3 rotated rounds. This node
was noisy: runs of the same build spread by up to 23%, and the engines' cache sizes at load varied by run. The
numbers are listed for completeness; the native Fedora rows above are the clean A/B.

Card Model Measure Build Run 1 Run 2 Run 3
RX 9070 XT, Salad IQ4_NL prompt 302 main 372.3 400.6 401.5
RX 9070 XT, Salad IQ4_NL prompt 302 this PR 343.1 327.4 409.2
RX 9070 XT, Salad IQ4_NL prompt 2,048 main 859.3 831.3 858.4
RX 9070 XT, Salad IQ4_NL prompt 2,048 this PR 761.6 834.9 890.3
RX 9070 XT, Salad IQ4_NL prompt 8,192 main 281.8 329.3 312.9
RX 9070 XT, Salad IQ4_NL prompt 8,192 this PR 280.5 309.7 288.5
RX 9070 XT, Salad IQ4_NL decode after 302 main 38.4 42.8 44.0
RX 9070 XT, Salad IQ4_NL decode after 302 this PR 36.8 36.4 33.9
RX 9070 XT, Salad IQ4_NL decode after 2,048 main 33.1 33.6 37.0
RX 9070 XT, Salad IQ4_NL decode after 2,048 this PR 26.5 28.2 32.6
RX 9070 XT, Salad IQ4_NL decode after 8,192 main 19.2 21.2 22.7
RX 9070 XT, Salad IQ4_NL decode after 8,192 this PR 22.4 23.8 23.9

Experts at load in these runs: main 2,230-2,235, this PR 2,310-2,363.

VRAM: experts and staging buffer (RX 9070 XT, MiB)

Requests in one engine: 2,048 + 64, 8,192 + 64, 302 + 256, 2,048 + 64.

Card Context Build Staging buffer Experts after load after 2K after 8K after 302 after 2K
native Fedora 32,768 main 1,033 2,363 2,363 2,347 2,361 2,406
native Fedora 32,768 this PR 384 2,494 2,494 2,486 2,486 2,545
native Fedora 131,072 main 1,033 2,363 2,363 2,347 2,361 2,406
native Fedora 131,072 this PR 640 2,442 2,442 2,434 2,434 2,493
Salad 32,768 main 1,033 2,130 2,130 2,144 2,173 2,278
Salad 32,768 this PR 448 2,325 2,325 2,317 2,317 2,391
Salad 131,072 main 1,033 2,200 2,200 2,196 2,196 2,270
Salad 131,072 this PR 640 2,310 2,310 2,302 2,331 2,391
  • The margin grows with the context: it covers what the attention caches may still take from the expert cache.
  • The BLAS handle (145 MiB) is not changed. ROCBLAS_DEVICE_MEMORY_SIZE=8388608 frees about 24 MiB (no more
    experts). ROCBLAS_USE_HIPBLASLT=0 frees 112 MiB, but prompts become about 2x slower.

Related discussion

None. This follows from a VRAM breakdown of Inkling 2 Mini on the RX 9070 XT, where the staging buffer was the
largest item after the expert cache and the dense weights.

Approach

  • Engine::trim_stage() runs in load() right after fill_expert_cache(), and again in resize_cache().
    • It sizes the buffer to the most bytes any layer's uncached experts take, plus a margin: 4 experts, plus what the
      attention caches may still map and the reserve, spread over the layers by bytes.
    • It keeps at least vram_ahead()'s 7 groups of 4 slots and card_rows() slots, so the router launches and
      far_plan() stay as they were. HIP builds no mirror, so nothing is uploaded into those slots.
    • The freed VRAM goes to the expert cache (grow_cache()).
  • On this path the buffer is a VMM reservation mapped in equal 64 MiB pieces, and the cut unmaps whole pieces.
    • Freeing a cudaMalloc buffer and allocating a smaller one made ROCm 7.2.4 later fail to re-reserve the expert
      cache's addresses at the same place. The engine then stopped on the next VRAM resize.
    • A smaller last piece after the 64 MiB ones was refused on native Fedora ("no VRAM"), as under WSL.
  • resize_cache() maps the whole layer again before it refills the cache, then cuts it for the new cache.
  • Only where STRATA_VMM_RELEASE_WITH_ADDRESSES is set (ROCm on Linux, WSL included), and only for Inkling 2
    (trims_stage()). CUDA, Metal, Windows HIP and the other families are unchanged.
  • Not with --stage-mib, the VRAM pool or a layer split. STRATA_POLY_STAGE_FIT=0 keeps the whole layer. (STRATA_STAGE_TRIM is the program engine's split-stack weight trim; strata-poly does not read it.)

Validation

Passed:

  • Setting: STRATA_POLY_STAGE_FIT=0 (new name, in the settings register; was read as STRATA_STAGE_TRIM). python3 tools/settings_register.py --check and tools/test_settings_register.py: OK at this commit.
  • Build at this PR's head: engine.cpp for HIP gfx1201 (ROCm 7.2.4). An independent review of the diff found no bugs.
  • Same cached set: the output is the same as main's. SAME means identical token ids and top-20 probabilities at
    every step, 32 tokens generated.
    • RX 9070 XT, native Fedora 9070 XT, ROCm 7.2.4 container, commit e979e95:
      • STRATA_FREEZE_EXPERTS=1. The PR's cache growth was turned off in a scratch build, and main was given
        --vram-reserve-mib 576, so both cache 2,350 experts.
      • Prompts of 302, 2,048, 5,040 and 8,192 tokens: SAME 4/4.
      • STRATA_GPU_EXPERTS=N fails on main on native Linux (see Limits), so it could not pin the set there.
    • RX 9070 XT, Salad container (gwts-rdna4b), ROCm 7.2.4, commit e979e95: STRATA_GPU_EXPERTS=2000 STRATA_FREEZE_EXPERTS=1, the same cache fnv in both builds. Prompts of 302, 2,048, 5,040 and 8,192 tokens, 8,192 with --no-graphs, and a forced multi-round --stage-mib 70 vs main at 70 MiB and vs main at its default: SAME 7/7.
  • The case that used to abort: an 8K prompt, then VRAM, a 2K prompt, then VRAM 700, then another 8K prompt,
    then VRAM. All 3 requests finished, at contexts 32,768 and 131,072, on native Fedora and on Salad.
  • Builds:
    • HIP (gfx1201, ROCm 7.2.4) on both boxes.
    • CUDA (sm_75, CUDA 12.9) locally.
  • Speed and VRAM: the tables above. Speed on the Salad node was too noisy to call (details block); the native box is the A/B. Commands:
    • VRAM: strata-poly --serve --prompt-cache 0 --conversation-cache-mib 0 --kv fp16 --prompt-batch 1024 --max-context N.
    • Speed: the same flags at a 32K context, with --threads 11 on the native box.
    • A trace patch was used only to read the VRAM figures. It is off without STRATA_VRAM_TRACE=1.

Failed:

  • None on the final commit.
  • An earlier version of the patch freed the cudaMalloc buffer and allocated a smaller one. It aborted in
    vmm_unmap() on the first VRAM resize after an 8K prompt. That is fixed by the VMM pieces above.

Not run:

  • RX 7900 XT/XTX, RX 7800 XT and other RDNA3 cards.
  • AMD CDNA.
  • Models other than Inkling 2 Mini IQ4_NL: the UD-Q2_K_XL loop file and the DSpark drafter.
  • KL vs a CUDA reference. Not needed: same bits for the same cached set.

Limits

  • If the cache later loses more experts than the margin covers (for example another program takes VRAM), a layer's
    call copies its misses in rounds. That is slower. On RDNA4's default BLAS loop it can also change the last bits of
    those calls, because a row's product there depends on the launch's rows. A frozen cache never does this.
  • At large contexts the margin is larger, so less is freed: 640 MiB stays at 131,072 tokens instead of 384 MiB.
  • On native Linux STRATA_GPU_EXPERTS=N already fails on main: the expert cache's smaller last VMM piece is
    refused, and vmm_equal_pieces() is only on under WSL. This PR does not change that.

🤖 Generated with Claude Code

…ROCm on Linux

Same bits for a given cached set (placement: more experts cached).  ROCm on Linux lends
nothing back to the expert cache (vmm_unmap() frees VRAM only with its addresses), so the
staging buffer was a whole layer for the whole run: 1,033 MiB for Inkling 2 Mini IQ4_NL,
whose Q8_0 layers have 128 experts of 8.1 MiB.  Once the cache is filled, load() now cuts
it to the most a layer's call copies through it (the experts the layer lacks, plus a
margin for what the attention caches may still take from the cache), with room for
vram_ahead()'s groups of 4 slots so the router launches stay as they were.  The freed
VRAM goes to the expert cache.  A cache that later loses more than the margin copies a
layer in rounds; on RDNA4's BLAS loop that changes the last bits of those calls.
Inkling 2 only (trims_stage()); CUDA, Metal and Windows HIP are unchanged.
STRATA_STAGE_TRIM=0 keeps the whole layer.

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

STRATA_STAGE_TRIM is the program engine's split-stack weight trim; strata-poly no longer reads it, so setting it for a split config cannot change poly's staging buffer. The new STRATA_POLY_STAGE_FIT=0 keeps the whole layer, as STRATA_STAGE_TRIM=0 did, and is registered.

Co-Authored-By: Claude Sonnet 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