|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | + |
| 4 | +# This source code is licensed under the license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +# mypy: ignore-errors |
| 8 | +import unittest |
| 9 | +import torch |
| 10 | +from torch._export import capture_pre_autograd_graph |
| 11 | +from torch.ao.quantization.quantize_pt2e import ( |
| 12 | + prepare_pt2e, |
| 13 | + convert_pt2e, |
| 14 | +) |
| 15 | +from torch.ao.quantization.quantizer.xnnpack_quantizer import ( |
| 16 | + XNNPACKQuantizer, |
| 17 | + get_symmetric_quantization_config, |
| 18 | +) |
| 19 | + |
| 20 | +from torchao.quantization.quant_api import _replace_with_custom_fn_if_matches_filter |
| 21 | +from torchao.quantization.quant_api import apply_dynamic_quant |
| 22 | +from torchao.quantization.quant_api import ( |
| 23 | + Quantizer, |
| 24 | + TwoStepQuantizer, |
| 25 | +) |
| 26 | + |
| 27 | +def dynamic_quant(model, example_inputs): |
| 28 | + m = capture_pre_autograd_graph(model, example_inputs) |
| 29 | + quantizer = XNNPACKQuantizer().set_global(get_symmetric_quantization_config(is_dynamic=True)) |
| 30 | + m = prepare_pt2e(m, quantizer) |
| 31 | + m = convert_pt2e(m) |
| 32 | + return m |
| 33 | + |
| 34 | +def _apply_dynamic_quant(model): |
| 35 | + """ |
| 36 | + Applies dynamic symmetric per-token activation and per-channel weight |
| 37 | + quantization to all linear layers in the given model using |
| 38 | + module swaps. |
| 39 | + """ |
| 40 | + _replace_with_custom_fn_if_matches_filter( |
| 41 | + model, |
| 42 | + lambda linear_mod: dynamic_quant(linear_mod, (torch.randn(1, linear_mod.in_features))), |
| 43 | + lambda mod, fqn: isinstance(mod, torch.nn.Linear), |
| 44 | + ) |
| 45 | + return model |
| 46 | + |
| 47 | + |
| 48 | +def capture_and_prepare(model, example_inputs): |
| 49 | + m = capture_pre_autograd_graph(model, example_inputs) |
| 50 | + quantizer = XNNPACKQuantizer().set_global(get_symmetric_quantization_config(is_dynamic=True)) |
| 51 | + m = prepare_pt2e(m, quantizer) |
| 52 | + # TODO: we can run the weight observer in convert_pt2e so that user don't need to run this |
| 53 | + m(*example_inputs) |
| 54 | + return m |
| 55 | + |
| 56 | +class XNNPackDynamicQuantizer(TwoStepQuantizer): |
| 57 | + |
| 58 | + def prepare(self, model: torch.nn.Module) -> torch.nn.Module: |
| 59 | + _replace_with_custom_fn_if_matches_filter( |
| 60 | + model, |
| 61 | + lambda linear_mod: capture_and_prepare(linear_mod, (torch.randn(1, linear_mod.in_features))), |
| 62 | + lambda mod, fqn: isinstance(mod, torch.nn.Linear), |
| 63 | + ) |
| 64 | + return model |
| 65 | + |
| 66 | + def convert(self, model: torch.nn.Module) -> torch.nn.Module: |
| 67 | + _replace_with_custom_fn_if_matches_filter( |
| 68 | + model, |
| 69 | + lambda linear_mod: convert_pt2e(linear_mod), |
| 70 | + lambda mod, fqn: isinstance(mod, torch.fx.GraphModule), |
| 71 | + ) |
| 72 | + return model |
| 73 | + |
| 74 | +class TorchCompileDynamicQuantizer(Quantizer): |
| 75 | + def quantize(self, model: torch.nn.Module) -> torch.nn.Module: |
| 76 | + apply_dynamic_quant(model) |
| 77 | + return model |
| 78 | + |
| 79 | +class M(torch.nn.Module): |
| 80 | + def __init__(self): |
| 81 | + super().__init__() |
| 82 | + self.linear1 = torch.nn.Linear(5, 5).to(torch.float) |
| 83 | + self.linear2 = torch.nn.Linear(5, 5).to(torch.float) |
| 84 | + |
| 85 | + def forward(self, x): |
| 86 | + x = self.linear1(x) |
| 87 | + x = self.linear2(x) |
| 88 | + return x |
| 89 | + |
| 90 | +class TestQuantFlow(unittest.TestCase): |
| 91 | + def test_dynamic_quant_gpu_singleline(self): |
| 92 | + m = M().eval() |
| 93 | + m = _apply_dynamic_quant(m) |
| 94 | + example_inputs = (torch.randn(1, 5).to(dtype=torch.float32),) |
| 95 | + quantized = m(*example_inputs) |
| 96 | + # AssertionError: Expecting input to have dtype torch.float32, but got dtype: torch.float64 |
| 97 | + # While executing %choose_qparams_tensor_1 : [num_users=2] = call_function[target=torch.ops.quantized_decomposed.choose_qparams.tensor](args = (%arg0_3, -128, 127, 0.000244140625, torch.int8), kwargs = {}) |
| 98 | + # m = torch.compile(m, mode="max-autotune") |
| 99 | + # print(example_inputs[0].dtype) |
| 100 | + # compiled = m(*example_inputs) |
| 101 | + # torch.testing.assert_close(quantized, compiled, atol=0, rtol=0) |
| 102 | + |
| 103 | + @unittest.skip("skipping for now due to torch.compile error") |
| 104 | + def test_dynamic_quant_gpu_unified_api_unified_impl(self): |
| 105 | + quantizer = XNNPackDynamicQuantizer() |
| 106 | + m = M().eval() |
| 107 | + m = quantizer.prepare(m) |
| 108 | + m = quantizer.convert(m) |
| 109 | + example_inputs = (torch.randn(1, 5).to(dtype=torch.float32),) |
| 110 | + quantized = m(*example_inputs) |
| 111 | + # AssertionError: Expecting input to have dtype torch.float32, but got dtype: torch.float64 |
| 112 | + # While executing %choose_qparams_tensor_1 : [num_users=2] = call_function[target=torch.ops.quantized_decomposed.choose_qparams.tensor](args = (%arg0_3, -128, 127, 0.000244140625, torch.int8), kwargs = {}) |
| 113 | + m = torch.compile(m, mode="max-autotune") |
| 114 | + # print(example_inputs[0].dtype) |
| 115 | + compiled = m(*example_inputs) |
| 116 | + torch.testing.assert_close(quantized, compiled, atol=0, rtol=0) |
| 117 | + |
| 118 | + def test_dynamic_quant_gpu_unified_api_eager_mode_impl(self): |
| 119 | + quantizer = TorchCompileDynamicQuantizer() |
| 120 | + m = M().eval() |
| 121 | + m = quantizer.quantize(m) |
| 122 | + example_inputs = (torch.randn(1, 5).to(dtype=torch.float32),) |
| 123 | + quantized = m(*example_inputs) |
| 124 | + m = torch.compile(m, mode="max-autotune") |
| 125 | + compiled = m(*example_inputs) |
| 126 | + torch.testing.assert_close(quantized, compiled, atol=0, rtol=0) |
| 127 | + |
| 128 | + def test_gptq(self): |
| 129 | + # should be similar to TorchCompileDynamicQuantizer |
| 130 | + pass |
| 131 | + |
| 132 | +if __name__ == "__main__": |
| 133 | + unittest.main() |
0 commit comments