@@ -2028,7 +2028,7 @@ def set_device_mesh(
20282028 """
20292029 Set DeviceMesh(s) used for sharding weights and convert main weights into DTensor
20302030 depending on the TransformerEngine class to support FSDP-TP sharding with FSDP2.
2031-
2031+
20322032 TransformerEngine manages tensor parallel mechanics, while DTensor offers seamless
20332033 integration with Torch DCP checkpointing. This method should only be invoked when
20342034 using DTensor parameters, e.g. when using FSDP2 or DCP.
@@ -2060,47 +2060,38 @@ def set_device_mesh(
20602060 # Validate TP DeviceMesh / Group. Must be consistent with tp_size.
20612061 assert (
20622062 tp_mesh .ndim == 1 and self .tp_size == tp_mesh .size (),
2063- f"TransformerEngine { self .__class__ .__name__ } TP init size ({ self .tp_size } ) "
2064- f"does not match the size of the provided TP DeviceMesh ({ tp_mesh .size ()} )."
2063+ (
2064+ f"TransformerEngine { self .__class__ .__name__ } TP init size ({ self .tp_size } ) "
2065+ f"does not match the size of the provided TP DeviceMesh ({ tp_mesh .size ()} )."
2066+ ),
20652067 )
20662068 # Set the tensor parallel group from the mesh.
20672069 self .set_tensor_parallel_group (tp_mesh .get_group ())
20682070
20692071 # Construct TP-sharded DTensors.
20702072 from torch .distributed .tensor .placement_types import Replicate , Shard
2073+
20712074 # FC1 -> Column-Parallel -> Shard(dim=0)
20722075 self .fc1_weight = _convert_param_to_dtensor_param (
2073- self .fc1_weight ,
2074- tp_mesh ,
2075- placements = (Shard (dim = 0 ),)
2076+ self .fc1_weight , tp_mesh , placements = (Shard (dim = 0 ),)
20762077 )
20772078 self .fc1_bias = _convert_param_to_dtensor_param (
2078- self .fc1_bias ,
2079- tp_mesh ,
2080- placements = (Shard (dim = 0 ),)
2079+ self .fc1_bias , tp_mesh , placements = (Shard (dim = 0 ),)
20812080 )
20822081 # FC2 Weight -> Row-Parallel -> Shard(dim=1)
20832082 self .fc2_weight = _convert_param_to_dtensor_param (
2084- self .fc2_weight ,
2085- tp_mesh ,
2086- placements = (Shard (dim = 1 ),)
2083+ self .fc2_weight , tp_mesh , placements = (Shard (dim = 1 ),)
20872084 )
20882085 # LN & FC2 Bias -> Replicate()
20892086 self .fc2_bias = _convert_param_to_dtensor_param (
2090- self .fc2_bias ,
2091- tp_mesh ,
2092- placements = (Replicate (),)
2087+ self .fc2_bias , tp_mesh , placements = (Replicate (),)
20932088 )
20942089 self .layer_norm_weight = _convert_param_to_dtensor_param (
2095- self .layer_norm_weight ,
2096- tp_mesh ,
2097- placements = (Replicate (),)
2090+ self .layer_norm_weight , tp_mesh , placements = (Replicate (),)
20982091 )
20992092 if self .layer_norm_bias is not None :
21002093 self .layer_norm_bias = _convert_param_to_dtensor_param (
2101- self .layer_norm_bias ,
2102- tp_mesh ,
2103- placements = (Replicate (),)
2094+ self .layer_norm_bias , tp_mesh , placements = (Replicate (),)
21042095 )
21052096
21062097 # Set amax_reduction_group to the FSDP and/or TP sharding mesh
@@ -2583,7 +2574,7 @@ def _get_weight_tensors(self) -> List[Union[torch.Tensor, QuantizedTensorStorage
25832574 if isinstance (fc2_weight , DTensor ):
25842575 fc2_weight = fc2_weight .to_local ()
25852576 return [fc1_weight , fc2_weight ]
2586-
2577+
25872578 def _get_bias_tensors (self ) -> List [torch .Tensor ]:
25882579 """Get the bias tensors of the module."""
25892580 fc1_bias = self .fc1_bias if self .use_bias else None
@@ -2593,7 +2584,7 @@ def _get_bias_tensors(self) -> List[torch.Tensor]:
25932584 if isinstance (fc2_bias , DTensor ):
25942585 fc2_bias = fc2_bias .to_local ()
25952586 return [fc1_bias , fc2_bias ]
2596-
2587+
25972588 def _get_layernorm_weight_and_bias (self ) -> List [Optional [torch .Tensor ]]:
25982589 """Get the weight and bias of the layer norm."""
25992590 ln_weight = self .layer_norm_weight
@@ -2651,7 +2642,7 @@ def backward_dw(self):
26512642 del fc1_bias_grad
26522643 self ._trigger_wgrad_accumulation_and_reduce_hooks ()
26532644
2654- def _set_tensor_parallel_attributes (self , defer_init = False ) -> None :
2645+ def _set_tensor_parallel_attributes (self , defer_init = False ) -> None :
26552646 """Set tensor and sequence parallelism attributes."""
26562647 if not defer_init :
26572648 # Set parallel attributes for layer norm parameters
@@ -2665,4 +2656,4 @@ def _set_tensor_parallel_attributes(self, defer_init = False) -> None:
26652656 if self .use_bias :
26662657 set_tensor_model_parallel_attributes (self .fc1_bias , True , 0 , 1 )
26672658 if self .set_parallel_mode :
2668- setattr (self .fc2_bias , "sequence_parallel" , self .sequence_parallel )
2659+ setattr (self .fc2_bias , "sequence_parallel" , self .sequence_parallel )
0 commit comments