Address resize shortcomings
Merged
YU
5mo ago+281+281+012345678901234567890123456789-20-20-01234567890123456789
This pull request strengthens input validation and error handling for the ONNX Resize and Upsample operators, particularly around the axes, scales, and roi attributes. It ensures compliance with the ONNX specification and prevents invalid or ambiguous input from causing incorrect behavior or crashes. The changes also introduce comprehensive unit tests to verify these new validation paths.
Key improvements include:
Validation and Error Handling
- Axes Attribute Validation:
Theaxesattribute is validated after negative-axis normalization once the input rank is known — at construction when the rank is statically available, otherwise on the firstCompute(). This rejects duplicates that collide only after canonicalization (e.g.{-1, rank-1}) as well as out-of-range entries, with clear error messages. The normalized axes are cached to keep the inference hot path allocation-free. - Scales Validation:
All scale values are checked for finiteness (noNaNorInfallowed), and for non-Resize modes, scale values must be ≥ 1. This prevents invalid scaling operations.
ROI and Sizes Validation
- ROI Length Check:
Whenaxesare provided, theroiinput length must be either2 * len(axes)(per-axis ROI) or2 * rank(default ROI). Anything else is rejected. The default-ROI path no longer scatters spurious zeros into the canonical[0..0, 1..1]buffer whenaxesis partial. - Sizes/Axes Count Consistency:
Ifaxesare given, the number of elements in thesizesinput must match the number of axes, ensuring correct output shape computation.
Error Propagation and Refactoring
- Status Return for ROI Calculation:
TheComputeROIWithAxesfunction now returns aStatusobject, and all its callers (CPU, CUDA, WebGPU EPs) are updated to propagate errors instead of assuming success.
Testing
- Comprehensive Unit Tests:
New tests cover negative and out-of-range axes, post-normalization duplicate axes (including{-1, rank-1}), mismatched sizes/axes counts, non-finite scale values, and invalid ROI length. These tests ensure that invalid input is consistently rejected across all relevant code paths and work correctly in builds without exceptions.
Miscellaneous
- Test File Improvements:
The test file now includes<limits>to support testing ofNaNandInfvalues in scales.
These changes collectively make the Resize and Upsample operators more robust and standards-compliant, and help prevent subtle bugs due to invalid input.