tvm.driver.tvmc.TVMCImportError: onnx

I was learn about : Compiling and Optimizing a Model with TVMC¶

when i try run code like blow, i get an error :tvm.driver.tvmc.TVMCImportError: onnx

the code and shell history is here:

  • shell history:
# 1. env
(base) ➜  model git:(main) ✗ conda activate tvm-build

# 2. onnx is here
(tvm-build) ➜  model git:(main) ✗ conda list | grep onnx  
onnx                      1.11.0                   pypi_0    pypi
onnxoptimizer             0.2.6                    pypi_0    pypi

#3. run cmd
(tvm-build) ➜  model git:(main) ✗ python tvmcpythonintro.py
Traceback (most recent call last):
  File "/Users/chendongsheng/github/tvm/python/tvm/driver/tvmc/frontends.py", line 82, in lazy_import
    return importlib.import_module(pkg_name, package=from_pkg_name)
  File "/Users/chendongsheng/opt/anaconda3/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'onnx'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "tvmcpythonintro.py", line 5, in <module>
    model = tvmc.load('my_model.onnx')  # Step 1: Load
  File "/Users/chendongsheng/github/tvm/python/tvm/driver/tvmc/frontends.py", line 396, in load_model
    mod, params = frontend.load(path, shape_dict, **kwargs)
  File "/Users/chendongsheng/github/tvm/python/tvm/driver/tvmc/frontends.py", line 161, in load
    onnx = lazy_import("onnx")
  File "/Users/chendongsheng/github/tvm/python/tvm/driver/tvmc/frontends.py", line 84, in lazy_import
    raise TVMCImportError(pkg_name) from error
tvm.driver.tvmc.TVMCImportError: onnx
  • code :
from tvm.driver import tvmc

# 1. 导入模型
# If you’d like to see the Relay, you can run: model.summary()
model = tvmc.load('my_model.onnx')  # Step 1: Load

package = tvmc.compile(model, target="llvm")  # Step 2: Compile

result = tvmc.run(package, device="cpu")  # Step 3: Run


tvmc.tune(model, target="llvm")  # Step 1.5: Optional Tune

# 1. 存储模型
model = tvmc.load('my_model.onnx')  # Step 1: Load
model.save(desired_model_path)


# 2. 存储编译后的模型
tvmc.compile(model, target="llvm", package_path="whatever")  # Step 2: Compile

new_package = tvmc.TVMCPackage(package_path="whatever")
result = tvmc.run(new_package, device="cpu")  # Step 3: Run


# # 使用自动优化
# tvmc.tune(model, target="llvm", enable_autoscheduler=True)

# # 保存优化结果
# log_file = "hello.json"

# # Run tuning
# tvmc.tune(model, target="llvm", tuning_records=log_file)

# ...

# # Later run tuning and reuse tuning results
# tvmc.tune(model, target="llvm", tuning_records=log_file)


Hi. So it seems you don’t have ONNX installed as a package on your environment?

Can you try to install the package, supposing you are using a virtualenv, with:

pip3 install \
    onnx==1.10.2 \
    onnxruntime==1.9.0 \
    onnxoptimizer==0.2.6

Also, can you clarify how did you install your TVM package?

[quote=“sexjun, post:1, topic:12787”]

# 2. onnx is here
(tvm-build) ➜  model git:(main) ✗ conda list | grep onnx  
onnx                      1.11.0                   pypi_0    pypi
onnxoptimizer             0.2.6                    pypi_0    pypi

[/quote]# 2. onnx is here (tvm-build) ➜ model git:(main) ✗ conda list | grep onnx
onnx 1.11.0 pypi_0 pypi onnxoptimizer 0.2.6 pypi_0 pypi

very tks for your helping,you can see my shell history , onnx is installed , i fix the error like this: i have no idear why, but it worked!

close debug mode in file named cmake.config , and rebuild tvm , the error is gone.

set(USE_RELAY_DEBUG OFF)

if someone want using relay debug log try it : :slight_smile: [fix log error](https://discuss.tvm.apache.org/t/tvmerror-tvm-log-debug-ill-formed-invalid-

I see, sorry I missed your original point about ONNX being installed. It seems there is an issue with Conda and the mechanisms we’re using for lazy importing dependencies. I’m not very familiar with Conda, but will try to have a look.

Happy your problem got solved.