|
| 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_4bit_dynamic_activation(self): |
| 73 | + """Test dynamic activation quantization via Int8DynamicActivationUIntxWeightConfig.""" |
| 74 | + from torchao.prototype.quantization.quant_api import ( |
| 75 | + Int8DynamicActivationUIntxWeightConfig, |
| 76 | + ) |
| 77 | + |
| 78 | + model = torch.nn.Linear(512, 256, bias=False).to( |
| 79 | + device="cuda", dtype=torch.float16 |
| 80 | + ) |
| 81 | + config = Int8DynamicActivationUIntxWeightConfig( |
| 82 | + group_size=64, bit_width=4, packing_bitwidth=32 |
| 83 | + ) |
| 84 | + quantize_(model, config) |
| 85 | + |
| 86 | + from torchao.prototype.quantization.uintx.uintx_bit_packed_tensor import ( |
| 87 | + UIntxBitPackedTensor, |
| 88 | + ) |
| 89 | + |
| 90 | + self.assertIsInstance(model.weight, UIntxBitPackedTensor) |
| 91 | + |
| 92 | + x = torch.randn(2, 512, device="cuda", dtype=torch.float16) |
| 93 | + out = model(x) |
| 94 | + self.assertEqual(out.shape, (2, 256)) |
| 95 | + self.assertFalse(torch.isnan(out).any()) |
| 96 | + self.assertFalse(torch.isinf(out).any()) |
| 97 | + |
| 98 | + def test_slice_dim0(self): |
| 99 | + """Test narrow/slice on dim 0 (out_features) for tensor parallelism.""" |
| 100 | + from torchao.prototype.quantization.quant_api import UIntxWeightOnlyConfig |
| 101 | + |
| 102 | + model = torch.nn.Linear(512, 256, bias=False).to( |
| 103 | + device="cuda", dtype=torch.float16 |
| 104 | + ) |
| 105 | + quantize_( |
| 106 | + model, |
| 107 | + UIntxWeightOnlyConfig(group_size=64, bit_width=4, packing_bitwidth=32), |
| 108 | + ) |
| 109 | + |
| 110 | + sliced = model.weight.narrow(0, 0, 64) |
| 111 | + self.assertEqual(sliced.shape[0], 64) |
| 112 | + |
| 113 | + def test_slice_dim1(self): |
| 114 | + """Test narrow/slice on dim 1 (in_features) for tensor parallelism.""" |
| 115 | + from torchao.prototype.quantization.quant_api import UIntxWeightOnlyConfig |
| 116 | + |
| 117 | + model = torch.nn.Linear(512, 256, bias=False).to( |
| 118 | + device="cuda", dtype=torch.float16 |
| 119 | + ) |
| 120 | + quantize_( |
| 121 | + model, |
| 122 | + UIntxWeightOnlyConfig(group_size=64, bit_width=4, packing_bitwidth=32), |
| 123 | + ) |
| 124 | + |
| 125 | + sliced = model.weight.narrow(1, 0, 128) |
| 126 | + self.assertEqual(sliced.shape[1], 128) |
| 127 | + |
| 128 | + def test_non_standard_shapes(self): |
| 129 | + """Test shapes not divisible by 128 but divisible by 32 (gemlite requirement).""" |
| 130 | + from torchao.prototype.quantization.quant_api import UIntxWeightOnlyConfig |
| 131 | + |
| 132 | + # gemlite requires in_features divisible by 32 or group_size |
| 133 | + model = torch.nn.Linear(1024, 1025, bias=False).to( |
| 134 | + device="cuda", dtype=torch.float16 |
| 135 | + ) |
| 136 | + config = UIntxWeightOnlyConfig( |
| 137 | + group_size=None, bit_width=4, packing_bitwidth=32 |
| 138 | + ) |
| 139 | + quantize_(model, config) |
| 140 | + |
| 141 | + x = torch.randn(1, 1024, device="cuda", dtype=torch.float16) |
| 142 | + out = model(x) |
| 143 | + self.assertEqual(out.shape, (1, 1025)) |
| 144 | + |
| 145 | + def test_import_from_prototype_api(self): |
| 146 | + """Verify UIntxWeightOnlyConfig is available from the prototype API.""" |
| 147 | + from torchao.prototype.quantization.quant_api import ( |
| 148 | + UIntxWeightOnlyConfig, # noqa: F401 |
| 149 | + ) |
| 150 | + |
| 151 | + |
| 152 | +if __name__ == "__main__": |
| 153 | + run_tests() |
0 commit comments