Repository navigation
[OMNIML-6054] [1/2] Move fsdp2_wrap out of distributed.py into fsdp2_utils #3140
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
shengliangxu
wants to merge
9
commits into
main
Choose a base branch
from
shengliangx/hfep-13a-move-fsdp2-wrap
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.
+251
−149
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2dc637d
Move fsdp2_wrap to the model loaders, keeping its old path
shengliangxu 480c340
Name the hf extra when fsdp2_wrap's old path cannot reach the loaders
shengliangxu e6db81b
Move fsdp2_wrap's dtype helpers with it
shengliangxu 5fa18fe
Name the hf extra only when transformers or accelerate is missing
shengliangxu 447cfb5
Name the hf extra when huggingface_hub is missing too
shengliangxu 7f16c2c
Name the hf extra only when a whole package is missing
shengliangxu 368a533
Skip the transformers-submodule row of the old-path test without tran…
shengliangxu 9a55cde
Keep fsdp2_wrap free of the Hugging Face packages, in a module of its…
shengliangxu 30ab95b
Drop the old path's mock-only forwarding test
shengliangxu 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,132 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # 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. | ||
|
|
||
| """FSDP2 wrapping of a model's decoder layers, which needs no Hugging Face package. | ||
|
|
||
| The loaders (:mod:`modelopt.torch.utils.plugins.model_load_utils`) wrap with :func:`fsdp2_wrap` and | ||
| export it; it lives here so that a model whose decoder layers are registered with | ||
| ``LayerActivationCollector`` can be wrapped without ``transformers`` or ``huggingface_hub``. | ||
| """ | ||
|
|
||
| from typing import Any | ||
|
|
||
| import torch | ||
| from torch.distributed.fsdp import CPUOffloadPolicy, fully_shard | ||
| from torch.distributed.tensor import DTensor | ||
|
|
||
| from modelopt.torch.utils.logging import warn_rank_0 | ||
|
|
||
| __all__ = ["fsdp2_wrap"] | ||
|
|
||
|
|
||
| def _off_dtype_params(model) -> set[torch.nn.Parameter]: | ||
| """Params whose dtype differs from the model's dominant (by element count) param dtype. | ||
|
|
||
| FSDP2 needs one dtype per shard group, but HF models routinely keep a few params in fp32 for | ||
| stability (e.g. MoE router gates). Pass these to ``fully_shard(ignored_params=...)``. | ||
|
|
||
| TODO: Drop this and shard the off-dtype params once a stable PyTorch release includes FSDP2 | ||
| mixed-precision parameter dtype support (already on nightly). | ||
| """ | ||
| numel_by_dtype: dict[torch.dtype, int] = {} | ||
| for param in model.parameters(): | ||
| numel_by_dtype[param.dtype] = numel_by_dtype.get(param.dtype, 0) + param.numel() | ||
| if len(numel_by_dtype) <= 1: | ||
| return set() | ||
|
|
||
| dominant = max(numel_by_dtype, key=lambda d: numel_by_dtype[d]) | ||
| off_dtype = {n: p for n, p in model.named_parameters() if p.dtype != dominant} | ||
| off_numel = sum(numel_by_dtype[d] for d in numel_by_dtype if d != dominant) | ||
| names = sorted(off_dtype) | ||
| warn_rank_0( | ||
| f"Model has mixed parameter dtypes {set(numel_by_dtype)}; FSDP2 needs one dtype per shard " | ||
| f"group, so {len(names)} non-{dominant} parameter(s) " | ||
| f"({100 * off_numel / sum(numel_by_dtype.values()):.2f}% of elements) will stay replicated " | ||
| f"rather than sharded: {names[:3]}{' ...' if len(names) > 3 else ''}" | ||
| ) | ||
| return set(off_dtype.values()) | ||
|
|
||
|
|
||
| def _move_to_fsdp_device(model, params: set[torch.nn.Parameter]) -> None: | ||
| """Move ``params`` onto the device FSDP2 computes on for ``model``. | ||
|
|
||
| ``fully_shard`` only moves the params it manages, so ignored ones would be stranded on | ||
| whatever device the caller built the model on. Meta params are left alone for deferred init. | ||
|
|
||
| The device comes from a sharded param's mesh rather than its local shard: under | ||
| ``cpu_offload`` the shard rests on CPU while compute still happens on the accelerator. | ||
| """ | ||
| mesh = next((p.device_mesh for p in model.parameters() if isinstance(p, DTensor)), None) | ||
| if mesh is None: | ||
| warn_rank_0( | ||
| f"FSDP2 sharded no parameter of {type(model).__name__}, so the compute device for " | ||
| f"{len(params)} unsharded off-dtype parameter(s) cannot be determined; leaving them " | ||
| "where they are. Move them to the compute device or the forward will fail." | ||
| ) | ||
| return | ||
|
|
||
| device = ( | ||
| torch.device("cpu") | ||
| if mesh.device_type == "cpu" | ||
| else torch.device(mesh.device_type, getattr(torch, mesh.device_type).current_device()) | ||
| ) | ||
| for param in params: | ||
| if not param.is_meta and param.device != device: | ||
| param.data = param.data.to(device) | ||
|
|
||
|
|
||
| def fsdp2_wrap(model, shard_root=True, mp_policy=None, cpu_offload: bool = False): | ||
| """Auto-detect a HF causal-LM's decoder layers and FSDP2 ``fully_shard`` each one. | ||
|
|
||
| By default (``shard_root=True``) the root module is wrapped too, so embed/lm_head/norm are | ||
| sharded instead of replicated per rank; pass ``shard_root=False`` to leave the root replicated | ||
| (only decoder layers sharded). Returns the detected decoder layers so callers can reuse the | ||
| detection result. | ||
|
|
||
| Parameters whose dtype differs from the model's dominant one are excluded from the wrap (see | ||
| :func:`_off_dtype_params`), since FSDP2 rejects a shard group that mixes dtypes. They stay | ||
| replicated and are moved onto the shards' device, which ``fully_shard`` does not do for the | ||
| params it ignores. | ||
| """ | ||
| # Importing the quantization package registers every mode and plugin; only a wrap needs it. | ||
| from modelopt.torch.quantization.utils.layerwise_calib import LayerActivationCollector | ||
|
|
||
| decoder_layers = LayerActivationCollector.get_decoder_layers(model) | ||
| if decoder_layers is None: | ||
| raise RuntimeError( | ||
| "Could not auto-detect decoder layers; FSDP2 wrap requires a standard HF causal-LM layout." | ||
| ) | ||
|
|
||
| fsdp_kwargs: dict[str, Any] = {"reshard_after_forward": True} | ||
| if mp_policy is not None: | ||
| fsdp_kwargs["mp_policy"] = mp_policy | ||
| if cpu_offload: | ||
| fsdp_kwargs["offload_policy"] = CPUOffloadPolicy() | ||
| ignored_params = _off_dtype_params(model) | ||
| if ignored_params: | ||
| fsdp_kwargs["ignored_params"] = ignored_params | ||
|
|
||
| # Snapshot/restore config.architectures: some HF builders mutate it during fully_shard. | ||
| config = getattr(model, "config", None) | ||
| architectures = list(getattr(config, "architectures", []) or []) | ||
| for layer in decoder_layers: | ||
| fully_shard(layer, **fsdp_kwargs) | ||
| if shard_root: | ||
| fully_shard(model, **fsdp_kwargs) | ||
| if ignored_params: | ||
| _move_to_fsdp_device(model, ignored_params) | ||
| if config is not None and architectures: | ||
| config.architectures = architectures | ||
| return decoder_layers |
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.
Uh oh!
There was an error while loading. Please reload this page.