Skip to content

[Engine] Long prompts: save checkpoints via pinned memory - #298

Open
okada-shogo wants to merge 1 commit into
simplexai-labs:mainfrom
okada-shogo:port/d2h-state-save
Open

okada-shogo wants to merge 1 commit into
simplexai-labs:mainfrom
okada-shogo:port/d2h-state-save

Conversation

@okada-shogo

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

Copy link
Copy Markdown
Contributor

Problem and result

With the conversation cache on (--prompt-cache N, a checkpoint every 2,048 tokens), a long prompt copies each checkpoint's layer state from the GPU to host memory while it runs. Only the first checkpoint went through pinned memory. The others were copied into pageable memory, and such a copy returns only after the GPU has finished all work queued before it. So the host stopped issuing launches at every layer of those checkpoints, and the card sat idle. In an 8,192-token Inkling 2 Mini prompt (3 checkpoints), that was 257 blocking copy calls on an RTX 2070 Super.

This PR gives the other checkpoints pinned buffers too. The buffers are allocated between requests (in trim()), as many as a prompt has needed, and kept. The first long prompt of a process therefore runs as before, and every later one uses pinned memory. The output is bit-identical to main.

The end-to-end gain is small: prompt speed changes by -0.8% to +1.3%, while host time in blocking copy calls drops by 92%.

End-to-end, RTX 2070 Super 8 GB (Ryzen 5 3600XT), main's default flags plus --prompt-cache 6 on both sides. Each run uses one --serve engine with a 16K warm-up prompt first (so this PR has made its buffers); 3 rounds in alternating order. Prompt tok/s, mean of 3 runs:

Card Model Measure main this PR alone Change
RTX 2070 Super 8 GB IQ4_NL prompt 8K (tok/s) 575.4 583.2 +1.3%
RTX 2070 Super 8 GB IQ4_NL prompt 32K (tok/s) 625.2 627.3 +0.3%
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 8K (tok/s) 583.0 578.3 -0.8%
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 32K (tok/s) 599.2 602.9 +0.6%

Decode tok/s, the same runs, mean of 3 runs (not in the changed path):

Card Model Measure main this PR alone Change
RTX 2070 Super 8 GB IQ4_NL decode after 8K (tok/s) 33.6 33.8 +0.7%
RTX 2070 Super 8 GB UD-Q2_K_XL decode after 8K (tok/s) 43.3 43.2 -0.2%
Each run (tok/s)

Prompt, each run (tok/s):

Card Model Measure Build Run 1 Run 2 Run 3
RTX 2070 Super 8 GB IQ4_NL prompt 8K (tok/s) main 569.3 578.5 578.5
RTX 2070 Super 8 GB IQ4_NL prompt 8K (tok/s) this PR alone 584.0 582.8 582.7
RTX 2070 Super 8 GB IQ4_NL prompt 32K (tok/s) main 631.0 622.8 621.9
RTX 2070 Super 8 GB IQ4_NL prompt 32K (tok/s) this PR alone 625.8 624.3 631.7
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 8K (tok/s) main 582.1 583.2 583.7
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 8K (tok/s) this PR alone 559.8 587.4 587.8
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 32K (tok/s) main 601.9 602.1 593.6
RTX 2070 Super 8 GB UD-Q2_K_XL prompt 32K (tok/s) this PR alone 604.9 599.4 604.4

Decode, each run (tok/s):

Card Model Measure Build Run 1 Run 2 Run 3
RTX 2070 Super 8 GB IQ4_NL decode after 8K (tok/s) main 31.2 34.6 35.0
RTX 2070 Super 8 GB IQ4_NL decode after 8K (tok/s) this PR alone 34.4 31.8 35.3
RTX 2070 Super 8 GB UD-Q2_K_XL decode after 8K (tok/s) main 43.3 43.3 43.2
RTX 2070 Super 8 GB UD-Q2_K_XL decode after 8K (tok/s) this PR alone 43.2 43.2 43.2

Experts in VRAM at load (main / this PR): IQ4_NL d8k [916]/[916]; IQ4_NL p32k [916]/[916]; UD-Q2_K_XL d8k [1967]/[1967]; UD-Q2_K_XL p32k [1967]/[1967]

Prompt processing in an nsys CUDA trace, 8,192-token IQ4_NL prompt after the 16K warm-up, --prompt-cache 6 (ms, lower is better; one trace each):

Card Part main this PR alone Change
RTX 2070 Super 8 GB host time in cudaMemcpyAsync calls of 100 µs or more (ms) 2430.4 183.6 −92.4%
RTX 2070 Super 8 GB card idle in the request (ms) 1928.1 1779.2 −7.7%

GPU-to-host copies into pageable memory in that request: main 257 (327 MiB), this PR 1 (0 MiB).

Copy-call microbenchmark (µs)

Copy-call microbenchmark, RTX 2070 Super (µs, lower is better; mean of 3 runs):

Card Part pageable (main) pinned (this PR) Change
RTX 2070 Super 8 GB one 2 MiB D2H call with 5 ms of work queued, host time (µs) 5953.6 5.9 −99.9%
RTX 2070 Super 8 GB read-like loop, a visit: 3 ms kernel + save 2 × 2 MiB + 1.5 ms host work (µs) 5388.2 3732.4 −30.7%

Related discussion

none (one optimization per PR, as asked on #200). It extends VecSzn's pinned staging of a prompt's first checkpoint.

Approach

  • stage_marks() gives each checkpoint after the first a buffer from h_marks_ if one is available; otherwise it uses the checkpoint's state as before. After the prompt, state_out() copies the buffers into their states, as it already did for the first one.
  • mark_buffers() (called from trim(), after a request) allocates the buffers a prompt asked for. cudaMallocHost() of Inkling 2 Mini's 330 MiB state takes ~210 ms. Done during the prompt, even on its own thread, it held up the prompt's CUDA calls for as long, so it runs between requests.
  • The buffers stay pinned. A new one is allocated only while free RAM still holds the buffers yet to be made, the pageable states the next prompt fills, and two more states.
  • read() and read_groups() serve every model family: any model whose prompts cross more than one checkpoint keeps up to (checkpoints − 1) × the card's state size of extra pinned RAM per card (Inkling 2 Mini with the default 6 checkpoints: 5 × 330 MiB).

Validation

All on branch a56441f = main 36e654b + this PR.

Passed:

  • Checkpoint identity on an RTX 2070 8 GB: one --serve engine, --prompt-cache 6, STRATA_FREEZE_EXPERTS=1 STRATA_GPU_EXPERTS=850, default flags, IQ4_NL. Sequence: an 8K prompt; three prompts resuming from its checkpoints at 6144 / 4096 / 2048; a second 8K prompt (run after the buffers were made); and two prompts resuming from its 6144 / 4096 checkpoints (which went through the new buffers). Token ids and the DONE fields are the same as main's.
  • Engine identity without checkpoints, experts frozen and capped, token ids + top-20 of 32 tokens: same as main for IQ4_NL 302 / 2,048 and UD-Q2_K_XL 2,048 (default flags). Before the review fix (RAM rule, names), all 12 cases of IQ4_NL / UD-Q2_K_XL × 302 / 2,048 × defaults, --no-graphs and the int8/w4a8 prompt flags were also the same as main.
  • poly_inkling2_test: 425 checks, 0 failed. HIP build (ROCm 7.2.4) compiles.
  • nsys trace and E2E above (RTX 2070 Super, all 24 runs rc=0).

Failed:

  • UD-Q2_K_XL prompt 8K: −0.8% (one slow run of this PR, 559.8 tok/s; the other two are +0.8% over main's mean).

Not run:

  • HIP and Metal runs (host code; cudaMallocHost maps to the same calls there).
  • Several cards (--layer-split): each card's engine keeps its own buffers, but this was not run.

Limits

  • The first long prompt of a process still uses pageable memory (it tells the engine how many buffers it needs).
  • Pinned RAM grows by up to (checkpoints − 1) × the state size per card.
  • The end-to-end gain is small on this card: the copies were a few % of the card's idle time.

🤖 Generated with Claude Code

read() and read_groups() copy a checkpoint's layers in the stream behind
the call that ends at it.  Only the first checkpoint went to pinned
memory (h_state_); the others went straight to their pageable states.
A D2H copy into pageable memory returns only after the stream has done
the work before it, so the host stopped issuing launches at every layer
of every checkpoint after the first, and the card waited: 257 such
calls held the host 1.0 s in an 8,192-token Inkling 2 Mini prompt with
the conversation cache on (RTX 2070 Super, nsys).

Each checkpoint after the first now goes to a pinned buffer too
(h_marks_, stage_marks()), and from there to its state after the read,
as the first one did (state_out()).  The buffers are made between
requests (trim() -> mark_buffers()), as many as a read has had
checkpoints after the first, and kept: cudaMallocHost() of a 330 MiB
state took 210 ms, and made on a thread of its own during the read it
held the read's CUDA calls up as long.  So the first long read of a
process goes as before, and the reads after it go through the buffers.
A buffer is made only while the RAM free holds the buffers still to
make, the pageable states the next read fills and two states more; a
checkpoint without one goes straight to its state as before.

read() and read_groups() serve every family, so any model whose reads
cross more than one checkpoint keeps up to (checkpoints - 1) x the
card's state size of extra pinned RAM per card (Inkling 2 Mini with the
default 6 checkpoints: 5 x 330 MiB).

Same bits: the same copies of the same bytes, only the host memory they
land in first differs.

This extends VecSzn's pinned staging of a read's first checkpoint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@okada-shogo okada-shogo changed the title [Engine] Every checkpoint of a long prompt through pinned memory (same bits) [Engine] Long prompts: save checkpoints through pinned memory instead of blocking copies Nov 24, 2026
@okada-shogo okada-shogo changed the title [Engine] Long prompts: save checkpoints through pinned memory instead of blocking copies [Engine] Long prompts: save checkpoints via pinned memory 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