Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ONNX] Initial work to import pre-quantized ONNX Models #7802

Merged
merged 3 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions python/tvm/relay/frontend/onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from .. import expr as _expr
from .. import function as _function
from .. import op as _op
from .. import qnn as _qnn
from .. import vision as _vision
from .. import loops as _loops
from .. import ty as _ty
Expand Down Expand Up @@ -2782,6 +2783,60 @@ def _impl_v1(cls, inputs, attr, params):
return cls._op_dispatch(operator, inputs, attr, params)


class QuantizeLinear(OnnxOpConverter):
"""Operator converter for QuantizeLinear."""

@classmethod
def _impl_v10(cls, inputs, attr, params):
data, scale, zp = inputs
out_dtype = infer_type(zp).checked_type.dtype
return _qnn.op.quantize(data, scale, _op.cast(zp, "int32"), 0, out_dtype)

@classmethod
def _impl_v13(cls, inputs, attr, params):
data, scale, zp = inputs
out_dtype = infer_type(zp).checked_type.dtype
axis = attr.get("axis", 1)
return _qnn.op.quantize(data, scale, _op.cast(zp, "int32"), axis, out_dtype)


class DequantizeLinear(OnnxOpConverter):
"""Operator converter for QuantizeLinear."""

@classmethod
def _impl_v10(cls, inputs, attr, params):
data, scale, zp = inputs
return _qnn.op.dequantize(data, scale, _op.cast(zp, "int32"), 0)

@classmethod
def _impl_v13(cls, inputs, attr, params):
data, scale, zp = inputs
axis = attr.get("axis", 1)
return _qnn.op.dequantize(data, scale, _op.cast(zp, "int32"), axis)


class DynamicQuantizeLinear(OnnxOpConverter):
"""Operator converter for QuantizeLinear."""

@classmethod
def _impl_v11(cls, inputs, attr, params):
"""This op is deprecated an only supports uint8"""
data = inputs[0]
data_dtype = infer_type(data).checked_type.dtype
zero = _op.const(0, dtype=data_dtype)
maximum = _op.maximum(zero, _op.max(data))
minimum = _op.minimum(zero, _op.min(data))
scale = (maximum - minimum) / _op.const(255, dtype=data_dtype)
zp = zero - _op.min(data) / scale
zp = _op.cast(_op.round(_op.clip(zp, 0, 255)), "uint8")
return _expr.TupleWrapper(
_expr.Tuple(
[_qnn.op.quantize(data, scale, _op.cast(zp, "int32"), 0, "uint8"), scale, zp]
),
size=3,
)


# compatible operators that do NOT require any conversion.
_identity_list = []

Expand Down Expand Up @@ -2947,6 +3002,10 @@ def _get_convert_map(opset):
"If": If.get_converter(opset),
# Torch ATen Dispatcher.
"ATen": ATen.get_converter(opset),
# Quantization
"QuantizeLinear": QuantizeLinear.get_converter(opset),
"DequantizeLinear": DequantizeLinear.get_converter(opset),
"DynamicQuantizeLinear": DynamicQuantizeLinear.get_converter(opset),
}


Expand Down
8 changes: 0 additions & 8 deletions tests/python/frontend/onnx/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -4165,15 +4165,8 @@ def verify_cumsum(indata, axis, exclusive=0, reverse=0, type="float32"):
"test_cumsum_2d_axis_0/",
"test_cumsum_2d_axis_1/",
"test_cumsum_2d_negative_axis/",
"test_dequantizelinear/",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we run these tests on CI?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we're running all of the pre-serialized node tests that ship with ONNX against CPU now, except what's skipped in this list. Working on reducing what we skip, and I'll start enabling GPU soon.

"test_det_2d/",
"test_det_nd/",
"test_dynamicquantizelinear/",
"test_dynamicquantizelinear_expanded/",
"test_dynamicquantizelinear_max_adjusted/",
"test_dynamicquantizelinear_max_adjusted_expanded/",
"test_dynamicquantizelinear_min_adjusted/",
"test_dynamicquantizelinear_min_adjusted_expanded/",
"test_eyelike_populate_off_main_diagonal/",
"test_eyelike_with_dtype/",
"test_eyelike_without_dtype/",
Expand Down Expand Up @@ -4201,7 +4194,6 @@ def verify_cumsum(indata, axis, exclusive=0, reverse=0, type="float32"):
"test_qlinearconv/",
"test_qlinearmatmul_2D/",
"test_qlinearmatmul_3D/",
"test_quantizelinear/",
"test_range_float_type_positive_delta_expanded/",
"test_range_int32_type_negative_delta_expanded/",
"test_resize_downsample_scales_cubic/",
Expand Down