Skip to content

fix: release the device cache through the active accelerator on the vLLM resume/teardown paths - #13438

Merged
danielhanchen merged 4 commits into
unslothai:mainfrom
li-lizhe:fix/vllm-resume-device-cache-release
Oct 20, 2026
Merged

danielhanchen merged 4 commits into
unslothai:mainfrom
li-lizhe:fix/vllm-resume-device-cache-release

Conversation

@li-lizhe

@li-lizhe li-lizhe commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

What

_maybe_prepare_vllm_for_resume() returned early whenever torch.cuda.is_available() was False, so the vLLM sleep() plus the cache release that follows it never ran on NPU / XPU. The two release loops also called torch.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 to clean_gpu_cache() / device_synchronize() for the same reason.

  • unsloth/models/rl.py: gate on DEVICE_COUNT (the active accelerator's device count) instead of CUDA availability, and flush through clean_gpu_cache().
  • unsloth/dataprep/synthetic.py: the vLLM server-teardown release loop flushes through clean_gpu_cache().
  • tests/test_device_helpers.py: test_model_call_sites_use_shared_cache_dispatch now covers these two files as well.

CUDA / HIP behaviour is unchanged: DEVICE_COUNT >= 1 there, and clean_gpu_cache() dispatches to torch.cuda.empty_cache().

Verification

Real machine, 4x Ascend 910B, torch 2.15.0.dev20260917+cpu / torch_npu 2.15.0.dev20260917:

torch.cuda.is_available()        = False          -> old guard returned before flushing
reserved after 256 MiB npu alloc = 258.0 MiB
  del + gc.collect() + torch.cuda.empty_cache()  -> 258.0 MiB   (old call: released nothing)
  npu.empty_cache()  (= clean_gpu_cache() path)  ->   0.0 MiB

pytest tests/test_device_helpers.py::test_model_call_sites_use_shared_cache_dispatch - 1 passed with the change. Reverting only unsloth/models/rl.py and unsloth/dataprep/synthetic.py to the pre-change revision, the same test fails at assert "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_cache and is:pr is:open clean_gpu_cache both return 0).

Tests

tests/test_device_helpers.py now checks the resume helper by itself instead of all of rl.py. Since #13866, rl.py has a CUDA-only optimizer parking path with its own torch.cuda.is_available() gate and torch.cuda.empty_cache() call. That made the file-wide string checks fail once main was merged in. The helper is pulled out with ast and run against a fake vLLM engine:

  • DEVICE_COUNT of 1 or 4: llm.sleep(1) runs once and the cache is flushed 3 times through clean_gpu_cache(). This covers both trainer.llm and model.vllm_engine.
  • Sleep mode off: nothing is released.
  • DEVICE_COUNT of 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() and clean_gpu_cache() calls torch.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=True and vLLM sleep mode on trl-internal-testing/tiny-Qwen3ForCausalLM, 2 steps, save, resume from checkpoint-2 to 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.

…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]>
@li-lizhe
li-lizhe force-pushed the fix/vllm-resume-device-cache-release branch from 2cd039f to 7b3d10f Compare October 13, 2026 02:21
@li-lizhe

Copy link
Copy Markdown
Contributor Author

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!

@danielhanchen danielhanchen self-assigned this Oct 20, 2026
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]
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-20T15:48:29.119904Z de8d38b Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: de8d38b8b2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants