When migrating from standard PyTorch FSDP to YaFSDP, the primary interface change involves how modules are wrapped for sharding. YaFSDP replaces the auto_wrap_policy with a more explicit configuration involving module names, layer norm identification, and layer norm types. Additionally, you must explicitly provide the number of gradient_accumulation_steps to YaFSDP.
Key Interface Changes:
- Module Wrapping: Instead of
auto_wrap_policy, use:modules_to_wrap_with_names: A list of tuples (module, name) specifying which modules to shard. The names provided here are used in the state dict.rogue_layer_norm_modules_with_names: A dictionary mapping the first layer after transformer blocks (which typically contains only layer norm parameters) to its name.layer_norm_module_cls: The specific class type of the layer norm layers used in your model.
- Sharding Strategy: The
sharding_strategy is mapped to a zero_stage integer (e.g., 3 for FULL_SHARD, 2 for SHARD_GRAD_OP). - Gradient Accumulation: You must pass
gradient_accumulation_steps as an argument.
model: LlamaForCausalLM = ...
YaFSDP(
model,
zero_stage={
"ShardingStrategy.FULL_SHARD": 3,
"ShardingStrategy.SHARD_GRAD_OP": 2
}[sharding_strategy],
modules_to_wrap_with_names=[
(model.model.embed_tokens, "model.embed_tokens"),
*((m, f"model.layers.{i}") for i, m in enumerate(model.model.layers)),
(model.lm_head, "lm_head")
],
rogue_layer_norm_modules_with_names={model.norm: "model.norm"},
layer_norm_module_cls=LlamaRMSNorm,
param_dtype=param_dtype,
sync_module_states=sync_module_states,
param_init_fn=param_init_fn,
device_id=device,
gradient_accumulation_steps=gradient_accumulation_steps,
)