Check weight shape dimensions in ConvTranspose shape inference msrc116345#28524
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR targets improved robustness for the contrib op ConvTransposeWithDynamicPads by hardening its schema shape inference against invalid weight (W) ranks and adding regression tests for rank-0 and rank-1 weight inputs.
Changes:
- Added a weight-shape rank guard in
convTransposeWithDynamicPadsShapeInferencebefore accessingW.dim(1). - Reused a cached
weight_shapefor computing the output channel dimension. - Added two unit tests covering rank-0 (scalar) and rank-1 weights expecting failure.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
onnxruntime/core/graph/contrib_ops/contrib_defs.cc |
Adds a weight rank check in the ConvTransposeWithDynamicPads shape inference function and uses a cached weight_shape in channel inference. |
onnxruntime/test/contrib_ops/conv_transpose_with_dynamic_pads_test.cc |
Adds negative tests to ensure invalid W ranks are rejected. |
Comments suppressed due to low confidence (1)
onnxruntime/test/contrib_ops/conv_transpose_with_dynamic_pads_test.cc:49
- Same as the rank-0 case: please assert an expected failure message substring for this rank-1 test so it validates the intended weight-rank rejection path (rather than any downstream shape/attribute mismatch).
test.AddOutput<float>("Y", {}, std::vector<float>{0.0f});
test.Run(OpTester::ExpectResult::kExpectFailure, "", {kTensorrtExecutionProvider});
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
edgchen1
reviewed
May 18, 2026
This was referenced May 18, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 9 out of 9 changed files in this pull request and generated 8 comments.
Comments suppressed due to low confidence (1)
onnxruntime/core/providers/cuda/nn/conv_transpose.cc:382
- For dynamic_padding=true, pads are populated from the Pads input without validating that its element count matches 2*kernel_shape.size(). ComputePadsAndOutputShape writes to p_pads->at(dim) / at(rank+dim), so a too-short Pads tensor can trigger out_of_range/exception instead of a clean INVALID_ARGUMENT Status. Consider validating Pads rank/size up-front (similar to the CPU PrepareForCompute path) before calling ComputePadsAndOutputShape.
ORT_RETURN_IF_ERROR(conv_transpose_attrs_.ComputePadsAndOutputShape(input_shape, num_output_channels, kernel_shape,
strides, dilations, local_output_padding, N, &pads, &y_dims, channels_last));
s_.y_dims = gsl::make_span(y_dims);
s_.Y = context->Output(0, s_.y_dims);
justinchuby
pushed a commit
to onnx/onnx
that referenced
this pull request
May 19, 2026
This pull request strengthens shape inference and error handling for the `MaxUnpool` and `ConvTranspose` ONNX operators. It adds stricter input validation to prevent silent failures or crashes when given invalid input shapes, and introduces new tests to ensure these cases are properly handled. **Shape inference validation improvements:** * Added a check in `maxUnpoolShapeInference` to ensure the indices input exists and has at least 2 dimensions, raising a shape inference error otherwise. * In `convTransposeShapeInference`, added explicit checks to ensure both the input tensor and the weight tensor have at least 2 dimensions, raising clear errors if not. * Updated logic in `convTransposeShapeInference` to consistently use the local `weight_shape` variable for shape calculations and checks, improving code clarity and correctness. [[1]](diffhunk://#diff-b9e98b88715e8a7e56c0dfa317f21a39582f919c87185f827c4a011ad9010639L1124-R1141) [[2]](diffhunk://#diff-b9e98b88715e8a7e56c0dfa317f21a39582f919c87185f827c4a011ad9010639L1194-R1206) **Test coverage enhancements:** * Added new unit tests to verify that shape inference raises appropriate errors for invalid input shapes, such as rank-0 or rank-1 indices/weights in `MaxUnpool` and `ConvTranspose`. **References** microsoft/onnxruntime#28524 microsoft/onnxruntime#28550 --------- Signed-off-by: Dmitri Smirnov <dmitrism@microsoft.com>
yuslepukhin
added a commit
that referenced
this pull request
May 21, 2026
This pull request enhances the robustness of the `MaxUnpool` operator in ONNX Runtime by adding additional input validation and expanding test coverage for invalid input scenarios. The changes improve error handling for mismatched shapes, invalid dimensions, and other edge cases, ensuring the operator fails gracefully and predictably when given incorrect inputs. **Operator input validation improvements:** * Added runtime checks in `MaxUnpool::Compute` to ensure the `kernel_shape` rank matches the expected pooling dimensions, and that the indices tensor is present and correctly shaped. * Added validation to ensure that computed output dimensions are positive, with descriptive error messages if not. * Enforced that the `output_shape` tensor, if provided, must have the same number of elements as the rank of the input tensor. **Test coverage enhancements:** * Introduced multiple new tests in `unpool_op_test.cc` to cover invalid input cases, including mismatched indices shapes, rank-0 and rank-2 input tensors, negative indices, and incorrect `output_shape` element counts. These tests confirm that the operator fails with appropriate error messages in these scenarios. **References** onnx/onnx#7997 #28524
edgchen1
reviewed
May 22, 2026
edgchen1
previously approved these changes
May 26, 2026
tadani3
pushed a commit
to tadani3/onnx
that referenced
this pull request
May 26, 2026
This pull request strengthens shape inference and error handling for the `MaxUnpool` and `ConvTranspose` ONNX operators. It adds stricter input validation to prevent silent failures or crashes when given invalid input shapes, and introduces new tests to ensure these cases are properly handled. **Shape inference validation improvements:** * Added a check in `maxUnpoolShapeInference` to ensure the indices input exists and has at least 2 dimensions, raising a shape inference error otherwise. * In `convTransposeShapeInference`, added explicit checks to ensure both the input tensor and the weight tensor have at least 2 dimensions, raising clear errors if not. * Updated logic in `convTransposeShapeInference` to consistently use the local `weight_shape` variable for shape calculations and checks, improving code clarity and correctness. [[1]](diffhunk://#diff-b9e98b88715e8a7e56c0dfa317f21a39582f919c87185f827c4a011ad9010639L1124-R1141) [[2]](diffhunk://#diff-b9e98b88715e8a7e56c0dfa317f21a39582f919c87185f827c4a011ad9010639L1194-R1206) **Test coverage enhancements:** * Added new unit tests to verify that shape inference raises appropriate errors for invalid input shapes, such as rank-0 or rank-1 indices/weights in `MaxUnpool` and `ConvTranspose`. **References** microsoft/onnxruntime#28524 microsoft/onnxruntime#28550 --------- Signed-off-by: Dmitri Smirnov <dmitrism@microsoft.com> Signed-off-by: Tommaso Adani <tommasoadani@microsoft.com>
edgchen1
approved these changes
May 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request introduces comprehensive validation and error handling improvements for the ConvTranspose operator across CPU, CUDA, WebGPU, and XNNPACK backends, as well as in shape inference and unit tests. The main focus is to ensure that invalid input shapes (especially rank-0 or rank-1 tensors) are properly detected and reported, preventing undefined behavior and improving robustness. Additionally, error messages are clarified, and several helper functions now return
Statusfor better error propagation.Validation and Error Handling Improvements:
Xand filterWtensors have at least 3 dimensions, returning clear error messages if not. ([[1]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8R65-R79),[[2]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-d1bbcb0542b5acea587ac929cd6362cfd11172c522505c6db8b457a9d470c63dR273-R289),[[3]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-b615243d0702e9613bd815173108306495b0f690294001e606823b77322f6fafR22-L28))ConvTransposeWithDynamicPadsnow fails gracefully with descriptive errors if input or weight tensors have fewer than 2 dimensions. ([onnxruntime/core/graph/contrib_ops/contrib_defs.ccL62-R67](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-81f57d9adc2cce94f85a2949a895b7ff82efcc13d05e23ee6567661f0fecb7c0L62-R67))output_paddingand dynamic pads have correct sizes, and thatoutput_paddingvalues are within ONNX-specified limits. ([[1]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8R138-R153),[[2]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8R171-R187))Refactoring for Robustness:
ComputePadsAndOutputShapeandComputeTransposePadAndOutputShapenow returnStatus, allowing errors to propagate and be handled appropriately rather than causing crashes or silent failures. ([[1]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8L165-R234),[[2]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8L194-R262),[[3]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8L220-R282),[[4]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8R291-R302))ORT_RETURN_IF_ERRORorORT_THROW_IF_ERROR. ([[1]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8R171-R187),[[2]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-d1bbcb0542b5acea587ac929cd6362cfd11172c522505c6db8b457a9d470c63dL362-R379),[[3]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-b615243d0702e9613bd815173108306495b0f690294001e606823b77322f6fafL48-R60),[[4]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-6a2f8672090f25850b90b266aff3c7212552fc81b14bb7b539e9e5161c9fd526L494-R497))Unit Test Enhancements:
[onnxruntime/test/contrib_ops/conv_transpose_with_dynamic_pads_test.ccR22-R56](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-cb5bfc51d0c8096922eb674d142f0e970d5becd140b47bdfd7729a06a818b598R22-R56))Minor Code Quality Improvements:
BufferUniquePtrimmediately to prevent leaks if exceptions are thrown. ([onnxruntime/core/providers/cpu/nn/conv_transpose.ccR79-R89](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-0dcb5a9c8ba0c4e67940e9d77f77cb706bbf82d67bf6757967099b0a69c797b5R79-R89))SafeIntfor overflow protection). ([[1]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8R22),[[2]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8R291-R302))Summary of Most Important Changes:
1. Validation and Error Handling
[[1]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8R65-R79),[[2]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-d1bbcb0542b5acea587ac929cd6362cfd11172c522505c6db8b457a9d470c63dR273-R289),[[3]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-b615243d0702e9613bd815173108306495b0f690294001e606823b77322f6fafR22-L28))ConvTransposeWithDynamicPadsfails with descriptive errors for invalid input or weight tensor ranks. ([onnxruntime/core/graph/contrib_ops/contrib_defs.ccL62-R67](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-81f57d9adc2cce94f85a2949a895b7ff82efcc13d05e23ee6567661f0fecb7c0L62-R67))output_paddingand dynamic pads sizes and values, with ONNX spec compliance. ([[1]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8R138-R153),[[2]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8R171-R187))2. Error Propagation and Refactoring
Statusand propagate errors; all call sites updated to handle these errors. ([[1]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8L165-R234),[[2]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8L194-R262),[[3]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8L220-R282),[[4]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8R291-R302),[[5]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-d1bbcb0542b5acea587ac929cd6362cfd11172c522505c6db8b457a9d470c63dL362-R379),[[6]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-b615243d0702e9613bd815173108306495b0f690294001e606823b77322f6fafL48-R60),[[7]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-6a2f8672090f25850b90b266aff3c7212552fc81b14bb7b539e9e5161c9fd526L494-R497))3. Unit Testing
[onnxruntime/test/contrib_ops/conv_transpose_with_dynamic_pads_test.ccR22-R56](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-cb5bfc51d0c8096922eb674d142f0e970d5becd140b47bdfd7729a06a818b598R22-R56))4. Code Quality
[[1]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-0dcb5a9c8ba0c4e67940e9d77f77cb706bbf82d67bf6757967099b0a69c797b5R79-R89),[[2]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8R22),[[3]](https://github.com/microsoft/onnxruntime/pull/28524/files#diff-72fa27d94d5d92dd1e78ff510ef9a84d1ad74426c19af9722cf6511f8d38a5a8R291-R302))