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)