Prepares the model for FSDP2 in-place. Also returns the model to avoid misuse of the original model. Args: accelerator (`Accelerator`): The accelerator instance model (`torch.nn.Module`): The model to prepare Returns: `torch.nn.Module`: Prepared model
(model: torch.nn.Module,mesh:dist.device_mesh.DeviceMesh)
| 61 | |
| 62 | |
| 63 | def fsdp2_prepare_model(model: torch.nn.Module,mesh:dist.device_mesh.DeviceMesh) -> torch.nn.Module: |
| 64 | """Prepares the model for FSDP2 in-place. Also returns the model to avoid misuse of the original model. |
| 65 | |
| 66 | Args: |
| 67 | accelerator (`Accelerator`): The accelerator instance |
| 68 | model (`torch.nn.Module`): The model to prepare |
| 69 | |
| 70 | Returns: |
| 71 | `torch.nn.Module`: Prepared model |
| 72 | """ |
| 73 | from torch.distributed.fsdp import FSDPModule, MixedPrecisionPolicy, fully_shard |
| 74 | |
| 75 | is_type_fsdp = isinstance(model, FSDPModule) or ( |
| 76 | is_compiled_module(model) and isinstance(model._orig_mod, FSDPModule) |
| 77 | ) |
| 78 | if is_type_fsdp: |
| 79 | return model |
| 80 | |
| 81 | fsdp2_plugin = FullyShardedDataParallelPlugin() |
| 82 | |
| 83 | original_sd = model.state_dict() |
| 84 | |
| 85 | from torch.distributed.fsdp.wrap import size_based_auto_wrap_policy, transformer_auto_wrap_policy |
| 86 | |
| 87 | # We need the `auto_wrap_policy` original type to create a custom poilicy function for sharding |
| 88 | # This is because `fully_shard` doesn't support old auto wrap policies, rather we have to imitate the behaviour |
| 89 | auto_wrap_policy_type = None |
| 90 | if fsdp2_plugin.auto_wrap_policy is transformer_auto_wrap_policy: |
| 91 | auto_wrap_policy_type = "transformer" |
| 92 | elif fsdp2_plugin.auto_wrap_policy is size_based_auto_wrap_policy: |
| 93 | auto_wrap_policy_type = "size" |
| 94 | |
| 95 | # We set `auto_wrap_policy` to `functools.partial` to avoid creating it again |
| 96 | # This is because of `apply_activation_checkpointing` which will can reuse this function |
| 97 | fsdp2_plugin.set_auto_wrap_policy(model) |
| 98 | |
| 99 | if fsdp2_plugin.activation_checkpointing: |
| 100 | from torch.distributed.algorithms._checkpoint.checkpoint_wrapper import ( |
| 101 | CheckpointImpl, |
| 102 | apply_activation_checkpointing, |
| 103 | checkpoint_wrapper, |
| 104 | ) |
| 105 | |
| 106 | # Apply activation checkpointing before applying `fully_shard` |
| 107 | apply_activation_checkpointing( |
| 108 | model, |
| 109 | checkpoint_wrapper_fn=functools.partial( |
| 110 | checkpoint_wrapper, |
| 111 | checkpoint_impl=CheckpointImpl.NO_REENTRANT, |
| 112 | ), |
| 113 | auto_wrap_policy=fsdp2_plugin.auto_wrap_policy, |
| 114 | ) |
| 115 | fsdp2_kwargs = { |
| 116 | "reshard_after_forward": fsdp2_plugin.reshard_after_forward, |
| 117 | "offload_policy": fsdp2_plugin.cpu_offload, |
| 118 | "mesh": mesh, |
| 119 | # `fully_shard` doesn't accept `None` in case of `MixedPrecisionPolicy` |
| 120 | "mp_policy": fsdp2_plugin.mixed_precision_policy or MixedPrecisionPolicy(), |