[Bug][TOPI] rsqrt with uint16 input fails during LLVM code generation

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

维护者通常 3 天内回复

@charan-rathore 已经在做这个了。

开始于 2026年12月2日。

  • #21114 来自 @charan-rathore —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
python
领域
backend, compilers

调研方向

Read python/tvm/topi/math.py around the rsqrt implementation linked in the issue and compare its dtype promotion guard with the signed integer case. Reproduce the failure using the provided uint16 example and check nearby TOPI tests for reciprocal square root coverage. Done means uint16 either compiles with the expected floating point results or reports the unsupported dtype during operator construction; an open linked pull request is already addressing this issue.

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

描述

needs-triage type: bug
Expected behavior

If topi.rsqrt supports unsigned integer inputs, I would expect it to promote
uint16 to a floating-point type as it does for int16. For the input
[1, 4, 16, 64], the expected reciprocal square roots are exactly
[1, 0.5, 0.25, 0.125]. If unsigned inputs are unsupported, could TOPI report
the unsupported dtype when constructing the operator?

Actual behavior

TOPI accepts all three inputs below and generates TIR. Only the direct
uint16 case fails at tvm.compile(..., target="llvm").

Input to rsqrt Generated TIR LLVM CPU result
int16 Casts to float32 before T.rsqrt [1, 0.5, 0.25, 0.125] (float32)
uint16 explicitly cast to float32 Applies T.rsqrt to the cast value Same values and dtype
uint16 Emits T.rsqrt(x[v_i0]) with a uint16 output buffer Compilation fails
RuntimeError: Check failed: (f) is false: Cannot find intrinsic declaration, possible type mismatch: llvm.sqrt

The earlier report used an int16 input. Its fix added promotion for signed
integers. At the commit tested here, the promotion guard in rsqrt checks
DataTypeCode.INT. Issue #17952 · PR #17953 · Implementation at the tested commit

Environment
  • TVM 0.28.dev0, built and tested at commit 84657c01e5d3672cfd299bd3d5c77bff3888a1a9.
  • Ubuntu 26.04.1 LTS on WSL2, Linux 6.18.58.1, x86_64.
  • Intel Core i7-14700KF, LLVM 21.1.8, target llvm.
  • Python 3.10.22, NumPy 1.26.4.
Steps to reproduce

Save as repro.py and run python repro.py in a TVM environment with LLVM enabled.
The first two calls print their outputs. The third raises the error above.

import numpy as np
import tvm
import tvm_tirx
from tvm import te, topi
from tvm.s_tir import Schedule


def run(dtype, explicit_cast=False):
    values = np.array([1, 4, 16, 64], dtype=dtype)
    x = te.placeholder(values.shape, dtype=dtype, name="x")
    operand = x
    if explicit_cast:
        operand = te.compute(x.shape, lambda i: x[i].astype("float32"), name="cast_f32")
    y = topi.rsqrt(operand)
    mod = Schedule(te.create_function([x, y])).mod
    print(f"input={dtype}, explicit_cast={explicit_cast}, output={y.dtype}", flush=True)
    exe = tvm.compile(mod, target="llvm")
    output = tvm_tirx.runtime.tensor(np.empty(values.shape, dtype=str(y.dtype)))
    exe.jit()["main"](tvm_tirx.runtime.tensor(values), output)
    print(output.numpy().tolist(), flush=True)


run("int16")
run("uint16", explicit_cast=True)
run("uint16")
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 摘要。