Skip to content

[Relax][Frontend][ONNX] Support Modern QDQ opset attributes#19993

Open
napronald wants to merge 1 commit into
apache:mainfrom
napronald:relax-onnx-qdq
Open

[Relax][Frontend][ONNX] Support Modern QDQ opset attributes#19993
napronald wants to merge 1 commit into
apache:mainfrom
napronald:relax-onnx-qdq

Conversation

@napronald

Copy link
Copy Markdown
Contributor

Summary

Adds support for newer QuantizeLinear and DequantizeLinear attributes in the Relax ONNX frontend.

This includes output_dtype, saturate, and newer opset behavior, while rejecting unsupported blocked quantization and precision cases.

For QuantizeLinear and DequantizeLinear, opsets 24 and 25 use the existing converter for currently supported types. Support for float8e8m0, int2, and uint2 are outside this PR’s scope.

Testing

Added structural and rejection tests for opsets 19, 21, 23, 24, and 25.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment on lines +402 to +429
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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

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.

Suggested change
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)

Comment on lines +467 to +479
@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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

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.

Suggested change
@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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant