Fix FSDP_CPU_RAM_EFFICIENT_LOADING (#43749)#43785
Open
MengAiDev wants to merge 3 commits intohuggingface:mainfrom
Open
Fix FSDP_CPU_RAM_EFFICIENT_LOADING (#43749)#43785MengAiDev wants to merge 3 commits intohuggingface:mainfrom
MengAiDev wants to merge 3 commits intohuggingface:mainfrom
Conversation
- Add _is_hf_initialized flag in _load_parameter_into_model to prevent unnecessary random initialization - Skip state_dict loading for non-rank0 processes when FSDP is enabled to avoid wasting CPU RAM - This fixes the issue where all ranks temporarily allocate model-sized CPU RAM and experience long delays Modified: - src/transformers/modeling_utils.py: Set _is_hf_initialized=True on new parameters - src/transformers/core_model_loading.py: Add FSDP check to skip loading on non-rank0
ArthurZucker
reviewed
Feb 6, 2026
Comment on lines
+1109
to
+1114
| from .integrations import is_fsdp_enabled | ||
| from .modeling_utils import is_local_dist_rank_0 | ||
|
|
||
| if is_fsdp_enabled() and not is_local_dist_rank_0() and hf_quantizer is None: | ||
| state_dict = [] | ||
|
|
Collaborator
There was a problem hiding this comment.
I don't think this is where the skip should happen, you r are creating a thread pool for notthing
| @@ -476,6 +476,10 @@ def _load_parameter_into_model(model: "PreTrainedModel", param_name: str, tensor | |||
| parent, param_type = get_module_from_name(model, param_name) | |||
| if param_type in parent._parameters and not isinstance(tensor, nn.Parameter): | |||
Collaborator
There was a problem hiding this comment.
this is only used in _move_missing_keys_from_meta_to_device for missing keys, it does not really make sense for me can you elaborate on why?
Collaborator
|
I don't think this behavior should happen at this low of a level. There are a whole host of other cases where this would break ND-parallel loading (HSDP for example), where you would have FSDP, but have multiple data parallel meshes that each would need the model weights too. |
- Move FSDP check to function entry point to avoid creating empty thread pool - Add detailed documentation for _is_hf_initialized flag usage - Add should_skip_non_rank0_weight_loading() helper to support HYBRID_SHARD strategies - Ensure compatibility with ND-parallel scenarios like HSDP This addresses review feedback: - @ArthurZucker: Fix thread pool creation issue and explain _is_hf_initialized purpose - @winglian: Ensure compatibility with HYBRID_SHARD and HYBRID_SHARD_ZERO2 strategies
Member
|
Agreed, everything should simply be skipped both in |
This was referenced Apr 29, 2026
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.
Modified:
Fixes #43749
@Cyrilvallez @ArthurZucker