Skip to content

[Engine] Retry instead of failing when a CUDA graph is out of VRAM - #301

Open
okada-shogo wants to merge 1 commit into
simplexai-labs:mainfrom
okada-shogo:port/graph-oom
Open

okada-shogo wants to merge 1 commit into
simplexai-labs:mainfrom
okada-shogo:port/graph-oom

Conversation

@okada-shogo

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

Copy link
Copy Markdown
Contributor

Problem and result

When a CUDA graph cannot get VRAM, the engine now frees its older graphs and retries instead of failing the request.
Three cases that failed on main on 8 GB cards now finish, with the same output bits and unchanged decode speed.

Background: on 8 GB cards, cudaGraphInstantiate can run out of VRAM in the middle of a session, and the request then
ends with ERR cudaGraphInstantiate: out of memory. The engine records decode launches into CUDA graphs as it goes
(one per token count, context room, window and drafter block length) and keeps every recording. Their VRAM comes out
of the reserve that the expert cache leaves free, so a long session uses it up.

Each case was run on main and on this PR:

Card Case main this PR
RTX 2070 Super 8 GB --serve --max-context 32768 --kv q8_0 --window 4: 302 tokens, then 2,048 tokens (+64 each) 2nd request: ERR both requests done
RTX 2070 Super 8 GB DSpark drafter (--draft-model, --prompt-int8, STRATA_PREFILL_NVFP4=w4a8 STRATA_INK_FAST_CTRL=1 STRATA_INK_PRESTAGE=1), default reserve: 302 tokens (+1), then 2,048 tokens (+512) 2nd request: ERR both requests done
RTX 2070 Super 8 GB --serve --window 6: 302 tokens, then 2,048, then 4,096 2nd request: ERR all done
RTX 2070 8 GB first and third cases above ERR done

Output bits are unchanged (frozen identity, below). Decode speed is unchanged (RTX 2070 8 GB, mean of 3 runs):

Card Model Measure main this PR alone Change
RTX 2070 8 GB IQ4_NL decode tok/s, after 302 42.8 42.2 -1.5%
RTX 2070 8 GB IQ4_NL decode tok/s, after 2,048 36.5 36.4 -0.2%
RTX 2070 8 GB UD-Q2_K_XL (loop) decode tok/s, after 302 51.8 51.8 +0.1%
RTX 2070 8 GB UD-Q2_K_XL (loop) decode tok/s, after 2,048 49.7 49.8 +0.2%

The PR does not change the path that runs when there is no out-of-memory error. The IQ4_NL -1.5% comes from one slow
run (40.8 tok/s); the other two runs match main.

Each run (decode tok/s)
Card Model Measure Build Run 1 Run 2 Run 3
RTX 2070 8 GB IQ4_NL after 302 main 43.1 42.6 42.7
RTX 2070 8 GB IQ4_NL after 302 this PR 43.4 42.3 40.8
RTX 2070 8 GB IQ4_NL after 2,048 main 36.9 36.6 36.0
RTX 2070 8 GB IQ4_NL after 2,048 this PR 36.7 36.4 36.2
RTX 2070 8 GB UD-Q2_K_XL (loop) after 302 main 51.9 51.7 51.7
RTX 2070 8 GB UD-Q2_K_XL (loop) after 302 this PR 51.8 51.9 51.8
RTX 2070 8 GB UD-Q2_K_XL (loop) after 2,048 main 49.9 49.6 49.7
RTX 2070 8 GB UD-Q2_K_XL (loop) after 2,048 this PR 49.8 49.9 49.8

Related discussion

none

Approach

When cudaGraphInstantiate returns out of memory (in record() and in the drafter's block recording):

  1. Drop the step recordings, as main already does.
  2. Drop the recordings made before this call. They are recorded again the next time they are needed, so the
    bits do not change. In every run above, this step was enough.
  3. If there is still no VRAM, launch the recording's kernels directly for the rest of this call. These are the same
    kernels, reading positions from at_. Requests for experts in RAM (the doorbell, STRATA_POLY_BELL: the GPU asks
    the CPU workers through a flag in pinned memory) are answered layer by layer. At the start of the next call,
    the expert cache gives up one 64 MiB piece, and mind_vram() keeps that piece free from then on. This is the
    only step that changes which experts are in VRAM, and it only happens where main would have failed the request. If the
    cache cannot shrink (frozen experts, --vram-pool, no VMM), the engine records nothing for the next 256 calls.

Two test knobs force the fallbacks:

  • STRATA_POLY_RECORD_FAIL=N: every Nth recording fails.
  • STRATA_POLY_RECORD_DROP=N: drops the recordings at every Nth recording and every Nth call.

Metal keeps its old path. Other model families only see a change when instantiation fails, which used to fail the
request.

Merge notes:

Validation

Passed:

  • Repro on main, then the fix (go-repro.sh). RTX 2070 Super, main 36e654b vs this PR 4319541, CUDA 12.9 sm_75.
    • Main: every case gave ERR cudaGraphInstantiate: out of memory.
    • This PR: every request finished. The log shows "recordings: dropped 260..363 made before this call" and
      "0 runs unrecorded".
    • Same result on the RTX 2070 8 GB for the --window 4 and --window 6 cases.
  • Identity, experts capped and frozen (STRATA_GPU_EXPERTS=800 STRATA_FREEZE_EXPERTS=1), main vs this PR:
    • poly_logits_dump, IQ4_NL, md5 of all logits: 302-token and 2,048-token prompts, each followed by 64 tokens
      fed one by one. SAME for this PR, RECORD_FAIL=1, RECORD_FAIL=3 and RECORD_DROP=2. RTX 2070 Super and
      RTX 2070.
    • strata-poly --serve, DSpark drafter with the flags from the table above (--prompt-int8,
      STRATA_PREFILL_NVFP4=w4a8 STRATA_INK_FAST_CTRL=1 STRATA_INK_PRESTAGE=1), 2,048-token prompt + 128 greedy token ids: SAME for this PR and all
      three knob settings. RTX 2070 Super.
    • --window 4 --kv q8_0, 2,048-token prompt + 128 greedy tokens, with STRATA_SPEC_FIXED=1 (fixed draft-cost table): SAME for
      main run twice, this PR, and all three knob settings. RTX 2070. Without the fixed table, main differs from main
      between runs, because the lookup policy learns from timings.
  • Decode speed: the table above (e2e-nw.sh, 3 rounds, RTX 2070 8 GB).
  • Builds: CUDA sm_75; HIP gfx1201 (compile only).

Failed:

  • none observed

Not run:

  • Metal build: no Mac was available. Its code path is #if-guarded to main's behavior.
  • HIP was not run on an AMD card. On HIP, dropped recordings are destroyed after a stream sync.
  • Step 3 (cutting the expert cache) never triggered in these runs. It is covered only by code review, not by a
    measured run.

Limits

  • Step 3 changes which experts are in VRAM for the rest of the session. It only happens after an out-of-memory error
    that main would have failed on.
  • On the drafter case, step 2 ran several times in one request: the old recordings were dropped and recorded again
    about 360 at a time. The request still decoded at 43.7 tok/s.
  • cudaGraphLaunch itself returning out of memory is not handled; it was not seen in any run.

🤖 Generated with Claude Code

cudaGraphInstantiate ran out of VRAM in the middle of an answer on 8 GB cards
(RTX 2070 Super, Inkling 2 Mini): --serve at 32K with --kv q8_0 on the second
request, --window 6, and the DSpark drafter after a 2K prompt.  The request
ended with "ERR cudaGraphInstantiate: out of memory".

On out of memory, record() (and the drafter's block recording) now
1. drops the step recordings, as before;
2. drops record()'s recordings made before this call (made again where they
   are next needed);
3. if the card still has no VRAM: runs the recording's launches unrecorded
   (the same launches, positions from at_), and at the next call's start the
   expert cache gives up one 64 MiB piece, which mind_vram() keeps free from
   then on.  Where the cache cannot change (frozen, pool, no VMM) the next 256
   calls run unrecorded.

Class: same bits, except step 3's cache cut (placement), which only happens
where main failed the request.  Test knobs: STRATA_POLY_RECORD_FAIL=N,
STRATA_POLY_RECORD_DROP=N.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@okada-shogo okada-shogo changed the title [Engine] poly: a CUDA graph the card has no VRAM for no longer fails the request [Fix] Don't fail a request when a CUDA graph can't get VRAM: free old graphs and retry Nov 24, 2026
@okada-shogo okada-shogo changed the title [Fix] Don't fail a request when a CUDA graph can't get VRAM: free old graphs and retry [Engine] Don't fail a request when a CUDA graph can't get VRAM: free old graphs and retry Nov 24, 2026
@okada-shogo okada-shogo changed the title [Engine] Don't fail a request when a CUDA graph can't get VRAM: free old graphs and retry [Engine] Retry instead of failing when a CUDA graph is out of VRAM Nov 24, 2026
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