|
| 1 | +# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, |
| 10 | +# software distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +""" |
| 16 | +Set of helper objects that are used to modify |
| 17 | +the HuggingFace transformer models |
| 18 | +""" |
| 19 | + |
| 20 | +import torch |
| 21 | + |
| 22 | + |
| 23 | +__all__ = [ |
| 24 | + "QuantizableIdentity", |
| 25 | + "QuantizableMatMul", |
| 26 | + "QuantizableBatchMatmul", |
| 27 | + "QATMatMul", |
| 28 | + "QATLinear", |
| 29 | +] |
| 30 | + |
| 31 | + |
| 32 | +class QuantizableIdentity(torch.nn.Module): |
| 33 | + """ |
| 34 | + Identity model that is introduced to be used |
| 35 | + together with QuantizableMatMul to allow for |
| 36 | + SparseML quantization scheme |
| 37 | + """ |
| 38 | + |
| 39 | + def forward(self, x): |
| 40 | + return x |
| 41 | + |
| 42 | + |
| 43 | +class QuantizableMatMul(torch.nn.Module): |
| 44 | + """ |
| 45 | + Wrapper around torch.matmul with distinct inputs/output class |
| 46 | + instances that could be quantized through SparseML recipe |
| 47 | +
|
| 48 | + :param left_input_cls: class instance that is used to quantize the left input |
| 49 | + :param right_input_cls: class instance that is used to quantize the right input |
| 50 | + :param output_cls: class instance that is used to quantize the output (optional) |
| 51 | + :return: the output of the matrix multiplication |
| 52 | + """ |
| 53 | + |
| 54 | + def __init__(self, left_input_cls, right_input_cls, output_cls=None): |
| 55 | + super().__init__() |
| 56 | + self.left_input = left_input_cls() |
| 57 | + self.right_input = right_input_cls() |
| 58 | + self.output = output_cls() if output_cls is not None else None |
| 59 | + |
| 60 | + def forward(self, a: torch.Tensor, b: torch.Tensor): |
| 61 | + out = torch.matmul(self.left_input(a), self.right_input(b)) |
| 62 | + if self.output is not None: |
| 63 | + return self.output(out) |
| 64 | + return out |
| 65 | + |
| 66 | + |
| 67 | +class QuantizableBatchMatmul(QuantizableMatMul): |
| 68 | + """ |
| 69 | + Wrapper around torch.bmm with distinct inputs/output class |
| 70 | + instances that could be quantized through SparseML recipe |
| 71 | +
|
| 72 | + :param left_input_cls: class instance that is used to quantize the left input |
| 73 | + :param right_input_cls: class instance that is used to quantize the right input |
| 74 | + :param output_cls: class instance that is used to quantize the output (optional) |
| 75 | + :return: the output of the batch matrix multiplication |
| 76 | + """ |
| 77 | + |
| 78 | + def forward(self, a: torch.Tensor, b: torch.Tensor): |
| 79 | + out = torch.bmm(self.left_input(a), self.right_input(b)) |
| 80 | + if self.output is not None: |
| 81 | + return self.output(out) |
| 82 | + return out |
| 83 | + |
| 84 | + |
| 85 | +class QATMatMul(torch.nn.Module): |
| 86 | + """ |
| 87 | + Behaves like normal torch.matmul unless a SparseML QuantizationModifier |
| 88 | + is initialized (Quantization-Aware-Training is invoked) |
| 89 | + """ |
| 90 | + |
| 91 | + def __init__(self): |
| 92 | + super().__init__() |
| 93 | + |
| 94 | + self.wrap_qat = True |
| 95 | + self.qat_wrapper_kwargs = { |
| 96 | + "num_inputs": 2, |
| 97 | + "input_qconfigs": ["asymmetric", "symmetric"], |
| 98 | + } |
| 99 | + |
| 100 | + def forward(self, a: torch.Tensor, b: torch.Tensor): |
| 101 | + return torch.matmul(a, b) |
| 102 | + |
| 103 | + |
| 104 | +class QATLinear(torch.nn.Module): |
| 105 | + """ |
| 106 | + Behaves like normal torch.nn.Linear unless a SparseML QuantizationModifier |
| 107 | + is initialized (Quantization-Aware-Training is invoked) |
| 108 | + When initialized does not quantize inputs. Only weights are quantized |
| 109 | + (inputs may come quantized) |
| 110 | + """ |
| 111 | + |
| 112 | + def __init__(self, in_features, out_features): |
| 113 | + super().__init__() |
| 114 | + |
| 115 | + self.wrap_qat = True |
| 116 | + self.qat_wrapper_kwargs = { |
| 117 | + "num_inputs": 0, |
| 118 | + "num_outputs": 1, |
| 119 | + } |
| 120 | + |
| 121 | + self.linear = torch.nn.Linear(in_features, out_features) |
| 122 | + |
| 123 | + def forward(self, x: torch.Tensor): |
| 124 | + return self.linear(x) |
0 commit comments