[Bug] [Relax] `relax.op.median` legalization reduces only the first of several axes and drops kept dimensions for `axis=None`
维护者通常 3 天内回复
还没有人认领这个 Issue。
评估
调研方向
Start with _te_median in python/tvm/relax/transform/legalize_ops/statistical.py and compare its axis handling with the type inference and docstring links in the issue. Run both reproduction scripts to confirm the multiple-axis and axis=None, keepdims=True failures. Done when legalization returns the inferred shape and expected values for both cases.
由索引模型根据 Issue 内容生成。
描述
Expected behavior
- For the
(2, 3, 2)input below,relax.op.median(x, axis=[1, 2])infers one tensor of shape(2,). Extending the single-axis lower-middle convention to both axes would give[2. 8.]. Type inference - For the
(2, 3)input,relax.op.median(x, axis=None, keepdims=True)should return shape(1, 1), as inferred. The docstring says thatkeepdims=Trueretains each reduced axis with size one. Docstring
Actual behavior
- With
axis=[1, 2], the VM returns a pair of(2, 2)tensors. The values are[[4. 5.] [10. 11.]]. The lowering computes the middle index from both axes but sorts and gathers alongaxis[0]only. For a(2, 3, 4)input with the same axes, the legalized TIR gathers at index 5 on an axis of length 3. - With
axis=None, keepdims=True, the VM returns[2.]with shape(1,). The lowering flattens the input and returns the kept dimension of the flattened tensor.
Both paths are in _te_median. Should the lowering reduce the listed axes together and return one values tensor for the static-shape case, or should type inference reject multiple axes?
Found while reading the relax.op.median legalization (_te_median) and checking it against its docstring and inferred type; reproduced with the scripts 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 at84657c01e5d3(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
Several axes
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.arange(12, dtype="float32").reshape(2, 3, 2)
bb = relax.BlockBuilder()
v = relax.Var("x", R.Tensor((2, 3, 2), "float32"))
with bb.function("main", [v]):
out = bb.emit(relax.op.median(v, axis=[1, 2]))
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())
res = vm["main"](tvm_tirx.runtime.tensor(x))
outs = [res] if hasattr(res, "numpy") else list(res)
print("returned", len(outs), "tensor(s) with shapes", [o.numpy().shape for o in outs])
print("first output:", outs[0].numpy())
expected = np.quantile(x, 0.5, axis=(1, 2), method="lower")
print("expected (lower middle value over axes 1 and 2, as on the single-axis path):", expected)
if len(outs) != 1 or outs[0].numpy().shape != expected.shape or not np.allclose(outs[0].numpy(), expected):
raise SystemExit("relax.op.median(axis=[1, 2]) did not reduce both axes")
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: R.Tensor((2,), dtype="float32")
returned 2 tensor(s) with shapes [(2, 2), (2, 2)]
first output: [[ 4. 5.]
[10. 11.]]
expected (lower middle value over axes 1 and 2, as on the single-axis path): [2. 8.]
relax.op.median(axis=[1, 2]) did not reduce both axes
axis=None with keepdims=True
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.arange(6, dtype="float32").reshape(2, 3)
bb = relax.BlockBuilder()
v = relax.Var("x", R.Tensor((2, 3), "float32"))
with bb.function("main", [v]):
out = bb.emit(relax.op.median(v, axis=None, keepdims=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())
actual = vm["main"](tvm_tirx.runtime.tensor(x)).numpy()
print("expected shape (1, 1): docstring says reduced axes are kept with size one")
print("actual:", actual, "shape", actual.shape)
if actual.shape != (1, 1):
raise SystemExit("relax.op.median(axis=None, keepdims=True) dropped the kept dimensions")
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: R.Tensor((1, 1), dtype="float32")
expected shape (1, 1): docstring says reduced axes are kept with size one
actual: [2.] shape (1,)
relax.op.median(axis=None, keepdims=True) dropped the kept dimensions
Triage
- needs-triage
- type: bug
- 主要语言
- Python
- 星标
- 13.9k
- 派生
- 4.1k
- 平均合并
- 4 天 22 小时
- 30 天内合并 PR
- 22
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 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可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 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 于 5 天前认领。 未关闭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 小时以内 新手友好度 92/100
Techiebutler/freeframe#687 ·
维护者通常 1 天内回复
-
bug verbose GC
难度 2/5 1-3 小时 新手友好度 74/100
IBM/javacore-analyser#677 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 79/100
维护者通常 2 天内回复
-
area/config comp/plugins P1 platform/qqbot tool/file type/bug
难度 2/5 1-3 小时 新手友好度 75/100
NousResearch/hermes-agent#163932 · 1 条评论 ·
维护者通常 1 天内回复
-
plugins/dsh: todayStr() computes UTC but is documented as local — journal entries written 00:00–08:00 local land in the previous day's file可能已有人在做 @Beehtrix 今天认领。 未关闭bug
难度 2/5 1-3 小时 新手友好度 82/100
zilliztech/memsearch#907 · 1 条评论 ·
维护者通常 1 天内回复