From e2c402b7edcddf23e6eaa37479bfabf0a48be431 Mon Sep 17 00:00:00 2001 From: Chaitanya140904 Date: Tue, 10 Nov 2026 01:24:54 +0530 Subject: [PATCH 1/4] [Relax][Frontend][ONNX] Add QLinearConv operator converter --- .../tvm/relax/frontend/onnx/onnx_frontend.py | 162 ++++++++++++++++++ tests/python/relax/test_frontend_onnx.py | 85 +++++++++ 2 files changed, 247 insertions(+) diff --git a/python/tvm/relax/frontend/onnx/onnx_frontend.py b/python/tvm/relax/frontend/onnx/onnx_frontend.py index badb3f9975f8..05af37796e38 100644 --- a/python/tvm/relax/frontend/onnx/onnx_frontend.py +++ b/python/tvm/relax/frontend/onnx/onnx_frontend.py @@ -2243,6 +2243,167 @@ def _impl_v11(cls, bb, inputs, attr, params): return conv_out +class QLinearConv(OnnxOpConverter): + """Converts an onnx QLinearConv node into an equivalent Relax expression.""" + + @classmethod + def _impl_v10(cls, bb, inputs, attr, params): + data = inputs[0] + x_scale = inputs[1] + x_zero_point = inputs[2] + weight = inputs[3] + w_scale = inputs[4] + w_zero_point = inputs[5] + y_scale = inputs[6] + y_zero_point = inputs[7] + bias = inputs[8] if len(inputs) > 8 and inputs[8] is not None else None + + if hasattr(data.ty, "ndim"): + ndim = data.ty.ndim + else: + ndim = len(data.ty.shape) + + if "kernel_shape" not in attr: + attr["kernel_shape"] = weight.ty.shape.values[2:] + + if ndim == 3: + op = relax.op.nn.conv1d + data_layout = "NCW" + kernel_layout = "OIW" + elif ndim == 4: + op = relax.op.nn.conv2d + data_layout = "NCHW" + kernel_layout = "OIHW" + elif ndim == 5: + op = relax.op.nn.conv3d + data_layout = "NCDHW" + kernel_layout = "OIDHW" + else: + raise NotImplementedError("Ndim > 5 not supported for QLinearConv.") + + # Dequantize x + if x_zero_point is None: + x_zero_point = relax.const(0, data.ty.dtype.dtype) + x_zp_norm = bb.normalize(x_zero_point) + if len(x_zp_norm.ty.shape) == 1: + x_zp = bb.normalize(relax.op.reshape(x_zero_point, [1, -1] + [1] * (ndim - 2))) + else: + x_zp = x_zero_point + + x_scale_norm = bb.normalize(x_scale) + if len(x_scale_norm.ty.shape) == 1: + x_scale_reshaped = bb.normalize(relax.op.reshape(x_scale, [1, -1] + [1] * (ndim - 2))) + else: + x_scale_reshaped = x_scale + + x_float = relax.op.subtract( + relax.op.astype(data, "float32"), relax.op.astype(x_zp, "float32") + ) + x_float = relax.op.multiply(x_float, relax.op.astype(x_scale_reshaped, "float32")) + + # Dequantize weight + if w_zero_point is None: + w_zero_point = relax.const(0, weight.ty.dtype.dtype) + w_zp_norm = bb.normalize(w_zero_point) + if len(w_zp_norm.ty.shape) == 1: + w_zp = bb.normalize(relax.op.reshape(w_zero_point, [-1, 1] + [1] * (ndim - 2))) + else: + w_zp = w_zero_point + + w_scale_norm = bb.normalize(w_scale) + if len(w_scale_norm.ty.shape) == 1: + w_scale_reshaped = bb.normalize(relax.op.reshape(w_scale, [-1, 1] + [1] * (ndim - 2))) + else: + w_scale_reshaped = w_scale + + w_float = relax.op.subtract( + relax.op.astype(weight, "float32"), relax.op.astype(w_zp, "float32") + ) + w_float = relax.op.multiply(w_float, relax.op.astype(w_scale_reshaped, "float32")) + + if "auto_pad" in attr: + attr["auto_pad"] = attr["auto_pad"].decode("utf-8") + if attr["auto_pad"] in ("SAME_UPPER", "SAME_LOWER"): + x_float = autopad( + bb, + x_float, + attr.get("strides", [1] * (ndim - 2)), + attr["kernel_shape"], + attr.get("dilations", [1] * (ndim - 2)), + mode=attr["auto_pad"], + deconv=False, + ) + elif attr["auto_pad"] == "VALID": + attr["pads"] = [0 for _ in range(ndim - 2)] + elif attr["auto_pad"] == "NOTSET": + pass + else: + msg = ( + f'Value {attr["auto_pad"]} in attribute "auto_pad" of operator QLinearConv is invalid.' + ) + raise AttributeError(msg) + attr.pop("auto_pad") + + conv_out = bb.normalize( + op( + data=x_float, + weight=w_float, + strides=attr.get("strides", 1), + padding=attr.get("pads", 0), + dilation=attr.get("dilations", 1), + groups=attr.get("group", 1), + data_layout=data_layout, + kernel_layout=kernel_layout, + ) + ) + + if bias is not None: + b_float = bb.normalize(relax.op.astype(bias, "float32")) + b_scale = bb.normalize( + relax.op.multiply( + relax.op.astype(w_scale, "float32"), + relax.op.astype(x_scale, "float32"), + ) + ) + b_real = bb.normalize(relax.op.multiply(b_float, b_scale)) + b_bias = bb.normalize(relax.op.reshape(b_real, [1, -1] + [1] * (ndim - 2))) + conv_out = bb.normalize(relax.op.add(conv_out, b_bias)) + + if y_zero_point is None: + y_zero_point = relax.const(0, "uint8") + out_dtype = y_zero_point.ty.dtype.dtype + + y_scale_norm = bb.normalize(y_scale) + if len(y_scale_norm.ty.shape) == 1: + y_scale_reshaped = bb.normalize(relax.op.reshape(y_scale, [1, -1] + [1] * (ndim - 2))) + else: + y_scale_reshaped = y_scale + + y_zp_norm = bb.normalize(y_zero_point) + if len(y_zp_norm.ty.shape) == 1: + y_zp_reshaped = bb.normalize(relax.op.reshape(y_zero_point, [1, -1] + [1] * (ndim - 2))) + else: + y_zp_reshaped = y_zero_point + + y_scaled = relax.op.divide(conv_out, relax.op.astype(y_scale_reshaped, "float32")) + y_round = relax.op.round(y_scaled) + y_val = relax.op.add(y_round, relax.op.astype(y_zp_reshaped, "float32")) + + if out_dtype == "uint8": + min_val, max_val = 0, 255 + elif out_dtype == "int8": + min_val, max_val = -128, 127 + elif out_dtype == "uint16": + min_val, max_val = 0, 65535 + elif out_dtype == "int16": + min_val, max_val = -32768, 32767 + else: + min_val, max_val = 0, 255 + + clipped = relax.op.clip(y_val, min_val, max_val) + return relax.op.astype(clipped, out_dtype) + + class ConvTranspose(OnnxOpConverter): """Converts an onnx ConvTranspose node into an equivalent Relax expression.""" @@ -6281,6 +6442,7 @@ def _get_convert_map(): "MaxUnpool": MaxUnpool, "Conv": Conv, "ConvTranspose": ConvTranspose, + "QLinearConv": QLinearConv, "Flatten": Flatten, "Identity": Identity, "Dropout": Dropout, diff --git a/tests/python/relax/test_frontend_onnx.py b/tests/python/relax/test_frontend_onnx.py index 5e8c408d6151..806ddfa0fda9 100644 --- a/tests/python/relax/test_frontend_onnx.py +++ b/tests/python/relax/test_frontend_onnx.py @@ -14582,5 +14582,90 @@ def test_quantizelinear_output_dtype_mismatch(opset): from_onnx(model, opset=opset, keep_params_in_input=True) +def test_qlinearconv(): + """Verify numerical correctness of QLinearConv against ONNX Runtime.""" + + def verify_qlinearconv( + x_shape, + w_shape, + strides=[1, 1], + pads=[0, 0, 0, 0], + dilations=[1, 1], + group=1, + with_bias=True, + opset=10, + ): + x = np.random.randint(0, 255, size=x_shape, dtype=np.uint8) + x_scale = np.array(0.5, dtype=np.float32) + x_zp = np.array(128, dtype=np.uint8) + + w = np.random.randint(0, 255, size=w_shape, dtype=np.uint8) + w_scale = np.array(0.25, dtype=np.float32) + w_zp = np.array(120, dtype=np.uint8) + + y_scale = np.array(0.1, dtype=np.float32) + y_zp = np.array(10, dtype=np.uint8) + + inputs_list = ["x", "x_scale", "x_zp", "w", "w_scale", "w_zp", "y_scale", "y_zp"] + graph_inputs = [ + helper.make_tensor_value_info("x", TensorProto.UINT8, x_shape), + helper.make_tensor_value_info("x_scale", TensorProto.FLOAT, []), + helper.make_tensor_value_info("x_zp", TensorProto.UINT8, []), + helper.make_tensor_value_info("w", TensorProto.UINT8, w_shape), + helper.make_tensor_value_info("w_scale", TensorProto.FLOAT, []), + helper.make_tensor_value_info("w_zp", TensorProto.UINT8, []), + helper.make_tensor_value_info("y_scale", TensorProto.FLOAT, []), + helper.make_tensor_value_info("y_zp", TensorProto.UINT8, []), + ] + + feed_inputs = { + "x": x, + "x_scale": x_scale, + "x_zp": x_zp, + "w": w, + "w_scale": w_scale, + "w_zp": w_zp, + "y_scale": y_scale, + "y_zp": y_zp, + } + + if with_bias: + B = np.random.randint(-10, 10, size=[w_shape[0]], dtype=np.int32) + inputs_list.append("B") + graph_inputs.append(helper.make_tensor_value_info("B", TensorProto.INT32, [w_shape[0]])) + feed_inputs["B"] = B + + node = helper.make_node( + "QLinearConv", + inputs=inputs_list, + outputs=["y"], + kernel_shape=w_shape[2:], + strides=strides, + pads=pads, + dilations=dilations, + group=group, + ) + + out_h = (x_shape[2] + pads[0] + pads[2] - dilations[0] * (w_shape[2] - 1) - 1) // strides[0] + 1 + out_w = (x_shape[3] + pads[1] + pads[3] - dilations[1] * (w_shape[3] - 1) - 1) // strides[1] + 1 + y_shape = [x_shape[0], w_shape[0], out_h, out_w] + + graph = helper.make_graph( + [node], + "qlinearconv_test", + inputs=graph_inputs, + outputs=[helper.make_tensor_value_info("y", TensorProto.UINT8, y_shape)], + ) + model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", opset)]) + check_correctness(model, inputs=feed_inputs, opset=opset) + + # 2D Conv with bias + verify_qlinearconv([1, 1, 4, 4], [1, 1, 2, 2], with_bias=True) + # 2D Conv without bias + verify_qlinearconv([1, 1, 4, 4], [1, 1, 2, 2], with_bias=False) + # Multi-channel + verify_qlinearconv([2, 3, 5, 5], [4, 3, 3, 3], pads=[1, 1, 1, 1], with_bias=True) + + if __name__ == "__main__": tvm_tirx.testing.main() From 83c8648faa4dc4215bc18ec69a1db8f74a3252b2 Mon Sep 17 00:00:00 2001 From: Chaitanya140904 Date: Fri, 13 Nov 2026 18:25:32 +0530 Subject: [PATCH 2/4] [FIX][RELAX][ONNX] Address review feedback for QLinearConv converter --- .../tvm/relax/frontend/onnx/onnx_frontend.py | 143 +++++++-------- tests/python/relax/test_frontend_onnx.py | 167 ++++++++++++++---- 2 files changed, 200 insertions(+), 110 deletions(-) diff --git a/python/tvm/relax/frontend/onnx/onnx_frontend.py b/python/tvm/relax/frontend/onnx/onnx_frontend.py index 05af37796e38..1dccccf4e513 100644 --- a/python/tvm/relax/frontend/onnx/onnx_frontend.py +++ b/python/tvm/relax/frontend/onnx/onnx_frontend.py @@ -2258,13 +2258,17 @@ def _impl_v10(cls, bb, inputs, attr, params): y_zero_point = inputs[7] bias = inputs[8] if len(inputs) > 8 and inputs[8] is not None else None - if hasattr(data.ty, "ndim"): - ndim = data.ty.ndim + data_norm = bb.normalize(data) + if hasattr(data_norm.ty, "ndim"): + ndim = data_norm.ty.ndim else: - ndim = len(data.ty.shape) + ndim = len(data_norm.ty.shape) + weight_norm = bb.normalize(weight) if "kernel_shape" not in attr: - attr["kernel_shape"] = weight.ty.shape.values[2:] + attr["kernel_shape"] = [int(dim) for dim in weight_norm.ty.shape[2:]] + else: + attr["kernel_shape"] = [int(dim) for dim in attr["kernel_shape"]] if ndim == 3: op = relax.op.nn.conv1d @@ -2281,113 +2285,102 @@ def _impl_v10(cls, bb, inputs, attr, params): else: raise NotImplementedError("Ndim > 5 not supported for QLinearConv.") - # Dequantize x - if x_zero_point is None: - x_zero_point = relax.const(0, data.ty.dtype.dtype) - x_zp_norm = bb.normalize(x_zero_point) - if len(x_zp_norm.ty.shape) == 1: - x_zp = bb.normalize(relax.op.reshape(x_zero_point, [1, -1] + [1] * (ndim - 2))) - else: - x_zp = x_zero_point - - x_scale_norm = bb.normalize(x_scale) - if len(x_scale_norm.ty.shape) == 1: - x_scale_reshaped = bb.normalize(relax.op.reshape(x_scale, [1, -1] + [1] * (ndim - 2))) - else: - x_scale_reshaped = x_scale + # Widen data and weight to int32 before centering and arithmetic to prevent overflow + data_i32 = relax.op.astype(data, "int32") + weight_i32 = relax.op.astype(weight, "int32") - x_float = relax.op.subtract( - relax.op.astype(data, "float32"), relax.op.astype(x_zp, "float32") - ) - x_float = relax.op.multiply(x_float, relax.op.astype(x_scale_reshaped, "float32")) + if x_zero_point is None: + x_zero_point = relax.const(0, "int32") + x_zp_i32 = relax.op.astype(x_zero_point, "int32") + x_zp_norm = bb.normalize(x_zp_i32) + if hasattr(x_zp_norm.ty, "shape") and len(x_zp_norm.ty.shape) == 1: + x_zp_i32 = bb.normalize(relax.op.reshape(x_zp_i32, [1, -1] + [1] * (ndim - 2))) + x_centered = relax.op.subtract(data_i32, x_zp_i32) - # Dequantize weight if w_zero_point is None: - w_zero_point = relax.const(0, weight.ty.dtype.dtype) - w_zp_norm = bb.normalize(w_zero_point) - if len(w_zp_norm.ty.shape) == 1: - w_zp = bb.normalize(relax.op.reshape(w_zero_point, [-1, 1] + [1] * (ndim - 2))) - else: - w_zp = w_zero_point - - w_scale_norm = bb.normalize(w_scale) - if len(w_scale_norm.ty.shape) == 1: - w_scale_reshaped = bb.normalize(relax.op.reshape(w_scale, [-1, 1] + [1] * (ndim - 2))) - else: - w_scale_reshaped = w_scale - - w_float = relax.op.subtract( - relax.op.astype(weight, "float32"), relax.op.astype(w_zp, "float32") - ) - w_float = relax.op.multiply(w_float, relax.op.astype(w_scale_reshaped, "float32")) + w_zero_point = relax.const(0, "int32") + w_zp_i32 = relax.op.astype(w_zero_point, "int32") + w_zp_norm = bb.normalize(w_zp_i32) + if hasattr(w_zp_norm.ty, "shape") and len(w_zp_norm.ty.shape) == 1: + w_zp_i32 = bb.normalize(relax.op.reshape(w_zp_i32, [-1, 1] + [1] * (ndim - 2))) + w_centered = relax.op.subtract(weight_i32, w_zp_i32) if "auto_pad" in attr: - attr["auto_pad"] = attr["auto_pad"].decode("utf-8") - if attr["auto_pad"] in ("SAME_UPPER", "SAME_LOWER"): - x_float = autopad( + auto_pad = attr["auto_pad"] + if isinstance(auto_pad, bytes): + auto_pad = auto_pad.decode("utf-8") + if auto_pad in ("SAME_UPPER", "SAME_LOWER"): + x_centered = bb.normalize(x_centered) + x_centered = autopad( bb, - x_float, + x_centered, attr.get("strides", [1] * (ndim - 2)), attr["kernel_shape"], attr.get("dilations", [1] * (ndim - 2)), - mode=attr["auto_pad"], + mode=auto_pad, deconv=False, ) - elif attr["auto_pad"] == "VALID": - attr["pads"] = [0 for _ in range(ndim - 2)] - elif attr["auto_pad"] == "NOTSET": + elif auto_pad == "VALID": + attr["pads"] = [0 for _ in range((ndim - 2) * 2)] + elif auto_pad == "NOTSET": pass else: - msg = ( - f'Value {attr["auto_pad"]} in attribute "auto_pad" of operator QLinearConv is invalid.' - ) + msg = f'Value {auto_pad} in attribute "auto_pad" of operator QLinearConv is invalid.' raise AttributeError(msg) attr.pop("auto_pad") + x_centered = bb.normalize(x_centered) + w_centered = bb.normalize(w_centered) conv_out = bb.normalize( op( - data=x_float, - weight=w_float, + data=x_centered, + weight=w_centered, strides=attr.get("strides", 1), padding=attr.get("pads", 0), dilation=attr.get("dilations", 1), groups=attr.get("group", 1), data_layout=data_layout, kernel_layout=kernel_layout, + out_dtype="int32", ) ) if bias is not None: - b_float = bb.normalize(relax.op.astype(bias, "float32")) - b_scale = bb.normalize( - relax.op.multiply( - relax.op.astype(w_scale, "float32"), - relax.op.astype(x_scale, "float32"), - ) - ) - b_real = bb.normalize(relax.op.multiply(b_float, b_scale)) - b_bias = bb.normalize(relax.op.reshape(b_real, [1, -1] + [1] * (ndim - 2))) + b_i32 = bb.normalize(relax.op.astype(bias, "int32")) + b_bias = bb.normalize(relax.op.reshape(b_i32, [1, -1] + [1] * (ndim - 2))) conv_out = bb.normalize(relax.op.add(conv_out, b_bias)) + conv_float = relax.op.astype(conv_out, "float32") + if y_zero_point is None: y_zero_point = relax.const(0, "uint8") out_dtype = y_zero_point.ty.dtype.dtype - y_scale_norm = bb.normalize(y_scale) - if len(y_scale_norm.ty.shape) == 1: - y_scale_reshaped = bb.normalize(relax.op.reshape(y_scale, [1, -1] + [1] * (ndim - 2))) - else: - y_scale_reshaped = y_scale + x_scale_f = relax.op.astype(x_scale, "float32") + w_scale_f = relax.op.astype(w_scale, "float32") + y_scale_f = relax.op.astype(y_scale, "float32") + y_zp_f = relax.op.astype(y_zero_point, "float32") - y_zp_norm = bb.normalize(y_zero_point) - if len(y_zp_norm.ty.shape) == 1: - y_zp_reshaped = bb.normalize(relax.op.reshape(y_zero_point, [1, -1] + [1] * (ndim - 2))) - else: - y_zp_reshaped = y_zero_point + x_s_norm = bb.normalize(x_scale_f) + if hasattr(x_s_norm.ty, "shape") and len(x_s_norm.ty.shape) == 1: + x_scale_f = bb.normalize(relax.op.reshape(x_scale_f, [1, -1] + [1] * (ndim - 2))) + + w_s_norm = bb.normalize(w_scale_f) + if hasattr(w_s_norm.ty, "shape") and len(w_s_norm.ty.shape) == 1: + w_scale_f = bb.normalize(relax.op.reshape(w_scale_f, [1, -1] + [1] * (ndim - 2))) + + y_s_norm = bb.normalize(y_scale_f) + if hasattr(y_s_norm.ty, "shape") and len(y_s_norm.ty.shape) == 1: + y_scale_f = bb.normalize(relax.op.reshape(y_scale_f, [1, -1] + [1] * (ndim - 2))) + + y_z_norm = bb.normalize(y_zero_point) + if hasattr(y_z_norm.ty, "shape") and len(y_z_norm.ty.shape) == 1: + y_zp_f = bb.normalize(relax.op.reshape(y_zp_f, [1, -1] + [1] * (ndim - 2))) - y_scaled = relax.op.divide(conv_out, relax.op.astype(y_scale_reshaped, "float32")) - y_round = relax.op.round(y_scaled) - y_val = relax.op.add(y_round, relax.op.astype(y_zp_reshaped, "float32")) + scale_mult = relax.op.divide(relax.op.multiply(x_scale_f, w_scale_f), y_scale_f) + scaled = relax.op.multiply(conv_float, scale_mult) + rounded = relax.op.round(scaled) + y_val = relax.op.add(rounded, y_zp_f) if out_dtype == "uint8": min_val, max_val = 0, 255 diff --git a/tests/python/relax/test_frontend_onnx.py b/tests/python/relax/test_frontend_onnx.py index 806ddfa0fda9..aa47d5a22416 100644 --- a/tests/python/relax/test_frontend_onnx.py +++ b/tests/python/relax/test_frontend_onnx.py @@ -14588,34 +14588,50 @@ def test_qlinearconv(): def verify_qlinearconv( x_shape, w_shape, - strides=[1, 1], - pads=[0, 0, 0, 0], - dilations=[1, 1], + strides=None, + pads=None, + dilations=None, + auto_pad=None, group=1, with_bias=True, + dtype="uint8", + w_scale_shape=(), + w_zp_shape=(), opset=10, + seed=42, ): - x = np.random.randint(0, 255, size=x_shape, dtype=np.uint8) - x_scale = np.array(0.5, dtype=np.float32) - x_zp = np.array(128, dtype=np.uint8) - - w = np.random.randint(0, 255, size=w_shape, dtype=np.uint8) - w_scale = np.array(0.25, dtype=np.float32) - w_zp = np.array(120, dtype=np.uint8) + rng = np.random.RandomState(seed) + is_int8 = dtype == "int8" + proto = TensorProto.INT8 if is_int8 else TensorProto.UINT8 + np_dtype = np.int8 if is_int8 else np.uint8 + + if is_int8: + x = rng.randint(-8, 8, size=x_shape).astype(np_dtype) + x_zp = np.array(0, dtype=np_dtype) + w = rng.randint(-8, 8, size=w_shape).astype(np_dtype) + w_zp = np.full(w_zp_shape, 0, dtype=np_dtype) + y_zp = np.array(0, dtype=np_dtype) + else: + x = rng.randint(120, 136, size=x_shape).astype(np_dtype) + x_zp = np.array(128, dtype=np_dtype) + w = rng.randint(116, 128, size=w_shape).astype(np_dtype) + w_zp = np.full(w_zp_shape, 120, dtype=np_dtype) + y_zp = np.array(128, dtype=np_dtype) - y_scale = np.array(0.1, dtype=np.float32) - y_zp = np.array(10, dtype=np.uint8) + x_scale = np.array(0.5, dtype=np.float32) + w_scale = np.full(w_scale_shape, 0.25, dtype=np.float32) + y_scale = np.array(2.0, dtype=np.float32) inputs_list = ["x", "x_scale", "x_zp", "w", "w_scale", "w_zp", "y_scale", "y_zp"] graph_inputs = [ - helper.make_tensor_value_info("x", TensorProto.UINT8, x_shape), + helper.make_tensor_value_info("x", proto, x_shape), helper.make_tensor_value_info("x_scale", TensorProto.FLOAT, []), - helper.make_tensor_value_info("x_zp", TensorProto.UINT8, []), - helper.make_tensor_value_info("w", TensorProto.UINT8, w_shape), - helper.make_tensor_value_info("w_scale", TensorProto.FLOAT, []), - helper.make_tensor_value_info("w_zp", TensorProto.UINT8, []), + helper.make_tensor_value_info("x_zp", proto, []), + helper.make_tensor_value_info("w", proto, w_shape), + helper.make_tensor_value_info("w_scale", TensorProto.FLOAT, list(w_scale_shape)), + helper.make_tensor_value_info("w_zp", proto, list(w_zp_shape)), helper.make_tensor_value_info("y_scale", TensorProto.FLOAT, []), - helper.make_tensor_value_info("y_zp", TensorProto.UINT8, []), + helper.make_tensor_value_info("y_zp", proto, []), ] feed_inputs = { @@ -14630,41 +14646,122 @@ def verify_qlinearconv( } if with_bias: - B = np.random.randint(-10, 10, size=[w_shape[0]], dtype=np.int32) + B = rng.randint(-5, 5, size=[w_shape[0]]).astype(np.int32) inputs_list.append("B") graph_inputs.append(helper.make_tensor_value_info("B", TensorProto.INT32, [w_shape[0]])) feed_inputs["B"] = B + kwargs = {"group": group, "kernel_shape": w_shape[2:]} + if strides is not None: + kwargs["strides"] = strides + if dilations is not None: + kwargs["dilations"] = dilations + if pads is not None: + kwargs["pads"] = pads + if auto_pad is not None: + kwargs["auto_pad"] = auto_pad + node = helper.make_node( "QLinearConv", inputs=inputs_list, outputs=["y"], - kernel_shape=w_shape[2:], - strides=strides, - pads=pads, - dilations=dilations, - group=group, + **kwargs, ) - out_h = (x_shape[2] + pads[0] + pads[2] - dilations[0] * (w_shape[2] - 1) - 1) // strides[0] + 1 - out_w = (x_shape[3] + pads[1] + pads[3] - dilations[1] * (w_shape[3] - 1) - 1) // strides[1] + 1 - y_shape = [x_shape[0], w_shape[0], out_h, out_w] - graph = helper.make_graph( [node], "qlinearconv_test", inputs=graph_inputs, - outputs=[helper.make_tensor_value_info("y", TensorProto.UINT8, y_shape)], + outputs=[helper.make_tensor_value_info("y", proto, None)], ) - model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", opset)]) + model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", opset)], ir_version=8) check_correctness(model, inputs=feed_inputs, opset=opset) - # 2D Conv with bias - verify_qlinearconv([1, 1, 4, 4], [1, 1, 2, 2], with_bias=True) - # 2D Conv without bias - verify_qlinearconv([1, 1, 4, 4], [1, 1, 2, 2], with_bias=False) - # Multi-channel + # 1. 2D Conv with bias (in-range uint8) + verify_qlinearconv([1, 1, 4, 4], [1, 1, 2, 2], pads=[0, 0, 0, 0], with_bias=True) + # 2. 2D Conv without bias + verify_qlinearconv([1, 1, 4, 4], [1, 1, 2, 2], pads=[0, 0, 0, 0], with_bias=False) + # 3. Multi-channel 2D Conv verify_qlinearconv([2, 3, 5, 5], [4, 3, 3, 3], pads=[1, 1, 1, 1], with_bias=True) + # 4. 1D Conv + verify_qlinearconv([1, 1, 8], [1, 1, 3], strides=[1], dilations=[1], pads=[0, 0]) + # 5. 3D Conv + verify_qlinearconv( + [1, 1, 4, 4, 4], [1, 1, 2, 2, 2], strides=[1, 1, 1], dilations=[1, 1, 1], pads=[0, 0, 0, 0, 0, 0] + ) + # 6. SAME_UPPER automatic padding + verify_qlinearconv([1, 1, 4, 4], [1, 1, 3, 3], auto_pad="SAME_UPPER") + # 7. SAME_LOWER automatic padding + verify_qlinearconv([1, 1, 4, 4], [1, 1, 3, 3], auto_pad="SAME_LOWER") + # 8. VALID automatic padding + verify_qlinearconv([1, 1, 4, 4], [1, 1, 3, 3], auto_pad="VALID") + # 9. Non-unit strides and dilations + verify_qlinearconv([1, 1, 8, 8], [1, 1, 3, 3], strides=[2, 2], dilations=[2, 2], pads=[0, 0, 0, 0]) + # 10. Grouped Conv + verify_qlinearconv([1, 4, 4, 4], [4, 2, 2, 2], group=2, pads=[0, 0, 0, 0]) + # 11. Signed int8 quantization + verify_qlinearconv([1, 1, 4, 4], [1, 1, 2, 2], dtype="int8", pads=[0, 0, 0, 0], with_bias=True) + # 12. Distinct per-output-channel weight scales and zero-points + verify_qlinearconv( + [1, 2, 4, 4], [4, 2, 2, 2], w_scale_shape=(4,), w_zp_shape=(4,), pads=[0, 0, 0, 0], with_bias=True + ) + # 13. Unequal input/output channels with bias + verify_qlinearconv([1, 3, 5, 5], [5, 3, 2, 2], pads=[0, 0, 0, 0], with_bias=True) + + +def test_qlinearconv_precision_cancellation(): + """Verify that 1x1 conv over 261 uint8 channels with large bias keeps precision in int32.""" + channels = 261 + x = np.full([1, channels, 1, 1], 254, dtype=np.uint8) + w = np.full([1, channels, 1, 1], 254, dtype=np.uint8) + x_scale = np.array(1.0, dtype=np.float32) + x_zp = np.array(0, dtype=np.uint8) + w_scale = np.array(1.0, dtype=np.float32) + w_zp = np.array(0, dtype=np.uint8) + y_scale = np.array(1.0, dtype=np.float32) + y_zp = np.array(0, dtype=np.uint8) + bias = np.array([-16838675], dtype=np.int32) + + inputs_list = ["x", "x_scale", "x_zp", "w", "w_scale", "w_zp", "y_scale", "y_zp", "B"] + graph_inputs = [ + helper.make_tensor_value_info("x", TensorProto.UINT8, [1, channels, 1, 1]), + helper.make_tensor_value_info("x_scale", TensorProto.FLOAT, []), + helper.make_tensor_value_info("x_zp", TensorProto.UINT8, []), + helper.make_tensor_value_info("w", TensorProto.UINT8, [1, channels, 1, 1]), + helper.make_tensor_value_info("w_scale", TensorProto.FLOAT, []), + helper.make_tensor_value_info("w_zp", TensorProto.UINT8, []), + helper.make_tensor_value_info("y_scale", TensorProto.FLOAT, []), + helper.make_tensor_value_info("y_zp", TensorProto.UINT8, []), + helper.make_tensor_value_info("B", TensorProto.INT32, [1]), + ] + node = helper.make_node( + "QLinearConv", + inputs=inputs_list, + outputs=["y"], + kernel_shape=[1, 1], + ) + graph = helper.make_graph( + [node], + "qlinearconv_cancel_test", + inputs=graph_inputs, + outputs=[helper.make_tensor_value_info("y", TensorProto.UINT8, [1, 1, 1, 1])], + ) + model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", 10)], ir_version=8) + feed_inputs = { + "x": x, + "x_scale": x_scale, + "x_zp": x_zp, + "w": w, + "w_scale": w_scale, + "w_zp": w_zp, + "y_scale": y_scale, + "y_zp": y_zp, + "B": bias, + } + tvm_out = run_in_tvm(model, inputs=feed_inputs, opset=10) + tvm_val = tvm_out[0].numpy() if isinstance(tvm_out, (list, tuple)) else tvm_out.numpy() + assert tvm_val[0, 0, 0, 0] == 1 + check_correctness(model, inputs=feed_inputs, opset=10) if __name__ == "__main__": From dd4308546fdc98b08b3b37b7310ac3e4b00f900b Mon Sep 17 00:00:00 2001 From: Chaitanya140904 Date: Mon, 16 Nov 2026 23:05:32 +0530 Subject: [PATCH 3/4] [Fix][ONNX] QLinearConv: use shape.values[2:] for kernel_shape and float64 scale product Two fixes: 1. kernel_shape inference: weight_norm.ty.shape[2:] raises TypeError because ShapeExpr.__getitem__ does not accept slice arguments. Use weight_norm.ty.shape.values[2:] (which returns a list/tuple that can be sliced) so symbolic kernel dimensions remain supported. Adds a regression test that omits kernel_shape from the ONNX node and verifies correct output. 2. Scale overflow: x_scale * w_scale can overflow float32 even when all three scales and the final requantized result are finite (e.g. x_scale=w_scale=2^65, y_scale=2^124 gives a correct output of 64 but float32 product is inf, clipping to 255). Compute x_scale * w_scale / y_scale in float64, then cast to float32 before multiplying with the conv output. Adds a regression asserting that the canonical overflow case produces 64 rather than 255. Addresses GitHub Copilot review comments: https://github.com/apache/tvm/pull/20953#discussion_r4581476091 https://github.com/apache/tvm/pull/20953#discussion_r4581476116 --- .../tvm/relax/frontend/onnx/onnx_frontend.py | 15 ++- tests/python/relax/test_frontend_onnx.py | 110 ++++++++++++++++++ 2 files changed, 123 insertions(+), 2 deletions(-) diff --git a/python/tvm/relax/frontend/onnx/onnx_frontend.py b/python/tvm/relax/frontend/onnx/onnx_frontend.py index 1dccccf4e513..60facc294030 100644 --- a/python/tvm/relax/frontend/onnx/onnx_frontend.py +++ b/python/tvm/relax/frontend/onnx/onnx_frontend.py @@ -2266,7 +2266,9 @@ def _impl_v10(cls, bb, inputs, attr, params): weight_norm = bb.normalize(weight) if "kernel_shape" not in attr: - attr["kernel_shape"] = [int(dim) for dim in weight_norm.ty.shape[2:]] + # Use .shape.values to avoid TypeError from ShapeExpr.__getitem__ with slices; + # keep values as-is (may be symbolic PrimExprs) so symbolic kernels stay supported. + attr["kernel_shape"] = weight_norm.ty.shape.values[2:] else: attr["kernel_shape"] = [int(dim) for dim in attr["kernel_shape"]] @@ -2377,7 +2379,16 @@ def _impl_v10(cls, bb, inputs, attr, params): if hasattr(y_z_norm.ty, "shape") and len(y_z_norm.ty.shape) == 1: y_zp_f = bb.normalize(relax.op.reshape(y_zp_f, [1, -1] + [1] * (ndim - 2))) - scale_mult = relax.op.divide(relax.op.multiply(x_scale_f, w_scale_f), y_scale_f) + # Compute the combined scale multiplier in float64 to avoid overflow when + # x_scale * w_scale is very large (e.g. 2**65 * 2**65 = 2**130 >> float32 max). + # Cast back to float32 before the multiply with the int32 conv output. + x_scale_f64 = relax.op.astype(x_scale_f, "float64") + w_scale_f64 = relax.op.astype(w_scale_f, "float64") + y_scale_f64 = relax.op.astype(y_scale_f, "float64") + scale_mult_f64 = relax.op.divide( + relax.op.multiply(x_scale_f64, w_scale_f64), y_scale_f64 + ) + scale_mult = relax.op.astype(scale_mult_f64, "float32") scaled = relax.op.multiply(conv_float, scale_mult) rounded = relax.op.round(scaled) y_val = relax.op.add(rounded, y_zp_f) diff --git a/tests/python/relax/test_frontend_onnx.py b/tests/python/relax/test_frontend_onnx.py index aa47d5a22416..b8e4f766e7a7 100644 --- a/tests/python/relax/test_frontend_onnx.py +++ b/tests/python/relax/test_frontend_onnx.py @@ -14764,5 +14764,115 @@ def test_qlinearconv_precision_cancellation(): check_correctness(model, inputs=feed_inputs, opset=10) +def test_qlinearconv_no_kernel_shape_attr(): + """QLinearConv must work when kernel_shape attribute is omitted (inferred from weight shape). + + Uses a 2D conv without supplying kernel_shape in the ONNX node attributes to + exercise the weight_norm.ty.shape.values[2:] inference path. + """ + x = np.array([[[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]]], dtype=np.uint8) + w = np.ones((1, 1, 2, 2), dtype=np.uint8) + x_scale = np.array(1.0, dtype=np.float32) + x_zp = np.array(0, dtype=np.uint8) + w_scale = np.array(1.0, dtype=np.float32) + w_zp = np.array(0, dtype=np.uint8) + y_scale = np.array(4.0, dtype=np.float32) + y_zp = np.array(0, dtype=np.uint8) + + # NOTE: no kernel_shape attribute — it must be inferred from w.shape[2:] + node = helper.make_node( + "QLinearConv", + inputs=["x", "x_scale", "x_zp", "w", "w_scale", "w_zp", "y_scale", "y_zp"], + outputs=["y"], + # no kernel_shape here + ) + graph = helper.make_graph( + [node], + "qlinearconv_no_kshape", + inputs=[ + helper.make_tensor_value_info("x", TensorProto.UINT8, [1, 1, 4, 4]), + helper.make_tensor_value_info("x_scale", TensorProto.FLOAT, []), + helper.make_tensor_value_info("x_zp", TensorProto.UINT8, []), + helper.make_tensor_value_info("w", TensorProto.UINT8, [1, 1, 2, 2]), + helper.make_tensor_value_info("w_scale", TensorProto.FLOAT, []), + helper.make_tensor_value_info("w_zp", TensorProto.UINT8, []), + helper.make_tensor_value_info("y_scale", TensorProto.FLOAT, []), + helper.make_tensor_value_info("y_zp", TensorProto.UINT8, []), + ], + outputs=[helper.make_tensor_value_info("y", TensorProto.UINT8, None)], + ) + model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", 10)], ir_version=8) + feed_inputs = { + "x": x, + "x_scale": x_scale, + "x_zp": x_zp, + "w": w, + "w_scale": w_scale, + "w_zp": w_zp, + "y_scale": y_scale, + "y_zp": y_zp, + } + check_correctness(model, inputs=feed_inputs, opset=10) + + +def test_qlinearconv_scale_overflow(): + """Combined scale x_scale * w_scale must not overflow float32. + + With x_scale = w_scale = 2**65 and y_scale = 2**124, the correct uint8 output is 64, + but computing the product in float32 gives infinity and clips to 255. + The combined scale is computed in float64 to prevent this. + """ + channels = 1 + x = np.full([1, channels, 1, 1], 1, dtype=np.uint8) + w = np.full([1, channels, 1, 1], 1, dtype=np.uint8) + x_scale = np.array(2.0**65, dtype=np.float32) + x_zp = np.array(0, dtype=np.uint8) + w_scale = np.array(2.0**65, dtype=np.float32) + w_zp = np.array(0, dtype=np.uint8) + y_scale = np.array(2.0**124, dtype=np.float32) + y_zp = np.array(0, dtype=np.uint8) + + node = helper.make_node( + "QLinearConv", + inputs=["x", "x_scale", "x_zp", "w", "w_scale", "w_zp", "y_scale", "y_zp"], + outputs=["y"], + kernel_shape=[1, 1], + ) + graph = helper.make_graph( + [node], + "qlinearconv_scale_overflow_test", + inputs=[ + helper.make_tensor_value_info("x", TensorProto.UINT8, [1, channels, 1, 1]), + helper.make_tensor_value_info("x_scale", TensorProto.FLOAT, []), + helper.make_tensor_value_info("x_zp", TensorProto.UINT8, []), + helper.make_tensor_value_info("w", TensorProto.UINT8, [1, channels, 1, 1]), + helper.make_tensor_value_info("w_scale", TensorProto.FLOAT, []), + helper.make_tensor_value_info("w_zp", TensorProto.UINT8, []), + helper.make_tensor_value_info("y_scale", TensorProto.FLOAT, []), + helper.make_tensor_value_info("y_zp", TensorProto.UINT8, []), + ], + outputs=[helper.make_tensor_value_info("y", TensorProto.UINT8, [1, 1, 1, 1])], + ) + model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", 10)], ir_version=8) + feed_inputs = { + "x": x, + "x_scale": x_scale, + "x_zp": x_zp, + "w": w, + "w_scale": w_scale, + "w_zp": w_zp, + "y_scale": y_scale, + "y_zp": y_zp, + } + tvm_out = run_in_tvm(model, inputs=feed_inputs, opset=10) + tvm_val = tvm_out[0].numpy() if isinstance(tvm_out, (list, tuple)) else tvm_out.numpy() + # x=1, w=1 (no zero-point), matmul=1, scale=2^65*2^65/2^124 = 2^130/2^124 = 2^6 = 64 + # Expected output: clip(round(1 * 64), 0, 255) = 64 + assert tvm_val[0, 0, 0, 0] == 64, ( + f"Expected 64 (scale product computed in float64) but got {tvm_val[0, 0, 0, 0]}. " + "Scale product likely overflowed float32." + ) + + if __name__ == "__main__": tvm_tirx.testing.main() From f1ec98219f0b7efc7cb1d9d79ce1a240a32d4dfa Mon Sep 17 00:00:00 2001 From: Chaitanya140904 Date: Tue, 17 Nov 2026 10:03:49 +0530 Subject: [PATCH 4/4] [Fix][Lint] Fix UP038 isinstance union syntax and formatting in QLinearConv --- .../tvm/relax/frontend/onnx/onnx_frontend.py | 8 ++--- tests/python/relax/test_frontend_onnx.py | 29 ++++++++++++++----- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/python/tvm/relax/frontend/onnx/onnx_frontend.py b/python/tvm/relax/frontend/onnx/onnx_frontend.py index 60facc294030..44b56b5a1854 100644 --- a/python/tvm/relax/frontend/onnx/onnx_frontend.py +++ b/python/tvm/relax/frontend/onnx/onnx_frontend.py @@ -2327,7 +2327,9 @@ def _impl_v10(cls, bb, inputs, attr, params): elif auto_pad == "NOTSET": pass else: - msg = f'Value {auto_pad} in attribute "auto_pad" of operator QLinearConv is invalid.' + msg = ( + f'Value {auto_pad} in attribute "auto_pad" of operator QLinearConv is invalid.' + ) raise AttributeError(msg) attr.pop("auto_pad") @@ -2385,9 +2387,7 @@ def _impl_v10(cls, bb, inputs, attr, params): x_scale_f64 = relax.op.astype(x_scale_f, "float64") w_scale_f64 = relax.op.astype(w_scale_f, "float64") y_scale_f64 = relax.op.astype(y_scale_f, "float64") - scale_mult_f64 = relax.op.divide( - relax.op.multiply(x_scale_f64, w_scale_f64), y_scale_f64 - ) + scale_mult_f64 = relax.op.divide(relax.op.multiply(x_scale_f64, w_scale_f64), y_scale_f64) scale_mult = relax.op.astype(scale_mult_f64, "float32") scaled = relax.op.multiply(conv_float, scale_mult) rounded = relax.op.round(scaled) diff --git a/tests/python/relax/test_frontend_onnx.py b/tests/python/relax/test_frontend_onnx.py index b8e4f766e7a7..86626ce9ce69 100644 --- a/tests/python/relax/test_frontend_onnx.py +++ b/tests/python/relax/test_frontend_onnx.py @@ -14674,7 +14674,9 @@ def verify_qlinearconv( inputs=graph_inputs, outputs=[helper.make_tensor_value_info("y", proto, None)], ) - model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", opset)], ir_version=8) + model = helper.make_model( + graph, opset_imports=[helper.make_opsetid("", opset)], ir_version=8 + ) check_correctness(model, inputs=feed_inputs, opset=opset) # 1. 2D Conv with bias (in-range uint8) @@ -14687,7 +14689,11 @@ def verify_qlinearconv( verify_qlinearconv([1, 1, 8], [1, 1, 3], strides=[1], dilations=[1], pads=[0, 0]) # 5. 3D Conv verify_qlinearconv( - [1, 1, 4, 4, 4], [1, 1, 2, 2, 2], strides=[1, 1, 1], dilations=[1, 1, 1], pads=[0, 0, 0, 0, 0, 0] + [1, 1, 4, 4, 4], + [1, 1, 2, 2, 2], + strides=[1, 1, 1], + dilations=[1, 1, 1], + pads=[0, 0, 0, 0, 0, 0], ) # 6. SAME_UPPER automatic padding verify_qlinearconv([1, 1, 4, 4], [1, 1, 3, 3], auto_pad="SAME_UPPER") @@ -14696,14 +14702,21 @@ def verify_qlinearconv( # 8. VALID automatic padding verify_qlinearconv([1, 1, 4, 4], [1, 1, 3, 3], auto_pad="VALID") # 9. Non-unit strides and dilations - verify_qlinearconv([1, 1, 8, 8], [1, 1, 3, 3], strides=[2, 2], dilations=[2, 2], pads=[0, 0, 0, 0]) + verify_qlinearconv( + [1, 1, 8, 8], [1, 1, 3, 3], strides=[2, 2], dilations=[2, 2], pads=[0, 0, 0, 0] + ) # 10. Grouped Conv verify_qlinearconv([1, 4, 4, 4], [4, 2, 2, 2], group=2, pads=[0, 0, 0, 0]) # 11. Signed int8 quantization verify_qlinearconv([1, 1, 4, 4], [1, 1, 2, 2], dtype="int8", pads=[0, 0, 0, 0], with_bias=True) # 12. Distinct per-output-channel weight scales and zero-points verify_qlinearconv( - [1, 2, 4, 4], [4, 2, 2, 2], w_scale_shape=(4,), w_zp_shape=(4,), pads=[0, 0, 0, 0], with_bias=True + [1, 2, 4, 4], + [4, 2, 2, 2], + w_scale_shape=(4,), + w_zp_shape=(4,), + pads=[0, 0, 0, 0], + with_bias=True, ) # 13. Unequal input/output channels with bias verify_qlinearconv([1, 3, 5, 5], [5, 3, 2, 2], pads=[0, 0, 0, 0], with_bias=True) @@ -14759,7 +14772,7 @@ def test_qlinearconv_precision_cancellation(): "B": bias, } tvm_out = run_in_tvm(model, inputs=feed_inputs, opset=10) - tvm_val = tvm_out[0].numpy() if isinstance(tvm_out, (list, tuple)) else tvm_out.numpy() + tvm_val = tvm_out[0].numpy() if isinstance(tvm_out, list | tuple) else tvm_out.numpy() assert tvm_val[0, 0, 0, 0] == 1 check_correctness(model, inputs=feed_inputs, opset=10) @@ -14770,7 +14783,9 @@ def test_qlinearconv_no_kernel_shape_attr(): Uses a 2D conv without supplying kernel_shape in the ONNX node attributes to exercise the weight_norm.ty.shape.values[2:] inference path. """ - x = np.array([[[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]]], dtype=np.uint8) + x = np.array( + [[[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]]], dtype=np.uint8 + ) w = np.ones((1, 1, 2, 2), dtype=np.uint8) x_scale = np.array(1.0, dtype=np.float32) x_zp = np.array(0, dtype=np.uint8) @@ -14865,7 +14880,7 @@ def test_qlinearconv_scale_overflow(): "y_zp": y_zp, } tvm_out = run_in_tvm(model, inputs=feed_inputs, opset=10) - tvm_val = tvm_out[0].numpy() if isinstance(tvm_out, (list, tuple)) else tvm_out.numpy() + tvm_val = tvm_out[0].numpy() if isinstance(tvm_out, list | tuple) else tvm_out.numpy() # x=1, w=1 (no zero-point), matmul=1, scale=2^65*2^65/2^124 = 2^130/2^124 = 2^6 = 64 # Expected output: clip(round(1 * 64), 0, 255) = 64 assert tvm_val[0, 0, 0, 0] == 64, (