Skip to content

CPU Resize with half_pixel + round_prefer_ceil selects wrong nearest index #28291

@ALinrunrun

Description

@ALinrunrun

Describe the issue

ONNX Runtime CPUExecutionProvider produces an incorrect result for Resize with mode="nearest", coordinate_transformation_mode="half_pixel", and nearest_mode="round_prefer_ceil".

For a 1D resize from width 20 to 6, the source coordinate for output element 4 is:

(4 + 0.5) * (20 / 6) - 0.5 = 14.5

With round_prefer_ceil, this tie should round to index 15, but ORT CPU returns the value from index 14.

In the reproducer below, the expected value at output element 4 is 15 / 19 ~= 0.7894737, while ORT returns 14 / 19 ~= 0.7368421.

To reproduce

import numpy as np
import onnx
from onnx import TensorProto, helper
import onnxruntime as ort

x = np.linspace(0, 1, 20, dtype=np.float32).reshape(1, 1, 1, 20)

X = helper.make_tensor_value_info("X", TensorProto.FLOAT, [1, 1, 1, 20])
Y = helper.make_tensor_value_info("Y", TensorProto.FLOAT, [1, 1, 1, 6])
sizes = helper.make_tensor("sizes", TensorProto.INT64, [4], [1, 1, 1, 6])

node = helper.make_node(
    "Resize",
    ["X", "", "", "sizes"],
    ["Y"],
    mode="nearest",
    coordinate_transformation_mode="half_pixel",
    nearest_mode="round_prefer_ceil",
)

graph = helper.make_graph([node], "resize_nearest_tie", [X], [Y], initializer=[sizes])
model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", 13)])
model.ir_version = 8
onnx.checker.check_model(model)

sess = ort.InferenceSession(
    model.SerializeToString(),
    providers=["CPUExecutionProvider"],
)

ort_out = sess.run(None, {"X": x})[0].flatten()

expected_indices = [1, 4, 8, 11, 15, 18]
expected = x.flatten()[expected_indices]

print("Expected indices:", expected_indices)
print("Expected:        ", expected)
print("ORT output:      ", ort_out)
print("Expected elem 4: ", expected[4])
print("ORT elem 4:      ", ort_out[4])
print("PASS=", np.allclose(ort_out, expected, rtol=1e-5, atol=1e-5))

Urgency

Expected output

Expected indices: [1, 4, 8, 11, 15, 18]
Expected:         [0.05263158 0.21052632 0.42105263 0.57894737 0.7894737  0.94736844]
ORT output:       [0.05263158 0.21052632 0.42105263 0.57894737 0.7894737  0.94736844]
Expected elem 4:  0.7894737
ORT elem 4:       0.7894737
PASS=True

Actual output

Expected indices: [1, 4, 8, 11, 15, 18]
Expected:         [0.05263158 0.21052632 0.42105263 0.57894737 0.7894737  0.94736844]
ORT output:       [0.05263158 0.21052632 0.42105263 0.57894737 0.7368421  0.94736844]
Expected elem 4:  0.7894737
ORT elem 4:       0.7368421
PASS=False

Platform

Linux

OS Version

Linux-6.17.0-20-generic-x86_64-with-glibc2.39

ONNX Runtime Installation

Released Package

ONNX Runtime Version or Commit ID

1.25.1

ONNX Runtime API

Python

Architecture

X86

Execution Provider

Default CPU

Execution Provider Library Version

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions