|
| 1 | +import pytest |
| 2 | +from torchao.prototype.autoround.utils import is_auto_round_available |
| 3 | + |
| 4 | +if not is_auto_round_available(): |
| 5 | + pytest.skip("AutoRound is not available", allow_module_level=True) |
| 6 | + |
| 7 | +import torch |
| 8 | +from torch.testing._internal.common_utils import ( |
| 9 | + instantiate_parametrized_tests, |
| 10 | + parametrize, |
| 11 | + run_tests, |
| 12 | + TestCase, |
| 13 | +) |
| 14 | +from torchao import quantize_ |
| 15 | + |
| 16 | +from torchao.dtypes import AffineQuantizedTensor |
| 17 | +from torchao.prototype.autoround.core import ( |
| 18 | + apply_auto_round, |
| 19 | + prepare_model_for_applying_auto_round_, |
| 20 | +) |
| 21 | +from torchao.prototype.autoround.multi_tensor import MultiTensor |
| 22 | +from torchao.utils import TORCH_VERSION_AT_LEAST_2_5 |
| 23 | + |
| 24 | +_AVAILABLE_DEVICES = ["cpu"] + (["cuda"] if torch.cuda.is_available() else []) |
| 25 | + |
| 26 | + |
| 27 | +# Copied from https://github.com/pytorch/ao/pull/721 |
| 28 | +class TwoLinear(torch.nn.Module): |
| 29 | + def __init__(self, in_features=64, out_features=128): |
| 30 | + super().__init__() |
| 31 | + self.linear1 = torch.nn.Linear(in_features, out_features) |
| 32 | + self.linear2 = torch.nn.Linear(in_features, out_features) |
| 33 | + |
| 34 | + def forward(self, x, y): |
| 35 | + x = self.linear1(x) |
| 36 | + y = self.linear2(y) |
| 37 | + return x + y |
| 38 | + |
| 39 | + |
| 40 | +class M(torch.nn.Module): |
| 41 | + def __init__(self): |
| 42 | + super().__init__() |
| 43 | + self.two_linear1 = TwoLinear() |
| 44 | + self.two_linear2 = TwoLinear(128, 256) |
| 45 | + |
| 46 | + def forward(self, x, y): |
| 47 | + x1 = self.two_linear1(x, y) |
| 48 | + x2 = self.two_linear2(x1, x1) |
| 49 | + return x2 |
| 50 | + |
| 51 | + |
| 52 | +def _is_two_linear(mod, fqn): |
| 53 | + return isinstance(mod, TwoLinear) |
| 54 | + |
| 55 | + |
| 56 | +class ModelWithInplaceOp(torch.nn.Module): |
| 57 | + def __init__(self, DIM=128): |
| 58 | + super().__init__() |
| 59 | + self.lin = torch.nn.Linear(DIM, DIM) |
| 60 | + self.register_buffer("other", torch.zeros(DIM, DIM)) |
| 61 | + |
| 62 | + def forward(self, x, idx): |
| 63 | + x = x + self.lin(x) |
| 64 | + # update buffer |
| 65 | + self.other[idx] = x |
| 66 | + return x |
| 67 | + |
| 68 | + |
| 69 | +class M2(torch.nn.Module): |
| 70 | + def __init__(self, DIM=128): |
| 71 | + super().__init__() |
| 72 | + self.m1 = ModelWithInplaceOp(DIM) |
| 73 | + self.m2 = ModelWithInplaceOp(DIM) |
| 74 | + |
| 75 | + def forward(self, x, idx): |
| 76 | + x = self.m1(x, idx) |
| 77 | + x = self.m2(x, idx) |
| 78 | + return x |
| 79 | + |
| 80 | + |
| 81 | +def _check_params_and_buffers_type(module, check_fun): |
| 82 | + return [check_fun(p) for p in module.parameters()] + [ |
| 83 | + check_fun(b) for b in module.buffers() |
| 84 | + ] |
| 85 | + |
| 86 | + |
| 87 | +class TestAutoRound(TestCase): |
| 88 | + |
| 89 | + @pytest.mark.skip(not TORCH_VERSION_AT_LEAST_2_5, "Requires torch 2.5 or later") |
| 90 | + @parametrize("device", _AVAILABLE_DEVICES) |
| 91 | + @torch.no_grad() |
| 92 | + def test_auto_round(self, device: str): |
| 93 | + example_inputs = ( |
| 94 | + torch.randn(32, 64).to(device), |
| 95 | + torch.randn(32, 64).to(device), |
| 96 | + ) |
| 97 | + m = M().eval().to(device) |
| 98 | + before_quant = m(*example_inputs) |
| 99 | + prepare_model_for_applying_auto_round_( |
| 100 | + m, |
| 101 | + is_target_module=_is_two_linear, |
| 102 | + bits=7, |
| 103 | + group_size=32, |
| 104 | + iters=20, |
| 105 | + device=device, |
| 106 | + ) |
| 107 | + assert all( |
| 108 | + _check_params_and_buffers_type(m, lambda x: isinstance(x, MultiTensor)) |
| 109 | + ), "Expected all parameters and buffers to be `MultiTensor`." |
| 110 | + input1 = [] |
| 111 | + input2 = [] |
| 112 | + for _ in range(10): |
| 113 | + input1.append(torch.randn(32, 64).to(device)) |
| 114 | + input2.append(torch.randn(32, 64).to(device)) |
| 115 | + |
| 116 | + mt_input1 = MultiTensor(input1) |
| 117 | + mt_input2 = MultiTensor(input2) |
| 118 | + out = m(mt_input1, mt_input2) |
| 119 | + assert isinstance(out, MultiTensor), f"Expected MultiTensor, got {type(out)}" |
| 120 | + assert all( |
| 121 | + _check_params_and_buffers_type(m, lambda x: not isinstance(x, MultiTensor)) |
| 122 | + ), "Expected all parameters and buffers have been converted back to tensor." |
| 123 | + quantize_(m, apply_auto_round(), _is_two_linear, device=device) |
| 124 | + for l in m.modules(): |
| 125 | + if isinstance(l, torch.nn.Linear): |
| 126 | + assert isinstance(l.weight, AffineQuantizedTensor) |
| 127 | + after_quant = m(*example_inputs) |
| 128 | + assert after_quant is not None, "Quantized model forward pass failed" |
| 129 | + |
| 130 | + @pytest.mark.skip(not TORCH_VERSION_AT_LEAST_2_5, "Requires torch 2.5 or later") |
| 131 | + @parametrize("device", _AVAILABLE_DEVICES) |
| 132 | + @torch.no_grad() |
| 133 | + def test_wrap_model_with_multi_tensor(self, device: str): |
| 134 | + |
| 135 | + _is_model_with_inplace_op = lambda mod, fqn: isinstance(mod, ModelWithInplaceOp) |
| 136 | + |
| 137 | + DIM = 128 |
| 138 | + m = M2(DIM).eval().to(device) |
| 139 | + prepare_model_for_applying_auto_round_( |
| 140 | + m, |
| 141 | + is_target_module=_is_model_with_inplace_op, |
| 142 | + bits=7, |
| 143 | + group_size=32, |
| 144 | + iters=20, |
| 145 | + device=device, |
| 146 | + ) |
| 147 | + assert all( |
| 148 | + _check_params_and_buffers_type(m, lambda x: isinstance(x, MultiTensor)) |
| 149 | + ), "Expected all parameters and buffers to be `MultiTensor`." |
| 150 | + input1 = [] |
| 151 | + input2 = [] |
| 152 | + for _ in range(2): |
| 153 | + input1.append(torch.randn(DIM, DIM).to(device)) |
| 154 | + input2.append(torch.randint(0, DIM, (DIM,), dtype=torch.long).to(device)) |
| 155 | + |
| 156 | + mt_input1 = MultiTensor(input1) |
| 157 | + mt_input2 = MultiTensor(input2) |
| 158 | + out = m(mt_input1, mt_input2) |
| 159 | + assert isinstance(out, MultiTensor), f"Expected MultiTensor, got {type(out)}" |
| 160 | + assert all( |
| 161 | + _check_params_and_buffers_type(m, lambda x: not isinstance(x, MultiTensor)) |
| 162 | + ), "Expected all parameters and buffers have been converted back to tensor." |
| 163 | + quantize_(m, apply_auto_round(), _is_model_with_inplace_op, device=device) |
| 164 | + for l in m.modules(): |
| 165 | + if isinstance(l, torch.nn.Linear): |
| 166 | + assert isinstance(l.weight, AffineQuantizedTensor) |
| 167 | + after_quant = m(input1[0], input2[0]) |
| 168 | + assert after_quant is not None, "Quantized model forward pass failed" |
| 169 | + |
| 170 | + |
| 171 | +instantiate_parametrized_tests(TestAutoRound) |
| 172 | + |
| 173 | +if __name__ == "__main__": |
| 174 | + run_tests() |
0 commit comments