Repository navigation
perf(agent): reuse CUDA autocast weight cache across batches - #700
Merged
NandhaKishorM merged 1 commit intoSep 29, 2026
Merged
Conversation
Owner
|
Passed checks and merged. Thank you @RagingSilence! It ships in 0.3.22. |
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.
What
Keep CUDA autocast's weight-cast cache alive across eager
predict_batchforward chunks. The enclosing scope is disabled for operations between forwards, respects a caller-owned autocast scope and itscache_enabledsetting, and closes before prediction hooks run. If a CUDA OOM triggers the CPU retry inside this new scope, clear the cached copies before moving the model; unrelated OOM paths are unchanged. CPU/MPS, single-forward, fast, and compiled paths do not use the new scope.Why
predict_longcan split one request into many windows and feed them through severalpredict_batchforwards. Previously, each CUDA forward entered and exited its own autocast scope, discarding cached BF16 casts of the same FP32 weights. The next forward then repeated those casts.On a local RTX 4060 (8 GiB), using the English checkpoint and a 30-window request with
batch_size=4(8 forwards), 12 alternating before/after pairs measured a median 664.6 ms before → 599.7 ms after (9.8% lower; all 12 pairs faster). Per-forward raw outputs and the final answer were identical. With one forward (batch_size=None), medians were 548.3 ms and 548.8 ms, within run-to-run variation. In a separate 10-pair run, peak allocated CUDA memory for the multi-forward case rose from 2359.8 MiB to 2382.5 MiB (+22.7 MiB); the cached copies remain live until the prediction finishes.How it was verified
All passed locally. The batch tests cover scope selection and cleanup before end/error hooks; the runtime tests cover clearing before the CPU move only when this batch scope is active. A CUDA run also observed one cache clear across three forwards and confirmed that a caller's
cache_enabled=Falsewas preserved.Checklist
main