Skip to content

Commit f2ba317

Browse files
[Relax][Frontend][ONNX] Add QLinearConv operator converter (#20953)
This PR adds support for the ONNX `QLinearConv` operator in the Relax ONNX frontend, resolving #20959. ### Key Changes - In `python/tvm/relax/frontend/onnx/onnx_frontend.py`: - Implemented `QLinearConv` op converter supporting opset 10. - Supports 1D, 2D, and 3D convolutions with dequantization of input activations and weights. - Supports per-channel and per-tensor scales and zero-points with channel broadcasting (`[1, C, 1, 1]` and `[O, 1, 1, 1]`). - Supports `auto_pad` modes (`SAME_UPPER`, `SAME_LOWER`, `VALID`, `NOTSET`), strides, dilations, and groups. - Handles quantized bias scaling (`B * x_scale * w_scale`). - Requantizes and clamps results to the output integer type range. - Registered `"QLinearConv": QLinearConv` in `_get_convert_map()`. - In `tests/python/relax/test_frontend_onnx.py`: - Added `test_qlinearconv` covering 2D convolutions with and without bias, and multi-channel convolutions with custom padding, validated against ONNX Runtime. Closes #20959. --------- Co-authored-by: Masahiro Hiramori <contact@mshr-h.com>
1 parent 28ff386 commit f2ba317

2 files changed

Lines changed: 492 additions & 0 deletions

File tree

‎python/tvm/relax/frontend/onnx/onnx_frontend.py‎

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2242,6 +2242,173 @@ def _impl_v11(cls, bb, inputs, attr, params):
22422242
return conv_out
22432243

22442244

2245+
class QLinearConv(OnnxOpConverter):
2246+
"""Converts an onnx QLinearConv node into an equivalent Relax expression."""
2247+
2248+
@classmethod
2249+
def _impl_v10(cls, bb, inputs, attr, params):
2250+
data = inputs[0]
2251+
x_scale = inputs[1]
2252+
x_zero_point = inputs[2]
2253+
weight = inputs[3]
2254+
w_scale = inputs[4]
2255+
w_zero_point = inputs[5]
2256+
y_scale = inputs[6]
2257+
y_zero_point = inputs[7]
2258+
bias = inputs[8] if len(inputs) > 8 and inputs[8] is not None else None
2259+
2260+
data_norm = bb.normalize(data)
2261+
if hasattr(data_norm.ty, "ndim"):
2262+
ndim = data_norm.ty.ndim
2263+
else:
2264+
ndim = len(data_norm.ty.shape)
2265+
2266+
weight_norm = bb.normalize(weight)
2267+
if "kernel_shape" not in attr:
2268+
# Use .shape.values to avoid TypeError from ShapeExpr.__getitem__ with slices;
2269+
# keep values as-is (may be symbolic PrimExprs) so symbolic kernels stay supported.
2270+
attr["kernel_shape"] = weight_norm.ty.shape.values[2:]
2271+
else:
2272+
attr["kernel_shape"] = [int(dim) for dim in attr["kernel_shape"]]
2273+
2274+
if ndim == 3:
2275+
op = relax.op.nn.conv1d
2276+
data_layout = "NCW"
2277+
kernel_layout = "OIW"
2278+
elif ndim == 4:
2279+
op = relax.op.nn.conv2d
2280+
data_layout = "NCHW"
2281+
kernel_layout = "OIHW"
2282+
elif ndim == 5:
2283+
op = relax.op.nn.conv3d
2284+
data_layout = "NCDHW"
2285+
kernel_layout = "OIDHW"
2286+
else:
2287+
raise NotImplementedError("Ndim > 5 not supported for QLinearConv.")
2288+
2289+
# Widen data and weight to int32 before centering and arithmetic to prevent overflow
2290+
data_i32 = relax.op.astype(data, "int32")
2291+
weight_i32 = relax.op.astype(weight, "int32")
2292+
2293+
if x_zero_point is None:
2294+
x_zero_point = relax.const(0, "int32")
2295+
x_zp_i32 = relax.op.astype(x_zero_point, "int32")
2296+
x_zp_norm = bb.normalize(x_zp_i32)
2297+
if hasattr(x_zp_norm.ty, "shape") and len(x_zp_norm.ty.shape) == 1:
2298+
x_zp_i32 = bb.normalize(relax.op.reshape(x_zp_i32, [1, -1] + [1] * (ndim - 2)))
2299+
x_centered = relax.op.subtract(data_i32, x_zp_i32)
2300+
2301+
if w_zero_point is None:
2302+
w_zero_point = relax.const(0, "int32")
2303+
w_zp_i32 = relax.op.astype(w_zero_point, "int32")
2304+
w_zp_norm = bb.normalize(w_zp_i32)
2305+
if hasattr(w_zp_norm.ty, "shape") and len(w_zp_norm.ty.shape) == 1:
2306+
w_zp_i32 = bb.normalize(relax.op.reshape(w_zp_i32, [-1, 1] + [1] * (ndim - 2)))
2307+
w_centered = relax.op.subtract(weight_i32, w_zp_i32)
2308+
2309+
if "auto_pad" in attr:
2310+
auto_pad = attr["auto_pad"]
2311+
if isinstance(auto_pad, bytes):
2312+
auto_pad = auto_pad.decode("utf-8")
2313+
if auto_pad in ("SAME_UPPER", "SAME_LOWER"):
2314+
x_centered = bb.normalize(x_centered)
2315+
x_centered = autopad(
2316+
bb,
2317+
x_centered,
2318+
attr.get("strides", [1] * (ndim - 2)),
2319+
attr["kernel_shape"],
2320+
attr.get("dilations", [1] * (ndim - 2)),
2321+
mode=auto_pad,
2322+
deconv=False,
2323+
)
2324+
elif auto_pad == "VALID":
2325+
attr["pads"] = [0 for _ in range((ndim - 2) * 2)]
2326+
elif auto_pad == "NOTSET":
2327+
pass
2328+
else:
2329+
msg = (
2330+
f'Value {auto_pad} in attribute "auto_pad" of operator QLinearConv is invalid.'
2331+
)
2332+
raise AttributeError(msg)
2333+
attr.pop("auto_pad")
2334+
2335+
x_centered = bb.normalize(x_centered)
2336+
w_centered = bb.normalize(w_centered)
2337+
conv_out = bb.normalize(
2338+
op(
2339+
data=x_centered,
2340+
weight=w_centered,
2341+
strides=attr.get("strides", 1),
2342+
padding=attr.get("pads", 0),
2343+
dilation=attr.get("dilations", 1),
2344+
groups=attr.get("group", 1),
2345+
data_layout=data_layout,
2346+
kernel_layout=kernel_layout,
2347+
out_dtype="int32",
2348+
)
2349+
)
2350+
2351+
if bias is not None:
2352+
b_i32 = bb.normalize(relax.op.astype(bias, "int32"))
2353+
b_bias = bb.normalize(relax.op.reshape(b_i32, [1, -1] + [1] * (ndim - 2)))
2354+
conv_out = bb.normalize(relax.op.add(conv_out, b_bias))
2355+
2356+
conv_float = relax.op.astype(conv_out, "float32")
2357+
2358+
if y_zero_point is None:
2359+
y_zero_point = relax.const(0, "uint8")
2360+
out_dtype = y_zero_point.ty.dtype.dtype
2361+
2362+
x_scale_f = relax.op.astype(x_scale, "float32")
2363+
w_scale_f = relax.op.astype(w_scale, "float32")
2364+
y_scale_f = relax.op.astype(y_scale, "float32")
2365+
y_zp_f = relax.op.astype(y_zero_point, "float32")
2366+
2367+
x_s_norm = bb.normalize(x_scale_f)
2368+
if hasattr(x_s_norm.ty, "shape") and len(x_s_norm.ty.shape) == 1:
2369+
x_scale_f = bb.normalize(relax.op.reshape(x_scale_f, [1, -1] + [1] * (ndim - 2)))
2370+
2371+
w_s_norm = bb.normalize(w_scale_f)
2372+
if hasattr(w_s_norm.ty, "shape") and len(w_s_norm.ty.shape) == 1:
2373+
w_scale_f = bb.normalize(relax.op.reshape(w_scale_f, [1, -1] + [1] * (ndim - 2)))
2374+
2375+
y_s_norm = bb.normalize(y_scale_f)
2376+
if hasattr(y_s_norm.ty, "shape") and len(y_s_norm.ty.shape) == 1:
2377+
y_scale_f = bb.normalize(relax.op.reshape(y_scale_f, [1, -1] + [1] * (ndim - 2)))
2378+
2379+
y_z_norm = bb.normalize(y_zero_point)
2380+
if hasattr(y_z_norm.ty, "shape") and len(y_z_norm.ty.shape) == 1:
2381+
y_zp_f = bb.normalize(relax.op.reshape(y_zp_f, [1, -1] + [1] * (ndim - 2)))
2382+
2383+
# Compute the combined scale multiplier and perform scaling in float64 to avoid overflow
2384+
# when x_scale * w_scale is very large (e.g. 2**65 * 2**65 = 2**130 >> float32 max).
2385+
# Keeping the product in float64 also ensures that zero accumulators (0.0 * scale_mult)
2386+
# evaluate cleanly to 0.0 rather than producing NaN if scale_mult overflows float32.
2387+
conv_f64 = relax.op.astype(conv_float, "float64")
2388+
x_scale_f64 = relax.op.astype(x_scale_f, "float64")
2389+
w_scale_f64 = relax.op.astype(w_scale_f, "float64")
2390+
y_scale_f64 = relax.op.astype(y_scale_f, "float64")
2391+
scale_mult_f64 = relax.op.divide(relax.op.multiply(x_scale_f64, w_scale_f64), y_scale_f64)
2392+
scaled_f64 = relax.op.multiply(conv_f64, scale_mult_f64)
2393+
scaled = relax.op.astype(scaled_f64, "float32")
2394+
rounded = relax.op.round(scaled)
2395+
y_val = relax.op.add(rounded, y_zp_f)
2396+
2397+
if out_dtype == "uint8":
2398+
min_val, max_val = 0, 255
2399+
elif out_dtype == "int8":
2400+
min_val, max_val = -128, 127
2401+
elif out_dtype == "uint16":
2402+
min_val, max_val = 0, 65535
2403+
elif out_dtype == "int16":
2404+
min_val, max_val = -32768, 32767
2405+
else:
2406+
min_val, max_val = 0, 255
2407+
2408+
clipped = relax.op.clip(y_val, min_val, max_val)
2409+
return relax.op.astype(clipped, out_dtype)
2410+
2411+
22452412
class ConvTranspose(OnnxOpConverter):
22462413
"""Converts an onnx ConvTranspose node into an equivalent Relax expression."""
22472414

@@ -6762,6 +6929,7 @@ def _get_convert_map():
67626929
"MaxUnpool": MaxUnpool,
67636930
"Conv": Conv,
67646931
"ConvTranspose": ConvTranspose,
6932+
"QLinearConv": QLinearConv,
67656933
"Flatten": Flatten,
67666934
"Identity": Identity,
67676935
"Dropout": Dropout,

0 commit comments

Comments
 (0)