CoolFace
Apppublic

ALSv/self-forcing

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
fsdp.py34 linesDownload Raw Back to distributed
1# Copyright 2024-2025 The Alibaba Wan Team Authors. All rights reserved.2from functools import partial3 4import torch5from torch.distributed.fsdp import FullyShardedDataParallel as FSDP6from torch.distributed.fsdp import MixedPrecision, ShardingStrategy7from torch.distributed.fsdp.wrap import lambda_auto_wrap_policy8 9 10def shard_model(11    model,12    device_id,13    param_dtype=torch.bfloat16,14    reduce_dtype=torch.float32,15    buffer_dtype=torch.float32,16    process_group=None,17    sharding_strategy=ShardingStrategy.FULL_SHARD,18    sync_module_states=True,19):20    model = FSDP(21        module=model,22        process_group=process_group,23        sharding_strategy=sharding_strategy,24        auto_wrap_policy=partial(25            lambda_auto_wrap_policy, lambda_fn=lambda m: m in model.blocks),26        mixed_precision=MixedPrecision(27            param_dtype=param_dtype,28            reduce_dtype=reduce_dtype,29            buffer_dtype=buffer_dtype),30        device_id=device_id,31        use_orig_params=True,32        sync_module_states=sync_module_states)33    return model34