-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[torchlib] Implement quantize/dequantize operators
- Loading branch information
1 parent
f8ee736
commit 31513ef
Showing
2 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
onnxscript/function_libs/torch_lib/ops/quantized_decomposed.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
# mypy: disable-error-code="misc,arg-type,type-arg,valid-type,assignment,return-value" | ||
# pylint: disable=unused-argument | ||
"""quantized_decomposed ops defined in https://github.com/pytorch/pytorch/blob/main/torch/ao/quantization/fx/_decomposed.py | ||
- No inplace operators. | ||
- All functions should not have the script() decorator. This is because | ||
we want to delay the compilation of the function. | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from onnxscript.function_libs.torch_lib.registration import torch_op | ||
from onnxscript.onnx_opset import opset18 as op | ||
from onnxscript.onnx_types import TensorType | ||
|
||
|
||
@torch_op( | ||
( | ||
"quantized_decomposed::quantize_per_tensor", | ||
"quantized_decomposed::quantize_per_tensor.tensor", | ||
"quantized_decomposed::quantize_per_tensor.tensor2", | ||
), | ||
trace_only=True, | ||
) | ||
def quantized_decomposed_quantize_per_tensor( | ||
input: TensorType, | ||
scale: float, | ||
zero_point: int, | ||
quant_min: int, | ||
quant_max: int, | ||
dtype: int, | ||
) -> TensorType: | ||
# TODO(justinchuby): Use quant_min and quant_max | ||
# TODO(justinchuby): Use dtype when we use opset 21 | ||
return op.QuantizeLinear(input, scale, zero_point) | ||
|
||
|
||
@torch_op( | ||
( | ||
"quantized_decomposed::dequantize_per_tensor", | ||
"quantized_decomposed::dequantize_per_tensor.tensor", | ||
"quantized_decomposed::dequantize_per_tensor.tensor2", | ||
), | ||
trace_only=True, | ||
) | ||
def quantized_decomposed_dequantize_per_tensor( | ||
input: TensorType, | ||
scale: float, | ||
zero_point: int, | ||
quant_min: int, | ||
quant_max: int, | ||
dtype: int, | ||
*, | ||
out_dtype: int | None = None, | ||
) -> TensorType: | ||
# TODO(justinchuby): Use quant_min and quant_max | ||
# TODO(justinchuby): Use dtype when we use opset 21 | ||
return op.DequantizeLinear(input, scale, zero_point) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters