-
Notifications
You must be signed in to change notification settings - Fork 33.8k
Add native FSDP2 module + migration #46707
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
3outeille
wants to merge
48
commits into
main
Choose a base branch
from
split/a-pr-2-fsdp-module
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 47 commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
799ac94
add distributed config
3outeille 22d4b52
Add native FSDP2 module and migrate FSDP imports (Phase A PR-2).
3outeille 4bfd1a6
linting
3outeille 9487bdd
unecessary
3outeille 588884e
copyright edit
3outeille 8cc48a0
revert
3outeille 5bbc796
Merge branch 'main' into split/a-pr-1-distributed-config
3outeille 6fd7813
Merge branch 'split/a-pr-1-distributed-config' into split/a-pr-2-fsdp…
3outeille 79457b3
fix
3outeille f3e8021
fix
3outeille acacae8
Merge branch 'split/a-pr-1-distributed-config' into split/a-pr-2-fsdp…
3outeille 54c1f4e
Merge branch 'main' into split/a-pr-1-distributed-config
3outeille f219c74
Merge branch 'split/a-pr-1-distributed-config' into split/a-pr-2-fsdp…
3outeille ea8243f
remove redundant test file
3outeille db31b04
Merge branch 'main' into split/a-pr-1-distributed-config
3outeille 4d840dc
Merge branch 'split/a-pr-1-distributed-config' into split/a-pr-2-fsdp…
3outeille c384fcd
Update src/transformers/distributed/fsdp.py
3outeille 9625816
avoid looping, just look at dict
3outeille 59bcec5
expand_fsdp returns reshard_targets, no_reshard_targets right away
3outeille ebf3585
better _resolve_tied_embed_lm_head_plan
3outeille e969325
cleaning
3outeille 2376965
ruff
3outeille d830114
Merge branch 'main' into split/a-pr-1-distributed-config
3outeille a44f81f
Merge branch 'split/a-pr-1-distributed-config' into split/a-pr-2-fsdp…
3outeille 0f62c45
more robust detection of embed and lm_head
3outeille 020f7d3
Merge branch 'split/a-pr-2-fsdp-module' of https://github.com/hugging…
3outeille da302ad
cleaning
3outeille dfc665c
ruff
3outeille 446fd6e
typo
3outeille 5aeaff7
cleaner
3outeille 819ff14
cleaner
3outeille 413d775
Merge branch 'main' into split/a-pr-1-distributed-config
3outeille 7bc3722
Merge branch 'split/a-pr-1-distributed-config' into split/a-pr-2-fsdp…
3outeille f8f27ff
Merge branch 'main' into split/a-pr-2-fsdp-module
3outeille ce2f001
Merge branch 'main' into split/a-pr-2-fsdp-module
3outeille ec87fff
Merge branch 'main' into split/a-pr-2-fsdp-module
3outeille 6e156fd
Merge branch 'main' into split/a-pr-2-fsdp-module
3outeille 606df0a
Merge branch 'main' into split/a-pr-2-fsdp-module
3outeille c4aa4b7
Merge branch 'main' into split/a-pr-2-fsdp-module
3outeille c5ad67b
Merge branch 'main' into split/a-pr-2-fsdp-module
3outeille 04c124e
Merge branch 'main' into split/a-pr-2-fsdp-module
3outeille 8b57aa4
Merge branch 'main' into split/a-pr-2-fsdp-module
3outeille dd1000b
Merge branch 'main' into split/a-pr-2-fsdp-module
3outeille 5c95559
expand_fsdp_plan iterate over modules
3outeille 00a11b6
Merge branch 'split/a-pr-2-fsdp-module' of https://github.com/hugging…
3outeille e4613e6
comment about tie embedding
3outeille d558f99
add comment tied embedding
3outeille 1541674
Merge branch 'main' into split/a-pr-2-fsdp-module
3outeille File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,286 @@ | ||
| # Copyright 2026 The HuggingFace Team. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| from __future__ import annotations | ||
|
|
||
| import inspect | ||
| import os | ||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| from ..integrations.tensor_parallel import replace_layer_number_by_wildcard | ||
| from ..utils import is_torch_available, is_torch_greater_or_equal, logging, strtobool | ||
| from ..utils.quantization_config import QuantizationMethod | ||
|
|
||
|
|
||
| if TYPE_CHECKING: | ||
| import torch.nn as nn | ||
|
|
||
| from .configuration_utils import DistributedConfig | ||
|
|
||
| if is_torch_available(): | ||
| import torch | ||
|
|
||
| if is_torch_available() and is_torch_greater_or_equal("2.6"): | ||
| from torch.distributed._composable.fsdp import fully_shard | ||
| from torch.distributed.fsdp import CPUOffloadPolicy, MixedPrecisionPolicy | ||
|
|
||
| logger = logging.get_logger(__name__) | ||
|
|
||
|
|
||
| def is_fsdp_enabled() -> bool: | ||
| """Check if FSDP is active via Accelerate (env var based) — covers FSDP1 only.""" | ||
| if not is_torch_available(): | ||
| return False | ||
|
|
||
| return ( | ||
| torch.distributed.is_available() | ||
| and torch.distributed.is_initialized() | ||
| and strtobool(os.environ.get("ACCELERATE_USE_FSDP", "False")) == 1 | ||
| and strtobool(os.environ.get("FSDP_CPU_RAM_EFFICIENT_LOADING", "False")) == 1 | ||
| ) | ||
|
|
||
|
|
||
| def is_fsdp_managed_module(module: nn.Module) -> bool: | ||
| """Check if a module is managed by FSDP (1 or 2).""" | ||
| if not is_torch_available(): | ||
| return False | ||
| if not torch.distributed.is_available(): | ||
| return False | ||
|
|
||
| # FSDP2: attribute set by apply_fsdp2() | ||
| if getattr(module, "_is_fsdp_managed_module", False): | ||
| return True | ||
| # FSDP1: wrapped by FullyShardedDataParallel | ||
| try: | ||
| from torch.distributed.fsdp import FullyShardedDataParallel | ||
| except ImportError: | ||
| return False | ||
| return isinstance(module, FullyShardedDataParallel) | ||
|
|
||
|
|
||
| def _get_fsdp_policy_kwargs(distributed_config: DistributedConfig | None) -> dict[str, Any]: | ||
| """Build ``fully_shard`` policy kwargs from ``DistributedConfig`` runtime flags.""" | ||
| if distributed_config is None: | ||
| return {} | ||
|
|
||
| fsdp_policy_kwargs = {} | ||
| if distributed_config.fsdp_cpu_offload: | ||
| fsdp_policy_kwargs["offload_policy"] = CPUOffloadPolicy() | ||
| if distributed_config.fsdp_mixed_precision: | ||
| fsdp_policy_kwargs["mp_policy"] = MixedPrecisionPolicy( | ||
| param_dtype=torch.bfloat16, | ||
| reduce_dtype=torch.float32, | ||
| output_dtype=None, | ||
| ) | ||
| return fsdp_policy_kwargs | ||
|
|
||
|
|
||
| def _get_input_output_embeddings(model: nn.Module) -> tuple[nn.Module | None, nn.Module | None]: | ||
| input_embed = None | ||
| output_head = None | ||
| if hasattr(model, "get_input_embeddings"): | ||
| input_embed = model.get_input_embeddings() | ||
| if hasattr(model, "get_output_embeddings"): | ||
| output_head = model.get_output_embeddings() | ||
| return input_embed, output_head | ||
|
|
||
|
|
||
| def is_norm_and_head_pair(no_reshard_targets: list[tuple[str, nn.Module]], model: nn.Module) -> bool: | ||
| if len(no_reshard_targets) != 2: | ||
| return False | ||
| input_embed, output_head = _get_input_output_embeddings(model) | ||
| head_modules = {module for module in (input_embed, output_head) if module is not None} | ||
|
|
||
| names, modules = [], [] | ||
| for name, module in no_reshard_targets: | ||
| names.append(name) | ||
| modules.append(module) | ||
|
|
||
| has_final_norm = any(name == "norm" or name.endswith(".norm") for name in names) | ||
| has_output_head = any(module in head_modules for module in modules) | ||
| return has_final_norm and has_output_head | ||
|
|
||
|
|
||
| def _resolve_tied_embed_lm_head_plan( | ||
| fsdp_plan: dict[str, str], | ||
| model: nn.Module, | ||
| ) -> dict[str, str]: | ||
| """ | ||
| Rewrite the plan so tied embed/lm_head weights are wrapped once. | ||
| Example: | ||
| {"model.embed_tokens": "free_full_weight", | ||
| "model.layers.*": "free_full_weight", | ||
| "model.norm": "keep_full_weight", | ||
| "lm_head": "keep_full_weight"} | ||
| -> | ||
| {"model.layers.*": "free_full_weight", | ||
| "model.norm": "keep_full_weight", | ||
| "model.embed_tokens": "keep_full_weight"} | ||
| """ | ||
| tied_keys = getattr(model, "all_tied_weights_keys", None) or {} | ||
| if not tied_keys: | ||
| return fsdp_plan | ||
|
|
||
| input_embed, output_head = _get_input_output_embeddings(model) | ||
| name_by_module = {module: name for name, module in model.named_modules()} | ||
| embed_module = name_by_module.get(input_embed) | ||
| head_module = name_by_module.get(output_head) | ||
|
|
||
| if embed_module is None or head_module is None: | ||
| return fsdp_plan | ||
|
|
||
| adapted_plan = fsdp_plan.copy() | ||
| adapted_plan.pop(embed_module, None) | ||
|
|
||
| if fsdp_plan.get(head_module) == "keep_full_weight": | ||
| adapted_plan.pop(head_module, None) | ||
| adapted_plan[embed_module] = "keep_full_weight" | ||
|
|
||
| return adapted_plan | ||
|
|
||
|
|
||
| def expand_fsdp_plan( | ||
| model: nn.Module, | ||
| fsdp_plan: dict[str, str], | ||
| ) -> tuple[list[tuple[str, nn.Module]], list[tuple[str, nn.Module]]]: | ||
| """Expand plan keys into reshard and no-reshard ``(module_name, module)`` shard targets.""" | ||
| reshard_targets: list[tuple[str, nn.Module]] = [] | ||
| no_reshard_targets: list[tuple[str, nn.Module]] = [] | ||
|
|
||
| for module_name, module in model.named_modules(): | ||
| plan_key = module_name if module_name in fsdp_plan else replace_layer_number_by_wildcard(module_name) | ||
| if plan_key in fsdp_plan: | ||
| if fsdp_plan[plan_key] == "keep_full_weight": | ||
| no_reshard_targets.append((module_name, module)) | ||
| else: | ||
| reshard_targets.append((module_name, module)) | ||
|
|
||
| return reshard_targets, no_reshard_targets | ||
|
|
||
|
|
||
| def verify_fsdp_plan(module_names: list[str], fsdp_plan: dict[str, str] | None) -> None: | ||
| """ | ||
| Verify the FSDP plan of the model, log a warning if plan keys were not applied or strategies are invalid. | ||
| """ | ||
| if not fsdp_plan: | ||
| return | ||
|
|
||
| name_lookup = dict.fromkeys(module_names) | ||
| unused_rules: dict[str, str] = {} | ||
| invalid_strategies: dict[str, str] = {} | ||
|
|
||
| for key, strategy in fsdp_plan.items(): | ||
| if strategy not in {"free_full_weight", "keep_full_weight"}: | ||
| invalid_strategies[key] = strategy | ||
| elif key not in name_lookup and not any(replace_layer_number_by_wildcard(name) == key for name in name_lookup): | ||
| unused_rules[key] = strategy | ||
|
|
||
| if invalid_strategies: | ||
| logger.warning(f"The following FSDP entries have unknown strategies: {invalid_strategies}") | ||
| if unused_rules: | ||
| logger.warning(f"The following FSDP rules were not applied to any module: {unused_rules}") | ||
|
|
||
|
|
||
| def apply_fully_sharded_data_parallel( | ||
| model: nn.Module, fsdp_mesh: torch.distributed.device_mesh.DeviceMesh | ||
| ) -> nn.Module: | ||
| """ | ||
| Apply FSDP2 (fully_shard) to a model. | ||
| """ | ||
| if not is_torch_available(): | ||
| raise ImportError("PyTorch is required for FSDP support") | ||
|
|
||
| if not is_torch_greater_or_equal("2.6"): | ||
| raise OSError("FSDP2 requires torch>=2.6") | ||
|
|
||
| fsdp_plan = dict(getattr(model, "_fsdp_plan", None) or {}) | ||
| if not fsdp_plan: | ||
| raise ValueError( | ||
| f"{type(model).__name__} does not have a FSDP2 plan declared. Set " | ||
| "`base_model_fsdp_plan` on the config and `_fsdp_plan` on the head class." | ||
| ) | ||
|
|
||
| distributed_config = getattr(model.config, "distributed_config", None) | ||
| fsdp_policy_kwargs = _get_fsdp_policy_kwargs(distributed_config) | ||
|
|
||
| adapted_fsdp_plan = _resolve_tied_embed_lm_head_plan(fsdp_plan, model) | ||
| reshard_targets, no_reshard_targets = expand_fsdp_plan(model, adapted_fsdp_plan) | ||
|
|
||
| for module_name, module in reshard_targets: | ||
| fully_shard(module, mesh=fsdp_mesh, reshard_after_forward=True, **fsdp_policy_kwargs) | ||
| logger.debug(f"Applied fully_shard to {module_name} (reshard=True)") | ||
|
|
||
| # Optimization: when the keep buffer is exactly the (final_norm, lm_head/embed) | ||
| # tail pair, bundle them into one fully_shard so that we dont need to do all-gather during backward pass. | ||
| if is_norm_and_head_pair(no_reshard_targets, model): | ||
| names, modules = [], [] | ||
| for name, module in no_reshard_targets: | ||
| names.append(name) | ||
| modules.append(module) | ||
| fully_shard(modules, mesh=fsdp_mesh, reshard_after_forward=False, **fsdp_policy_kwargs) | ||
| logger.debug(f"Grouped tail {names} (reshard=False)") | ||
| else: | ||
| for name, module in no_reshard_targets: | ||
| fully_shard(module, mesh=fsdp_mesh, reshard_after_forward=False, **fsdp_policy_kwargs) | ||
| logger.debug(f"Applied fully_shard to {name} (reshard=False)") | ||
|
|
||
| # Apply FSDP2 to the root module | ||
| fully_shard(model, mesh=fsdp_mesh, **fsdp_policy_kwargs) | ||
|
|
||
| logger.info(f"FSDP2 applied to model via _fsdp_plan: {len(fsdp_plan)} entries") | ||
|
|
||
| # Used by generation code to detect FSDP and enable synced_gpus. | ||
| model._is_fsdp_managed_module = True | ||
|
|
||
| # NOTE(3outeille): No need to tie the word embeddings here, it will be done _finalize_model_loading in modeling_utils.py | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. perfect |
||
|
|
||
| return model | ||
|
|
||
|
|
||
| # ========================= PEFT compatibility ========================= | ||
| # TODO(3outeille): make sure new FSDP works with PEFT | ||
| def get_fsdp_ckpt_kwargs(): | ||
| """ | ||
| Returns checkpoint kwargs for FSDP model saving. | ||
|
|
||
| Checks if the `adapter_only` parameter is supported by `save_fsdp_model` from accelerate | ||
| and returns the appropriate kwargs. | ||
| """ | ||
| from accelerate.utils import save_fsdp_model | ||
|
|
||
| if "adapter_only" in list(inspect.signature(save_fsdp_model).parameters): | ||
| return {"adapter_only": True} | ||
| else: | ||
| return {} | ||
|
|
||
|
|
||
| def update_fsdp_plugin_peft(model, accelerator): | ||
| """ | ||
| Updates the FSDP plugin for PEFT LoRA/QLoRA compatibility. | ||
|
|
||
| When using FSDP with PEFT LoRA, the auto wrap policy needs to be updated to additionally wrap | ||
| LoRA trainable layers separately. When using FSDP with QLoRA, the mixed precision policy needs | ||
| to be updated to use the quantization storage data type. | ||
| """ | ||
| from peft import PeftConfig | ||
| from peft.utils.other import fsdp_auto_wrap_policy | ||
|
|
||
| if isinstance(model.active_peft_config, PeftConfig): | ||
| accelerator.state.fsdp_plugin.auto_wrap_policy = fsdp_auto_wrap_policy(model) | ||
| if ( | ||
| getattr(model, "quantization_method", None) == QuantizationMethod.BITS_AND_BYTES | ||
| and model.hf_quantizer.quantization_config.bnb_4bit_quant_storage.is_floating_point | ||
| ): | ||
| accelerator.state.fsdp_plugin.set_mixed_precision( | ||
| model.hf_quantizer.quantization_config.bnb_4bit_quant_storage, override=True | ||
| ) | ||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
much better