apache / apache/tvm

[Bug] TVM crashes with default relax pipeline when opt_level=1: InternalError: Check failed: (slot->value_computed) is false

Open
#17,876 0 comments 0 reactions 0 assignees View on GitHub
needs-triage type: bug
Dominant language
Python
Stars
13.7k
Forks
4k
Avg merge
2d 19h
Merged PRs (30d)
111

Description

### Expected behavior

TVM should compile the model correctly with the default relax optimization pipeline.

### Actual behavior

When compiling the model with the default relax optimization pipeline when opt_level=1, TVM crashes as follows:
```c
Traceback (most recent call last):
File "/home/carla/Documents/test/test.py", line 63, in
main()
File "/home/carla/Documents/test/test.py", line 52, in main
ex = relax.build(tvm_model, target="llvm", relax_pipeline=relax_pipeline)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/carla/Documents/tvm/python/tvm/relax/vm_build.py", line 253, in build
mod = relax_pipeline(mod)
^^^^^^^^^^^^^^^^^^^
File "/home/carla/Documents/tvm/python/tvm/ir/transform.py", line 238, in __call__
return _ffi_transform_api.RunPass(self, mod)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "tvm/_ffi/_cython/./packed_func.pxi", line 339, in tvm._ffi._cy3.core.PackedFuncBase.__call__
File "tvm/_ffi/_cython/./packed_func.pxi", line 270, in tvm._ffi._cy3.core.FuncCall
File "tvm/_ffi/_cython/./packed_func.pxi", line 259, in tvm._ffi._cy3.core.FuncCall3
File "tvm/_ffi/_cython/./base.pxi", line 185, in tvm._ffi._cy3.core.CHECK_CALL
File "/home/carla/Documents/tvm/python/tvm/_ffi/base.py", line 468, in raise_last_ffi_error
raise py_err
File "tvm/_ffi/_cython/./packed_func.pxi", line 56, in tvm._ffi._cy3.core.tvm_callback
File "/home/carla/Documents/tvm/python/tvm/relax/backend/cpu_generic/pipeline.py", line 73, in _pipeline
mod = seq(mod)
^^^^^^^^
File "/home/carla/Documents/tvm/python/tvm/ir/transform.py", line 238, in __call__
return _ffi_transform_api.RunPass(self, mod)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "tvm/_ffi/_cython/./packed_func.pxi", line 339, in tvm._ffi._cy3.core.PackedFuncBase.__call__
File "tvm/_ffi/_cython/./packed_func.pxi", line 270, in tvm._ffi._cy3.core.FuncCall
File "tvm/_ffi/_cython/./packed_func.pxi", line 259, in tvm._ffi._cy3.core.FuncCall3
File "tvm/_ffi/_cython/./base.pxi", line 185, in tvm._ffi._cy3.core.CHECK_CALL
File "/home/carla/Documents/tvm/python/tvm/_ffi/base.py", line 468, in raise_last_ffi_error
raise py_err
tvm.error.InternalError: Traceback (most recent call last):
27: tvm::runtime::PackedFuncObj::Extractor::AssignTypedLambda(tvm::transform::{lambda(tvm::transform::Pass, tvm::IRModule)#7}, std::__cxx11::basic_string, std::allocator >)::{lambda(tvm::runtime::TVMArgs const&, tvm::runtime::TVMRetValue*)#1}> >::Call(tvm::runtime::PackedFuncObj const*, tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*)
26: tvm::transform::Pass::operator()(tvm::IRModule) const
25: tvm::transform::Pass::operator()(tvm::IRModule, tvm::transform::PassContext const&) const
24: tvm::transform::SequentialNode::operator()(tvm::IRModule, tvm::transform::PassContext const&) const
23: tvm::transform::Pass::operator()(tvm::IRModule, tvm::transform::PassContext const&) const
22: tvm::transform::ModulePassNode::operator()(tvm::IRModule, tvm::transform::PassContext const&) const
21: tvm::runtime::PackedFuncObj::Extractor::AssignTypedLambda(tvm::relax::transform::VMShapeLower(bool)::{lambda(tvm::IRModule, tvm::transform::PassContext)#1})::{lambda(tvm::runtime::TVMArgs const&, tvm::runtime::TVMRetValue*)#1}> >::Call(tvm::runtime::PackedFuncObj const*, tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*)
20: tvm::relax::VMShapeLowerMutator::Lower(tvm::IRModule, bool)
19: tvm::relax::VMShapeLowerMutator::Rewrite(tvm::GlobalVar, tvm::relax::Function)
18: tvm::relax::ExprMutator::VisitWithNewScope(tvm::RelaxExpr const&, tvm::runtime::Optional >)
17: tvm::relax::ExprMutator::VisitExpr(tvm::RelaxExpr const&)
16: tvm::relax::ExprFunctor::VisitExpr(tvm::RelaxExpr const&)
15: _ZZN3tvm5relax11ExprFunctorIFNS_9RelaxExprERKS2_EE10InitVTableEvENUlRKNS_7r
14: tvm::relax::ExprMutator::VisitExpr_(tvm::relax::SeqExprNode const*)
13: tvm::relax::ExprMutator::VisitBindingBlock(tvm::relax::BindingBlock const&)
12: tvm::relax::ExprMutator::VisitBindingBlock_(tvm::relax::BindingBlockNode const*)
11: tvm::relax::ExprMutator::VisitBinding(tvm::relax::Binding const&)
10: tvm::relax::ExprMutator::VisitBinding_(tvm::relax::VarBindingNode const*)
9: tvm::relax::ExprMutator::VisitBinding_(tvm::relax::VarBindingNode const*, tvm::relax::ConstantNode const*)
8: tvm::relax::ExprMutator::VisitExpr(tvm::RelaxExpr const&)
7: tvm::relax::ExprFunctor::VisitExpr(tvm::RelaxExpr const&)
6: _ZZN3tvm5relax11ExprFunctorIFNS_9RelaxExprERKS2_EE10InitVTableEvENUlRKNS_7r
5: tvm::relax::ExprMutatorBase::VisitExpr_(tvm::relax::CallNode const*)
4: tvm::relax::ExprMutator::VisitExpr(tvm::RelaxExpr const&)
3: tvm::relax::ExprFunctor::VisitExpr(tvm::RelaxExpr const&)
2: _ZZN3tvm5relax11ExprFunctorIFNS_9RelaxExprERKS2_EE10InitVTableEvENUlRKNS_7r
1: tvm::relax::VMShapeLowerMutator::VisitExpr_(tvm::relax::ShapeExprNode const*)
0: tvm::relax::VMShapeLowerMutator::MakeSymbolicShapeArg(tvm::PrimExpr const&)
File "/home/carla/Documents/tvm/src/relax/backend/vm/vm_shape_lower.cc", line 365
InternalError: Check failed: (slot->value_computed) is false: PrimExpr T.int64(4) * (x_0 * x_1 * x_2 * x_3) in function I.GlobalVar("main") has not been computed

```

