Repository navigation
[Engine] Long prompts: save checkpoints via pinned memory - #298
Open
okada-shogo wants to merge 1 commit into
Open
okada-shogo wants to merge 1 commit into
okada-shogo wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 6on both sides. Each run uses one--serveengine 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:Decode tok/s, the same runs, mean of 3 runs (not in the changed path):
Each run (tok/s)
Prompt, each run (tok/s):
Decode, each run (tok/s):
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):cudaMemcpyAsynccalls of 100 µs or more (ms)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):
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 fromh_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 fromtrim(), 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.read()andread_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:
--serveengine,--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.--no-graphsand 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.Failed:
Not run:
cudaMallocHostmaps to the same calls there).--layer-split): each card's engine keeps its own buffers, but this was not run.Limits
🤖 Generated with Claude Code