Repository navigation
[OMNIML-6054] Route load_pretrained by the distribution plan #2779
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
base: shengliangx/hfep-2-plan-decision
Are you sure you want to change the base?
Changes from all commits
dd3885a
7a1c3cf
5ae6375
383b532
4e41b36
0ba3164
2714a15
2555520
49a5589
153594d
3c5f078
3ede1c0
a0d517e
aa3689d
afbd944
376587b
77f40ac
7d681c8
1b3296a
410ca6a
cc95eba
f1e23aa
8fe1734
172c114
6ecf3f3
abd3dd9
17d6195
8e4aa77
30a58b9
0c5596a
a0bf4ba
4be027e
49949f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,7 +30,7 @@ | |
|
|
||
| import torch | ||
| import torch.distributed | ||
| from torch.distributed.fsdp import CPUOffloadPolicy, FSDPModule, fully_shard | ||
| from torch.distributed.fsdp import FSDPModule | ||
| from torch.distributed.tensor import DTensor | ||
|
|
||
| __all__ = [ | ||
|
|
@@ -734,49 +734,15 @@ def _move_to_fsdp_device(model, params: set[torch.nn.Parameter]) -> None: | |
|
|
||
|
|
||
| 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. | ||
| """FSDP2-shard a HF causal LM's decoder layers, and the root unless ``shard_root=False``. | ||
|
|
||
| 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. | ||
| It loads a model, so it lives with the loaders now: | ||
| :func:`modelopt.torch.utils.plugins.model_load_utils.fsdp2_wrap`. This keeps the old path, which | ||
| now needs the ``hf`` extra, as the loaders do. | ||
| """ | ||
| # Lazy import: layerwise_calib imports this module at top level (circular). | ||
| 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." | ||
| ) | ||
| from modelopt.torch.utils.plugins.model_load_utils import fsdp2_wrap as wrap | ||
|
|
||
| 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 | ||
| return wrap(model, shard_root=shard_root, mp_policy=mp_policy, cpu_offload=cpu_offload) | ||
|
Comment on lines
736
to
+745
Contributor
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. [SUGGESTION] Impact is small in practice — the function only works on "a standard HF causal-LM layout", so any real caller already has transformers — and the docstring does say so. But per the repo's changelog rule ("backward breaking changes" get an entry), the
Collaborator
Author
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. Added a Backward Breaking Changes entry: |
||
|
|
||
|
|
||
| def broadcast_state_dict( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.