Skip to content

[Bug] [Relax][TOPI] Max and min reductions replace infinities with finite extrema #20935

Open

Description

@sunni13830

Expected behavior

The maximum of a nonempty all-negative-infinity tensor is -inf; the minimum of an all-positive-infinity tensor is +inf. ONNX ReferenceEvaluator agrees.

Actual behavior

On LLVM CPU, ReduceMax([-inf,-inf,-inf]) returns -3.4028234663852886e38 and ReduceMin([inf,inf,inf]) returns +3.4028234663852886e38. Finite-value controls return 7 correctly. This concerns nonempty infinity-only inputs, separately from CUDA NaN propagation in #19518 and ArgMin indices in #20866.

Environment

TVM 0.28.dev0, LLVM 18.1.3, Linux x86_64; Python 3.12.3, NumPy 2.5.4, ONNX 1.22.0. Installed binary build revision is unknown. Current main was inspected statically, not rebuilt.

Steps to reproduce

import numpy as np
import onnx
import tvm
from onnx import TensorProto as T, helper as h
from onnx.reference import ReferenceEvaluator
from tvm import relax
from tvm.relax.frontend.onnx import from_onnx

for operation, extreme in [("ReduceMax", -np.inf), ("ReduceMin", np.inf)]:
    model = h.make_model(h.make_graph([
        h.make_node(operation, ["x"], ["y"], keepdims=0)], "reduce",
        [h.make_tensor_value_info("x", T.FLOAT, [3])],
        [h.make_tensor_value_info("y", T.FLOAT, [])]),
        opset_imports=[h.make_opsetid("", 13)], ir_version=10)
    onnx.checker.check_model(model, full_check=True)
    mod = from_onnx(model, keep_params_in_input=False)
    vm = relax.VirtualMachine(relax.build(mod, target="llvm"), tvm.cpu())
    normal = np.full(3, 7, dtype="float32")
    np.testing.assert_array_equal(vm["main"](tvm.runtime.tensor(normal)).numpy(), 7)
    x = np.full(3, extreme, dtype="float32")
    reference = ReferenceEvaluator(model).run(None, {"x": x})[0]
    np.testing.assert_array_equal(reference, np.array(extreme, dtype="float32"))
    actual = vm["main"](tvm.runtime.tensor(x)).numpy()
    assert np.isfinite(actual) and abs(actual) == np.finfo("float32").max
    print(operation, "finite control passed; expected", extreme, "actual", actual.item())

Triage

  • needs-triage

Activity

  1. added
    needs-triagePRs or issues that need to be investigated by maintainers to find the right assignees to address it
    on Nov 7, 2026
  2. GoyalIshaan commented on Nov 9, 2026

    @GoyalIshaan

    Could I work on the max/min reduction identities here, with LLVM CPU regression tests for all-infinite inputs and finite controls?

    This request was generated and posted by an AI assistant on my behalf; I would do the implementation and submit the PR myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-triagePRs or issues that need to be investigated by maintainers to find the right assignees to address ittype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions