From 6448d4f33b451bfe8befdb913177f7aeb99455f4 Mon Sep 17 00:00:00 2001 From: Mathieu Poumeyrol Date: Thu, 6 Aug 2026 09:45:24 +0000 Subject: [PATCH 1/2] core: support tf_half_pixel_for_nn resize coordinate mode Resize rejected the legacy tf_half_pixel_for_nn coordinate transformation mode, so ONNX models using it failed to build. Add it to CoordTransformer as a length-independent, ROI-free coordinate map ((x + 0.5) / scale). --- core/src/ops/nn/resize.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/core/src/ops/nn/resize.rs b/core/src/ops/nn/resize.rs index 5c3f9f0f93..9ba7a12149 100644 --- a/core/src/ops/nn/resize.rs +++ b/core/src/ops/nn/resize.rs @@ -10,6 +10,7 @@ pub enum CoordTransformer { Asymmetric, PytorchHalfPixel, HalfPixelSymmetric, + TfHalfPixelForNn, } impl CoordTransformer { @@ -37,6 +38,7 @@ impl CoordTransformer { let offset = len_in as f32 / 2.0 * (1.0 - adjustment); offset + (x_out as f32 + 0.5) / scale - 0.5 } + CoordTransformer::TfHalfPixelForNn => (x_out as f32 + 0.5) / scale, } } @@ -47,6 +49,7 @@ impl CoordTransformer { CoordTransformer::Asymmetric => "asymmetric", CoordTransformer::PytorchHalfPixel => "pytorch_half_pixel", CoordTransformer::HalfPixelSymmetric => "half_pixel_symmetric", + CoordTransformer::TfHalfPixelForNn => "tf_half_pixel_for_nn", } } @@ -57,6 +60,7 @@ impl CoordTransformer { "asymmetric" => CoordTransformer::Asymmetric, "pytorch_half_pixel" => CoordTransformer::PytorchHalfPixel, "half_pixel_symmetric" => CoordTransformer::HalfPixelSymmetric, + "tf_half_pixel_for_nn" => CoordTransformer::TfHalfPixelForNn, s => bail!("coordinate_transformation_mode: {s}"), }) } @@ -466,12 +470,14 @@ impl TypedOp for Resize { } } -/// An axis length to build a probe plan on. `HalfPixel` and `Asymmetric` map -/// coordinates without consulting the axis lengths, so a symbolic axis can -/// still be probed on a stand-in; the others cannot. +/// An axis length to build a probe plan on. `HalfPixel`, `Asymmetric` and +/// `TfHalfPixelForNn` map coordinates without consulting the axis lengths, so a +/// symbolic axis can still be probed on a stand-in; the others cannot. pub fn probe_length(coord_transformer: &CoordTransformer, len: &TDim) -> Option { len.to_usize().ok().or(match coord_transformer { - CoordTransformer::HalfPixel | CoordTransformer::Asymmetric => Some(4), + CoordTransformer::HalfPixel + | CoordTransformer::Asymmetric + | CoordTransformer::TfHalfPixelForNn => Some(4), _ => None, }) } From aeb5576fff5a55d3a9385ad5e5b5ea8910413e89 Mon Sep 17 00:00:00 2001 From: Mathieu Poumeyrol Date: Thu, 6 Aug 2026 12:07:19 +0000 Subject: [PATCH 2/2] onnx: import opset-10 Resize with asymmetric coordinates Resize-10 has no coordinate_transformation_mode and is defined as asymmetric, but the importer left the opset-11 default of half_pixel, so opset-10 models resampled on the wrong grid. Force asymmetric in resize_10. Also gate, via since: in the onnx suite node list, a few ONNX 1.5.0/1.6.0 resize conformance tests whose bundled models predate or contradict the nearest attributes their reference outputs assume. --- onnx/src/ops/resize.rs | 3 ++- test-rt/suite-onnx/node.txt | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/onnx/src/ops/resize.rs b/onnx/src/ops/resize.rs index 700d35d438..9e29384406 100644 --- a/onnx/src/ops/resize.rs +++ b/onnx/src/ops/resize.rs @@ -1,7 +1,7 @@ use crate::model::ParsingContext; use crate::pb::*; use tract_hir::internal::*; -use tract_nnef::tract_core::ops::nn::resize::Interpolator; +use tract_nnef::tract_core::ops::nn::resize::{CoordTransformer, Interpolator}; use tract_nnef::tract_num_traits::Zero as _; use tract_onnx_opl::resize::{AspectRatio, CoordTransform, Nearest, Resize}; @@ -21,6 +21,7 @@ pub fn resize( fn resize_10(node: &NodeProto) -> TractResult { Ok(Resize { + coord_transformer: CoordTransform::Plain(CoordTransformer::Asymmetric), optional_roi_input: None, optional_scales_input: Some(1), optional_sizes_input: None, diff --git a/test-rt/suite-onnx/node.txt b/test-rt/suite-onnx/node.txt index a14d1f7bba..2b639f206d 100644 --- a/test-rt/suite-onnx/node.txt +++ b/test-rt/suite-onnx/node.txt @@ -488,6 +488,10 @@ test_reshape_reordered_dims test_reshape_reordered_last_dims input:data test_reshape_zero_and_negative_dim input:data test_reshape_zero_dim input:data +test_resize_upsample_nearest input:X since:11 +test_resize_upsample_sizes_nearest_ceil_half_pixel input:X since:12 +test_resize_upsample_sizes_nearest_floor_align_corners input:X since:12 +test_resize_upsample_sizes_nearest_round_prefer_ceil_asymmetric input:X since:12 test_resize.* input:X test_rnn_seq_length test_round