Repository navigation
Studio: decode with CUDA graphs by default in the transformers chat backend - #15993
Conversation
…tion ineligibility, stream join on failed capture - studio_generation turns graphs on by default only off native Windows (unverified there); =1 still forces. - The CUDA test gates on real_accelerator.has_real_cuda (the CUDA spoof patches torch.cuda.is_available). - A generation the graphs cannot serve (DoRA / non-LoRA adapter, several devices) frees the captured steps and is re-checked at the next generate() instead of disabling graphs until unload. - _capture joins the side stream in a finally, so a failed warmup / capture cannot race the eager step.
…an ineligible generation frees them [skip ci]
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. 👍 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review |
|
Codex Review: Didn't find any major issues. What shall we delve into next? Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
…r (native Windows) [skip ci]
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep it up! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
# Conflicts: # unsloth/models/_decode_graphs.py [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. 👍 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
What changes
Studio's transformers / safetensors chat backend now runs
generate()with CUDA-graph decode on by default. Core's plainmodel.generate()stays eager by default, as #15188 chose.unsloth/models/_decode_graphs.pygets astudio_generationscope. It works likerl_generation: it is per thread and sets no environment variable. Inside it, withUNSLOTH_DECODE_CUDA_GRAPHSunset, graphs are on only on NVIDIA CUDA (_DEFAULT_OK) and not on native Windows (_STUDIO_DEFAULT_OK). The order ofenabled()is unchanged, soUNSLOTH_DECODE_CUDA_GRAPHS=0still turns graphs off and=1still forces them on.studio/backend/core/inference/inference.py:generate_streamwraps only itsmodel.generate(**generation_kwargs)call, on the generate thread, in that scope. This one call serves chat, compare mode and the tool-calling loop.unload_modelnow frees the model's captured graphs, graph pool and decode buffers beforeclear_gpu_cache(). It skips this while a generation still holds the generation lock. In that case the generating thread's reference frees them when it ends.Why only Studio: the transformers backend runs in its own worker process and only serves inference, so there is no training afterwards that the graphs could interfere with.
Graphs and their KV buffers stay alive across chat turns while the model is loaded. They are freed on:
Which models use graphs: the decode path supports
llamaandqwen3(_SUPPORTED_MODEL_TYPES) on one CUDA device, 4-bit or 16-bit, base or with LoRA. Everything else is unchanged and eager:Model changes between generations re-capture through the existing fingerprint check, which runs on the first graphed step of every
generate():Sampling settings act on the returned logits, outside the graph. If a capture fails, the model falls back to eager with the same output.
Results
The benchmark ran on a Colab Tesla T4 with torch 2.14.1+cu130 and transformers 5.17.0. It goes through Studio's real backend (
InferenceBackend.load_model+generate_chat_response): batch 1, greedy, up to 512 new tokens, thinking off.Decode speed with the eager and graph arms rotated in one process (ABBA, 3 rounds = 6 runs per arm for 4-bit, 2 rounds = 4 runs for 16-bit). Eager is
UNSLOTH_DECODE_CUDA_GRAPHS=0; graphs is the new default. Values are the median with [min, max]:Main vs this branch, each in a fresh process with the variable unset, on the same card:
Tests
tests/test_decode_cuda_graphs_studio.py. The decision tests load_decode_graphs.pyby file path and run on CPU. They cover:rl_generation;=0andENABLED=Falsewinning;_DEFAULT_OK=False) staying eager;A CUDA test loads a tiny Qwen3 and checks that plain
generate()stays eager, that generation inside the scope replays with bitwise-equal sequences and logits, and that release frees the graphs and buffers. On main 5 fail; on this branch all pass (the CUDA test was skipped on the CPU host).studio/backend/tests/test_transformers_decode_graphs.py(CPU, real_decode_graphs.pyunder a spoofed NVIDIA default). It checks that:=0disables graphs;On main 2 fail (graphs on, unload frees); on this branch all 6 pass.
tests/test_decode_cuda_graphs_default.pyis unchanged and passes.Neighbour Studio tests (15 files: stop sequences, generation budget, reasoning stream, active generations, chat EOS, tool turns, templates, diffusion LM, fine-tuned serving, chat during training, training VRAM coexistence, unload): 1436 passed, 1 skipped, on both main and this branch.
Neighbour Core decode-graph / PPO / cached attention tests: 79 passed, 27 skipped, on both main and this branch.
The changed and new files compile on Python 3.9 through 3.14.
Not covered
_DEFAULT_OKis False there). Only the CPU tests cover that decision.UNSLOTH_DECODE_CUDA_GRAPHS=1. Linux and WSL get the default.gc.collect().Notes
generate()checks again, so switching back to base or a plain LoRA is graphed again.Review fixes and A100 / L4 re-run
tests/test_decode_cuda_graphs_studio.pynow gates onreal_accelerator.has_real_cuda.torch.cuda.is_available()is patched to True by the CUDA spoof in the CPU job, whichtests/python/test_accelerator_skip_guards.pyrejects.=1still forces graphs on everywhere;rl_generationis unchanged.disabledwithout freeing the captured steps, so graphs stayed off until unload. Now that generation frees them and runs eager, and the next one is re-checked._capturejoins the side stream in afinally, so the eager fallback after a failed warmup / capture is ordered after the side-stream work.GPU tests on Colab A100, torch 2.14.1 (
tests/test_decode_cuda_graphs.py+tests/test_decode_cuda_graphs_studio.py): 29 passed on this branch. With this branch's tests on main's code, the 9 new tests fail (the two regression tests above and the 7 Studio scope tests) and the other 20 pass.Studio's real backend (
load_model+generate_chat_response), greedy, 512 new tokens, main vs this branch in fresh processes, 3 rounds:After the main merge (L4, torch 2.14.1, transformers 5.17.0, merge base 3ecb1ba)
The merge conflict in
studio/backend/core/inference/inference.pywas resolved by keeping both sides: the decode-graph scope and main's_supported_generate_kwargsfilter. GPU pytest oftests/test_decode_cuda_graphs.pyandtests/test_decode_cuda_graphs_studio.py, each arm importing its own tree:The 9 that fail on main are the new tests: the Studio scope (on for NVIDIA CUDA, off elsewhere and on native Windows, per thread, honours the variable, unwinds on error, plain generate stays eager), the ineligible-generation recovery and the side-stream join after a failed capture. The Studio backend CPU tests touching generate kwargs and inference also pass on the merged tree (400 passed).