tvm icon indicating copy to clipboard operation
tvm copied to clipboard

[Bug] Relax ONNX ArgMax returns different index from ONNX Runtime when input contains NaN

Open ALinrunrun opened this issue 6 months ago • 2 comments

Expected behavior

TVM Relax should execute ONNX ArgMax consistently with ONNX Runtime when the input contains NaN.

For these inputs, ONNX Runtime matches NumPy's argmax behavior:

x = [2, NaN, 7, 4, 1]   -> index 1
x = [NaN, 2, 7, 4, 1]   -> index 0
x = [2, 4, 7, 1, NaN]   -> index 4

TVM Relax should return the same indices after importing the ONNX model through the Relax ONNX frontend.

Actual behavior

TVM Relax returns different indices for cases where NaN appears before the finite maximum:

> x=[2, NaN, 7, 4, 1]   np=1 ORT=1 TVM=2
> x=[NaN, 2, 7, 4, 1]   np=0 ORT=0 TVM=2
> x=[2, 4, 7, 1, NaN]   np=4 ORT=4 TVM=4

The discrepancy appears when importing an ONNX ArgMax model through the Relax ONNX frontend and compiling it for the llvm target.

Environment

TVM: 0.14 environment / Relax ONNX frontend ONNX Runtime: 1.23 Python: 3.11 Target: llvm OS: Linux

Steps to reproduce

import warnings

warnings.filterwarnings("ignore")

import numpy as np
import onnx
import onnxruntime as ort
import tvm
from onnx import TensorProto, helper
from tvm import relax
from tvm.relax.frontend.onnx import from_onnx


node = helper.make_node("ArgMax", ["x"], ["y"], axis=0, keepdims=0)

graph = helper.make_graph(
    [node],
    "g",
    [helper.make_tensor_value_info("x", TensorProto.FLOAT, [5])],
    [helper.make_tensor_value_info("y", TensorProto.INT64, [])],
)

model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", 18)])
model.ir_version = 9

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

mod = from_onnx(model)

with tvm.transform.PassContext(opt_level=3):
    ex = tvm.compile(mod, target=tvm.target.Target("llvm"))

vm = relax.VirtualMachine(ex, tvm.cpu())


def run_both(x):
    ort_out = int(sess.run(None, {"x": x})[0])

    out = vm["main"](tvm.runtime.tensor(x, tvm.cpu()))
    tvm_out = int((out[0] if isinstance(out, (list, tuple)) else out).numpy())

    numpy_out = int(np.argmax(x))
    return numpy_out, ort_out, tvm_out


cases = [
    np.array([2.0, np.nan, 7.0, 4.0, 1.0], dtype=np.float32),
    np.array([np.nan, 2.0, 7.0, 4.0, 1.0], dtype=np.float32),
    np.array([2.0, 4.0, 7.0, 1.0, np.nan], dtype=np.float32),
]

for x in cases:
    numpy_out, ort_out, tvm_out = run_both(x)
    print(f"x={x.tolist()} np={numpy_out} ORT={ort_out} TVM={tvm_out}")

Triage

  • needs-triage

ALinrunrun avatar May 13 '26 17:05 ALinrunrun

The ONNX ArgMax spec doesn't define NaN handling. ORT matches NumPy (NaN survives > comparisons and wins), but that's a reference-impl convention, not a spec requirement. TVM's topi.argmax drops NaN via the same val > current_max reducer — also self-consistent.

So this is really an ORT-compatibility gap, not a spec bug. Fixable on the Relax ONNX frontend by substituting NaN with +inf before argmax, but worth deciding whether TVM wants to commit to NumPy/ORT NaN semantics here.

swjng avatar May 15 '26 05:05 swjng

Implemented in https://github.com/apache/tvm/pull/19671.

mvanhorn avatar Jun 04 '26 06:06 mvanhorn