Skip to content

[Engine] Inkling 2: give expert-cache VRAM back while serving - #319

Open
okada-shogo wants to merge 1 commit into
simplexai-labs:mainfrom
okada-shogo:port/poly-vram-elastic
Open

okada-shogo wants to merge 1 commit into
simplexai-labs:mainfrom
okada-shogo:port/poly-vram-elastic

Conversation

@okada-shogo

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

Copy link
Copy Markdown
Contributor

Problem and result

On an AMD card that drives a desktop, the advice so far was a fixed --vram-reserve-mib 3072. With Inkling 2 Mini
on an RX 9070 XT that left 1,043 of 3,072 experts in VRAM, against 2,335 without it, for the whole run.

strata-poly's expert cache is already mapped in 64 MiB VMM pieces, and it already resizes when a request starts.
What was missing: nothing watches the free VRAM while the engine is idle, it only gives VRAM back once less than half
the reserve is free, and POST /v1/vram fills the whole cache again.

With "vram_elastic": true (engine flag --vram-elastic, opt-in, NVIDIA and AMD), the cache keeps its whole reserve
free while it serves, also while idle. When another program takes VRAM, the cache gives pieces back. When that VRAM is
free again, the cache takes it back, the experts it gave up first. Without the flag nothing changes.

Inkling 2 Mini IQ4_NL, frozen experts (STRATA_FREEZE_EXPERTS=1), greedy, 302-token prompt, 128 tokens; a second
process takes VRAM while the engine is idle, then ends:

Card Step Experts in VRAM Time of the resize Tokens vs no flag
RX 9070 XT class (gfx1201, WSL2), reserve 4000 MiB after load 1,486 SAME
other program holds 2,816 MiB 1,018 0.49 s SAME
other program ended 1,489 3 steps, 0.7-0.8 s each SAME
RX 9070 XT 16 GB, native Fedora (amdgpu), reserve 4000 MiB after load 1,645 SAME
other program holds 2,816 MiB 1,153 0.13 s (kept pieces mapped again) differs (expected)
other program ended 1,635 3 steps, 31-35 ms each SAME
RTX 2070 8 GB (Linux), reserve 2048 MiB after load 565 SAME
other program holds 1,280 MiB 284 0.03 s differs (expected, see below)
other program ended 540 2 steps, 0.11 + 0.05 s SAME
RTX 2070 Super 8 GB (desktop PC), reserve 2048 MiB after load 559 SAME as the same run's regrown cache (see below)
other program holds 1,280 MiB 284 0.06 s differs from the full cache (expected)
other program ended 534 2 steps, 0.10 + 0.04 s SAME as before the shrink

Decode tok/s, the same request at each step (one run each; most experts are on the CPU on these cards, so the decode
is CPU-bound and the spread between runs is as large as the change):

Card Model Measure full cache shrunk regrown
RX 9070 XT 16 GB (native Fedora) Inkling 2 Mini IQ4_NL decode tok/s 36.4 32.7 36.3
gfx1201 16 GB (Salad, WSL2) Inkling 2 Mini IQ4_NL decode tok/s 18.3 16.0 18.4
RTX 2070 8 GB Inkling 2 Mini IQ4_NL decode tok/s 17.4 16.9 17.4
RTX 2070 Super 8 GB Inkling 2 Mini IQ4_NL decode tok/s 17.2 18.2 18.9

Related discussion

#560 and #516 (AMD desktop OOM), #533 (the Qwen engine's --vram-elastic). The check follows strata-poly's own
Engine::mind_vram (VecSzn).

Approach

  • --vram-elastic keeps the whole reserve free, instead of half of it. When another program took a quarter of the
    reserve (64 MiB at least), the cache gives at least that much back in whole pieces. When a piece and 128 MiB more are
    free, it grows again. The two thresholds never meet, so the cache does not go up and down by itself.
  • An elastic shrink notes the experts it takes out. A grow brings those back first, so the cache after a shrink and a
    grow holds the same experts. A shrink and a grow use the existing shrink_cache / grow_cache bookkeeping
    (settle_swaps, the mirror, refill_). No expert is marked cached before its bytes are copied.
  • Idle watch: every 2 s while the engine waits for a line and no batch slot is writing. Never inside a request. A grow
    step takes at most 1 GiB, so a request that arrives waits at most that long. A freeze (STRATA_FREEZE_EXPERTS) gives
    way to it, as it does to POST /v1/vram. STRATA_VRAM_WATCH=0 turns it off; STRATA_VRAM_WATCH_MS sets the period.
  • POST /v1/vram with the flag resizes piece by piece to the asked number, instead of refilling the cache.
  • HIP under WSL2 (librocdxg): a released piece's VRAM comes back at the release, so a shrink no longer unmaps and
    remaps the kept pieces, the cycle that took seconds before. On native amdgpu this is checked once, at the first
    shrink. Under WSL2 no expert-cache piece is asked for unless its bytes and 256 MiB more are free, because an
    allocation past the card hangs there instead of failing.
  • Server: passes --vram-elastic to strata-poly when the config says "vram_elastic": true. On an AMD desktop the
    start note recommends that instead of --vram-reserve-mib 3072.
  • Class: placement only.
Each run, frozen identity (tokens vs the run without the flag)
Card Run full 2nd 3rd
gfx1201 no flag SAME SAME SAME
gfx1201 --vram-elastic, no resize SAME SAME SAME
Card --vram-elastic run full shrunk regrown VRAM 6000 VRAM (back)
gfx1201 experts 1,486 1,018 1,489 1,121 1,489
gfx1201 tokens SAME SAME SAME differs SAME

| RTX 2070 | no flag / flag without resize | SAME (all 3) | | | | |
| RTX 2070 | --vram-elastic experts | 565 | 284 | 540 | 149 | 540 |
| RTX 2070 | tokens | SAME | differs | SAME | differs | SAME |

| RX 9070 XT, native | no flag / flag without resize | SAME (all 3) | | | | |
| RX 9070 XT, native | --vram-elastic experts | 1,645 | 1,153 | 1,635 | 1,295 | 1,635 |
| RX 9070 XT, native | tokens | SAME | differs | SAME | differs | SAME |
| RTX 2070 Super | experts | 559 | 284 | 534 | 149 | 534 |
| RTX 2070 Super | tokens vs the same run's full cache | - | differs | SAME | differs | SAME |

On the RTX 2070 Super the run without the flag loaded 560 experts and the two runs with it 559 (the desktop's VRAM
moved by a few MiB between starts). With one expert fewer the tokens differ from token 77 on, in both runs with the
flag alike; each run with the flag is SAME with itself before the shrink and after the grow, and the flag without a
resize is SAME as the flag's full cache. On the two Linux cards with equal counts the flag without a resize is SAME
as no flag.

The RTX 2070 grows back to 540, not 565: the first request's buffers took about 140 MiB of the reserve after the load,
and the cache keeps the whole reserve free.

A smaller cache computes the missing experts on the CPU, which can round differently from the GPU: the shrunk request
was SAME in this run and differed in an earlier one with the same cache size. The 6000 MiB request differs for the same
reason. The cache after the grow back is SAME.

Merge notes:

Validation

Revision: port/poly-vram-elastic faa1456 on simplex/main 36e654b.

Passed:

  • Native Linux AMD: the user's RX 9070 XT 16 GB, Fedora 45, kernel 7.2.11-300.fc45 (in-tree amdgpu), ROCm 7.2.4 user
    space (HIP 7.2.53211) in an Ubuntu 24.04 container, kit.sh (reserve 4000 MiB, other program 2,816 MiB,
    --threads 11). The engine found that amdgpu keeps released VRAM until the addresses are freed ("comes back only with
    its addresses") and mapped the kept pieces again for each shrink: no abort, every run rc=0, results above.
  • AMD: gwts-rdna4 (Salad container, gfx1201 16 GB, WSL2/librocdxg, ROCm 7.2.4), the HIP build of this branch,
    kit.sh (three engines: no flag, the flag without resize, the flag with a second process taking 2,816 MiB and
    ending), results above.
  • CUDA: the user's RTX 2070 8 GB (Linux, sm_75 build, via the remote runner) and the local RTX 2070 Super 8 GB (desktop
    running), job-2070.sh, results above.
  • python3 -m unittest serve.test_vram serve.test_vision_elastic serve.test_server.DesktopVramNote: OK.
  • Builds: CUDA (sm_75) and HIP (gfx1200;gfx1201) of strata-poly.

Failed:

  • none observed

Not run:

  • Native Linux with a real desktop session growing (a second process stood in for it).
  • Windows (native) builds; Metal (the flag does nothing there: no VMM path).
  • A layer split with the flag.

Limits

  • Between requests and while idle only: a desktop that grows during a long answer waits until the answer ends.
  • GET /v1/status keeps the figures of the last POST /v1/vram after an idle resize.
  • Not with --vram-pool (the engine says the flag is off).

🤖 Generated with Claude Code

…erving

strata-poly's expert cache is already in 64 MiB VMM pieces under one address
range (CUDA and HIP), and mind_vram() resizes it at each request's start. With
the new opt-in --vram-elastic (the config's "vram_elastic": true, which the
server now passes to strata-poly):

- the whole reserve stays free: when another program took a quarter of it
  (64 MiB at least) the cache gives at least that back in whole pieces, and
  when a piece and 128 MiB more are free it takes them again;
- what an elastic shrink took out comes back first on the grow, so the cache
  after a shrink and a grow holds the same experts (gave_back_);
- an idle watch: every 2 s while the engine waits for a line with no batch slot
  writing, the same check runs (STRATA_VRAM_WATCH=0: off, STRATA_VRAM_WATCH_MS);
  a freeze gives way to it as it does to the VRAM command;
- the VRAM command resizes a piece at a time instead of filling the cache
  again;
- HIP: under WSL2 (librocdxg) a released piece's VRAM comes back at the
  release, so the kept pieces are not unmapped and mapped again for every
  shrink (decided once, never under WSL2); no piece is asked for unless its
  bytes and 256 MiB more are free (an allocation past the card hangs there).

Without the flag nothing changes. Placement only. The server's desktop note
recommends "vram_elastic": true for strata-poly on an AMD desktop instead of a
fixed 3072 MiB reserve.

The idle check follows strata-poly's own Engine::mind_vram (VecSzn); the WSL2
measurements come from the HIP port of the Qwen engine's --vram-elastic.

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