### Environment

OS: Ubuntu 20.04
TVM: 0.21.dev0(c00f52a70)

### Steps to reproduce
This bug can be reproduced by the following code with the model in the attachment. As shown in the code, the model can be executed by onnxruntime. However, tvm failed to the model with the default relax optimization pipeline when opt_level=1. If we set opt_level=0, this bug is gone.
```python
import sys

import numpy as np
import onnx
import onnxruntime

import tvm
from tvm import relax
from tvm.relax.frontend.onnx import from_onnx

import pickle

def main():
onnx_model = onnx.load("a240.onnx")

with open("inputs.pkl", "rb") as fp:
inputs = pickle.load(fp)

try:
ort_session = onnxruntime.InferenceSession(
onnx_model.SerializeToString(), providers=["CPUExecutionProvider"]
)
ort_output = ort_session.run([], inputs)
except Exception as e:
print(e)
sys.exit(1)

# Convert the onnx model into relax through the onnx importer.
tvm_model = from_onnx(onnx_model, keep_params_in_input=True)
# Convert operators for inference mode.
tvm_model = relax.transform.DecomposeOpsForInference()(tvm_model)
# Legalize any relax ops into tensorir.
tvm_model = relax.transform.LegalizeOps()(tvm_model)

# Separate model from parameters.
tvm_model, params = relax.frontend.detach_params(tvm_model)

# Prepare inputs.
input_list = [
inputs[key.name_hint] for key in tvm_model["main"].params if key.name_hint in inputs
]
if params:
input_list += params["main"]

# Compile the relax graph into a VM then run.
#----------------------cpu-----------------------
with tvm.transform.PassContext(opt_level=1):
target = tvm.target.Target("llvm", host="llvm")
relax_pipeline = relax.pipeline.get_default_pipeline(target)

ex = relax.build(tvm_model, target="llvm", relax_pipeline=relax_pipeline)
vm = relax.VirtualMachine(ex, tvm.cpu())

# Run model and check outputs.
vm.set_input("main", *input_list)
vm.invoke_stateful("main")
tvm_cpu_output = vm.get_outputs("main")
#----------------------cpu-----------------------

if __name__ == "__main__":
main()

```

[testcase.zip](https://github.com/user-attachments/files/19845075/testcase.zip)

### Triage

* needs-triage

Contributor guide

No contributing guide indexed for this repository

Research direction

Reproduce the failure with the Python script and testcase.zip using the default Relax pipeline at opt_level=1. Start at src/relax/backend/vm/vm_shape_lower.cc:365 in VMShapeLowerMutator::MakeSymbolicShapeArg and trace the uncomputed PrimExpr; done when the model compiles and runs without the reported InternalError.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
compilers, machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.