[Bug][Relax] `FoldConstant` leaks bindings emitted by a legalizer it only tries speculatively: `dynamic_strided_slice` leaves a `call_tir` to an undefined `shape_func` ("There is no definition of Module.shape_func")

Đang mở
#20,808 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
48/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
compilers

Hướng nghiên cứu

Start in src/relax/transform around FoldConstant and the legalizer it invokes; the issue does not name the implementation file or a test. Reproduce with the supplied dynamic_strided_slice module and relax.transform.FoldConstant(), then inspect the generated bindings and module functions. Done when speculative legalization no longer leaves a call_tir to an undefined shape_func, and the transformed module compiles with LLVM.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

needs-triage type: bug

The following code:

import tvm
from tvm import relax
from tvm.script import ir as I
from tvm.script import relax as R


@I.ir_module
class Module:
    @R.function
    def main(
        x: R.Tensor((8, 8), "float32"),
        begin: R.Tensor((2,), "int64"),
        end: R.Tensor((2,), "int64"),
        strides: R.Tensor((2,), "int64"),
    ):
        with R.dataflow():
            gv = R.dynamic_strided_slice(x, begin, end, strides)
            R.output(gv)
        return gv


tvm.compile(Module, target="llvm")  # OK

mod = relax.transform.FoldConstant()(Module)
mod.show()  # leftover call_tir(Module.shape_func, ...) + match_cast bindings; no shape_func in the module
print([gv.name_hint for gv in mod.functions.keys()])  # ['main']
# relax.analysis.well_formed(mod) -> ValueError: GlobalVar Module.shape_func is not defined.
tvm.compile(mod, target="llvm")
# tvm.error.InternalError: Check failed: (it != functions.end()) is false: There is no definition of Module.shape_func

After FoldConstant:

with R.dataflow():
    lv = R.call_tir(Module.shape_func, (x, begin, end, strides), ty_args=[R.Tensor((2,), dtype="int64")])
    lv1: R.Shape(ndim=2) = R.tensor_to_shape(lv)
    lv2: R.Shape([s, s_1]) = R.match_cast(lv1, R.Shape([s, s_1]))
    gv: R.Tensor(dtype="float32", ndim=2) = R.dynamic_strided_slice(x, begin, end, strides)
    R.output(gv)

Resulted in this output:

  File "src/relax/ir/transform.cc", line 140, in tvm::relax::transform::FunctionPassObj::operator()(tvm::IRModule, tvm::transform::PassContext const&) const
  File "src/relax/transform/rewrite_dataflow_reshape.cc", line 166, in operator()
  File "src/relax/transform/rewrite_dataflow_reshape.cc", line 159, in tvm::relax::RewriteDataflowReshape(tvm::relax::Function const&, tvm::IRModule const&)
  File "src/relax/ir/expr_functor.cc", line 786, in tvm::relax::ExprMutator::VisitExpr_(tvm::relax::FunctionNode const*)
  File "src/relax/ir/expr_functor.cc", line 828, in tvm::relax::ExprMutator::VisitExpr_(tvm::relax::SeqExprNode const*)
  File "src/relax/transform/rewrite_dataflow_reshape.cc", line 67, in tvm::relax::DataflowReshapeRewriter::VisitBindingBlock(tvm::relax::BindingBlock const&)
  File "src/relax/ir/expr_functor.cc", line 936, in tvm::relax::ExprMutator::VisitBindingBlock_(tvm::relax::DataflowBlockNode const*)
  File "src/relax/ir/expr_functor.cc", line 851, in tvm::relax::ExprMutator::VisitBinding_(tvm::relax::VarBindingNode const*, tvm::GenericConstNode const*)
  File "src/relax/transform/rewrite_dataflow_reshape.cc", line 94, in tvm::relax::DataflowReshapeRewriter::VisitExpr_(tvm::CallNode const*)
  File "src/ir/module.cc", line 206, in tvm::BaseFunc tvm::IRModuleNode::Lookup(const tvm::GlobalVar&) const
tvm.error.InternalError: Check failed: (it != functions.end()) is false: There is no definition of Module.shape_func

There are two possible fixes. One is to run the speculative legalization in a scratch scope (a fresh BlockBuilder or a discarded binding block) and keep its bindings and PrimFuncs only when the call actually folds. The other is to skip the legalize attempt unless every tensor argument is a constant. Any legalizer that emits bindings (not only dynamic_strided_slice) can leak the same way. Related open FoldConstant/legalizer issues: #20192, #20193 (different symptom: reshape legalizer assertion on a runtime shape).

No GPU is needed (the original failure was on nvptx, but llvm fails the same way). Reproduces 3/3.

To reproduce:

python3 min.py

Commit:

apache/tvm 46a45c5f574360e69e312236cafda7e7eb48b440 (2026-10-11)

Build configuration:

config.cmake + USE_LLVM "llvm-config-18 --link-static", USE_LIBBACKTRACE ON, USE_SORT ON, USE_RANDOM ON,
USE_VULKAN ON, USE_KHRONOS_SPIRV /usr; cmake -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo (gcc 11, lld)

Operating System:

Ubuntu 22.04.5 LTS (Docker image fusion-fuzz-tvm:latest), no GPU
Ngôn ngữ chính
Python
Star
13.9k
Fork
4k
Merge trung bình
7 giờ 59 phút
Pull request đã merge (30 ngày)
159

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của apache/tvm

Tất cả issue của apache/tvm

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.