|
| 1 | +# Copyright 2025 Arm Limited and/or its affiliates. |
| 2 | +# |
| 3 | +# This source code is licensed under the BSD-style license found in the |
| 4 | +# LICENSE file in the root directory of this source tree. |
| 5 | +from typing import Any, cast, NamedTuple |
| 6 | + |
| 7 | +import torch |
| 8 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 9 | + |
| 10 | +exir_ops = cast(Any, exir_ops) |
| 11 | +from executorch.backends.arm.constants import PER_CHANNEL_QDQ_OPS, PER_TENSOR_QDQ_OPS |
| 12 | +from torch import Tensor |
| 13 | + |
| 14 | + |
| 15 | +class QuantArgs(NamedTuple): |
| 16 | + scale: list[float] | float |
| 17 | + zp: list[int] | int |
| 18 | + qmin: int |
| 19 | + qmax: int |
| 20 | + dtype: torch.dtype |
| 21 | + axis: int = 0 |
| 22 | + per_channel: bool = False |
| 23 | + |
| 24 | + def quantize_value(self, x: torch.Tensor | float) -> Tensor: |
| 25 | + """Quantizes the input tensor or value to a quantized tensor. If the input is |
| 26 | + not a tensor, it is converted to a tensor first. If self.per_channel is True, |
| 27 | + the quantization is done per channel, otherwise it is done per tensor. |
| 28 | + """ |
| 29 | + if not isinstance(x, torch.Tensor): |
| 30 | + x = torch.Tensor([x]) |
| 31 | + x = x.to(torch.float32) |
| 32 | + if self.per_channel: |
| 33 | + q_op = exir_ops.edge.quantized_decomposed.quantize_per_channel.default |
| 34 | + args = ( |
| 35 | + x, |
| 36 | + torch.tensor(self.scale), |
| 37 | + torch.tensor(self.zp), |
| 38 | + self.axis, |
| 39 | + self.qmin, |
| 40 | + self.qmax, |
| 41 | + self.dtype, |
| 42 | + ) |
| 43 | + else: |
| 44 | + q_op = exir_ops.edge.quantized_decomposed.quantize_per_tensor.default |
| 45 | + args = (x, self.scale, self.zp, self.qmin, self.qmax, self.dtype) # type: ignore[assignment] |
| 46 | + return q_op(*args) |
| 47 | + |
| 48 | + def dequantize_value(self, qx: torch.Tensor) -> torch.Tensor: |
| 49 | + """Dequantizes the input tensor or value to a dequantized tensor If the input |
| 50 | + is not a tensor, it is converted to a tensor first. If self.per_channel is True, |
| 51 | + the dequantization is done per channel, otherwise it is done per tensor. |
| 52 | + """ |
| 53 | + if self.per_channel: |
| 54 | + dq_op = exir_ops.edge.quantized_decomposed.dequantize_per_channel.default |
| 55 | + args = ( |
| 56 | + qx, |
| 57 | + torch.tensor(self.scale), |
| 58 | + torch.tensor(self.zp), |
| 59 | + self.axis, |
| 60 | + self.qmin, |
| 61 | + self.qmax, |
| 62 | + self.dtype, |
| 63 | + ) |
| 64 | + else: |
| 65 | + dq_op = exir_ops.edge.quantized_decomposed.dequantize_per_tensor.default |
| 66 | + args = (qx, self.scale, self.zp, self.qmin, self.qmax, self.dtype) # type: ignore[assignment] |
| 67 | + return dq_op(*args) |
| 68 | + |
| 69 | + @classmethod |
| 70 | + def from_operator(cls, op, args): |
| 71 | + if op in PER_TENSOR_QDQ_OPS: |
| 72 | + return cls( |
| 73 | + scale=cast(float, args[1]), |
| 74 | + zp=cast(int, args[2]), |
| 75 | + qmin=cast(int, args[3]), |
| 76 | + qmax=cast(int, args[4]), |
| 77 | + dtype=cast(torch.dtype, args[5]), |
| 78 | + axis=0, |
| 79 | + per_channel=False, |
| 80 | + ) |
| 81 | + elif op in PER_CHANNEL_QDQ_OPS: |
| 82 | + return cls( |
| 83 | + scale=cast(list[float], args[1].tolist()), |
| 84 | + zp=cast(list[int], args[2].tolist()), |
| 85 | + axis=cast(int, args[3]), |
| 86 | + qmin=cast(int, args[4]), |
| 87 | + qmax=cast(int, args[5]), |
| 88 | + dtype=cast(torch.dtype, args[6]), |
| 89 | + per_channel=True, |
| 90 | + ) |
| 91 | + else: |
| 92 | + # We're only handling per tensor and per channel quantization |
| 93 | + raise NotImplementedError(f"Unsupported quantization operation: {op}") |
| 94 | + |
| 95 | + def get_scale_per_tensor(self) -> float: |
| 96 | + if not isinstance(self.scale, float): |
| 97 | + raise TypeError( |
| 98 | + f"Expected scale {self.scale} to be a float but found scale of " |
| 99 | + f"type {type(self.scale)}" |
| 100 | + ) |
| 101 | + return self.scale |
| 102 | + |
| 103 | + def get_zp_per_tensor(self) -> int: |
| 104 | + if not isinstance(self.zp, int): |
| 105 | + raise TypeError( |
| 106 | + f"Expected zero point {self.zp} to be an int but found zp of " |
| 107 | + f"type {type(self.zp)}" |
| 108 | + ) |
| 109 | + return self.zp |
| 110 | + |
| 111 | + def get_scale_per_channel(self) -> list[float]: |
| 112 | + if not isinstance(self.scale, list): |
| 113 | + raise TypeError( |
| 114 | + f"Expected scale {self.scale} to be a list but found scale of " |
| 115 | + f"type {type(self.scale)}" |
| 116 | + ) |
| 117 | + return self.scale |
| 118 | + |
| 119 | + def get_zp_per_channel(self) -> list[int]: |
| 120 | + if not isinstance(self.zp, list): |
| 121 | + raise TypeError( |
| 122 | + f"Expected zero point {self.zp} to be a list but found zp of " |
| 123 | + f"type {type(self.zp)}" |
| 124 | + ) |
| 125 | + return self.zp |
0 commit comments