|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# This source code is licensed under the license found in the |
| 4 | +# LICENSE file in the root directory of this source tree. |
| 5 | + |
| 6 | +import unittest |
| 7 | + |
| 8 | +import torch |
| 9 | +from torch.testing._internal.common_utils import TestCase, run_tests |
| 10 | + |
| 11 | +from torchao.quantization import quantize_ |
| 12 | + |
| 13 | +try: |
| 14 | + import gemlite # noqa: F401 |
| 15 | + |
| 16 | + has_gemlite = True |
| 17 | +except ModuleNotFoundError: |
| 18 | + has_gemlite = False |
| 19 | + |
| 20 | + |
| 21 | +@unittest.skipIf(not torch.cuda.is_available(), "Need CUDA available") |
| 22 | +@unittest.skipIf(not has_gemlite, "gemlite not available") |
| 23 | +class TestUIntxBitPackedTensor(TestCase): |
| 24 | + def _test_quantize_and_linear(self, bit_width, group_size, packing_bitwidth): |
| 25 | + """Helper: quantize a linear layer and verify forward pass produces valid output.""" |
| 26 | + from torchao.prototype.quantization.quant_api import UIntxWeightOnlyConfig |
| 27 | + |
| 28 | + in_features = 512 |
| 29 | + out_features = 256 |
| 30 | + model = torch.nn.Linear(in_features, out_features, bias=False).to( |
| 31 | + device="cuda", dtype=torch.float16 |
| 32 | + ) |
| 33 | + |
| 34 | + config = UIntxWeightOnlyConfig( |
| 35 | + group_size=group_size, |
| 36 | + bit_width=bit_width, |
| 37 | + packing_bitwidth=packing_bitwidth, |
| 38 | + ) |
| 39 | + quantize_(model, config) |
| 40 | + |
| 41 | + # Verify weight is now UIntxBitPackedTensor |
| 42 | + from torchao.prototype.quantization.uintx.uintx_bit_packed_tensor import ( |
| 43 | + UIntxBitPackedTensor, |
| 44 | + ) |
| 45 | + |
| 46 | + self.assertIsInstance(model.weight, UIntxBitPackedTensor) |
| 47 | + |
| 48 | + # Verify forward pass works |
| 49 | + x = torch.randn(2, in_features, device="cuda", dtype=torch.float16) |
| 50 | + out = model(x) |
| 51 | + self.assertEqual(out.shape, (2, out_features)) |
| 52 | + self.assertFalse(torch.isnan(out).any()) |
| 53 | + self.assertFalse(torch.isinf(out).any()) |
| 54 | + |
| 55 | + def test_4bit_group64_pack32(self): |
| 56 | + self._test_quantize_and_linear(bit_width=4, group_size=64, packing_bitwidth=32) |
| 57 | + |
| 58 | + def test_4bit_group128_pack32(self): |
| 59 | + self._test_quantize_and_linear(bit_width=4, group_size=128, packing_bitwidth=32) |
| 60 | + |
| 61 | + def test_4bit_group64_pack8(self): |
| 62 | + self._test_quantize_and_linear(bit_width=4, group_size=64, packing_bitwidth=8) |
| 63 | + |
| 64 | + def test_8bit_perchannel_pack32(self): |
| 65 | + self._test_quantize_and_linear( |
| 66 | + bit_width=8, group_size=None, packing_bitwidth=32 |
| 67 | + ) |
| 68 | + |
| 69 | + def test_8bit_perchannel_pack8(self): |
| 70 | + self._test_quantize_and_linear(bit_width=8, group_size=None, packing_bitwidth=8) |
| 71 | + |
| 72 | + def _test_dynamic_quantize_and_linear( |
| 73 | + self, bit_width, group_size, packing_bitwidth |
| 74 | + ): |
| 75 | + """Helper: quantize with dynamic activation and verify forward pass.""" |
| 76 | + from torchao.prototype.quantization.quant_api import ( |
| 77 | + Int8DynamicActivationUIntxWeightConfig, |
| 78 | + ) |
| 79 | + |
| 80 | + in_features = 512 |
| 81 | + out_features = 256 |
| 82 | + model = torch.nn.Linear(in_features, out_features, bias=False).to( |
| 83 | + device="cuda", dtype=torch.float16 |
| 84 | + ) |
| 85 | + |
| 86 | + config = Int8DynamicActivationUIntxWeightConfig( |
| 87 | + group_size=group_size, |
| 88 | + bit_width=bit_width, |
| 89 | + packing_bitwidth=packing_bitwidth, |
| 90 | + ) |
| 91 | + quantize_(model, config) |
| 92 | + |
| 93 | + from torchao.prototype.quantization.uintx.uintx_bit_packed_tensor import ( |
| 94 | + UIntxBitPackedTensor, |
| 95 | + ) |
| 96 | + |
| 97 | + self.assertIsInstance(model.weight, UIntxBitPackedTensor) |
| 98 | + |
| 99 | + x = torch.randn(2, in_features, device="cuda", dtype=torch.float16) |
| 100 | + out = model(x) |
| 101 | + self.assertEqual(out.shape, (2, out_features)) |
| 102 | + self.assertFalse(torch.isnan(out).any()) |
| 103 | + self.assertFalse(torch.isinf(out).any()) |
| 104 | + |
| 105 | + def test_dynamic_4bit_group64_pack32(self): |
| 106 | + self._test_dynamic_quantize_and_linear( |
| 107 | + bit_width=4, group_size=64, packing_bitwidth=32 |
| 108 | + ) |
| 109 | + |
| 110 | + def test_dynamic_4bit_group128_pack32(self): |
| 111 | + self._test_dynamic_quantize_and_linear( |
| 112 | + bit_width=4, group_size=128, packing_bitwidth=32 |
| 113 | + ) |
| 114 | + |
| 115 | + def test_dynamic_4bit_group64_pack8(self): |
| 116 | + self._test_dynamic_quantize_and_linear( |
| 117 | + bit_width=4, group_size=64, packing_bitwidth=8 |
| 118 | + ) |
| 119 | + |
| 120 | + def test_dynamic_8bit_perchannel_pack32(self): |
| 121 | + self._test_dynamic_quantize_and_linear( |
| 122 | + bit_width=8, group_size=None, packing_bitwidth=32 |
| 123 | + ) |
| 124 | + |
| 125 | + def test_dynamic_8bit_perchannel_pack8(self): |
| 126 | + self._test_dynamic_quantize_and_linear( |
| 127 | + bit_width=8, group_size=None, packing_bitwidth=8 |
| 128 | + ) |
| 129 | + |
| 130 | + def test_slice_dim0(self): |
| 131 | + """Test narrow/slice on dim 0 (out_features) for tensor parallelism.""" |
| 132 | + from torchao.prototype.quantization.quant_api import UIntxWeightOnlyConfig |
| 133 | + |
| 134 | + model = torch.nn.Linear(512, 256, bias=False).to( |
| 135 | + device="cuda", dtype=torch.float16 |
| 136 | + ) |
| 137 | + quantize_( |
| 138 | + model, |
| 139 | + UIntxWeightOnlyConfig(group_size=64, bit_width=4, packing_bitwidth=32), |
| 140 | + ) |
| 141 | + |
| 142 | + weight = model.weight |
| 143 | + sliced = weight.narrow(0, 0, 64) |
| 144 | + self.assertEqual(sliced.shape[0], 64) |
| 145 | + |
| 146 | + # Verify internal tensors match direct slicing |
| 147 | + # Data is stored transposed (K x N), so logical dim 0 -> data dim 1 |
| 148 | + self.assertEqual( |
| 149 | + sliced.packed_weight, |
| 150 | + weight.packed_weight.narrow(1, 0, 64), |
| 151 | + ) |
| 152 | + self.assertEqual( |
| 153 | + sliced.scale, |
| 154 | + weight.scale.narrow(1, 0, 64), |
| 155 | + ) |
| 156 | + |
| 157 | + def test_slice_dim1(self): |
| 158 | + """Test narrow/slice on dim 1 (in_features) for tensor parallelism.""" |
| 159 | + from torchao.prototype.quantization.quant_api import UIntxWeightOnlyConfig |
| 160 | + |
| 161 | + model = torch.nn.Linear(512, 256, bias=False).to( |
| 162 | + device="cuda", dtype=torch.float16 |
| 163 | + ) |
| 164 | + quantize_( |
| 165 | + model, |
| 166 | + UIntxWeightOnlyConfig(group_size=64, bit_width=4, packing_bitwidth=32), |
| 167 | + ) |
| 168 | + |
| 169 | + weight = model.weight |
| 170 | + sliced = weight.narrow(1, 0, 128) |
| 171 | + self.assertEqual(sliced.shape[1], 128) |
| 172 | + |
| 173 | + # Verify internal tensors match direct slicing |
| 174 | + # Data is stored transposed (K x N), so logical dim 1 -> data dim 0 |
| 175 | + # packed_weight dim 0 is packed by elements_per_sample |
| 176 | + eps = weight.gemlite_kwargs["elements_per_sample"] |
| 177 | + self.assertEqual( |
| 178 | + sliced.packed_weight, |
| 179 | + weight.packed_weight.narrow(0, 0, 128 // eps), |
| 180 | + ) |
| 181 | + # scale dim 0 corresponds to groups along in_features |
| 182 | + scale_ratio = 128 // 64 # in_features_slice / group_size |
| 183 | + self.assertEqual( |
| 184 | + sliced.scale, |
| 185 | + weight.scale.narrow(0, 0, scale_ratio), |
| 186 | + ) |
| 187 | + |
| 188 | + def test_non_standard_shapes(self): |
| 189 | + """Test shapes not divisible by 128 but divisible by 32 (gemlite requirement).""" |
| 190 | + from torchao.prototype.quantization.quant_api import UIntxWeightOnlyConfig |
| 191 | + |
| 192 | + # gemlite requires in_features divisible by 32 or group_size |
| 193 | + model = torch.nn.Linear(1024, 1025, bias=False).to( |
| 194 | + device="cuda", dtype=torch.float16 |
| 195 | + ) |
| 196 | + config = UIntxWeightOnlyConfig( |
| 197 | + group_size=None, bit_width=4, packing_bitwidth=32 |
| 198 | + ) |
| 199 | + quantize_(model, config) |
| 200 | + |
| 201 | + x = torch.randn(1, 1024, device="cuda", dtype=torch.float16) |
| 202 | + out = model(x) |
| 203 | + self.assertEqual(out.shape, (1, 1025)) |
| 204 | + |
| 205 | + |
| 206 | +if __name__ == "__main__": |
| 207 | + run_tests() |
0 commit comments