[Relax][Frontend][ONNX] Support Modern QDQ opset attributes#19993
[Relax][Frontend][ONNX] Support Modern QDQ opset attributes#19993napronald wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request adds support for ONNX opset versions 19, 21, and 23 for the QuantizeLinear and DequantizeLinear operators in the Relax frontend, along with comprehensive unit tests. The feedback suggests reducing code duplication in both operators by having newer opset implementations (v21 and v23) delegate to older ones (v19 and v21) after performing their specific attribute checks.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| def _impl_v23(cls, bb, inputs, attr, params): | ||
| if attr.get("block_size", 0) != 0: | ||
| raise ValueError("QuantizeLinear blocked quantization is not supported yet.") | ||
| if attr.get("precision", 0) != 0: | ||
| raise ValueError( | ||
| "QuantizeLinear with a non-default precision attribute is not supported yet." | ||
| ) | ||
| x, scale = inputs[0], inputs[1] | ||
| zp = inputs[2] if len(inputs) > 2 and inputs[2] is not None else None | ||
| axis = attr.get("axis", 1) | ||
| if hasattr(x.ty, "ndim") and x.ty.ndim <= 1 and axis == 1: | ||
| axis = 0 | ||
| output_dtype = attr.get("output_dtype", 0) | ||
| out_dtype = ( | ||
| get_type(output_dtype) if output_dtype else "uint8" if zp is None else zp.ty.dtype.dtype | ||
| ) | ||
| if attr.get("saturate", 1) != 1 and str(out_dtype).startswith("float8"): | ||
| raise ValueError( | ||
| "QuantizeLinear float8 quantization with saturate=0 is not supported yet." | ||
| ) | ||
| if zp is not None and zp.ty.dtype.dtype != out_dtype: | ||
| raise ValueError( | ||
| "QuantizeLinear output_dtype must match the zero-point dtype, " | ||
| f"but got {out_dtype} and {zp.ty.dtype.dtype}." | ||
| ) | ||
| if zp is None: | ||
| zp = relax.const(0, out_dtype) | ||
| return relax.op.quantize(x, scale, zp, axis=axis, out_dtype=out_dtype) |
There was a problem hiding this comment.
There is significant code duplication between _impl_v21 and _impl_v23 in QuantizeLinear. Since _impl_v23 is identical to _impl_v21 except for the additional precision attribute check, you can simplify _impl_v23 by checking precision and then delegating directly to _impl_v21.
| def _impl_v23(cls, bb, inputs, attr, params): | |
| if attr.get("block_size", 0) != 0: | |
| raise ValueError("QuantizeLinear blocked quantization is not supported yet.") | |
| if attr.get("precision", 0) != 0: | |
| raise ValueError( | |
| "QuantizeLinear with a non-default precision attribute is not supported yet." | |
| ) | |
| x, scale = inputs[0], inputs[1] | |
| zp = inputs[2] if len(inputs) > 2 and inputs[2] is not None else None | |
| axis = attr.get("axis", 1) | |
| if hasattr(x.ty, "ndim") and x.ty.ndim <= 1 and axis == 1: | |
| axis = 0 | |
| output_dtype = attr.get("output_dtype", 0) | |
| out_dtype = ( | |
| get_type(output_dtype) if output_dtype else "uint8" if zp is None else zp.ty.dtype.dtype | |
| ) | |
| if attr.get("saturate", 1) != 1 and str(out_dtype).startswith("float8"): | |
| raise ValueError( | |
| "QuantizeLinear float8 quantization with saturate=0 is not supported yet." | |
| ) | |
| if zp is not None and zp.ty.dtype.dtype != out_dtype: | |
| raise ValueError( | |
| "QuantizeLinear output_dtype must match the zero-point dtype, " | |
| f"but got {out_dtype} and {zp.ty.dtype.dtype}." | |
| ) | |
| if zp is None: | |
| zp = relax.const(0, out_dtype) | |
| return relax.op.quantize(x, scale, zp, axis=axis, out_dtype=out_dtype) | |
| @classmethod | |
| def _impl_v23(cls, bb, inputs, attr, params): | |
| if attr.get("precision", 0) != 0: | |
| raise ValueError( | |
| "QuantizeLinear with a non-default precision attribute is not supported yet." | |
| ) | |
| return cls._impl_v21(bb, inputs, attr, params) |
| @classmethod | ||
| def _impl_v21(cls, bb, inputs, attr, params): | ||
| if attr.get("block_size", 0) != 0: | ||
| raise ValueError("DequantizeLinear blocked quantization is not supported yet.") | ||
| x, scale = inputs[0], inputs[1] | ||
| zp = inputs[2] if len(inputs) > 2 and inputs[2] is not None else None | ||
| axis = attr.get("axis", 1) | ||
| if hasattr(x.ty, "ndim") and x.ty.ndim <= 1 and axis == 1: | ||
| axis = 0 | ||
| out_dtype = scale.ty.dtype.dtype | ||
| if zp is None: | ||
| zp = relax.const(0, x.ty.dtype.dtype) | ||
| return relax.op.dequantize(x, scale, zp, axis=axis, out_dtype=out_dtype) |
There was a problem hiding this comment.
The implementation of _impl_v21 in DequantizeLinear is identical to _impl_v19 except for the block_size check. You can reduce code duplication by checking block_size and then delegating to _impl_v19.
| @classmethod | |
| def _impl_v21(cls, bb, inputs, attr, params): | |
| if attr.get("block_size", 0) != 0: | |
| raise ValueError("DequantizeLinear blocked quantization is not supported yet.") | |
| x, scale = inputs[0], inputs[1] | |
| zp = inputs[2] if len(inputs) > 2 and inputs[2] is not None else None | |
| axis = attr.get("axis", 1) | |
| if hasattr(x.ty, "ndim") and x.ty.ndim <= 1 and axis == 1: | |
| axis = 0 | |
| out_dtype = scale.ty.dtype.dtype | |
| if zp is None: | |
| zp = relax.const(0, x.ty.dtype.dtype) | |
| return relax.op.dequantize(x, scale, zp, axis=axis, out_dtype=out_dtype) | |
| @classmethod | |
| def _impl_v21(cls, bb, inputs, attr, params): | |
| if attr.get("block_size", 0) != 0: | |
| raise ValueError("DequantizeLinear blocked quantization is not supported yet.") | |
| return cls._impl_v19(bb, inputs, attr, params) |
Summary
Adds support for newer
QuantizeLinearandDequantizeLinearattributes in the Relax ONNX frontend.This includes
output_dtype,saturate, and newer opset behavior, while rejecting unsupported blocked quantization andprecisioncases.For
QuantizeLinearandDequantizeLinear, opsets 24 and 25 use the existing converter for currently supported types. Support forfloat8e8m0,int2, anduint2are outside this PR’s scope.Testing
Added structural and rejection tests for opsets 19, 21, 23, 24, and 25.