tvm icon indicating copy to clipboard operation
tvm copied to clipboard

[Bug][Relax][CUDA/NVPTX] `R.vision.all_class_non_max_suppression` fails GPU compilation in `FlattenTensor`: "Tensor T_gather is used before its definition"

Open YuanchengJiang opened this issue 1 month ago • 1 comments

The following code:

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


@I.ir_module
class Module:
    @R.function
    def main(boxes: R.Tensor((1, 5, 4), "float32"), scores: R.Tensor((1, 2, 5), "float32")):
        with R.dataflow():
            gv = R.vision.all_class_non_max_suppression(
                boxes, scores, R.const(3, "int64"), R.const(0.5, "float32"),
                R.const(0.1, "float32"), "onnx")
            R.output(gv)
        return gv


tvm.compile(Module, target="llvm")
print("llvm: OK")
target = tvm.target.Target({"kind": "cuda", "arch": "sm_80"})
# Check failed: (it != flat_map_.end()) is false: Tensor T_gather is used before its definition
tvm.compile(Module, target=target)
print("cuda: OK")

Resulted in this output:

llvm: OK
  File "python/tvm/relax/vm_build.py", line 149, in _vmlink
  File "python/tvm/tirx/build.py", line 236, in build
  File "python/tvm/s_tir/pipeline.py", line 153, in _pipeline
  File "src/tirx/transform/flatten_tensor.cc", line 248, in tvm::tirx::TensorFlattener::Mutate_(tvm::TensorStoreNode const*, tvm::ffi::InplaceMode)
  File "src/tirx/transform/flatten_tensor.cc", line 302, in tvm::TensorStore tvm::tirx::TensorFlattener::VisitTensorAccess<tvm::TensorStore>(...)
  File "src/tirx/transform/flatten_tensor.cc", line 180, in const tvm::tirx::TensorFlattener::FlatInfo& tvm::tirx::TensorFlattener::Lookup(const tvm::tirx::TensorVar&)
tvm.error.InternalError: Check failed: (it != flat_map_.end()) is false: Tensor T_gather is used before its definition (AllocTensor/DeclTensor/Function param)

The same module compiles for llvm; nvptx fails the same way. The Relax legalization (legalize_ops/vision.py) uses the generic topi.vision.all_class_non_max_suppression, which sorts with the generic topi.argsort and then gathers the sorted scores (T_gather). After the GPU default scheduling the store into T_gather is no longer in the scope of its allocation, and FlattenTensor stops on it. Possibly the same family as R.median on GPU (also a generic-argsort-based legalization that does not go through DispatchSortScan).

Expected: either GPU code, or a diagnostic that this op has no GPU lowering — not an internal check in FlattenTensor.

No GPU is needed (compile only). 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), CUDA 13.4 toolkit, no GPU

YuanchengJiang avatar Oct 12 '26 11:10 YuanchengJiang

Hi I would like to work on this issue. Please assign this to me, if no one is already working on it.

GarvSaxena avatar Oct 16 '26 17:10 GarvSaxena