TVM安装编译指南

本文参考: TVM 官网安装指南

1. mac从源码形式安装tvm实操

  • 使用brew安装的llvm地址:

我推荐使用LLVM9来进行构建TVM

/usr/local/Cellar/llvm@9/9.0.1_4/bin/bin/llvm-config
# you can find it on here too!
/usr/local/opt/llvm
  • 开启LLVM

TVM编译步骤是需要copy一份构建的标准配置文件,基于该配置文件,修改配置项来编译。

  • 首先新建一个构建文件夹,然后将标准配置文件复制进去:
mkdir build
cp cmake/config.cmake build

在配置文件中新增:

set(USE_LLVM "/usr/local/Cellar/llvm@9/9.0.1_4/bin/llvm-config --link-static")
set(HIDE_PRIVATE_SYMBOLS ON)
  • 准备编译代码
mkdir build
cd build
cp cmake/config.cmake build
# 这里需要做一些配置,不建议打开USE_RELAY_DEBUG,容易报错,官网文档有问题。
#set(USE_RELAY_DEBUG OFF)
cmake ..
make -j 20

在这里插入图片描述

  • Python package安装
    只需要将以下变量放到环境变量中即可。
# add TVM path
export TVM_HOME=/Users/chendongsheng/github/tvm
export PYTHONPATH=$TVM_HOME/python:${PYTHONPATH}
  • 测试python环境

