onnxruntime
bcc9d30b - Handle non-finite ROI coordinates in CropAndResize (#29605)

Commit
2 days ago
Handle non-finite ROI coordinates in CropAndResize (#29605) ### Description The `CropAndResize` CPU contrib op (`com.microsoft::CropAndResize`) computes bilinear/nearest interpolation indices from the ROI box coordinates. The bounds guards were written as `if (in_y < 0 || in_y > height - 1)` / `if (in_x < 0 || in_x > width - 1)`. Because every comparison involving a NaN is false, a non-finite (NaN or ±inf) ROI coordinate slipped past both comparisons, skipped the extrapolation `continue`, and reached the integer index computation (`(int)floorf(NaN)`) with an invalid value. This is a robustness/correctness gap: ROI coordinates are a runtime input, and non-finite values are not handled gracefully. ### Fix - **NaN-safe bounds guards.** Rewrite both guards into negated-conjunction form: `if (!(in_y >= 0 && in_y <= height - 1))` / `if (!(in_x >= 0 && in_x <= width - 1))`. This is logically identical for all finite coordinates, but is `true` for NaN/±inf, so non-finite coordinates now take the extrapolation branch and are filled with `extrapolation_value` (matching the documented behavior for out-of-range coordinates). - **crop_size validation.** Add a Status-based `ORT_RETURN_IF_NOT(crop_height > 0 && crop_width > 0, ...)` check in `Compute` so non-positive crop sizes are rejected with a clear error rather than producing degenerate work. - **Index arithmetic hardening.** Use `SafeInt<int64_t>` for the interpolation index computation, which allows the now-unnecessary MSVC 26451 (arithmetic-overflow) suppression pragma to be removed. No behavior change for valid finite ROIs. CPU-only — there is no CUDA `CropAndResize` kernel. ### Tests Added to `onnxruntime/test/contrib_ops/crop_and_resize_op_test.cc`: - NaN ROI coordinate → extrapolation (bilinear height, bilinear width, and nearest). - ±inf ROI coordinate → extrapolation. - Finite boundary values (exact `[0, 1]` identity crop and just-outside `1.0001`) — no regression. - Non-positive `crop_size` (`{0, 2}` and `{-1, 2}`) rejected. - Out-of-range batch index rejected. Run with: ``` onnxruntime_provider_test --gtest_filter='CropAndResizeTest.*' ``` All 10 CropAndResize tests pass (5 new + 5 pre-existing, no regression). An AddressSanitizer build can additionally corroborate the index handling in CI. ### Motivation and Context Handles non-finite ROI coordinates gracefully and makes the CropAndResize index arithmetic robust, consistent with how out-of-range coordinates are already treated (extrapolation). Signed-off-by: Tita Wang <titaiwang@microsoft.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Author
Parents
Loading