[Bug] [Relax] `relax.op.unique(return_inverse=True)` returns an input-shaped inverse for N-D input when `axis` is None

未关闭 适合新手
#21,126 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

@tandede 已经在做这个了。

开始于 2026年12月4日。

  • #21131 来自 @tandede —— 未关闭

评估

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

调研方向

阅读 python/tvm/relax/op/set.py 中 numpy_unique 附近的代码,并将其逆输出与 src/relax/op/tensor/set.cc 中推断出的类型和契约进行比较。先运行 issue 中的复现,然后检查 tests/python/relax/test_frontend_onnx.py 中的 ONNX test_unique 用例。完成标准是:axis=None 时逆输出为一维,长度为 x.size,且受影响的测试通过。

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

描述

Expected behavior

relax.op.unique(x, return_inverse=True) with the default axis=None should return a 1-D int64 inverse of length x.size. For the (2, 2) input below, the inverse should be [2 0 0 1], consistent with the inferred type.

Actual behavior

The VM returns [[2 0] [0 1]] with shape (2, 2). On the default sorted=True path, numpy_unique passes NumPy's inverse through without reshaping it ("numpy.unique already returns 1D inverse_indices, so no reshaping needed"), but since NumPy 2.0 np.unique returns an inverse with the input's shape when axis=None (NumPy 2.4.6 here).

The same mismatch affects the ONNX importer. Running test_unique with --runxfail gives 28 passes and four failures for axis=None with three or four outputs:

RuntimeError: Check failed: input_shape.size() == size (2 vs. 1) : ErrorContext(match_cast, ty=R.Tensor((inverse_numbers,), dtype="int64"))  match_cast shape size mismatch.

Found while reading relax.op.unique's runtime (numpy_unique) and comparing it with the inferred type and the set.cc contract; reproduced with the script in this issue through the public API on main at 84657c0 (run on 2026-12-03).

Environment
  • Apache TVM 0.28.dev287, built from source at 84657c01e5d3 (tvm-tirx 0.1.0, tvm-ffi 0.1.16, LLVM 17.0.6)
  • Debian 12, x86_64, Python 3.11.2, NumPy 2.4.6
  • CPU only, target="llvm"
Steps to reproduce
import numpy as np
import tvm
import tvm_tirx
from tvm import relax
from tvm.relax import script as R

print("tvm", tvm.__version__, "numpy", np.__version__)
x = np.array([[3, 1], [1, 2]], dtype="int64")
bb = relax.BlockBuilder()
v = relax.Var("x", R.Tensor((2, 2), "int64"))
with bb.function("main", [v]):
    out = bb.emit(relax.op.unique(v, return_inverse=True))
    bb.emit_func_output(out)
mod = bb.get()
print("inferred type:", mod["main"].ret_ty)
vm = relax.VirtualMachine(tvm.compile(mod, target="llvm"), tvm_tirx.cpu())
expected = np.unique(x.ravel(), return_inverse=True)[1]
print("expected inverse (1-D, per the inferred type and ONNX Unique):", expected)
try:
    res = vm["main"](tvm_tirx.runtime.tensor(x))
    actual = res[1].numpy()
except Exception as e:  # noqa: BLE001
    raise SystemExit(f"relax.op.unique(return_inverse=True) on a 2-D input failed: {type(e).__name__}: {str(e).strip().splitlines()[-1]}")
print("actual inverse:", actual, "shape", actual.shape)
if actual.shape != expected.shape or not np.array_equal(actual, expected):
    raise SystemExit("inverse indices do not match the 1-D result")
print("OK")

Exit code 1. Output combines stdout and stderr, with one LLVM -mcpu fallback diagnostic omitted.

tvm 0.28.dev287 numpy 2.4.6
inferred type: T.Tuple(R.Tensor(dtype="int64", ndim=1), R.Tensor(dtype="int64", ndim=1))
expected inverse (1-D, per the inferred type and ONNX Unique): [2 0 0 1]
actual inverse: [[2 0]
 [0 1]] shape (2, 2)
inverse indices do not match the 1-D result
Triage
  • needs-triage
  • type: bug
主要语言
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 摘要。