Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions swift/model/npu_patch/fsdp.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,17 @@ def _cast_module_to_fp32_for_npu_if_needed(module: torch.nn.Module, accelerator:
# entering that path with bf16/fp16 parameters can fail before mixed
# precision policy has a chance to manage runtime compute dtype. Cast early
# while parameters are still on CPU or meta, so only dtype changes here.

# GRPO with vLLM colocate mode may preload the model onto NPU before
# Accelerator.prepare() is called. In that case, casting fp32 on NPU
# would temporarily duplicate the full model (bf16 + fp32), causing OOM.
# We move the model back to CPU first to free NPU memory, then cast.
try:
if param.device.type == 'npu':
import torch_npu
module = module.cpu()
torch_npu.npu.synchronize()
torch_npu.npu.empty_cache()
return module.to(torch.float32)
except Exception as exc:
raise NPUCastError(f'Failed to cast {module.__class__.__name__} to fp32.') from exc
Expand Down
Loading