Skip to content

Commit 32e60cc

Browse files
Apply code formatting with pre-commit
- Run black formatter on Python files - Fix trailing whitespace - Add missing newlines at end of files - Format spacing around operators Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 27ba5b3 commit 32e60cc

83 files changed

Lines changed: 1533 additions & 406 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

transformer_engine/common/recipe/__init__.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,18 @@ class Float8BlockScaling(Recipe):
345345
fp8_mha: bool = False
346346

347347
def __post_init__(self) -> None:
348-
assert self.x_block_scaling_dim in [1, 2], "Only 1D or 2D blocks supported for x"
349-
assert self.w_block_scaling_dim in [1, 2], "Only 1D or 2D blocks supported for w"
350-
assert self.grad_block_scaling_dim in [1, 2], "Only 1D or 2D blocks supported for grad"
348+
assert self.x_block_scaling_dim in [
349+
1,
350+
2,
351+
], "Only 1D or 2D blocks supported for x"
352+
assert self.w_block_scaling_dim in [
353+
1,
354+
2,
355+
], "Only 1D or 2D blocks supported for w"
356+
assert self.grad_block_scaling_dim in [
357+
1,
358+
2,
359+
], "Only 1D or 2D blocks supported for grad"
351360
assert not (
352361
self.x_block_scaling_dim == 2 and self.w_block_scaling_dim == 2
353362
), "2D by 2D block gemm not supported."

transformer_engine/debug/features/api.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,10 @@ def fp8_gemm_enabled(
135135
136136
Union[bool, Tuple[bool, Optional[int]]] - default is (True, None)
137137
"""
138-
return True, None # if it is false, fp8_gemm will be turned off. Otherwise nothing happens.
138+
return (
139+
True,
140+
None,
141+
) # if it is false, fp8_gemm will be turned off. Otherwise nothing happens.
139142

140143
def modify_tensor_enabled(
141144
self,
@@ -479,7 +482,11 @@ def call_feature(self, call, feat_config, layer_name, **kwargs):
479482
"""
480483
if call.__name__ == "inspect_tensor":
481484
kwargs_copy = kwargs.copy()
482-
for k in ["quantizer", "columnwise_quantized_tensor", "rowwise_quantized_tensor"]:
485+
for k in [
486+
"quantizer",
487+
"columnwise_quantized_tensor",
488+
"rowwise_quantized_tensor",
489+
]:
483490
if k not in call.__code__.co_varnames:
484491
kwargs_copy.pop(k)
485492
else:

transformer_engine/debug/features/log_fp8_tensor_stats.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
import nvdlfw_inspect.api as debug_api
1212

1313

14-
from nvdlfw_inspect.debug_features.log_tensor_stats import LogTensorStats as BaseLogTensorStats
14+
from nvdlfw_inspect.debug_features.log_tensor_stats import (
15+
LogTensorStats as BaseLogTensorStats,
16+
)
1517
from nvdlfw_inspect.registry import Registry, api_method
1618

1719
from transformer_engine.debug.features.utils.stats_buffer import STATS_BUFFERS
@@ -21,11 +23,21 @@
2123
Float8CurrentScalingQuantizer,
2224
)
2325
from transformer_engine.pytorch.tensor.mxfp8_tensor import MXFP8Quantizer
24-
from transformer_engine.pytorch.tensor.float8_blockwise_tensor import Float8BlockQuantizer
25-
from transformer_engine.debug.features.utils import get_reduction_params, next_enabled_iter
26+
from transformer_engine.pytorch.tensor.float8_blockwise_tensor import (
27+
Float8BlockQuantizer,
28+
)
29+
from transformer_engine.debug.features.utils import (
30+
get_reduction_params,
31+
next_enabled_iter,
32+
)
2633

2734

28-
ALL_RECIPE_NAMES = ["fp8_delayed_scaling", "fp8_current_scaling", "mxfp8", "fp8_block_scaling"]
35+
ALL_RECIPE_NAMES = [
36+
"fp8_delayed_scaling",
37+
"fp8_current_scaling",
38+
"mxfp8",
39+
"fp8_block_scaling",
40+
]
2941

3042

