Quantizes the input tensor or value to a quantized tensor. If the input is not a tensor, it is converted to a tensor first. If self.per_channel is True, the quantization is done per channel, otherwise it is done per tensor.
(self, x: torch.Tensor | float)
| 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. |
| 26 | |
| 27 | If the input is not a tensor, it is converted to a tensor first. If |
| 28 | self.per_channel is True, the quantization is done per channel, |
| 29 | otherwise it is done per tensor. |
| 30 | |
| 31 | """ |
| 32 | if not isinstance(x, torch.Tensor): |
| 33 | x = torch.Tensor([x]) |
| 34 | x = x.to(torch.float32) |
| 35 | if self.per_channel: |
| 36 | q_op = exir_ops.edge.quantized_decomposed.quantize_per_channel.default |
| 37 | args = ( |
| 38 | x, |
| 39 | torch.tensor(self.scale), |
| 40 | torch.tensor(self.zp), |
| 41 | self.axis, |
| 42 | self.qmin, |
| 43 | self.qmax, |
| 44 | self.dtype, |
| 45 | ) |
| 46 | else: |
| 47 | q_op = exir_ops.edge.quantized_decomposed.quantize_per_tensor.default |
| 48 | args = (x, self.scale, self.zp, self.qmin, self.qmax, self.dtype) # type: ignore[assignment] |
| 49 | return q_op(*args) |
| 50 | |
| 51 | def dequantize_value(self, qx: torch.Tensor) -> torch.Tensor: |
| 52 | """Dequantizes the input tensor or value to a dequantized tensor If the |