|
| 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 unittest |
| 8 | + |
| 9 | +import torch |
| 10 | +from torch.testing._internal.common_utils import ( |
| 11 | + TestCase, |
| 12 | + run_tests, |
| 13 | +) |
| 14 | + |
| 15 | +from torchao.quantization import ( |
| 16 | + IntxWeightOnlyConfig, |
| 17 | + quantize_, |
| 18 | +) |
| 19 | +from torchao.quantization.granularity import PerGroup |
| 20 | +from torchao.quantization.utils import compute_error |
| 21 | +from torchao.utils import ( |
| 22 | + TORCH_VERSION_AT_LEAST_2_8, |
| 23 | +) |
| 24 | + |
| 25 | + |
| 26 | +@unittest.skipIf(not TORCH_VERSION_AT_LEAST_2_8, "Need pytorch 2.8+") |
| 27 | +class TestIntxUnpackedTensor(TestCase): |
| 28 | + def setUp(self): |
| 29 | + self.config = IntxWeightOnlyConfig( |
| 30 | + weight_dtype=torch.int4, |
| 31 | + granularity=PerGroup(32), |
| 32 | + VERSION=2, |
| 33 | + ) |
| 34 | + |
| 35 | + def test_linear(self): |
| 36 | + dtype = torch.bfloat16 |
| 37 | + device = "cpu" |
| 38 | + input = torch.randn(1, 128, dtype=dtype, device=device) |
| 39 | + linear = torch.nn.Linear(128, 256, dtype=dtype, device=device) |
| 40 | + original = linear(input) |
| 41 | + quantize_(linear, self.config) |
| 42 | + quantized = linear(input) |
| 43 | + error = compute_error(original, quantized) |
| 44 | + self.assertTrue(error > 20) |
| 45 | + |
| 46 | + def test_slice(self): |
| 47 | + dtype = torch.bfloat16 |
| 48 | + device = "cpu" |
| 49 | + dummy = torch.nn.Linear(256, 256, bias=False, dtype=dtype, device=device) |
| 50 | + |
| 51 | + dummy1 = torch.nn.Linear(256, 64, bias=False, dtype=dtype, device=device) |
| 52 | + dummy1.weight = torch.nn.Parameter( |
| 53 | + dummy.weight.narrow(0, 0, 64), requires_grad=False |
| 54 | + ) |
| 55 | + |
| 56 | + dummy2 = torch.nn.Linear(128, 256, dtype=dtype, device=device) |
| 57 | + dummy2.weight = torch.nn.Parameter( |
| 58 | + dummy.weight.narrow(1, 0, 128), requires_grad=False |
| 59 | + ) |
| 60 | + |
| 61 | + quantize_(dummy, self.config) |
| 62 | + weight1 = dummy.weight.narrow(0, 0, 64) |
| 63 | + weight2 = dummy.weight.narrow(1, 0, 128) |
| 64 | + |
| 65 | + self.assertEqual(weight1.int_data, dummy.weight.int_data.narrow(0, 0, 64)) |
| 66 | + self.assertEqual(weight1.scale, dummy.weight.scale.narrow(0, 0, 64)) |
| 67 | + |
| 68 | + self.assertEqual(weight2.int_data, dummy.weight.int_data.narrow(1, 0, 128)) |
| 69 | + self.assertEqual(weight2.scale, dummy.weight.scale.narrow(1, 0, 4)) |
| 70 | + |
| 71 | + # check for sliced weight, before and after float8 quantization |
| 72 | + # does not differ too much |
| 73 | + input = torch.randn(2, 256, dtype=dtype, device=device) |
| 74 | + res_ref = dummy1(input) |
| 75 | + dummy.weight = torch.nn.Parameter(weight1, requires_grad=False) |
| 76 | + res = dummy(input) |
| 77 | + assert compute_error(res, res_ref) > 20 |
| 78 | + |
| 79 | + input = torch.randn(2, 128, dtype=dtype, device=device) |
| 80 | + res_ref = dummy2(input) |
| 81 | + dummy.weight = torch.nn.Parameter(weight2, requires_grad=False) |
| 82 | + res = dummy(input) |
| 83 | + assert compute_error(res, res_ref) > 15 |
| 84 | + |
| 85 | + def test_slice_and_copy_(self): |
| 86 | + device = "cpu" |
| 87 | + l = torch.nn.Linear(1024, 1024).to(device).to(torch.bfloat16) |
| 88 | + l.weight = torch.nn.Parameter( |
| 89 | + torch.zeros(1024, 1024, dtype=torch.bfloat16, device=device) |
| 90 | + ) |
| 91 | + quantize_(l, self.config) |
| 92 | + param = l.weight |
| 93 | + param_data = param.data |
| 94 | + param_data = param_data.narrow(0, 0, 512) |
| 95 | + assert param.data.int_data.data_ptr() == param_data.int_data.data_ptr() |
| 96 | + assert param.data.scale.data_ptr() == param_data.scale.data_ptr() |
| 97 | + assert param.data.zero_point.data_ptr() == param_data.zero_point.data_ptr() |
| 98 | + orig_value = param.data.int_data[0][0].item() |
| 99 | + |
| 100 | + # dummy_l has random input (shouldn't be 0) |
| 101 | + dummy_l = torch.nn.Linear(1024, 1024).to(device).to(torch.bfloat16) |
| 102 | + quantize_(dummy_l, self.config) |
| 103 | + quantized = dummy_l.weight |
| 104 | + quantized = quantized.narrow(0, 0, 512) |
| 105 | + |
| 106 | + param_data.copy_(quantized) |
| 107 | + |
| 108 | + # making sure param.data is updated |
| 109 | + assert param.data.int_data[0][0] != orig_value |
| 110 | + |
| 111 | + def test_to_dtype(self): |
| 112 | + activations_bf16 = torch.randn(1, 128, dtype=torch.bfloat16) |
| 113 | + activations_fp32 = torch.randn(1, 128, dtype=torch.float32) |
| 114 | + activations_fp16 = torch.randn(1, 128, dtype=torch.float16) |
| 115 | + |
| 116 | + linear = torch.nn.Linear(128, 256) |
| 117 | + quantize_(linear, self.config) |
| 118 | + |
| 119 | + linear.to(dtype=torch.float16) |
| 120 | + linear(activations_fp16) |
| 121 | + |
| 122 | + linear.to(dtype=torch.float32) |
| 123 | + linear(activations_fp32) |
| 124 | + |
| 125 | + linear.to(dtype=torch.bfloat16) |
| 126 | + linear(activations_bf16) |
| 127 | + |
| 128 | + def test_export(self): |
| 129 | + linear = torch.nn.Linear(128, 256) |
| 130 | + quantize_(linear, self.config) |
| 131 | + ep = torch.export.export(linear, (torch.randn(1, 128),)) |
| 132 | + assert "torch.ops.torchao.dequantize_affine.default" in ep.graph_module.code |
| 133 | + |
| 134 | + |
| 135 | +if __name__ == "__main__": |
| 136 | + run_tests() |
0 commit comments