[Bug][Relax][ONNX] Runtime float16 Mean overflows for inputs that fold to a finite result
メンテナーはふだん 3 日以内に返信
評価
- 難易度
- 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 の本文から書いたものです。
説明
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 commit3d89c4991c5a37492f470800231a5ea8f57a95c6. - 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
1091f2fc97187bdef99272cb2c598dc3dd572d7cwith 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時間
- マージ済み PR(30日)
- 22
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
apache/tvm のほかの issue
-
[Bug] [Relax] `relax.op.unique(return_inverse=True)` returns an input-shaped inverse for N-D input when `axis` is None対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 3 日以内に返信
-
[Bug][TOPI] `topi.scanop` (`cumsum` / `cumprod`) performs out-of-bounds write/read on empty tensorsオープンneeds-triage type: bug
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
メンテナーはふだん 3 日以内に返信
-
[Bug] [Relax] take_backward does not normalize negative axis causing heap OOB write and wrong gradients対応中かも @pierceluu10 が 4 日前に担当しました。 オープンneeds-triage type: bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 3 日以内に返信
-
[Bug][Relax][ONNX] NonZero returns the wrong output shape for scalar inputs対応中かも @tandede が 9 日前に担当しました。 オープンneeds-triage type: bug
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
メンテナーはふだん 3 日以内に返信
-
needs-triage type: bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 3 日以内に返信
似ている issue
-
難易度 1/5 1〜3時間 初心者へのやさしさ 78/100
web-push-libs/pywebpush#216 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
AgriciDaniel/claude-seo#572 ·
メンテナーはふだん 5 日以内に返信
-
area:print-dispatch
難易度 2/5 1〜3時間 初心者へのやさしさ 92/100
メンテナーはふだん 1 日以内に返信
-
area: docs domain: technical-writing good first issue type: documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 80/100
Deepak3699/Ai_Mentor#919 ·
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信