Skip to content

Commit 65ee2d3

Browse files
committed
Add support for resharding for fbgemm configs and int4 preshuffle kernel
Summary: added transpose and cat op support, and also some custom transpose/reshape/unflatten support for resharding. In the future we should probably provide examples for using distributed checkpoint for resharding Test Plan: python test/dtypes/test_fbgemm_int4.py -k test_transpose python test/dtypes/test_fbgemm_int4.py -k test_cat python test/dtypes/test_fbgemm_fp8.py -k test_transpose python test/dtypes/test_fbgemm_fp8.py -k test_cat python test/dtypes/test_int4_groupwise_preshuffle.py Reviewers: Subscribers: Tasks: Tags:
1 parent 6243040 commit 65ee2d3

File tree

10 files changed

+1211
-123
lines changed

10 files changed

+1211
-123
lines changed

test/dtypes/test_fbgemm_fp8.py

Lines changed: 94 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
from torch.testing._internal.common_utils import (
1111
TestCase,
1212
run_tests,
13+
parametrize,
14+
instantiate_parametrized_tests,
1315
)
1416

15-
from torchao.float8.config import e4m3_dtype
1617
from torchao.quantization import (
17-
FbgemmConfig,
18+
Float8DynamicActivationFloat8WeightConfig,
19+
PerRow,
1820
quantize_,
1921
)
2022
from torchao.quantization.utils import compute_error
@@ -24,35 +26,38 @@
2426
)
2527

2628

29+
30+
FBGEMM_CONFIG = Float8DynamicActivationFloat8WeightConfig(
31+
granularity=PerRow(),
32+
kernel="fbgemm"
33+
)
34+
ATEN_CONFIG = Float8DynamicActivationFloat8WeightConfig(
35+
granularity=PerRow(),
36+
kernel="aten"
37+
)
38+
39+
2740
@unittest.skipIf(not TORCH_VERSION_AT_LEAST_2_8, "Need pytorch 2.8+")
2841
@unittest.skipIf(not torch.cuda.is_available(), "Need CUDA available")
2942
@unittest.skipIf(not is_sm_at_least_90(), "Nedd sm90+")
3043
class TestFbgemmFp8Tensor(TestCase):
3144
def setUp(self):
32-
self.config = FbgemmConfig(
33-
input_dtype=e4m3_dtype,
34-
weight_dtype=e4m3_dtype,
35-
output_dtype=torch.bfloat16,
36-
)
37-
self.bmm_config = FbgemmConfig(
38-
input_dtype=e4m3_dtype,
39-
weight_dtype=e4m3_dtype,
40-
output_dtype=torch.bfloat16,
41-
transpose_input=True,
42-
)
4345
self.GPU_DEVICES = ["cuda"] if torch.cuda.is_available() else []
4446

45-
def test_linear(self):
47+
@parametrize("config", [FBGEMM_CONFIG, ATEN_CONFIG])
48+
def test_linear(self, config):
4649
dtype = torch.bfloat16
4750
device = "cuda"
4851
input = torch.randn(1, 128, dtype=dtype, device=device)
4952
linear = torch.nn.Linear(128, 256, dtype=dtype, device=device)
5053
original = linear(input)
51-
quantize_(linear, self.config)
54+
quantize_(linear, config)
5255
quantized = linear(input)
53-
self.assertTrue(compute_error(original, quantized) > 20)
56+
sqnr = compute_error(original, quantized)
57+
self.assertTrue(sqnr > 20, f"sqnr: {sqnr}")
5458

55-
def test_slice(self):
59+
@parametrize("config", [FBGEMM_CONFIG, ATEN_CONFIG])
60+
def test_slice(self, config):
5661
dtype = torch.bfloat16
5762
device = "cuda"
5863
dummy = torch.nn.Linear(256, 256, bias=False, dtype=dtype, device=device)
@@ -65,7 +70,7 @@ def test_slice(self):
6570
dummy.weight.narrow(1, 0, 128), requires_grad=False
6671
)
6772

68-
quantize_(dummy, self.config)
73+
quantize_(dummy, config)
6974
weight1 = dummy.weight.narrow(0, 0, 64)
7075
weight2 = dummy.weight.narrow(1, 0, 128)
7176
self.assertEqual(weight1.float8_data, dummy.weight.float8_data.narrow(0, 0, 64))
@@ -81,20 +86,23 @@ def test_slice(self):
8186
res_ref = dummy1(input)
8287
dummy.weight = torch.nn.Parameter(weight1, requires_grad=False)
8388
res = dummy(input)
84-
assert compute_error(res, res_ref) > 25
89+
sqnr = compute_error(res, res_ref)
90+
self.assertTrue(sqnr > 25, f"sqnr: {sqnr}")
8591

