Repository navigation
Conversation
TorchFSDPPlugin.configure() passed torch.cuda.current_device() as the FSDP device_id, so on a non-CUDA accelerator booster.boost() raised "AssertionError: Torch not compiled with CUDA enabled" before FSDP was even built. Use get_accelerator().get_current_device() instead, which is what LowLevelZeroPlugin already does. This was the only torch.cuda call under colossalai/booster/plugin. Add a pre-commit hook, no-hardcoded-device, that fails when a guarded directory references torch.cuda, .cuda() or a hardcoded nccl backend, so the same mistake does not come back. Only colossalai/booster/plugin is guarded: the rest of the tree still has around 130 such references outside colossalai/legacy and colossalai/accelerator, and directories can be added to GUARDED as they are cleaned up. A line that is genuinely device-specific can opt out with '# allow-hardcoded-device: <reason>'. Checked on an Ascend 910B2: the hook reports the call on main and passes with this change, and TorchFSDPPlugin now gets past device selection.
Collaborator
Author
|
Superseded by #6482, which carries the same commit (3fa26fe) from a branch in this repository instead of a fork. Reason: the GPU batches in Layer 2 require |
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.
Fixes #6480
1. Background and problem
Current behaviour.
TorchFSDPPlugin.configure()passestorch.cuda.current_device()as the FSDPdevice_id.Problem. On any accelerator other than CUDA,
booster.boost()raisesAssertionError: Torch not compiled with CUDA enabledbefore FSDP is built, so the plugin cannot be used at all. This is the onlytorch.cudacall left undercolossalai/booster/plugin;LowLevelZeroPluginalready goes throughcolossalai.accelerator.Reproduce. Construct any model, wrap it with
TorchFSDPPluginand callbooster.boost()on a non-CUDA build. Full snippet in the linked issue. Reproduced on an Ascend 910B2.Expected behaviour. The device comes from the accelerator the framework has already detected.
Why now. The failure is invisible on CUDA, so it survives review and CI. The NPU port hit it on the first run of the plugin.
2. What changed
colossalai/booster/plugin/torch_fsdp_plugin.py: useget_accelerator().get_current_device()for the FSDPdevice_id, and importget_accelerator..github/scripts/ci/check_no_hardcoded_device.py(new): fails when a guarded directory referencestorch.cuda,.cuda()or a hardcodedncclbackend. Reports file, line, the offending source and the suggested replacement..pre-commit-config.yaml: register it as theno-hardcoded-devicehook, so it runs in the existing Layer 1pre-commitjob. No GPU and no new workflow.Not included in this PR:
colossalai/legacyandcolossalai/acceleratorthere are still ~130; onlycolossalai/booster/pluginis added toGUARDED. Locking everything now would turnmainred. Directories go in one at a time as they are cleaned.3. User and compatibility impact
CudaAccelerator.get_current_device()returnstorch.device(f"cuda:{torch.cuda.current_device()}"), i.e. the same device, as atorch.devicerather than anint. FSDP'sdevice_idaccepts both.New contributor-facing behaviour:
pre-commitnow fails if someone adds a hardcoded device undercolossalai/booster/plugin. The message names the file, line and replacement, and# allow-hardcoded-device: <reason>is the opt-out.4. Test environment
Verified on Ascend NPU only so far. The template's NVIDIA fields do not apply; the Ascend equivalents are given instead.
NVIDIA Driver / torch.version.cuda / nvcc: not applicable on this host.
5. Test commands and results
5.1 The hook
main: exit code 1, reportscolossalai/booster/plugin/torch_fsdp_plugin.py:540: torch.cuda.*no hardcoded device found in 1 guarded directoryNo GPU/NPU needed; runs on any machine.
5.2 Plugin smoke on NPU
Not pytest: the repo's own plugin tests cannot be collected in the Ascend image (see section 6). This is a standalone script that builds a tiny Llama (2 layers, hidden 128) and runs fwd/bwd/optimizer step twice per plugin, each plugin in its own process.
torch.cuda.current_device()1 NPU, bf16, no FAST_TEST equivalent, no skips, no reruns. Script and raw JSON results can be attached on request.
5.3 GPU regression — NOT RUN
Not run on CUDA yet. Needed before merge:
(batch 5; 2 GPUs). The hook itself needs no GPU, so only the one-line plugin change is at risk. Results will be posted here before this is marked ready.
The only evidence so far comes from a non-CUDA accelerator, so this PR should not be treated as verified until the above runs.
6. Known issues
6.1 FSDP still does not run end to end on a non-CUDA accelerator
Test: TorchFSDP plugin smoke on Ascend 910B2
Symptom: after this fix,
configure()fails one step later atoptimizer.__init__(fsdp_model.parameters(), **optimizer.defaults)withTypeError: AdamW.__init__() got an unexpected keyword argument 'decoupled_weight_decay'Introduced by this PR: no. It is a torch 2.13 change:
AdamW.defaultscarriesdecoupled_weight_decay, whichAdamW.__init__does not accept.Also affects CUDA: yes. Reproduced in the CI image
wangzhijian/colossalai-ci:torch2.13-cu130-tf5.17.0-exp-20261019:Handling: out of scope here, to keep this PR to one change.
Issue: to be filed
Owner / timing: to be assigned
6.2 Repo plugin tests cannot be collected in the Ascend image
tests/test_booster/test_plugin/test_torch_fsdp_plugin.pyModuleNotFoundError: No module named 'timm'at collection. Installingtimmpulls torch 2.14.1 and breakstorch_npu; installing it with--no-depsthen fails ontorchvision, which has no matching build in this image.6.3 Gemini fails on NPU
Unrelated to this PR and not caused by it:
colo_tensor.py:81raisesRuntimeError: The tensor has a non-zero number of elements, but its data is not allocated yet.during backward. Recorded here only so the smoke table above is not misread. Not yet confirmed whether it is accelerator-specific.7. Risk and rollback
Risk. Low. One line, and the replacement resolves to the same CUDA device. The hook could annoy contributors if a legitimate device-specific line lands in a guarded directory; the
# allow-hardcoded-device:opt-out covers that.The hook runs on whole directories rather than changed files, so an unrelated PR that adds a hardcoded device under
colossalai/booster/pluginwill failpre-commitrather than the author's own files. That is the intent, but it does mean the failure can appear in a PR that did not expect it.Rollback. Revert the commit. The two parts are independent: dropping the hook (the
.pre-commit-config.yamlentry plus the script) leaves the one-line fix in place, and vice versa.8. Documentation and release impact
The hook's own usage and the opt-out marker are documented in the script docstring.