3143
def _get_recipe_name(quantizer: Optional[Quantizer]):
@@ -47,7 +59,10 @@ def _get_new_quantizer(recipe_name, fp8_dtype):
4759
return Float8BlockQuantizer(fp8_dtype=fp8_dtype, rowwise=True, columnwise=True)
4860
if recipe_name == "fp8_current_scaling":
4961
return Float8CurrentScalingQuantizer(
50-
fp8_dtype=fp8_dtype, device=torch.device("cuda"), rowwise=True, columnwise=True
62+
fp8_dtype=fp8_dtype,
63+
device=torch.device("cuda"),
64+
rowwise=True,
65+
columnwise=True,
5166
)
5267
if recipe_name == "mxfp8":
5368
return MXFP8Quantizer(fp8_dtype=fp8_dtype, rowwise=True, columnwise=True)
@@ -214,9 +229,14 @@ def update_aux_dict(
214229
Needs to clean after usage, because it possibly change the usage of the quantized tensor.
215230
"""
216231
fp8_dtype = None
217-
if recipe_name in ["fp8_delayed_scaling", "fp8_current_scaling", "fp8_block_scaling"]:
232+
if recipe_name in [
233+
"fp8_delayed_scaling",
234+
"fp8_current_scaling",
235+
"fp8_block_scaling",
236+
]:
218237
assert isinstance(
219-
quantizer, (Float8Quantizer, Float8CurrentScalingQuantizer, Float8BlockQuantizer)
238+
quantizer,
239+
(Float8Quantizer, Float8CurrentScalingQuantizer, Float8BlockQuantizer),
220240
)
221241
fp8_dtype = quantizer.dtype
222242

@@ -242,7 +262,8 @@ def update_aux_dict(
242262
finally:
243263
if isinstance(quantized_tensor, QuantizedTensor):
244264
quantized_tensor.update_usage(
245-
rowwise_usage=old_rowwise_usage, columnwise_usage=old_columnwise_usage
265+
rowwise_usage=old_rowwise_usage,
266+
columnwise_usage=old_columnwise_usage,
246267
)
247268

248269
@api_method

transformer_engine/debug/features/log_tensor_stats.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,26 @@
88

99
import torch
1010

11-
from nvdlfw_inspect.debug_features.log_tensor_stats import LogTensorStats as BaseLogTensorStats
11+
from nvdlfw_inspect.debug_features.log_tensor_stats import (
12+
LogTensorStats as BaseLogTensorStats,
13+
)
1214
from nvdlfw_inspect.registry import Registry, api_method
1315
import nvdlfw_inspect.api as debug_api
1416

1517
from transformer_engine.pytorch.tensor import QuantizedTensor, Quantizer
1618
from transformer_engine.pytorch.tensor.float8_tensor import Float8Tensor
1719
from transformer_engine.pytorch.tensor.mxfp8_tensor import MXFP8Tensor
18-
from transformer_engine.pytorch.tensor.storage.float8_tensor_storage import Float8TensorStorage
19-
from transformer_engine.pytorch.tensor.storage.mxfp8_tensor_storage import MXFP8TensorStorage
20+
from transformer_engine.pytorch.tensor.storage.float8_tensor_storage import (
21+
Float8TensorStorage,
22+
)
23+
from transformer_engine.pytorch.tensor.storage.mxfp8_tensor_storage import (
24+
MXFP8TensorStorage,
25+
)
2026
from transformer_engine.debug.features.utils.stats_buffer import STATS_BUFFERS
21-
from transformer_engine.debug.features.utils import next_enabled_iter, get_reduction_params
27+
from transformer_engine.debug.features.utils import (
28+
next_enabled_iter,
29+
get_reduction_params,
30+
)
2231

2332

2433
@Registry.register_feature(namespace="transformer_engine")
@@ -90,7 +99,10 @@ class LogTensorStats(BaseLogTensorStats):
9099

91100
def _get_supported_stats_list(self):
92101
"""Returns stats this feature can log."""
93-
return BaseLogTensorStats._get_supported_stats_list(None) | {"cur_amax", "dynamic_range"}
102+
return BaseLogTensorStats._get_supported_stats_list(None) | {
103+
"cur_amax",
104+
"dynamic_range",
105+
}
94106

95107
@api_method
96108
def inspect_tensor_enabled(

transformer_engine/debug/features/utils/stats_buffer.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ def log(self):
131131
combiner = STATS[stat_name][1]
132132
stat_value = combiner(gathered_helper_stats)
133133
MetricLogger.log_scalar(
134-
f"{self.layer_name}_{self.tensor_name}_{stat_name}", stat_value, self.iteration
134+
f"{self.layer_name}_{self.tensor_name}_{stat_name}",
135+
stat_value,
136+
self.iteration,
135137
)
136138
output[(self.layer_name, self.tensor_name, stat_name, self.iteration)] = (
137139
stat_value # for debugging purposes
@@ -195,7 +197,13 @@ def reset(self):
195197
self.layers_to_next_iter: Dict[str, int] = {}
196198

197199
def try_add_buffer(
198-
self, layer_name, tensor_name, stats, options, reduction_group, reduce_within_microbatch
200+
self,
201+
layer_name,
202+
tensor_name,
203+
stats,
204+
options,
205+
reduction_group,
206+
reduce_within_microbatch,
199207
):
200208
"""If buffer for such combination of stats/tensor_name/... is not present, this method creates it."""
201209
if (layer_name, tensor_name, options) in self.buffers:
@@ -205,7 +213,14 @@ def try_add_buffer(
205213
self.reduction_group_to_buffer[reduction_group].append(buffer)
206214

207215
def feed(
208-
self, layer_name, tensor_name, options, tensor, iteration, skip_reduction, aux_dict=None
216+
self,
217+
layer_name,
218+
tensor_name,
219+
options,
220+
tensor,
221+
iteration,
222+
skip_reduction,
223+
aux_dict=None,
209224
):
210225
"""
211226
Feeds the tensor into the respective buffer.

transformer_engine/debug/features/utils/stats_computation.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,24 @@ def _get(buffers, stat_name):
122122
}
123123

124124
STATS = {
125-
"min": (lambda x, aux_dict: torch.min(x), lambda buffers: min(_get(buffers, "min"))),
126-
"max": (lambda x, aux_dict: torch.max(x), lambda buffers: max(_get(buffers, "max"))),
127-
"sum": (lambda x, aux_dict: torch.sum(x), lambda buffers: sum(_get(buffers, "sum"))),
125+
"min": (
126+
lambda x, aux_dict: torch.min(x),
127+
lambda buffers: min(_get(buffers, "min")),
128+
),
129+
"max": (
130+
lambda x, aux_dict: torch.max(x),
131+
lambda buffers: max(_get(buffers, "max")),
132+
),
133+
"sum": (
134+
lambda x, aux_dict: torch.sum(x),
135+
lambda buffers: sum(_get(buffers, "sum")),
136+
),
128137
"mean": (
129138
lambda x, aux_dict: torch.mean(x),
130139
lambda buffers: sum(_get(buffers, "sum")) / sum(_get(buffers, "numel")),
131140
),
132141
"numel": (
133-
lambda x, aux_dict: x.numel() if hasattr(x, "numel") else x.get_data_tensors()[0].numel(),
142+
lambda x, aux_dict: (x.numel() if hasattr(x, "numel") else x.get_data_tensors()[0].numel()),
134143
lambda buffers: sum(_get(buffers, "numel")),
135144
),
136145
"l1_norm": (
@@ -151,7 +160,10 @@ def _get(buffers, stat_name):
151160
_get(buffers, "variance"), _get(buffers, "numel"), _get(buffers, "sum")
152161
),
153162
),
154-
"cur_amax": (lambda x, aux_dict: x.abs().max(), lambda buffers: max(_get(buffers, "cur_amax"))),
163+
"cur_amax": (
164+
lambda x, aux_dict: x.abs().max(),
165+
lambda buffers: max(_get(buffers, "cur_amax")),
166+
),
155167
"dynamic_range_top": (
156168
lambda x, aux_dict: _compute_dynamic_range_top(x),
157169
lambda buffers: max(_get(buffers, "dynamic_range_top")),

transformer_engine/debug/pytorch/debug_quantization.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ def get_plans_for_output_tensors(self) -> Tuple[bool, str]:
110110

111111
inspect_tensor_enabled = self.process_enabled_api_call(
112112
debug_api.transformer_engine.inspect_tensor_enabled(
113-
layer_name=self.layer_name, tensor_name=self.tensor_name, iteration=self.iteration
113+
layer_name=self.layer_name,
114+
tensor_name=self.tensor_name,
115+
iteration=self.iteration,
114116
)
115117
)
116118

@@ -135,7 +137,9 @@ def get_enabled_look_at_tensors(self):
135137

136138
inspect_tensor_enabled = self.process_enabled_api_call(
137139
debug_api.transformer_engine.inspect_tensor_enabled(
138-
layer_name=self.layer_name, tensor_name=self.tensor_name, iteration=self.iteration
140+
layer_name=self.layer_name,
141+
tensor_name=self.tensor_name,
142+
iteration=self.iteration,
139143
)
140144
)
141145

@@ -312,7 +316,10 @@ def quantize(
312316
self.parent_quantizer.set_usage(rowwise=True)
313317

314318
rowwise_gemm_tensor, columnwise_gemm_tensor = None, None
315-
if STANDARD_FP8_QUANTIZE in [self.rowwise_tensor_plan, self.columnwise_tensor_plan]:
319+
if STANDARD_FP8_QUANTIZE in [
320+
self.rowwise_tensor_plan,
321+
self.columnwise_tensor_plan,
322+
]:
316323
quantized_tensor = self.parent_quantizer(tensor)
317324
# if both rowwise_tensor_plan and columnwise_tensor_plan need to be in fp8,
318325
# one tensor with columnwise=True and rowwise=True is computed
@@ -542,7 +549,10 @@ def _update_parent_quantizer_usage(self):
542549
self.columnwise_usage and self.columnwise_tensor_plan == STANDARD_FP8_QUANTIZE
543550
)
544551

545-
if STANDARD_FP8_QUANTIZE in [self.rowwise_tensor_plan, self.columnwise_tensor_plan]:
552+
if STANDARD_FP8_QUANTIZE in [
553+
self.rowwise_tensor_plan,
554+
self.columnwise_tensor_plan,
555+
]:
546556
self.parent_quantizer.set_usage(
547557
rowwise=rowwise_gemm_quantize,
548558
columnwise=columnwise_gemm_quantize,

transformer_engine/jax/attention.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,10 @@ def run_length_fill_flattened(segment_ids_flattened) -> jnp.ndarray:
448448
# counts: [[2 3 1 1 1 5 3 0 0 0 0 0 0 0 0 0], [1 2 3 2 2 4 2 0 0 0 0 0 0 0 0 0]]
449449
# Returns segment_ids_run_length_1d: [[2 2 3 3 3 0 1 0 5 5 5 5 5 0 0 0], [1 0 0 3 3 3 0 0 2 2 4 4 4 4 0 0]]
450450
boundary = jnp.concatenate(
451-
[jnp.broadcast_to(True, (1,)), segment_ids_flattened[1:] != segment_ids_flattened[:-1]]
451+
[
452+
jnp.broadcast_to(True, (1,)),
453+
segment_ids_flattened[1:] != segment_ids_flattened[:-1],
454+
]
452455
)
453456
run_ids = jnp.cumsum(boundary) - 1
454457
# Each element could, in worst case, start a run
@@ -502,7 +505,11 @@ def _segment_ids_pos_to_seqlens_offsets(
502505
window_size == (-1, -1) and not attn_mask_type.is_bottom_right()
503506
):
504507
return _segment_ids_pos_to_seqlens_offsets_fast_causal_path(
505-
segment_ids_q, segment_ids_kv, segment_pos_q, segment_pos_kv, max_segments_per_seq
508+
segment_ids_q,
509+
segment_ids_kv,
510+
segment_pos_q,
511+
segment_pos_kv,
512+
max_segments_per_seq,
506513
)
507514

508515
# (1 = attend, 0 = masked)
@@ -622,7 +629,10 @@ def tree_flatten(self):
622629
"""
623630
Flatten method to register as a pytree node
624631
"""
625-
return ((self.seqlens, self.seq_offsets, self.segment_ids, self.segment_pos), None)
632+
return (
633+
(self.seqlens, self.seq_offsets, self.segment_ids, self.segment_pos),
634+
None,
635+
)
626636

627637
@classmethod
628638
def tree_unflatten(cls, aux_data, children):

transformer_engine/jax/checkpoint_policies.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919

2020
def te_gemms_saveable(prim, *_, **__) -> bool:
2121
"""Checkpoint policy for Transformer Engine GEMMs."""
22-
is_te_gemm = prim in {GemmPrimitive.outer_primitive, GroupedGemmPrimitive.outer_primitive}
22+
is_te_gemm = prim in {
23+
GemmPrimitive.outer_primitive,
24+
GroupedGemmPrimitive.outer_primitive,
25+
}
2326
# Workaround to include JAX's scaled_matmul until JAX checkpoint policies for dots are
2427
# updated to include it.
2528
is_jax_scaled_matmul = prim.name == "scaled_matmul_wrapper"

0 commit comments

Comments
 (0)