[Bug] [Relax] float16 Gelu rounds a representable negative result to zero

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

维护者通常 3 天内回复

@Anurag1428 已经在做这个了。

开始于 2026年12月1日。

  • #21107 来自 @Anurag1428 —— 未关闭

评估

难度
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 内容生成。

描述

needs-triage type: bug
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.erfc to 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 小时
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 摘要。