Skip to content

Commit 60b68f7

Browse files
pre-commit-ci[bot]cspades
authored andcommitted
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 6eaaa4b commit 60b68f7

12 files changed

Lines changed: 97 additions & 98 deletions

File tree

transformer_engine/pytorch/attention/dot_product_attention/dot_product_attention.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ def set_device_mesh(
555555
"""
556556
Set DeviceMesh(s) used for sharding weights and convert main weights into DTensor
557557
depending on the TransformerEngine class to support FSDP-TP sharding with FSDP2.
558-
558+
559559
TransformerEngine manages tensor parallel mechanics, while DTensor offers seamless
560560
integration with Torch DCP checkpointing. This method should only be invoked when
561561
using DTensor parameters, e.g. when using FSDP2 or DCP.
@@ -579,19 +579,20 @@ def set_device_mesh(
579579
# Validate TP DeviceMesh / Group. Must be consistent with tp_size.
580580
assert (
581581
tp_mesh.ndim == 1 and self.tp_size == tp_mesh.size(),
582-
f"TransformerEngine {self.__class__.__name__} TP init size ({self.tp_size}) "
583-
f"does not match the size of the provided TP DeviceMesh ({tp_mesh.size()})."
582+
(
583+
f"TransformerEngine {self.__class__.__name__} TP init size ({self.tp_size}) "
584+
f"does not match the size of the provided TP DeviceMesh ({tp_mesh.size()})."
585+
),
584586
)
585587
# Set the tensor parallel group from the mesh.
586588
self.set_tensor_parallel_group(tp_mesh.get_group())
587589

588590
# Construct TP-sharded DTensors.
589591
if self.softmax_type == "learnable":
590592
from torch.distributed.tensor.placement_types import Shard
593+
591594
self.softmax_offset = _convert_param_to_dtensor_param(
592-
self.softmax_offset,
593-
tp_mesh,
594-
placements=(Shard(dim=0),)
595+
self.softmax_offset, tp_mesh, placements=(Shard(dim=0),)
595596
)
596597

597598
def set_context_parallel_group(
@@ -862,7 +863,7 @@ def set_meta_tensor(self, fwd: bool, recipe: Union[Recipe, List[Recipe]]) -> Non
862863
self.quantizers[fp8_meta_tensor_key] = []
863864
for recipe_state in recipe_states:
864865
self.quantizers[fp8_meta_tensor_key].extend(recipe_state.make_quantizers())
865-
866+
866867
def _get_softmax_offset(self) -> torch.Tensor:
867868
"""Get the softmax offset."""
868869
softmax_offset = (

transformer_engine/pytorch/attention/multi_head_attention.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ def set_device_mesh(
611611
"""
612612
Set DeviceMesh(s) used for sharding weights and convert main weights into DTensor
613613
depending on the TransformerEngine class to support FSDP-TP sharding with FSDP2.
614-
614+
615615
TransformerEngine manages tensor parallel mechanics, while DTensor offers seamless
616616
integration with Torch DCP checkpointing. This method should only be invoked when
617617
using DTensor parameters, e.g. when using FSDP2 or DCP.
@@ -643,8 +643,10 @@ def set_device_mesh(
643643
# Validate TP DeviceMesh / Group. Must be consistent with tp_size.
644644
assert (
645645
tp_mesh.ndim == 1 and self.tp_size == tp_mesh.size(),
646-
f"TransformerEngine {self.__class__.__name__} TP init size ({self.tp_size}) "
647-
f"does not match the size of the provided TP DeviceMesh ({tp_mesh.size()})."
646+
(
647+
f"TransformerEngine {self.__class__.__name__} TP init size ({self.tp_size}) "
648+
f"does not match the size of the provided TP DeviceMesh ({tp_mesh.size()})."
649+
),
648650
)
649651
# Set the tensor parallel group from the mesh.
650652
self.set_tensor_parallel_group(tp_mesh.get_group())

transformer_engine/pytorch/distributed.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,6 +1943,7 @@ def _convert_param_to_dtensor_param(
19431943
):
19441944
"""Convert the parameter into a DTensor."""
19451945
from torch.distributed.tensor import DTensor
1946+
19461947
# If the parameter is already a DTensor, extract local Tensor.
19471948
# We overwrite the original DTensor's distributed configuration.
19481949
param_tensor = param

transformer_engine/pytorch/module/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,7 @@ def reset_parameters(self, defer_init: Optional[bool] = False) -> None:
13001300
# re-applying the nn.Parameter() wrap is a no-op when the input is already
13011301
# a parameter so we always re-apply it just for extra safety.
13021302
if is_dtensor:
1303-
# Recreate the DTensor from the Parameter, inheriting
1303+
# Recreate the DTensor from the Parameter, inheriting
13041304
# all attributes originally set on the Parameter.
13051305
dtensor_param = _convert_param_to_dtensor_param(
13061306
param,

transformer_engine/pytorch/module/grouped_linear.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ def set_device_mesh(
828828
"""
829829
Set DeviceMesh(s) used for sharding weights and convert main weights into DTensor
830830
depending on the TransformerEngine class to support FSDP-TP sharding with FSDP2.
831-
831+
832832
TransformerEngine manages tensor parallel mechanics, while DTensor offers seamless
833833
integration with Torch DCP checkpointing. This method should only be invoked when
834834
using DTensor parameters, e.g. when using FSDP2 or DCP.
@@ -860,36 +860,39 @@ def set_device_mesh(
860860
# Validate TP DeviceMesh / Group. Must be consistent with tp_size.
861861
assert (
862862
tp_mesh.ndim == 1 and self.tp_size == tp_mesh.size(),
863-
f"TransformerEngine {self.__class__.__name__} TP init size ({self.tp_size}) "
864-
f"does not match the size of the provided TP DeviceMesh ({tp_mesh.size()})."
863+
(
864+
f"TransformerEngine {self.__class__.__name__} TP init size ({self.tp_size}) "
865+
f"does not match the size of the provided TP DeviceMesh ({tp_mesh.size()})."
866+
),
865867
)
866868
# Set the tensor parallel group from the mesh.
867869
self.set_tensor_parallel_group(tp_mesh.get_group())
868870

869871
# Construct TP-sharded DTensors.
870872
from torch.distributed.tensor.placement_types import Replicate, Shard
873+
871874
for weight in self.weight_names:
872875
param = getattr(self, weight)
873876
placements = (Replicate(),)
874877
if self.parallel_mode == "column":
875878
placements = (Shard(dim=0),)
876879
elif self.parallel_mode == "row":
877880
placements = (Shard(dim=1),)
878-
setattr(self, weight, _convert_param_to_dtensor_param(
879-
param,
880-
tp_mesh,
881-
placements=placements
882-
))
881+
setattr(
882+
self,
883+
weight,
884+
_convert_param_to_dtensor_param(param, tp_mesh, placements=placements),
885+
)
883886
for bias in self.bias_names:
884887
param = getattr(self, bias)
885888
placements = (Replicate(),)
886889
if self.parallel_mode == "column":
887890
placements = (Shard(dim=0),)
888-
setattr(self, bias, _convert_param_to_dtensor_param(
889-
param,
890-
tp_mesh,
891-
placements=placements
892-
))
891+
setattr(
892+
self,
893+
bias,
894+
_convert_param_to_dtensor_param(param, tp_mesh, placements=placements),
895+
)
893896

894897
# Set amax_reduction_group to the FSDP and/or TP sharding mesh
895898
# for per-tensor scaling recipes. Parameters must be registered.
@@ -1105,7 +1108,7 @@ def _get_weight_tensors(self) -> List[Union[torch.Tensor, QuantizedTensorStorage
11051108
for w in weight_tensors
11061109
]
11071110
return weight_tensors
1108-
1111+
11091112
def _get_bias_tensors(self) -> List[torch.Tensor]:
11101113
"""Get the bias tensors of the module."""
11111114
bias_tensors = []

transformer_engine/pytorch/module/layernorm.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def set_device_mesh(
147147
"""
148148
Set DeviceMesh(s) used for sharding weights and convert main weights into DTensor
149149
depending on the TransformerEngine class to support FSDP-TP sharding with FSDP2.
150-
150+
151151
TransformerEngine manages tensor parallel mechanics, while DTensor offers seamless
152152
integration with Torch DCP checkpointing. This method should only be invoked when
153153
using DTensor parameters, e.g. when using FSDP2 or DCP.
@@ -173,15 +173,12 @@ def set_device_mesh(
173173
# with DTensor parameters in TP layers to support DTensor operations.
174174
from transformer_engine.pytorch.distributed import _convert_param_to_dtensor_param
175175
from torch.distributed.tensor.placement_types import Replicate
176+
176177
self.weight = _convert_param_to_dtensor_param(
177-
self.weight,
178-
tp_mesh,
179-
placements=(Replicate(),)
178+
self.weight, tp_mesh, placements=(Replicate(),)
180179
)
181180
self.bias = _convert_param_to_dtensor_param(
182-
self.bias,
183-
tp_mesh,
184-
placements=(Replicate(),)
181+
self.bias, tp_mesh, placements=(Replicate(),)
185182
)
186183

187184
@property

transformer_engine/pytorch/module/layernorm_linear.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,7 +1462,7 @@ def set_device_mesh(
14621462
"""
14631463
Set DeviceMesh(s) used for sharding weights and convert main weights into DTensor
14641464
depending on the TransformerEngine class to support FSDP-TP sharding with FSDP2.
1465-
1465+
14661466
TransformerEngine manages tensor parallel mechanics, while DTensor offers seamless
14671467
integration with Torch DCP checkpointing. This method should only be invoked when
14681468
using DTensor parameters, e.g. when using FSDP2 or DCP.
@@ -1494,14 +1494,17 @@ def set_device_mesh(
14941494
# Validate TP DeviceMesh / Group. Must be consistent with tp_size.
14951495
assert (
14961496
tp_mesh.ndim == 1 and self.tp_size == tp_mesh.size(),
1497-
f"TransformerEngine {self.__class__.__name__} TP init size ({self.tp_size}) "
1498-
f"does not match the size of the provided TP DeviceMesh ({tp_mesh.size()})."
1497+
(
1498+
f"TransformerEngine {self.__class__.__name__} TP init size ({self.tp_size}) "
1499+
f"does not match the size of the provided TP DeviceMesh ({tp_mesh.size()})."
1500+
),
14991501
)
15001502
# Set the tensor parallel group from the mesh.
15011503
self.set_tensor_parallel_group(tp_mesh.get_group())
15021504

15031505
# Construct TP-sharded DTensors.
15041506
from torch.distributed.tensor.placement_types import Replicate, Shard
1507+
15051508
# Linear
15061509
for weight in self.weight_names:
15071510
param = getattr(self, weight)
@@ -1510,35 +1513,31 @@ def set_device_mesh(
15101513
placements = (Shard(dim=0),)
15111514
elif self.parallel_mode == "row":
15121515
placements = (Shard(dim=1),)
1513-
setattr(self, weight, _convert_param_to_dtensor_param(
1514-
param,
1515-
tp_mesh,
1516-
placements=placements
1517-
))
1516+
setattr(
1517+
self,
1518+
weight,
1519+
_convert_param_to_dtensor_param(param, tp_mesh, placements=placements),
1520+
)
15181521
for bias in self.bias_names:
15191522
param = getattr(self, bias)
15201523
placements = (Replicate(),)
15211524
if self.parallel_mode == "column":
15221525
placements = (Shard(dim=0),)
1523-
setattr(self, bias, _convert_param_to_dtensor_param(
1524-
param,
1525-
tp_mesh,
1526-
placements=placements
1527-
))
1526+
setattr(
1527+
self,
1528+
bias,
1529+
_convert_param_to_dtensor_param(param, tp_mesh, placements=placements),
1530+
)
15281531
# LayerNorm
15291532
placements = (Replicate(),)
15301533
if self.parallel_mode == "row":
15311534
placements = (Shard(dim=0),)
15321535
self.layer_norm_weight = _convert_param_to_dtensor_param(
1533-
self.layer_norm_weight,
1534-
tp_mesh,
1535-
placements=placements
1536+
self.layer_norm_weight, tp_mesh, placements=placements
15361537
)
15371538
if self.layer_norm_bias is not None:
15381539
self.layer_norm_bias = _convert_param_to_dtensor_param(
1539-
self.layer_norm_bias,
1540-
tp_mesh,
1541-
placements=placements
1540+
self.layer_norm_bias, tp_mesh, placements=placements
15421541
)
15431542

15441543
# Set amax_reduction_group to the FSDP and/or TP sharding mesh
@@ -1941,7 +1940,7 @@ def _get_weight_quantizers(self) -> List[Quantizer]:
19411940
weight_quantizer.internal = True
19421941
return [weight_quantizer]
19431942

1944-
def _set_tensor_parallel_attributes(self, defer_init = False) -> None:
1943+
def _set_tensor_parallel_attributes(self, defer_init=False) -> None:
19451944
"""Set tensor and sequence parallelism attributes."""
19461945
if not defer_init:
19471946
# Set parallelism attributes for layer norm parameters

transformer_engine/pytorch/module/layernorm_mlp.py

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)