fsdp2_utils#
FSDP2 wrapping of a model’s decoder layers, which needs no Hugging Face package.
The loaders (modelopt.torch.utils.plugins.model_load_utils) wrap with 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.
Functions
Auto-detect a HF causal-LM's decoder layers and FSDP2 |
- fsdp2_wrap(model, shard_root=True, mp_policy=None, cpu_offload=False)#
Auto-detect a HF causal-LM’s decoder layers and FSDP2
fully_shardeach one.By default (
shard_root=True) the root module is wrapped too, so embed/lm_head/norm are sharded instead of replicated per rank; passshard_root=Falseto 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
_off_dtype_params()), since FSDP2 rejects a shard group that mixes dtypes. They stay replicated and are moved onto the shards’ device, whichfully_sharddoes not do for the params it ignores.- Parameters:
cpu_offload (bool)