prepare_model: don't move DTensor-sharded models to the device - #4181
Open
qgallouedec wants to merge 2 commits into
Open
prepare_model: don't move DTensor-sharded models to the device#4181qgallouedec wants to merge 2 commits into
qgallouedec wants to merge 2 commits into
Conversation
A model sharded at load time (e.g. transformers DistributedConfig with FSDP2, optionally CPU-offloaded) manages its own parameter placement, and .to() on FSDP2-managed parameters raises RuntimeError: _apply(): Couldn't swap FSDPLinear.weight. model_has_dtensor already exists and is checked a few lines below for the DDP case; use it to skip the device move too.
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
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.
A model sharded at load time (e.g. transformers'
DistributedConfigwith FSDP2, optionally CPU-offloaded) manages its own parameter placement, butprepare_modelunconditionally callsmodel = model.to(self.device)whendevice_placementis set..to()recurses_applythrough FSDP2-managed parameters and raises:model_has_dtensoralready exists and is consulted a few lines below for the DDP case; use it to skip the device move too. Hit while fine-tuning GLM-5.2 (753B) withfsdp_cpu_offload=TruethroughTrainer(huggingface/transformers#48204); on-device sharded models are unaffected because the move was a same-device no-op for them.Reproduction
/!\ A standalone toy does not trigger this: plain FSDP2 (fresh or lazy-initialized, with or without CPU offload) tolerates the
.to().The failing
_applyswap needs the parameter arrangement that transformers' sharded loading produces (DTensor expert weights on a tp mesh combined with FSDP2-managed dense parameters, CPU-offloaded). With the transformers side installed (huggingface/transformers#48204):The guard is correct independently of the trigger's rarity: a model holding DTensor parameters manages its own placement, and
model_has_dtensoris already the codebase's test for exactly that condition a few lines below.