|
| 1 | +import torch |
| 2 | +from torch.library import impl, Library, register_fake |
| 3 | +from executorch.exir.dialects._ops import ( |
| 4 | + ops as exir_ops, |
| 5 | +) # To provide the implementation of the operators |
| 6 | + |
| 7 | +# New operator library with a custom namespace to allow fusion etc. |
| 8 | +lib = Library("cortex_m", "DEF") |
| 9 | + |
| 10 | +### |
| 11 | +# dequantize_per_tensor |
| 12 | +### |
| 13 | + |
| 14 | +lib.define( |
| 15 | + "quantize_per_tensor(Tensor input, float scale, int zero_point, int quant_min, int quant_max, ScalarType dtype) -> (Tensor Z)" |
| 16 | +) |
| 17 | + |
| 18 | +lib.define( |
| 19 | + "quantize_per_tensor.out(Tensor input, float scale, int zero_point, int quant_min, int quant_max, ScalarType dtype, *, Tensor(a!) out) -> Tensor(a!)" |
| 20 | +) |
| 21 | + |
| 22 | +@register_fake("cortex_m::quantize_per_tensor") |
| 23 | +def quantize_per_tensor_meta( |
| 24 | + input: torch.Tensor, |
| 25 | + scale: float, |
| 26 | + zero_point: int, |
| 27 | + quant_min: int, |
| 28 | + quant_max: int, |
| 29 | + dtype: torch.dtype, |
| 30 | +) -> torch.Tensor: |
| 31 | + return torch.empty_like(input, dtype=dtype) |
| 32 | + |
| 33 | + |
| 34 | +@impl(lib, "quantize_per_tensor", "CompositeExplicitAutograd") |
| 35 | +def quantize_per_tensor_impl( |
| 36 | + input: torch.Tensor, |
| 37 | + scale: float, |
| 38 | + zero_point: int, |
| 39 | + quant_min: int, |
| 40 | + quant_max: int, |
| 41 | + dtype: torch.dtype, |
| 42 | +) -> torch.Tensor: |
| 43 | + """ |
| 44 | + The implementation of the quantize_per_tensor operator is the same as the |
| 45 | + quantize_per_tensor operator in the edge dialect. |
| 46 | + """ |
| 47 | + return exir_ops.edge.quantized_decomposed.quantize_per_tensor.default( |
| 48 | + input, scale, zero_point, quant_min, quant_max, dtype |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +### |
| 53 | +# dequantize_per_tensor |
| 54 | +### |
| 55 | + |
| 56 | +lib.define( |
| 57 | + "dequantize_per_tensor(Tensor input, float scale, int zero_point, int quant_min, int quant_max, ScalarType dtype) -> (Tensor Z)" |
| 58 | +) |
| 59 | +lib.define( |
| 60 | + "dequantize_per_tensor.out(Tensor input, float scale, int zero_point, int quant_min, int quant_max, ScalarType dtype, *, Tensor(a!) out) -> Tensor(a!)" |
| 61 | +) |
| 62 | + |
| 63 | +@register_fake("cortex_m::dequantize_per_tensor") |
| 64 | +def dequantize_per_tensor_meta( |
| 65 | + input: torch.Tensor, |
| 66 | + scale: float, |
| 67 | + zero_point: int, |
| 68 | + quant_min: int, |
| 69 | + quant_max: int, |
| 70 | + dtype: torch.dtype, |
| 71 | +) -> torch.Tensor: |
| 72 | + return torch.empty_like(input, dtype=torch.float) |
| 73 | + |
| 74 | + |
| 75 | +@impl(lib, "dequantize_per_tensor", "CompositeExplicitAutograd") |
| 76 | +def dequantize_per_tensor_impl( |
| 77 | + input: torch.Tensor, |
| 78 | + scale: float, |
| 79 | + zero_point: int, |
| 80 | + quant_min: int, |
| 81 | + quant_max: int, |
| 82 | + dtype: torch.dtype, |
| 83 | +) -> torch.Tensor: |
| 84 | + """ |
| 85 | + The implementation of the dequantize_per_tensor operator is the same as the |
| 86 | + dequantize_per_tensor operator in the edge dialect. |
| 87 | + """ |
| 88 | + return exir_ops.edge.quantized_decomposed.dequantize_per_tensor.default( |
| 89 | + input, scale, zero_point, quant_min, quant_max, dtype |
| 90 | + ) |
0 commit comments