8692
input = torch.randn(2, 128, dtype=dtype, device=device)
8793
res_ref = dummy2(input)
8894
dummy.weight = torch.nn.Parameter(weight2, requires_grad=False)
8995
res = dummy(input)
90-
assert compute_error(res, res_ref) > 15
96+
sqnr = compute_error(res, res_ref)
97+
self.assertTrue(sqnr > 15, f"sqnr: {sqnr}")
9198

92-
def test_slice_and_copy_(self):
99+
@parametrize("config", [FBGEMM_CONFIG, ATEN_CONFIG])
100+
def test_slice_and_copy_(self, config):
93101
l = torch.nn.Linear(1024, 1024).to("cuda").to(torch.bfloat16)
94102
l.weight = torch.nn.Parameter(
95103
torch.zeros(1024, 1024, dtype=torch.bfloat16, device="cuda")
96104
)
97-
quantize_(l, self.config)
105+
quantize_(l, config)
98106
param = l.weight
99107
param_data = param.data
100108
param_data = param_data.narrow(0, 0, 512)
@@ -104,7 +112,7 @@ def test_slice_and_copy_(self):
104112

105113
# dummy_l has random input (shouldn't be 0)
106114
dummy_l = torch.nn.Linear(1024, 1024).to("cuda").to(torch.bfloat16)
107-
quantize_(dummy_l, self.config)
115+
quantize_(dummy_l, config)
108116
quantized = dummy_l.weight
109117
quantized = quantized.narrow(0, 0, 512)
110118

@@ -113,7 +121,8 @@ def test_slice_and_copy_(self):
113121
# making sure param.data is updated
114122
assert param.data.float8_data[0][0] != orig_value
115123

116-
def test_bmm(self):
124+
@parametrize("config", [FBGEMM_CONFIG])
125+
def test_bmm(self, config):
117126
class M(torch.nn.Module):
118127
def __init__(self, weight):
119128
super().__init__()
@@ -128,24 +137,80 @@ def forward(self, x):
128137
weight = torch.randn(10, 128, 256, dtype=dtype, device=device)
129138
m = M(weight).eval()
130139
original = m(input)
131-
quantize_(m, self.bmm_config, filter_fn=lambda x, fqn: True)
140+
# we need to transpose the weight first for bmm
141+
m.weight = torch.nn.Parameter(m.weight.transpose(1, 2).contiguous())
142+
quantize_(m, config, filter_fn=lambda x, fqn: True)
132143
quantized = m(input)
133144
self.assertTrue(compute_error(original, quantized) > 20)
134145

135-
def test_to_device(self):
146+
@parametrize("config", [FBGEMM_CONFIG, ATEN_CONFIG])
147+
def test_to_device(self, config):
136148
for device in self.GPU_DEVICES:
137149
linear = torch.nn.Linear(128, 256, dtype=torch.bfloat16)
138-
quantize_(linear, self.config)
150+
quantize_(linear, config)
139151
linear.to(device)
140152

141153
linear = torch.nn.Linear(128, 256, dtype=torch.bfloat16)
142-
quantize_(linear, self.config)
154+
quantize_(linear, config)
143155
linear.to(device=device)
144156

145157
linear = torch.nn.Linear(128, 256, dtype=torch.bfloat16)
146-
quantize_(linear, self.config)
158+
quantize_(linear, config)
147159
linear.to(device)
148160

161+
@parametrize("config", [FBGEMM_CONFIG, ATEN_CONFIG])
162+
def test_cat(self, config):
163+
dtype = torch.bfloat16
164+
device = "cuda"
165+
# weight: (256, 128)
166+
linear1 = torch.nn.Linear(128, 256, dtype=dtype)
167+
# weight: (256, 128)
168+
linear2 = torch.nn.Linear(128, 256, dtype=dtype)
169+
170+
cat_weight1 = torch.cat([linear1.weight, linear2.weight], dim=0)
171+
dummy1 = torch.nn.Linear(128, 512, bias=False, dtype=dtype, device=device)
172+
173+
dummy1.weight = torch.nn.Parameter(cat_weight1)
174+
quantize_(dummy1, config)
175+
176+
quantize_(linear1, config)
177+
quantize_(linear2, config)
178+
179+
cat_qweight1 = torch.cat([linear1.weight, linear2.weight], dim=0)
180+
self.assertTrue(cat_qweight1.shape, (512, 128))
181+
self.assertEqual(dummy1.weight.float8_data, cat_qweight1.float8_data)
182+
self.assertEqual(dummy1.weight.scale, cat_qweight1.scale)
183+
184+
# concat with dim == 1 is not really correct and will be fixed later
185+
# when we support distributed checkpointing
186+
cat_qweight2 = torch.cat([linear1.weight, linear2.weight], dim=1)
187+
self.assertTrue(cat_qweight2.shape, (256, 256))
188+
ref_float8_data = torch.cat(
189+
[linear1.weight.float8_data, linear2.weight.float8_data], dim=1
190+
)
191+
ref_scale = linear1.weight.scale
192+
self.assertEqual(cat_qweight2.float8_data, ref_float8_data)
193+
self.assertEqual(cat_qweight2.scale, ref_scale)
194+
195+
@parametrize("config", [FBGEMM_CONFIG])
196+
def test_transpose(self, config):
197+
dtype = torch.bfloat16
198+
device = "cuda"
199+
# weight: (256, 128)
200+
linear1 = torch.nn.Linear(128, 256, dtype=dtype, device=device)
201+
quantize_(linear1, config)
202+
linear1.weight = torch.nn.Parameter(linear1.weight.transpose(0, 1).contiguous())
203+
linear1.bias = torch.nn.Parameter(torch.randn(128, dtype=dtype, device=device))
204+
self.assertTrue(linear1.weight.shape, (128, 256))
205+
206+
input = torch.randn(32, 256, dtype=dtype, device=device)
207+
# make sure it runs
208+
res = linear1(input)
209+
self.assertTrue(res.shape, (32, 128))
210+
211+
212+
instantiate_parametrized_tests(TestFbgemmFp8Tensor)
213+
149214

