Address resize shortcomings

Merged
YU
5mo ago
+281-20

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:
    The axes attribute is validated after negative-axis normalization once the input rank is known — at construction when the rank is statically available, otherwise on the first Compute(). 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 (no NaN or Inf allowed), and for non-Resize modes, scale values must be ≥ 1. This prevents invalid scaling operations.

ROI and Sizes Validation

  • ROI Length Check:
    When axes are provided, the roi input length must be either 2 * len(axes) (per-axis ROI) or 2 * rank (default ROI). Anything else is rejected. The default-ROI path no longer scatters spurious zeros into the canonical [0..0, 1..1] buffer when axes is partial.
  • Sizes/Axes Count Consistency:
    If axes are given, the number of elements in the sizes input must match the number of axes, ensuring correct output shape computation.

Error Propagation and Refactoring

  • Status Return for ROI Calculation:
    The ComputeROIWithAxes function now returns a Status object, 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 of NaN and Inf values 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.