|  | 
|  | 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. | 
|  | 2 | +# All rights reserved. | 
|  | 3 | +# | 
|  | 4 | +# This source code is licensed under the BSD 3-Clause license found in the | 
|  | 5 | +# LICENSE file in the root directory of this source tree. | 
|  | 6 | + | 
|  | 7 | +import tempfile | 
|  | 8 | +import unittest | 
|  | 9 | + | 
|  | 10 | +import torch | 
|  | 11 | +from torch.testing._internal.common_utils import ( | 
|  | 12 | +    TestCase, | 
|  | 13 | +    instantiate_parametrized_tests, | 
|  | 14 | +    parametrize, | 
|  | 15 | +    run_tests, | 
|  | 16 | +) | 
|  | 17 | + | 
|  | 18 | +from torchao.quantization import Int4WeightOnlyConfig, quantize_ | 
|  | 19 | +from torchao.quantization.quantize_.common.packing_format import PackingFormat | 
|  | 20 | +from torchao.quantization.quantize_.workflows.int4.int4_tensor_core_tile_packed_tensor import ( | 
|  | 21 | +    Int4TensorCoreTilePackedTensor, | 
|  | 22 | +) | 
|  | 23 | +from torchao.quantization.utils import compute_error | 
|  | 24 | +from torchao.utils import TORCH_VERSION_AT_LEAST_2_4 | 
|  | 25 | + | 
|  | 26 | +TENSOR_CORE_TILED_CONFIG = Int4WeightOnlyConfig( | 
|  | 27 | +    group_size=128, | 
|  | 28 | +    packing_format=PackingFormat.TENSOR_CORE_TILE_PACKED, | 
|  | 29 | +    version=2, | 
|  | 30 | +) | 
|  | 31 | + | 
|  | 32 | + | 
|  | 33 | +@unittest.skipIf(not TORCH_VERSION_AT_LEAST_2_4, "Need pytorch 2.4+") | 
|  | 34 | +@unittest.skipIf(not torch.cuda.is_available(), "Need CUDA available") | 
|  | 35 | +class TestInt4TensorCoreTilePackedTensor(TestCase): | 
|  | 36 | +    def setUp(self): | 
|  | 37 | +        self.GPU_DEVICES = ["cuda"] if torch.cuda.is_available() else [] | 
|  | 38 | + | 
|  | 39 | +    @parametrize("config", [TENSOR_CORE_TILED_CONFIG]) | 
|  | 40 | +    @parametrize( | 
|  | 41 | +        "sizes", | 
|  | 42 | +        [ | 
|  | 43 | +            ((128,), 256, 128), | 
|  | 44 | +            ((32, 128), 512, 128), | 
|  | 45 | +            ((2, 32, 128), 256, 128), | 
|  | 46 | +        ], | 
|  | 47 | +    ) | 
|  | 48 | +    def test_linear(self, config, sizes): | 
|  | 49 | +        dtype = torch.bfloat16 | 
|  | 50 | +        device = "cuda" | 
|  | 51 | + | 
|  | 52 | +        M, N, K = sizes | 
|  | 53 | +        input = torch.randn(*M, K, dtype=dtype, device=device) | 
|  | 54 | +        linear = torch.nn.Linear(K, N, dtype=dtype, device=device) | 
|  | 55 | + | 
|  | 56 | +        original = linear(input) | 
|  | 57 | +        quantize_(linear, config) | 
|  | 58 | +        quantized = linear(input) | 
|  | 59 | +        self.assertTrue(compute_error(original, quantized) > 1) | 
|  | 60 | + | 
|  | 61 | +        compiled_linear = torch.compile(linear) | 
|  | 62 | +        quantized_and_compiled = compiled_linear(input) | 
|  | 63 | +        self.assertTrue(compute_error(original, quantized_and_compiled) > 1) | 
|  | 64 | + | 
|  | 65 | +    def test_from_hp(self): | 
|  | 66 | +        """Test creating Int4TensorCoreTilePackedTensor from high precision tensor""" | 
|  | 67 | +        dtype = torch.bfloat16 | 
|  | 68 | +        device = "cuda" | 
|  | 69 | +        hp_tensor = torch.randn(256, 128, dtype=dtype, device=device) | 
|  | 70 | +        block_size = (1, 64) | 
|  | 71 | + | 
|  | 72 | +        tensor = Int4TensorCoreTilePackedTensor.from_hp(hp_tensor, block_size) | 
|  | 73 | + | 
|  | 74 | +        self.assertEqual(tensor.shape, hp_tensor.shape) | 
|  | 75 | +        self.assertEqual(tensor.block_size, block_size) | 
|  | 76 | +        self.assertEqual(tensor.device.type, device) | 
|  | 77 | +        self.assertEqual(tensor.dtype, dtype) | 
|  | 78 | + | 
|  | 79 | +    @parametrize("config", [TENSOR_CORE_TILED_CONFIG]) | 
|  | 80 | +    def test_to_device(self, config): | 
|  | 81 | +        for device in self.GPU_DEVICES: | 
|  | 82 | +            linear = torch.nn.Linear(128, 256, dtype=torch.bfloat16) | 
|  | 83 | +            quantize_(linear.cuda(), config) | 
|  | 84 | +            linear.to(device) | 
|  | 85 | + | 
|  | 86 | +            linear = torch.nn.Linear(128, 256, dtype=torch.bfloat16) | 
|  | 87 | +            quantize_(linear.cuda(), config) | 
|  | 88 | +            linear.to(device=device) | 
|  | 89 | + | 
|  | 90 | +    @parametrize("config", [TENSOR_CORE_TILED_CONFIG]) | 
|  | 91 | +    def test_module_path(self, config): | 
|  | 92 | +        linear = torch.nn.Linear(128, 256, dtype=torch.bfloat16) | 
|  | 93 | +        quantize_(linear.cuda(), config) | 
|  | 94 | +        self.assertEqual( | 
|  | 95 | +            str(type(linear.weight)), | 
|  | 96 | +            "<class 'torchao.quantization.Int4TensorCoreTilePackedTensor'>", | 
|  | 97 | +        ) | 
|  | 98 | + | 
|  | 99 | +    def test_serialization(self): | 
|  | 100 | +        """Test saving and loading the tensor directly and via state_dict""" | 
|  | 101 | +        dtype = torch.bfloat16 | 
|  | 102 | +        device = "cuda" | 
|  | 103 | +        hp_tensor = torch.randn(128, 256, dtype=dtype, device=device) | 
|  | 104 | +        block_size = (1, 64) | 
|  | 105 | + | 
|  | 106 | +        tensor = Int4TensorCoreTilePackedTensor.from_hp(hp_tensor, block_size) | 
|  | 107 | + | 
|  | 108 | +        # Test direct tensor serialization | 
|  | 109 | +        with tempfile.NamedTemporaryFile() as f: | 
|  | 110 | +            torch.save(tensor, f) | 
|  | 111 | +            f.seek(0) | 
|  | 112 | +            loaded_tensor = torch.load(f) | 
|  | 113 | + | 
|  | 114 | +            self.assertEqual(loaded_tensor.shape, tensor.shape) | 
|  | 115 | +            self.assertEqual(loaded_tensor.block_size, tensor.block_size) | 
|  | 116 | +            self.assertEqual( | 
|  | 117 | +                str(type(loaded_tensor)), | 
|  | 118 | +                "<class 'torchao.quantization.Int4TensorCoreTilePackedTensor'>", | 
|  | 119 | +            ) | 
|  | 120 | + | 
|  | 121 | +        # Test state_dict serialization | 
|  | 122 | +        linear = torch.nn.Linear(128, 256, dtype=torch.bfloat16) | 
|  | 123 | +        quantize_(linear.cuda(), TENSOR_CORE_TILED_CONFIG) | 
|  | 124 | + | 
|  | 125 | +        with tempfile.NamedTemporaryFile() as f: | 
|  | 126 | +            torch.save(linear.state_dict(), f) | 
|  | 127 | +            f.seek(0) | 
|  | 128 | +            state_dict = torch.load(f) | 
|  | 129 | +            self.assertEqual( | 
|  | 130 | +                str(type(state_dict["weight"])), | 
|  | 131 | +                "<class 'torchao.quantization.Int4TensorCoreTilePackedTensor'>", | 
|  | 132 | +            ) | 
|  | 133 | + | 
|  | 134 | +    @parametrize("group_size", [32, 64, 128]) | 
|  | 135 | +    def test_different_group_sizes(self, group_size): | 
|  | 136 | +        """Test with different group sizes""" | 
|  | 137 | +        dtype = torch.bfloat16 | 
|  | 138 | +        device = "cuda" | 
|  | 139 | +        hp_tensor = torch.randn(256, 512, dtype=dtype, device=device) | 
|  | 140 | +        block_size = (1, group_size) | 
|  | 141 | + | 
|  | 142 | +        tensor = Int4TensorCoreTilePackedTensor.from_hp(hp_tensor, block_size) | 
|  | 143 | + | 
|  | 144 | +        self.assertEqual(tensor.shape, hp_tensor.shape) | 
|  | 145 | +        self.assertEqual(tensor.block_size, block_size) | 
|  | 146 | + | 
|  | 147 | +    def test_error_conditions(self): | 
|  | 148 | +        """Test various error conditions""" | 
|  | 149 | +        dtype = torch.bfloat16 | 
|  | 150 | +        device = "cuda" | 
|  | 151 | +        hp_tensor = torch.randn(128, 256, dtype=dtype, device=device) | 
|  | 152 | + | 
|  | 153 | +        # Test invalid block_size length | 
|  | 154 | +        with self.assertRaises(AssertionError): | 
|  | 155 | +            Int4TensorCoreTilePackedTensor.from_hp( | 
|  | 156 | +                hp_tensor, (64,) | 
|  | 157 | +            )  # block_size length mismatch | 
|  | 158 | + | 
|  | 159 | +        # Test non-groupwise quantization | 
|  | 160 | +        with self.assertRaises(AssertionError): | 
|  | 161 | +            Int4TensorCoreTilePackedTensor.from_hp( | 
|  | 162 | +                hp_tensor, (2, 64) | 
|  | 163 | +            )  # first element should be 1 | 
|  | 164 | + | 
|  | 165 | + | 
|  | 166 | +instantiate_parametrized_tests(TestInt4TensorCoreTilePackedTensor) | 
|  | 167 | + | 
|  | 168 | + | 
|  | 169 | +if __name__ == "__main__": | 
|  | 170 | +    run_tests() | 
0 commit comments