coffezhou opened a new issue, #18602:
URL: https://github.com/apache/tvm/issues/18602

   
   ### Expected behavior
   
   TVM should compile the model correctly.
   
   ### Actual behavior
   
   For the following model,
   
   <img width="535" height="424" alt="Image" 
src="https://github.com/user-attachments/assets/ad5d4fc3-84c5-4b07-9749-31b7f815acf0";
 />
   
   <img width="495" height="415" alt="Image" 
src="https://github.com/user-attachments/assets/cb3ce723-4aaf-4cec-b17c-f648fc65d946";
 />
   
   TVM crashes:
   ```
   Traceback (most recent call last):
     File "/home/ubuntu/Documents/test1.py", line 61, in test
       ex = tvm.compile(tvm_model, target="llvm")
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     File 
"/home/ubuntu/Documents/DLCompilers/tvm/python/tvm/driver/build_module.py", 
line 104, in compile
       return tvm.relax.build(
              ^^^^^^^^^^^^^^^^
     File 
"/home/ubuntu/Documents/DLCompilers/tvm/python/tvm/relax/vm_build.py", line 
263, in build
       return _vmlink(
              ^^^^^^^^
     File 
"/home/ubuntu/Documents/DLCompilers/tvm/python/tvm/relax/vm_build.py", line 
158, in _vmlink
       lib = tvm.tir.build(tir_mod, target=target, pipeline=tir_pipeline)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     File "/home/ubuntu/Documents/DLCompilers/tvm/python/tvm/tir/build.py", 
line 239, in build
       return tir_to_runtime(host_mod, device_mod_dict, target_host)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     File "/home/ubuntu/Documents/DLCompilers/tvm/python/tvm/tir/build.py", 
line 149, in tir_to_runtime
       mhost = codegen_build(mhost_all, target_host)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     File "/home/ubuntu/Documents/DLCompilers/tvm/python/tvm/tir/build.py", 
line 131, in codegen_build
       return bf(mod, target)
              ^^^^^^^^^^^^^^^
     File "python/tvm_ffi/cython/function.pxi", line 904, in 
tvm_ffi.core.Function.__call__
     File "<unknown>", line 0, in 
tvm::codegen::LLVMModuleNode::Init(tvm::IRModule const&, tvm::Target const&)
     File "<unknown>", line 0, in tvm::codegen::CodeGenCPU::Finish()
     File "<unknown>", line 0, in tvm::codegen::CodeGenLLVM::Finish()
     File "<unknown>", line 0, in tvm::codegen::CodeGenLLVM::Verify() const
     File "<unknown>", line 0, in 
tvm::runtime::detail::LogFatal::Entry::Finalize()
   tvm.error.InternalError: LLVM module verification failed with the following 
errors: 
   location of #dbg_declare must be a pointer or int
   #dbg_declare(float %v_input_red_temp.v1, !123, !DIExpression(), !99)
   float %v_input_red_temp.v1
   label %if_end5
   ptr @argmin_compute_
   location of #dbg_declare must be a pointer or int
   #dbg_declare(float %v_input_red_temp.v1, !123, !DIExpression(), !99)
   float %v_input_red_temp.v1
   label %if_end5
   ptr @argmin_compute_
   ```
   ```
   Traceback (most recent call last):
     File "/home/ubuntu/Documents/test1.py", line 61, in test
       ex = tvm.compile(tvm_model, target="llvm")
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     File 
"/home/ubuntu/Documents/DLCompilers/tvm/python/tvm/driver/build_module.py", 
line 104, in compile
       return tvm.relax.build(
              ^^^^^^^^^^^^^^^^
     File 
"/home/ubuntu/Documents/DLCompilers/tvm/python/tvm/relax/vm_build.py", line 
263, in build
       return _vmlink(
              ^^^^^^^^
     File 
"/home/ubuntu/Documents/DLCompilers/tvm/python/tvm/relax/vm_build.py", line 
158, in _vmlink
       lib = tvm.tir.build(tir_mod, target=target, pipeline=tir_pipeline)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     File "/home/ubuntu/Documents/DLCompilers/tvm/python/tvm/tir/build.py", 
line 239, in build
       return tir_to_runtime(host_mod, device_mod_dict, target_host)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     File "/home/ubuntu/Documents/DLCompilers/tvm/python/tvm/tir/build.py", 
line 149, in tir_to_runtime
       mhost = codegen_build(mhost_all, target_host)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     File "/home/ubuntu/Documents/DLCompilers/tvm/python/tvm/tir/build.py", 
line 131, in codegen_build
       return bf(mod, target)
              ^^^^^^^^^^^^^^^
     File "python/tvm_ffi/cython/function.pxi", line 904, in 
tvm_ffi.core.Function.__call__
     File "<unknown>", line 0, in 
tvm::codegen::LLVMModuleNode::Init(tvm::IRModule const&, tvm::Target const&)
     File "<unknown>", line 0, in tvm::codegen::CodeGenCPU::Finish()
     File "<unknown>", line 0, in tvm::codegen::CodeGenLLVM::Finish()
     File "<unknown>", line 0, in tvm::codegen::CodeGenLLVM::Verify() const
     File "<unknown>", line 0, in 
tvm::runtime::detail::LogFatal::Entry::Finalize()
   tvm.error.InternalError: LLVM module verification failed with the following 
errors: 
   location of #dbg_declare must be a pointer or int
   #dbg_declare(float %v_input_red_temp.v1, !123, !DIExpression(), !99)
   float %v_input_red_temp.v1
   label %if_end5
   ptr @argmax_compute_
   location of #dbg_declare must be a pointer or int
   #dbg_declare(float %v_input_red_temp.v1, !123, !DIExpression(), !99)
   float %v_input_red_temp.v1
   label %if_end5
   ptr @argmax_compute_
   ```
   
   ### Environment
   
   OS: Ubuntu 20.04
   TVM: 0.23.dev0 