150215
if __name__ == "__main__":
151216
run_tests()

test/dtypes/test_fbgemm_int4.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ def setUp(self):
3939
weight_dtype=torch.int4,
4040
output_dtype=torch.bfloat16,
4141
block_size=[1, 1, 128],
42-
transpose_input=True,
4342
)
4443
self.GPU_DEVICES = ["cuda"] if torch.cuda.is_available() else []
4544

@@ -134,6 +133,7 @@ def forward(self, x):
134133
weight = torch.randn(10, 128, 256, dtype=dtype, device=device)
135134
m = M(weight).eval()
136135
original = m(input)
136+
m.weight = torch.nn.Parameter(m.weight.transpose(1, 2).contiguous())
137137
quantize_(m, self.bmm_config, filter_fn=lambda x, fqn: True)
138138
quantized = m(input)
139139
self.assertTrue(compute_error(original, quantized) > 18)
@@ -152,6 +152,53 @@ def test_to_device(self):
152152
quantize_(linear, self.config)
153153
linear.to(device)
154154

155+
def test_cat(self):
156+
dtype = torch.bfloat16
157+
device = "cuda"
158+
# weight: (256, 128)
159+
linear1 = torch.nn.Linear(128, 256, dtype=dtype)
160+
# weight: (256, 128)
161+
linear2 = torch.nn.Linear(128, 256, dtype=dtype)
162+
163+
cat_weight1 = torch.cat([linear1.weight, linear2.weight], dim=0)
164+
cat_weight2 = torch.cat([linear1.weight, linear2.weight], dim=1)
165+
dummy1 = torch.nn.Linear(128, 512, bias=False, dtype=dtype, device=device)
166+
dummy2 = torch.nn.Linear(256, 256, bias=False, dtype=dtype, device=device)
167+
168+
dummy1.weight = torch.nn.Parameter(cat_weight1)
169+
dummy2.weight = torch.nn.Parameter(cat_weight2)
170+
quantize_(dummy1, self.config)
171+
quantize_(dummy2, self.config)
172+
173+
quantize_(linear1, self.config)
174+
quantize_(linear2, self.config)
175+
176+
cat_qweight1 = torch.cat([linear1.weight, linear2.weight], dim=0)
177+
self.assertTrue(cat_qweight1.shape, (512, 128))
178+
self.assertEqual(dummy1.weight.packed_weight, cat_qweight1.packed_weight)
179+
self.assertEqual(dummy1.weight.scale, cat_qweight1.scale)
180+
self.assertEqual(dummy1.weight.zero_point, cat_qweight1.zero_point)
181+
182+
cat_qweight2 = torch.cat([linear1.weight, linear2.weight], dim=1)
183+
self.assertTrue(cat_qweight2.shape, (256, 256))
184+
self.assertEqual(dummy2.weight.packed_weight, cat_qweight2.packed_weight)
185+
self.assertEqual(dummy2.weight.scale, cat_qweight2.scale)
186+
self.assertEqual(dummy2.weight.zero_point, cat_qweight2.zero_point)
187+
188+
def test_transpose(self):
189+
# weight: (256, 128)
190+
linear1 = torch.nn.Linear(128, 256, dtype=torch.bfloat16, device="cuda")
191+
quantize_(linear1, self.config)
192+
linear1.weight = torch.nn.Parameter(linear1.weight.transpose(0, 1).contiguous())
193+
# transpose again to return to the original state
194+
linear1.weight = torch.nn.Parameter(linear1.weight.transpose(0, 1).contiguous())
195+
self.assertTrue(linear1.weight.shape, (256, 128))
196+
197+
input = torch.randn(32, 128, dtype=torch.bfloat16, device="cuda")
198+
# make sure it runs
199+
res = linear1(input)
200+
self.assertTrue(res.shape, (32, 256))
201+
155202

156203
if __name__ == "__main__":
157204
run_tests()

0 commit comments

Comments
 (0)