[Bug][Relax][ONNX] Runtime float16 Mean overflows for inputs that fold to a finite result

未关闭
#21,104 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

@Nanmur 已经在做这个了。

开始于 2026年12月1日。

  • #21109 来自 @Nanmur —— 未关闭

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
python

调研方向

The report identifies the failing path as Relax ONNX Mean lowering to legalized TIR on LLVM CPU; it does not name source or test files. Start by tracing that lowering and the generated accumulator dtype, then reproduce with the supplied float16 graph-input example. Done means runtime Mean returns the expected finite vector for float16 inputs while preserving the reported float32 and constant-folding behavior.

由索引模型根据 Issue 内容生成。

描述

needs-triage type: bug
Expected behavior

ONNX Mean(x, x) should return x when each element and its mean are finite and exactly representable. With two identical float16 inputs [32752, 32768, -32752, -32768], the expected result is the same vector whether the operands are Constant nodes or graph inputs. ONNX Mean specification

Actual behavior

On LLVM CPU, the same TVM build returns different results for the two input representations. Only the float16 runtime case changes finite values to infinities.

Dtype and operand representation TVM output ONNX Runtime CPU output
float16, Constant nodes [32752, 32768, -32752, -32768] [32752, 32768, -32752, -32768]
float16, graph inputs [32752, inf, -32752, -inf] [32752, 32768, -32752, -32768]
float32, both representations [32752, 32768, -32752, -32768] [32752, 32768, -32752, -32768]

The runtime path's legalized TIR uses a float16 accumulator and divides the sum by T.float16(2.0). The sum of two 32752 values is exactly 65504, the largest finite float16 value. Two 32768 values sum to 65536, which overflows before division even though the mean is representable. The negative inputs show the same behavior with the opposite sign.

ONNX ReferenceEvaluator 1.21.0 also emits an overflow warning during float16 addition and returns [32752, inf, -32752, -inf] for both representations. The expected vector follows directly from Mean(x, x) = x and agrees with ONNX Runtime CPU.

Environment
  • TVM 0.28.dev0, native libraries built from commit 3d89c4991c5a37492f470800231a5ea8f57a95c6.
  • Ubuntu 26.04.1 LTS under WSL2, Linux 6.18.58.1, x86_64.
  • Intel Core i7-14700KF, LLVM 21.1.8.
  • Python 3.10.22, NumPy 1.26.4, ONNX 1.21.0, ONNX Runtime 1.23.2.
  • LLVM CPU target, Relax VM, default build pipeline.
  • The reproduction loaded the TVM Python frontend from commit 1091f2fc97187bdef99272cb2c598dc3dd572d7c with the native build above. The source changes between the two commits are limited to an LLM kernel helper and its test.
Steps to reproduce

Save the script as repro_mean.py and run python repro_mean.py in a TVM environment with LLVM support. It generates and checks four local ONNX models using opset 13.

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

for dtype, elem_type in [(np.float16, TensorProto.FLOAT16), (np.float32, TensorProto.FLOAT)]:
    values = np.array([32752, 32768, -32752, -32768], dtype=dtype)
    for constant in (True, False):
        inputs, nodes, feeds = [], [], {}
        for name in ("x", "y"):
            if constant:
                nodes.append(helper.make_node("Constant", [], [name], value=numpy_helper.from_array(values)))
            else:
                inputs.append(helper.make_tensor_value_info(name, elem_type, [4]))
                feeds[name] = values
        nodes.append(helper.make_node("Mean", ["x", "y"], ["z"]))
        output = helper.make_tensor_value_info("z", elem_type, [4])
        graph = helper.make_graph(nodes, "mean", inputs, [output])
        model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", 13)], ir_version=8)
        onnx.checker.check_model(model, full_check=True)
        session = ort.InferenceSession(model.SerializeToString(), providers=["CPUExecutionProvider"])
        expected = session.run(None, feeds)[0]
        module = from_onnx(model, keep_params_in_input=False)
        vm = relax.VirtualMachine(relax.build(module, target="llvm"), tvm_tirx.cpu())
        actual = vm["main"](*[tvm_tirx.runtime.tensor(x) for x in feeds.values()]).numpy()
        print(dtype.__name__, "constant" if constant else "runtime", "ORT", expected.tolist(), "TVM", actual.tolist())
Related issue

In #20146, the ONNX frontend passed constant operands as positional arguments to NumPy's mean, producing a scalar or raising a TypeError during import. Here, constant folding produces the expected result. The overflow occurs during execution when the lowered runtime reduction accumulates in float16 before dividing. Apache TVM issue #20146

Triage
  • needs-triage
主要语言
Python
星标
13.9k
派生
4.1k
平均合并
4 天 22 小时
30 天内合并 PR
22

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

apache/tvm 的其他 Issue

查看 apache/tvm 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。