Skip to content

Commit d54837c

Browse files
committed
fix format check Black failed
1 parent 5496ecb commit d54837c

4 files changed

Lines changed: 19 additions & 57 deletions

File tree

tests/pytorch/test_reference_activation.py

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,12 @@
4040
@pytest.fixture
4141
def standard_input():
4242
# Shape (2, 4) ensures .chunk(2, dim=-1) splits it into two (2, 2) tensors cleanly
43-
return torch.tensor(
44-
[[-1.0, 2.0, -3.0, 4.0], [5.0, -6.0, 7.0, -8.0]], dtype=torch.float32
45-
)
43+
return torch.tensor([[-1.0, 2.0, -3.0, 4.0], [5.0, -6.0, 7.0, -8.0]], dtype=torch.float32)
4644

4745

4846
@pytest.fixture
4947
def standard_grad():
50-
return torch.tensor(
51-
[[0.5, 1.5, 2.5, 3.5], [4.5, 5.5, 6.5, 7.5]], dtype=torch.float32
52-
)
48+
return torch.tensor([[0.5, 1.5, 2.5, 3.5], [4.5, 5.5, 6.5, 7.5]], dtype=torch.float32)
5349

5450

5551
# ==============================================================================
@@ -68,9 +64,7 @@ def test_basic_forwards(standard_input):
6864

6965
# 2. GeGLU
7066
a, b = standard_input.chunk(2, dim=-1)
71-
assert torch.allclose(
72-
geglu_torch(standard_input, quantizer), F.gelu(a, approximate="tanh") * b
73-
)
67+
assert torch.allclose(geglu_torch(standard_input, quantizer), F.gelu(a, approximate="tanh") * b)
7468

7569
# 3. Quick-GeLU (qgelu)
7670
assert torch.allclose(
@@ -79,9 +73,7 @@ def test_basic_forwards(standard_input):
7973
)
8074

8175
# 4. Quick-GeGLU (qgeglu)
82-
assert torch.allclose(
83-
qgeglu_torch(standard_input, quantizer), a * torch.sigmoid(1.702 * a) * b
84-
)
76+
assert torch.allclose(qgeglu_torch(standard_input, quantizer), a * torch.sigmoid(1.702 * a) * b)
8577

8678
# 5. ReLU & ReGLU
8779
assert torch.allclose(relu_torch(standard_input, quantizer), F.relu(standard_input))
@@ -91,9 +83,7 @@ def test_basic_forwards(standard_input):
9183
assert torch.allclose(
9284
srelu_torch(standard_input, quantizer), torch.square(F.relu(standard_input))
9385
)
94-
assert torch.allclose(
95-
sreglu_torch(standard_input, quantizer), torch.square(F.relu(a)) * b
96-
)
86+
assert torch.allclose(sreglu_torch(standard_input, quantizer), torch.square(F.relu(a)) * b)
9787

9888
# 7. SiLU & SwiGLU
9989
assert torch.allclose(silu_torch(standard_input, quantizer), F.silu(standard_input))
@@ -140,40 +130,28 @@ def test_basic_backwards(standard_grad, standard_input):
140130
)
141131

142132
# 3. dqgelu & dqgeglu
143-
assert (
144-
dqgelu_torch(standard_grad, standard_input, quantizer).shape
145-
== standard_input.shape
146-
)
133+
assert dqgelu_torch(standard_grad, standard_input, quantizer).shape == standard_input.shape
147134
assert (
148135
dqgeglu_torch(standard_grad[..., :2], standard_input, quantizer).shape
149136
== standard_input.shape
150137
)
151138

152139
# 4. drelu & dreglu
153-
assert (
154-
drelu_torch(standard_grad, standard_input, quantizer).shape
155-
== standard_input.shape
156-
)
140+
assert drelu_torch(standard_grad, standard_input, quantizer).shape == standard_input.shape
157141
assert (
158142
dreglu_torch(standard_grad[..., :2], standard_input, quantizer).shape
159143
== standard_input.shape
160144
)
161145

162146
# 5. dsrelu & dsreglu
163-
assert (
164-
dsrelu_torch(standard_grad, standard_input, quantizer).shape
165-
== standard_input.shape
166-
)
147+
assert dsrelu_torch(standard_grad, standard_input, quantizer).shape == standard_input.shape
167148
assert (
168149
dsreglu_torch(standard_grad[..., :2], standard_input, quantizer).shape
169150
== standard_input.shape
170151
)
171152

