Repository navigation
fix: release the device cache through the active accelerator on the vLLM resume/teardown paths - #13438
Conversation
c1d421a to
2cd039f
Compare
…LLM resume/teardown paths _maybe_prepare_vllm_for_resume() bailed out on anything that was not CUDA, so vLLM sleep plus the follow-up cache release never ran on NPU / XPU. The release loops themselves also called torch.cuda.empty_cache() directly, which is a no-op off CUDA, so the freed blocks stayed reserved. Gate the resume path on the active accelerator's device count and flush both loops through clean_gpu_cache(). Verified on 4x Ascend 910B (torch 2.15.0.dev20260917+cpu, torch_npu 2.15.0.dev20260917): torch.cuda.is_available() is False, 258.0 MiB reserved stays 258.0 MiB after torch.cuda.empty_cache() and drops to 0.0 MiB after npu.empty_cache(). test_model_call_sites_use_shared_cache_dispatch covers both files and fails on the pre-change sources. Signed-off-by: li-lizhe <[email protected]>
2cd039f to
7b3d10f
Compare
|
Gentle ping on this one — it's been quiet for 7 days since the last activity, so I wanted to check whether there's anything else you'd like me to adjust or add before this can move forward. Happy to make changes if so. Thanks! |
After merging main, rl.py carries a CUDA-only optimizer parking path with its own torch.cuda.is_available() gate and torch.cuda.empty_cache(), so the file-wide string checks failed. Check the resume helper alone and run it against a fake engine for 0, 1 and 4 devices. [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. Swish! 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
_maybe_prepare_vllm_for_resume()returned early whenevertorch.cuda.is_available()wasFalse, so the vLLMsleep()plus the cache release that follows it never ran on NPU / XPU. The two release loops also calledtorch.cuda.empty_cache()directly, which is a silent no-op off CUDA, so the blocks freed by the dropped model stayed reserved in the active accelerator's allocator - exactly the state this path exists to avoid before re-loading for a resume. The neighbouring loader-retry flush and the q_galore fence were moved toclean_gpu_cache()/device_synchronize()for the same reason.unsloth/models/rl.py: gate onDEVICE_COUNT(the active accelerator's device count) instead of CUDA availability, and flush throughclean_gpu_cache().unsloth/dataprep/synthetic.py: the vLLM server-teardown release loop flushes throughclean_gpu_cache().tests/test_device_helpers.py:test_model_call_sites_use_shared_cache_dispatchnow covers these two files as well.CUDA / HIP behaviour is unchanged:
DEVICE_COUNT >= 1there, andclean_gpu_cache()dispatches totorch.cuda.empty_cache().Verification
Real machine, 4x Ascend 910B,
torch 2.15.0.dev20260917+cpu/torch_npu 2.15.0.dev20260917:pytest tests/test_device_helpers.py::test_model_call_sites_use_shared_cache_dispatch- 1 passed with the change. Reverting onlyunsloth/models/rl.pyandunsloth/dataprep/synthetic.pyto the pre-change revision, the same test fails atassert "if not torch.cuda.is_available():\n return" not in rl_source.Not tested
An actual vLLM / vLLM-Ascend sleep-mode resume (no vLLM in the verification environment), Intel XPU hardware, and a CUDA box. No open PR rewrites these call sites (
is:pr is:open empty_cacheandis:pr is:open clean_gpu_cacheboth return 0).Tests
tests/test_device_helpers.pynow checks the resume helper by itself instead of all ofrl.py. Since #13866,rl.pyhas a CUDA-only optimizer parking path with its owntorch.cuda.is_available()gate andtorch.cuda.empty_cache()call. That made the file-wide string checks fail oncemainwas merged in. The helper is pulled out withastand run against a fake vLLM engine:DEVICE_COUNTof 1 or 4:llm.sleep(1)runs once and the cache is flushed 3 times throughclean_gpu_cache(). This covers bothtrainer.llmandmodel.vllm_engine.DEVICE_COUNTof 0: nothing is called.All 4 new or updated tests fail on the merge base and pass on this branch. Behaviour on CUDA / ROCm is unchanged: there
DEVICE_COUNT == torch.cuda.device_count()andclean_gpu_cache()callstorch.cuda.empty_cache().CUDA no-regression check (B200, vLLM 0.11.2, torch 2.9.0, transformers 4.57.6, TRL 0.24.0): GRPO with
fast_inference=Trueand vLLM sleep mode ontrl-internal-testing/tiny-Qwen3ForCausalLM, 2 steps, save, resume fromcheckpoint-2to step 4. Base and this branch match exactly:llm.sleep(1)called, 3 cache flushes, the same bytes released, and identical losses before and after the resume.