fix train deepseek V4 with fsdp2: AttributeError: 'Tensor' object has no attribute 'device_mesh'#4023
Open
frozenleaves wants to merge 1 commit intohuggingface:mainfrom
Open
fix train deepseek V4 with fsdp2: AttributeError: 'Tensor' object has no attribute 'device_mesh'#4023frozenleaves wants to merge 1 commit intohuggingface:mainfrom
frozenleaves wants to merge 1 commit intohuggingface:mainfrom
Conversation
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.
What does this PR do?
fix the bug about training deepseek v4 with fsdp2:
In
fsdp2_load_full_state_dictfunction. It iterates overmodel.state_dict()and assumes that every item is aDTensor, directly accessing the.device_meshattribute.However, when using FSDP2 (via
fully_shard), only model parameters are converted toDTensors, while persistent buffers remain standardtorch.Tensors.In the DeepSeek-V4 model, the MoE router registers persistent buffers (specifically
biasandtid2eid). Whenfsdp2_load_full_state_dictis called and iterates over these buffers, it triggers anAttributeError: 'Tensor' object has no attribute 'device_mesh'because they lack the DTensor-specific attributes, thus interrupting the model loading process.This PR modifies the
fsdp2_load_full_state_dictfunction inaccelerate/utils/fsdp_utils.py.In both the chief (primary) and non-chief process branches, an explicit type check has been added for items in the
state_dict:DTensor, the original loading and attribute access logic is retained.DTensor(i.e., it is a regular Tensor like a persistent buffer), the logic now bypasses the DTensor-specific attribute accesses. Instead, it directly broadcasts the tensor and keeps it in its original standardTensorstate.The test is based on this PR: huggingface/transformers#45643 . Both the main branch and the latest release version of accelerate can reproduce this issue.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.