[Bug] [Relax] float16 Gelu rounds a representable negative result to zero
メンテナーはふだん 3 日以内に返信
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 20/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 停滞
- 技術スタック
- python
調査の方向性
Start from the Relax gelu implementation and the legalization path that produces the shown TIR, then inspect the related tests for float16 inputs. Reproduce with the supplied script at x = -4 and compare direct float16 evaluation with widening to float32. Done means direct float16 Relax Gelu returns the representable negative result instead of negative zero; an open linked pull request is already addressing this issue.
索引モデルが issue の本文から書いたものです。
説明
Expected behavior
For a float16 input x = -4, relax.nn.gelu(x) should return the representable
float16 result -0.00012671947479248047 (approximately -1.267e-4). Applying
Gelu in float32 and converting the result back to float16 produces this value.
The same value is produced by ONNX Runtime CPU and by evaluating the float64
erfc formula and rounding to float16.
Actual behavior
The direct float16 Relax computation returns negative zero:
widen: False actual: -0.0 expected: -0.00012671947479248047
widen: True actual: -0.00012671947479248047 expected: -0.00012671947479248047
The following legalized TIR shows the float32 erf result converted to
float16 before the final combination:
T_multiply_1[v_ax0] = x[v_ax0] * T.float16(0.70710678118654757)
compute_1[v_i0_1] = T.erf(compute[v_i0_1]) # float32
compute_2[v_i0_2] = T.Cast("float16", compute_1[v_i0_2])
T_multiply_2[v_ax0_1] = compute_2[v_ax0_1] * T.float16(0.5)
T_add[v_ax0_2] = T.float16(0.5) + T_multiply_2[v_ax0_2]
At x = -4, erf(-4/sqrt(2)) is approximately -0.9999366575, which rounds
to -1.0 in float16. The following 0.5 + 0.5 * (-1.0) is zero, removing the
negative tail before multiplication by x.
An additional ONNX comparison used opset 20 and input [-5,-4,-3,-1,0,1].
Graph A contains one Gelu node with approximate="none"; graph B casts the
input to float32, applies Gelu, then casts the output to float16. At x=-4,
TVM graph A returns -0.0, while TVM graph B and ONNX Runtime CPU return
-1.267e-4. The ONNX ReferenceEvaluator also returns -0.0 for graph A, so it
does not independently validate this value. This differs from apache/tvm#18750,
which concerns routing the approximate="tanh" attribute.
Environment
- TVM 0.28.dev0; Python frontend and loaded dynamic library are from the same default LLVM build.
- Linux x86-64, LLVM CPU target, Relax VM runtime.
- Python 3.10; NumPy 1.26.4; ONNX 1.21.0; ONNX Runtime 1.23.2.
- The reproducer uses Python's standard-library
math.erfcto calculate the expected value.
Steps to reproduce
Run this standalone script in the environment above:
import math
import numpy as np
import tvm_tirx
from tvm import relax
x = np.array([-4], dtype=np.float16)
expected = np.float16(0.5 * x.item() * math.erfc(-x.item() / math.sqrt(2)))
for widen in (False, True):
builder = relax.BlockBuilder()
arg = relax.Var("x", relax.TensorType((1,), "float16"))
with builder.function("main", [arg]):
operand = relax.op.astype(arg, "float32") if widen else arg
result = relax.op.nn.gelu(operand)
builder.emit_func_output(relax.op.astype(result, "float16") if widen else result)
vm = relax.VirtualMachine(relax.build(builder.get(), target="llvm"), tvm_tirx.cpu())
actual = vm["main"](tvm_tirx.runtime.tensor(x)).numpy()[0]
print("widen:", widen, "actual:", float(actual), "expected:", float(expected))
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 日以内に返信