[Bug][Relax] TopologicalSort moves reshape before the MatchCast defining its symbolic dimensions
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
Start with src/relax/transform/topological_sort.cc, especially BindingOrderCollector, where the issue reports that symbolic dimensions used by ShapeExpr do not create dependency edges from their MatchCast definitions. Run the standalone reproducer in the issue and check whether TopologicalSort keeps the transformed module well formed. Done means the reported Var n is not defined failure is gone while the source module still produces the expected output.
由索引模型根据 Issue 内容生成。
描述
Expected behavior
TopologicalSort() should preserve dependencies introduced by MatchCast, including uses of its symbolic dimensions inside a ShapeExpr. A well-formed, executable dataflow module should remain well-formed after sorting.
Actual behavior
The default pass moves reshape(y, [n, m]) before the MatchCast that defines n and m. The source passes check_well_formed(check_ty=True) and its LLVM VM output is [[1, 2], [3, 4]]. The result fails the same checker:
ValueError: Var n is not defined.
The relevant reordered bindings are:
y = R.add(x, R.const(1.0, "float32"))
out = R.reshape(y, R.shape([n, m]))
dims = R.tensor_to_shape(shape)
bound = R.match_cast(dims, R.Shape([n, m]))
All supplied tensors have positive, concrete dimensions. This is an invalid-IR transformation, not a numerical-tolerance or ONNX-import issue. It reproduces on three freshly parsed copies of the minimal source; an ordinary static Add module remains well-formed under the same pass.
Environment
- Ubuntu 24.04.4 LTS, x86_64; Python 3.12.3; LLVM 18.1.3, CPU only.
- TVM
0.28.dev0, source revision27576ce2d1b2cf5212ad46d5710e594bdcbb5df0. - Matching TVM FFI revision
df463f9bd269867fc7f4d578d62d80cc2a11d004. - The script below was executed against that frozen build. It imports neither ONNX nor GraphOpt and needs no model files.
- Current main was checked at
e345029cedf0be76a087fcad9c89a97bed4f7204. The affected pass source file is byte-for-byte identical to the tested revision. I have not built or executed current main.
Steps to reproduce
Run the complete standalone script with the TVM build above and NumPy. Source execution and its output check complete before the final well-formedness check raises the error.
"""Standalone LLVM CPU reproducer; no ONNX or GraphOpt dependency."""
import numpy as np
import tvm
from tvm import relax
from tvm.script import ir as I
from tvm.script import relax as R
n = I.dynamic("n", dtype="int64")
m = I.dynamic("m", dtype="int64")
@I.ir_module
class Module:
@R.function
def main(x: R.Tensor((2, 2), "float32"), shape: R.Tensor((2,), "int64")) -> R.Tensor(dtype="float32", ndim=2):
with R.dataflow():
y = R.add(x, R.const(1.0, "float32"))
dims = R.tensor_to_shape(shape)
bound = R.match_cast(dims, R.Shape([n, m]))
out = R.reshape(y, R.shape([n, m]))
R.output(out)
return out
assert relax.analysis.check_well_formed(Module, check_ty=True)
before = Module.script(show_meta=True)
executable = relax.build(Module, target=tvm.target.Target("llvm", host="llvm"), relax_pipeline="default")
vm = relax.VirtualMachine(executable, tvm.cpu())
inputs = [np.arange(4, dtype="float32").reshape(2, 2), np.array([2, 2], dtype="int64")]
expected = np.array([[1, 2], [3, 4]], dtype="float32")
actual = vm["main"](*[tvm.runtime.tensor(x) for x in inputs]).numpy()
np.testing.assert_array_equal(actual, expected)
assert Module.script(show_meta=True) == before
print("TVM:", tvm.__version__, flush=True)
print("Source LLVM output:", actual.tolist(), flush=True)
optimized = relax.transform.TopologicalSort()(Module)
print("Target well formed:", bool(relax.analysis.check_well_formed(optimized, check_ty=True)), flush=True)
relax.analysis.well_formed(optimized, check_ty=True)
Root-cause investigation and related work
The dependency collector records edges for Relax variable references, but the ShapeExpr([n, m]) uses do not create an edge back to the MatchCast defining those primitive variables. See BindingOrderCollector at the inspected main revision. Tracking symbolic definitions and their uses, or retaining a conservative ordering around shape-defining bindings, should preserve this dependency.
I searched open and closed issues/PRs for TopologicalSort; the pass-introduction PR #16697 is related background, but I did not find an existing report for this symbolic-dependency failure.
Triage
- needs-triage
- Relax transformation correctness: well-formed input becomes invalid IR.
Report preparation used Codex; the included script was executed in the stated environment.
- 主要语言
- Python
- 星标
- 13.9k
- 派生
- 4k
- 平均合并
- 7 小时 59 分钟
- 30 天内合并 PR
- 159
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
apache/tvm 的其他 Issue
-
needs-triage type: bug
难度 2/5 1-3 小时 新手友好度 80/100
维护者通常 1 天内回复
-
[Bug] [Relax][ONNX] OneHot does not convert floating-point depth to int64可能已有人在做 @tandede 于 3 天前认领。 未关闭needs-triage type: bug
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
needs-triage type: bug
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复
-
needs-triage type: bug
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
[Bug][Relax] ReorderTakeAfterMatmul fails on vector dot products可能已有人在做 @hiyufan 于 13 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 74/100
维护者通常 1 天内回复
相似的 Issue
-
[BUG]: Compliance rules never see Unity Catalog views as views可能已有人在做 @wagnercsantos 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
databrickslabs/ontos#1348 ·
维护者通常 3 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
ibm-granite/granite.build#649 ·
维护者通常 1 天内回复
-
`forum_pr_poster` action will always return `none` when checking for pre-existing feedback threads, even if a feedback thread already exists.可能已有人在做 @pgmzeta 今天认领。 未关闭E-Verified P-Major
难度 2/5 1-3 小时 新手友好度 79/100
goonstation/goonstation#28259 ·
维护者通常 2 天内回复
-
enhancement
难度 2/5 1-3 小时 新手友好度 65/100
woonstadrotterdam/monumenten#70 ·
维护者通常 15 天内回复
-
binding:python bug
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 1 天内回复