Skip to content

Commit 194adfa

Browse files
committed
add bias handling for a_1_128_w_128_128 float8 scaling
Summary: As titled, adds support for bias and a unit test Test Plan: ``` pytest test/quantization/quantize_/workflows/float8/test_float8_tensor.py -s -k fp8_linear_variants ``` Reviewers: Subscribers: Tasks: Tags: ghstack-source-id: 48d5806 ghstack-comment-id: 3463298384 Pull-Request: #3259
1 parent 7eb642e commit 194adfa

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

test/quantization/quantize_/workflows/float8/test_float8_tensor.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@
3939

4040

4141
class ToyLinearModel(torch.nn.Module):
42-
def __init__(self, in_features, out_features):
42+
def __init__(self, in_features, out_features, bias):
4343
super().__init__()
44-
self.linear1 = torch.nn.Linear(in_features, out_features, bias=False)
45-
self.linear2 = torch.nn.Linear(out_features, in_features, bias=False)
44+
self.linear1 = torch.nn.Linear(in_features, out_features, bias=bias)
45+
self.linear2 = torch.nn.Linear(out_features, in_features, bias=bias)
4646

4747
def forward(self, x):
4848
x = self.linear1(x)
@@ -81,6 +81,8 @@ def setUp(self):
8181
((32, 128), 256, 512),
8282
],
8383
)
84+
@common_utils.parametrize("bias", [False, True])
85+
@torch.no_grad()
8486
def test_fp8_linear_variants(
8587
self,
8688
dtype: torch.dtype,
@@ -89,6 +91,7 @@ def test_fp8_linear_variants(
8991
granularity,
9092
kernel_preference: KernelPreference,
9193
sizes: Tuple,
94+
bias: bool,
9295
):
9396
if isinstance(granularity, PerTensor):
9497
if kernel_preference is KernelPreference.FBGEMM:
@@ -109,6 +112,16 @@ def test_fp8_linear_variants(
109112
):
110113
return unittest.skip("unimplemented")
111114

115+
if bias is True:
116+
sizes_to_keep = ((128,), 256, 128)
117+
if (
118+
sizes != sizes_to_keep
119+
or kernel_preference is not KernelPreference.TORCH
120+
):
121+
return unittest.skip(
122+
"cut down on number of options to save test time"
123+
)
124+
112125
error_message = None
113126
if isinstance(granularity, PerRow):
114127
if mode == "dynamic" and dtype != torch.bfloat16:
@@ -137,7 +150,7 @@ def test_fp8_linear_variants(
137150
input_tensor = torch.randn(*M, K, dtype=dtype, device="cuda")
138151

139152
# Create a linear layer with bfloat16 dtype
140-
model = ToyLinearModel(K, N).eval().to(dtype).to("cuda")
153+
model = ToyLinearModel(K, N, bias).eval().to(dtype).to("cuda")
141154

142155
quantized_model = copy.deepcopy(model)
143156

@@ -260,7 +273,7 @@ def test_kernel_preference_numerical_equivalence(self, granularity, sizes):
260273
dtype = torch.bfloat16
261274
input_tensor = torch.randn(*M, K, dtype=dtype, device="cuda")
262275
# Create a linear layer with bfloat16 dtype
263-
model = ToyLinearModel(K, N).eval().to(dtype).to("cuda")
276+
model = ToyLinearModel(K, N, bias=False).eval().to(dtype).to("cuda")
264277

265278
# reference kernel preference and results
266279
# we are using KerenelPreference.TORCH as the reference

torchao/quantization/quantize_/workflows/float8/float8_tensor.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,8 @@ def _(func, types, args, kwargs):
369369
w_scale.t(),
370370
block_size=128,
371371
)
372+
if bias is not None:
373+
res = res + bias
372374
else:
373375
res = addmm_float8_unwrapped_inference(
374376
inpt_data,

0 commit comments

Comments
 (0)