(base) ➜  build git:(heads/v0.8.0) python          
Python 3.8.17 (default, Jul  5 2023, 16:07:30) 
[Clang 14.0.6 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tvm
>>> print(tvm.__version__)
0.15.dev0
  • 安装python包
pip3 install --user onnx onnxoptimizer

2.使用TVMC命令行进行模型优化编译和运行

TVMC : tvm的命令行工具,用来完成TVM的模型导入,编译,优化等操作。

属于python工具

  • 本章节目标:
    1、为 TVM 运行时编译预训练的 ResNet-50 v2 模型。
    2、通过编译的模型运行真实图像,并解释输出和模型性能。
    3、使用 TVM 在 CPU 上调整模型。
    4、用 TVM 收集的调整数据重新编译优化模型。
    5、通过优化模型运行图像,并比较输出和模型性能。

下来进行实操:

1、 模型下载
wget https://github.com/onnx/models/raw/main/vision/classification/resnet/model/resnet50-v2-7.onnx


2、 使用TVM运行时编译ONNX模型


```shell

# This may take several minutes depending on your machine
tvmc compile \
--target "llvm" \
--output resnet50-v2-7-tvm.tar \
resnet50-v2-7.onnx

通过上述命令会生成:resnet50-v2-7-tvm.tar

让我们解压看一下:


mkdir model
tar -xvf resnet50-v2-7-tvm.tar -C model
ls model
  • mod.so is the model, represented as a C++ library, that can be loaded by the TVM runtime.

  • mod.json is a text representation of the TVM Relay computation graph.

  • mod.params is a file containing the parameters for the pre-trained model.

3 . 使用TVM运行刚刚编译的模型

我们需要准备2件东西:
	1. 刚刚编译生成的tar文件
	2. 模型的输入

为了确保模型输入的使用,我们需要对图像进行预处理,可以使用下面这段代码进行:

#!python ./preprocess.py
from tvm.contrib.download import download_testdata
from PIL import Image
import numpy as np

img_url = "https://s3.amazonaws.com/model-server/inputs/kitten.jpg"
img_path = download_testdata(img_url, "imagenet_cat.png", module="data")

# Resize it to 224x224
resized_image = Image.open(img_path).resize((224, 224))
img_data = np.asarray(resized_image).astype("float32")

# ONNX expects NCHW input, so convert the array
img_data = np.transpose(img_data, (2, 0, 1))

# Normalize according to ImageNet
imagenet_mean = np.array([0.485, 0.456, 0.406])
imagenet_stddev = np.array([0.229, 0.224, 0.225])
norm_img_data = np.zeros(img_data.shape).astype("float32")
for i in range(img_data.shape[0]):
      norm_img_data[i, :, :] = (img_data[i, :, :] / 255 - imagenet_mean[i]) / imagenet_stddev[i]

# Add batch dimension
img_data = np.expand_dims(norm_img_data, axis=0)

# Save to .npz (outputs imagenet_cat.npz)
np.savez("imagenet_cat", data=img_data)

这样我们就有了模型,还有了输入的数据,然后我们就可以运行这个模型了:

tvmc run \
--inputs imagenet_cat.npz \
--output predictions.npz \
resnet50-v2-7-tvm.tar

输出参数可视化代码段:

#!python ./postprocess.py
import os.path
import numpy as np

from scipy.special import softmax

from tvm.contrib.download import download_testdata

# Download a list of labels
labels_url = "https://s3.amazonaws.com/onnx-model-zoo/synset.txt"
labels_path = download_testdata(labels_url, "synset.txt", module="data")

with open(labels_path, "r") as f:
    labels = [l.rstrip() for l in f]

output_file = "predictions.npz"

# Open the output and read the output tensor
if os.path.exists(output_file):
    with np.load(output_file) as data:
        scores = softmax(data["output_0"])
        scores = np.squeeze(scores)
        ranks = np.argsort(scores)[::-1]

        for rank in ranks[0:5]:
            print("class='%s' with probability=%f" % (labels[rank], scores[rank]))

  1. 记录上述的运行时间,现在我们开始使用自动优化

使用自动优化需要我们提供:

1. 具体准备运行模型的设备型号
2. 存储最终自动优化记录的文件路径
3. 需要被优化的模型路径
# The default search algorithm requires xgboost, see below for further
# details on tuning search algorithms
pip install xgboost

tvmc tune \
--target "llvm" \
--output resnet50-v2-7-autotuner_records.json \
resnet50-v2-7.onnx

这一步骤特别的满,最后生成的文件长成这个样子:

在这里插入图片描述

  1. 使用自动优化数据进行优化编译模型

上一步中我们获取了一个文件名为:resnet50-v2-7-autotuner_records.json
,这个文件有二个作用:
1. 近一步优化模型
2. 作为编译器的输入

编译器将使用自动优化数据进行生成高性能的运行代码。

我们这时候使用TVM优化在编译一次:

tvmc compile \
--target "llvm" \
--tuning-records resnet50-v2-7-autotuner_records.json  \
--output resnet50-v2-7-tvm_autotuned.tar \
resnet50-v2-7.onnx

# 对比上一次没有使用优化的编译代码
tvmc compile \
--target "llvm" \
--output resnet50-v2-7-tvm.tar \
resnet50-v2-7.onnx

编译之后我们就可以使用优化后的模型,再次进行一次验证:

tvmc run \
--inputs imagenet_cat.npz \
--output predictions.npz \
resnet50-v2-7-tvm_autotuned.tar

对比一下优化前后的差距:

TVMC提供了基础的性能基础:集成在TVMC的指令里:

tvmc run \
--inputs imagenet_cat.npz \
--output predictions.npz  \
--print-time \
--repeat 100 \
resnet50-v2-7-tvm_autotuned.tar

# Execution time summary:
# mean (ms)   max (ms)    min (ms)    std (ms)
#     92.19     115.73       89.85        3.15

tvmc run \
--inputs imagenet_cat.npz \
--output predictions.npz  \
--print-time \
--repeat 100 \
resnet50-v2-7-tvm.tar

# Execution time summary:
# mean (ms)   max (ms)    min (ms)    std (ms)
#    193.32     219.97      185.04        7.11

在这里插入图片描述

  • 我的结果
    在这里插入图片描述

在这里插入图片描述

2.使用pyhton高级接口进行模型优化编译和运行

  1. 准备工作
mkdir myscripts
cd myscripts
wget https://github.com/onnx/models/raw/main/vision/classification/resnet/model/resnet50-v2-7.onnx
mv resnet50-v2-7.onnx my_model.onnx
touch tvmcpythonintro.py
  1. 导入库
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

这一步就想模型转化为relay的表达模式

  1. 编译
    make Relay into a lower-level language that the target machine can understand
package = tvmc.compile(model, target="llvm") #Step 2: Compile

  • 支持的平台:
    1. cuda (Nvidia GPU)
    2. llvm (CPU)
    3. llvm -mcpu=cascadelake (Intel CPU)
  1. 运行
result = tvmc.run(package, device="cpu") #Step 3: Run
  1. 优化
    比较推荐,优化可以提速。

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)

遇到的问题

在刚刚下载玩模型进行编译的时候,日志的登记设置不对会报错,这个是因为tvm官方文档的问题,进行如下修改就好了。

在进行模型优化编译的时候,会下砸什么东西,总是失败,可以参考:

  • WARNING:root:Failed to download tophub package for llvm: <urlopen error
    官方论坛
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
您可以按照以下步骤在Ubuntu上编译TVM: 1. 参考官方安装文档,确保您的系统环境是Ubuntu 18.04 LTS 64-bit,并根据文档中的说明进行准备工作。 2. 使用以下命令安装所需的依赖项: ``` sudo apt-get update sudo apt-get install -y python python-dev python-setuptools gcc libtinfo-dev zlib1g-dev build-essential cmake ``` 3. 在终端中使用以下命令下载TVM的源代码,并切换到源代码目录: ``` git clone --recursive https://github.com/apache/incubator-tvm.git cd incubator-tvm ``` 4. 执行以下命令编译TVM和LLVM部分: ``` mkdir build cp cmake/config.cmake build/ cd build cmake .. make -j4 ``` 5. 编译完成后,您可以在build目录中找到编译好的TVM库和可执行文件。 请注意,这里的步骤是基于Ubuntu 18.04 LTS 64-bit的环境进行的,其他环境或需求可能会有所不同。提供了更详细的信息和特定环境下的编译指南,您可以参考官方文档以满足您的需求。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Ubuntu下TVM编译安装](https://blog.csdn.net/weixin_43953703/article/details/94344889)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [dl-infer-perf:深度学习推理性能分析](https://download.csdn.net/download/weixin_42138545/16493843)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

早睡的叶子

你的鼓励就是我的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值