(https://github.com/apache/tvm/commit/f4e28d3153323ad97a7e74740c9fb22300fd6cd0)
   
   onnxruntime: 1.23.2
   
   ### 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.
   ```python
   from typing import Dict, List, Literal, Optional
   import sys
   import os
   
   import numpy as np
   import onnx
   from onnx.reference import ReferenceEvaluator
   
   import onnxruntime
   from onnx import ModelProto, TensorProto, helper
   
   import tvm
   import tvm.testing
   from tvm import relax
   from tvm.relax.frontend.onnx import from_onnx
   
   import argparse
   import pickle
   
   def test() -> None:
       model_name = "11.onnx" #"22.onnx"
       onnx_model = onnx.load(model_name)
       # Configure model format.
       onnx_model.ir_version = 8
       onnx_model.opset_import[0].version = 14
       
       with open("inputs.pkl", 'rb') as fp:
           inputs = pickle.load(fp)
       # Run the model through onnx to get the expected result.
       try:
           ort_session = onnxruntime.InferenceSession(
               onnx_model.SerializeToString(), 
providers=["CPUExecutionProvider"]
           )
           ort_output = ort_session.run([], inputs)
       except Exception as e:
           print(e)
           print("This model cannot be executed by onnxruntime!")
           sys.exit(1)
   
       print("onnxruntime:", ort_output[0].shape)
   
       # ReferenceEvaluator
       sess = ReferenceEvaluator(model_name)
       re_output = sess.run(None, inputs)
       print("ReferenceEvaluator:", re_output[0].shape)
   
       tvm.testing.assert_allclose(re_output[0], ort_output[0], rtol=0.1, 
atol=0.1)
   
       # TVM
      # Convert the onnx model into relax through the onnx importer.
       tvm_model = from_onnx(onnx_model, opset=14, 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)
       # Compile the relax graph into a VM then run.
       with tvm.transform.PassContext(opt_level=3):
           ex = tvm.compile(tvm_model, target="llvm")
       
   if __name__ == "__main__":
       test()
   
   ```
   
   
[testcase.zip](https://github.com/user-attachments/files/24306115/testcase.zip)
   
   ### Triage
   
   Please refer to the list of label tags 
[here](https://github.com/apache/tvm/wiki/Issue-Triage-Labels) to find the 
relevant tags and add them below in a bullet format (example below).
   
   * needs-triage
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to