fix: use key-based matching in fsdp2_load_full_state_dict#3982
Open
roycho96 wants to merge 1 commit intohuggingface:mainfrom
Open
fix: use key-based matching in fsdp2_load_full_state_dict#3982roycho96 wants to merge 1 commit intohuggingface:mainfrom
roycho96 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?
Replaces positional (
zip) matching with key-based matching infsdp2_load_full_state_dict()on rank 0, preventing deadlocks whenfull_sdandmeta_sharded_sdhave different numbers of keys.Problem
fsdp2_load_full_state_dict()currently uses different iteration strategies per rank:zip(full_sd.items(), meta_sharded_sd.values())— positionalmeta_sharded_sd.items()— key-basedThis assumes
full_sdandmeta_sharded_sdhave identical key counts and ordering. When they don't,dist.broadcastcalls become misaligned across ranks, causing a deadlock (all ranks hang indefinitely).When does this break?
Models whose
state_dict()contains extra sidecar entries not present in the sharded model's state dict. A concrete example is BnB QLoRA (Params4bit) withcpu_ram_efficient_loading=True:How the deadlock happens
With
zipon rank 0, iteration is positional — the i-th entry fromfull_sdis paired with the i-th entry frommeta_sharded_sd. Whenfull_sdhas extra sidecar keys interleaved, the pairing shifts:After N iterations (matching
meta_sharded_sdsize),zipstops on rank 0, but the data that was broadcast was wrong from step 2 onward. In practice, the size/dtype mismatch causes NCCL to hang waiting for matching collectives.Fix
Use the same iteration strategy on all ranks — iterate over
meta_sharded_sd.items()and look upfull_sdby key:This also adds a clear
KeyErrormessage when a sharded key is missing fromfull_sd, instead of a silent mismatch or cryptic broadcast error.For the normal case (no extra keys), behavior is identical — same keys, same order, same broadcasts.
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
@SunMarc