You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by "mvanhorn (via GitHub)" <gi...@apache.org> on 2026/06/04 06:44:52 UTC
[PR] fix: ONNX ConvTranspose bias broadcast mismatch in Relax frontend [tvm]
mvanhorn opened a new pull request, #19672:
URL: https://github.com/apache/tvm/pull/19672
## Summary
ONNX models with a `ConvTranspose` node that carries a bias now import through the Relax frontend instead of failing on a broadcast check, matching the output ONNX Runtime produces.
## Why this matters
Issue [#18600](https://github.com/apache/tvm/issues/18600) reports `InternalError: ... relax.add ... dim 1 is 16 and ... 32, which are not broadcastable` when importing a ConvTranspose model that runs fine under ONNX Runtime and onnx's ReferenceEvaluator. The converter derived output channels from `weight.shape[0]`, but for ConvTranspose the weight layout is `(in_channels, out_channels/groups, *kernel)`, so true output channels are `weight.shape[1] * groups`. The 16-length bias was then broadcast against a wrong 32-channel shape.
## Testing
The `ConvTranspose` converter in `python/tvm/relax/frontend/onnx/onnx_frontend.py` now computes `out_channels = weight.shape[1] * groups`, reshapes the bias to `[1, out_channels, 1, ...]`, and raises a clear `ValueError` when a static bias length genuinely mismatches the output channels. Tests in `tests/python/relax/test_frontend_onnx.py` were extended so `_verify_conv_transpose` checks 1D/2D/3D variants with `group=1` and `group=2`, plus a new `test_conv_transpose_invalid_bias_channel_count` asserting the error path. Covered by the updated tests in this PR; full suite runs in CI.
Fixes #18600
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] fix: ONNX ConvTranspose bias broadcast mismatch in Relax frontend [tvm]
Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on code in PR #19672:
URL: https://github.com/apache/tvm/pull/19672#discussion_r3354052581
##########
python/tvm/relax/frontend/onnx/onnx_frontend.py:
##########
@@ -1814,13 +1817,42 @@ def _impl_v1(cls, bb, inputs, attr, params):
padding=attr.get("pads", 0),
output_padding=output_padding,
dilation=dilations,
- groups=attr.get("group", 1),
+ groups=groups,
data_layout=data_layout,
kernel_layout=kernel_layout,
)
if inputs[2] is not None:
- bias = relax.op.reshape(inputs[2], [1, -1] + [1] * (ndim - 2))
+ bias_shape = inputs[2].struct_info.shape
+ if hasattr(inputs[2].struct_info, "ndim"):
+ bias_ndim = inputs[2].struct_info.ndim
+ else:
+ bias_ndim = len(bias_shape)
Review Comment:

Using `hasattr(inputs[2].struct_info, "ndim")` to check for the presence of `ndim` is safe, but if `inputs[2].struct_info` does not have `ndim` and `bias_shape` is `None` (which can happen if the shape is completely dynamic/unknown), calling `len(bias_shape)` will raise a `TypeError`. We should add a fallback or check to ensure `bias_shape` is not `None` before calling `len()` on it.
```suggestion
if hasattr(inputs[2].struct_info, "ndim") and inputs[2].struct_info.ndim is not None:
bias_ndim = inputs[2].struct_info.ndim
elif bias_shape is not None:
bias_ndim = len(bias_shape)
else:
bias_ndim = -1
```
##########
python/tvm/relax/frontend/onnx/onnx_frontend.py:
##########
@@ -1814,13 +1817,42 @@ def _impl_v1(cls, bb, inputs, attr, params):
padding=attr.get("pads", 0),
output_padding=output_padding,
dilation=dilations,
- groups=attr.get("group", 1),
+ groups=groups,
data_layout=data_layout,
kernel_layout=kernel_layout,
)
if inputs[2] is not None:
- bias = relax.op.reshape(inputs[2], [1, -1] + [1] * (ndim - 2))
+ bias_shape = inputs[2].struct_info.shape
+ if hasattr(inputs[2].struct_info, "ndim"):
+ bias_ndim = inputs[2].struct_info.ndim
+ else:
+ bias_ndim = len(bias_shape)
+ if bias_ndim != 1:
+ raise ValueError(f"ConvTranspose bias must be a 1D tensor, but got ndim={bias_ndim}")
+
+ def _as_static_int(dim):
+ try:
+ return int(dim)
+ except (TypeError, ValueError, TVMError):
+ return None
Review Comment:

The `_as_static_int` helper catches `TVMError`, but in some environments or FFI boundaries, TVM might raise a standard `RuntimeError` or other custom exceptions. To make this helper more robust and prevent unexpected compilation failures, we should also catch `RuntimeError`.
```suggestion
def _as_static_int(dim):
try:
return int(dim)
except (TypeError, ValueError, RuntimeError, TVMError):
return None
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] fix: ONNX ConvTranspose bias broadcast mismatch in Relax frontend [tvm]
Posted by "tqchen (via GitHub)" <gi...@apache.org>.
tqchen closed pull request #19672: fix: ONNX ConvTranspose bias broadcast mismatch in Relax frontend
URL: https://github.com/apache/tvm/pull/19672
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] fix: ONNX ConvTranspose bias broadcast mismatch in Relax frontend [tvm]
Posted by "mvanhorn (via GitHub)" <gi...@apache.org>.
mvanhorn commented on PR #19672:
URL: https://github.com/apache/tvm/pull/19672#issuecomment-4880245875
Added in 7f5415f: test_conv_transpose_valid_bias_channel_count builds exactly the case you described - input [1, 32, 8, 8], weight [32, 16, 3, 3], bias [16], group=1, output channels 16 - and checks the import result against ONNX Runtime. On main the old broadcast shape rejects it; with this PR it passes.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org