172153
# 6. dsilu & dswiglu
173-
assert (
174-
dsilu_torch(standard_grad, standard_input, quantizer).shape
175-
== standard_input.shape
176-
)
154+
assert dsilu_torch(standard_grad, standard_input, quantizer).shape == standard_input.shape
177155
assert (
178156
dswiglu_torch(standard_grad[..., :2], standard_input, quantizer).shape
179157
== standard_input.shape

tests/pytorch/test_reference_dropout.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ def test_dropout_bwd_standard_probability():
102102

103103
# Case B: Computation directly assigned into preallocated grad_input targets
104104
grad_input_buffer = torch.empty_like(grad_out)
105-
grad_in = dropout_bwd_torch(
106-
grad_out, mask, dropout_probability=p, grad_input=grad_input_buffer
107-
)
105+
grad_in = dropout_bwd_torch(grad_out, mask, dropout_probability=p, grad_input=grad_input_buffer)
108106
assert torch.equal(grad_in, grad_input_buffer)
109107
assert torch.allclose(grad_input_buffer, expected_grad)

tests/pytorch/test_reference_gemm.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,8 @@ def test_gemm_standard_and_device_mismatch():
5353
# Since out = torch.mm(B_comp, A_comp), shapes are:
5454
# B_comp: (M, K) = (2, 3) -> B is (2, 3) with transB=False
5555
# A_comp: (K, N) = (3, 2) -> A is (2, 3) with transA=True
56-
A = torch.tensor(
57-
[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], dtype=torch.float32
58-
) # Shape (2, 3)
59-
B = torch.tensor(
60-
[[1.0, 0.0, 1.0], [0.0, 2.0, 1.0]], dtype=torch.float32
61-
) # Shape (2, 3)
56+
A = torch.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], dtype=torch.float32) # Shape (2, 3)
57+
B = torch.tensor([[1.0, 0.0, 1.0], [0.0, 2.0, 1.0]], dtype=torch.float32) # Shape (2, 3)
6258

6359
# Intentionally trigger Device Mismatch path (A on CPU, but B explicitly bound to CPU)
6460
# This fully exercises: if A.device != target_device: A = A.to(target_device)
@@ -152,12 +148,8 @@ def test_gemm_fp8_precision_downcast():
152148

153149
def test_gemm_fusions_and_scaling():
154150
"""Verify alpha scaling, bias broadcast addition, and dtype downcasting pipelines."""
155-
A = torch.tensor(
156-
[[2.0], [2.0]], dtype=torch.float32
157-
) # (2, 1) -> transA=False -> A_comp=(2, 1)
158-
B = torch.tensor(
159-
[[3.0, 4.0]], dtype=torch.float32
160-
) # (1, 2) -> transB=False -> B_comp=(1, 2)
151+
A = torch.tensor([[2.0], [2.0]], dtype=torch.float32) # (2, 1) -> transA=False -> A_comp=(2, 1)
152+
B = torch.tensor([[3.0, 4.0]], dtype=torch.float32) # (1, 2) -> transB=False -> B_comp=(1, 2)
161153
# torch.mm(B_comp, A_comp) -> (1, 2) x (2, 1) -> (1, 1) matrix [[14.0]]
162154

163155
bias = torch.tensor([[1.0]], dtype=torch.float32)

transformer_engine/plugin/core/backends/reference/impl/softmax.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ def scaled_softmax_backward_torch(
3333
softmax_output_f32 = softmax_output.float()
3434

3535
grad_softmax = softmax_output_f32 * (
36-
output_grad_f32
37-
- (softmax_output_f32 * output_grad_f32).sum(dim=-1, keepdim=True)
36+
output_grad_f32 - (softmax_output_f32 * output_grad_f32).sum(dim=-1, keepdim=True)
3837
)
3938

4039
return (grad_softmax * scale).to(orig_dtype)
@@ -87,8 +86,7 @@ def scaled_masked_softmax_backward_torch(
8786
softmax_output_f32 = softmax_output.float()
8887

8988
grad_softmax = softmax_output_f32 * (
90-
output_grad_f32
91-
- (softmax_output_f32 * output_grad_f32).sum(dim=-1, keepdim=True)
89+
output_grad_f32 - (softmax_output_f32 * output_grad_f32).sum(dim=-1, keepdim=True)
9290
)
9391

9492
return (grad_softmax * scale).to(orig_dtype)
@@ -101,9 +99,7 @@ def scaled_upper_triang_masked_softmax_forward_torch(
10199
seq_len = input.size(-1)
102100

103101
causal_mask = torch.triu(
104-
torch.full(
105-
(seq_len, seq_len), float("-inf"), device=input.device, dtype=input.dtype
106-
),
102+
torch.full((seq_len, seq_len), float("-inf"), device=input.device, dtype=input.dtype),
107103
diagonal=1,
108104
)
109105

@@ -123,8 +119,7 @@ def scaled_upper_triang_masked_softmax_backward_torch(
123119
softmax_output_f32 = softmax_output.float()
124120

125121
grad_softmax = softmax_output_f32 * (
126-
output_grad_f32
127-
- (softmax_output_f32 * output_grad_f32).sum(dim=-1, keepdim=True)
122+
output_grad_f32 - (softmax_output_f32 * output_grad_f32).sum(dim=-1, keepdim=True)
128123
)
129124

130125
return (grad_softmax * scale).to(orig_dtype)
@@ -148,8 +143,7 @@ def scaled_aligned_causal_masked_softmax_backward_torch(
148143
softmax_output_f32 = softmax_output.float()
149144

150145
grad_softmax = softmax_output_f32 * (
151-
output_grad_f32
152-
- (softmax_output_f32 * output_grad_f32).sum(dim=-1, keepdim=True)
146+
output_grad_f32 - (softmax_output_f32 * output_grad_f32).sum(dim=-1, keepdim=True)
153147
)
154148

155149
return (grad_softmax * scale).to(orig_dtype)

0 commit comments

Comments
 (0)