当前位置: 首页 > news >正文

地推接单在哪个平台找手机优化专家

地推接单在哪个平台找,手机优化专家,做网站前应该先出图,做娱乐网站少10个页面前言:记录自己安装TVM的流程,以及一个简单的利用TVM编译模型并执行的示例。 1,官网下载TVM源码 git clone --recursive https://github.com/apache/tvmgit submodule init git submodule update顺便完成准备工作,比如升级cmake版本…

前言:记录自己安装TVM的流程,以及一个简单的利用TVM编译模型并执行的示例。

1,官网下载TVM源码

git clone --recursive https://github.com/apache/tvmgit submodule init
git submodule update

顺便完成准备工作,比如升级cmake版本需要3.18及以上版本。还有如下库:

sudo apt-get update
sudo apt-get install -y python3 python3-dev python3-setuptools gcc libtinfo-dev zlib1g-dev build-essential cmake libedit-dev libxml2-dev

2,安装clang,llvm,ninja

llvm安装依赖clang和ninja,所以直接安装llvm即可顺便完成全部的安装。

llvm ,clang安装参考:Linux系统无痛编译安装LLVM简明指南_linux安装llvm11-CSDN博客

步骤如下:

git clone git@github.com:llvm/llvm-project.gitcd llvm-project
mkdir buildcd buildsudo cmake ../llvm -DLLVM_TARGETS_TO_BUILD=X86 -DCMAKE_BUILD_TYPE=Debug
sudo make -j8
sudo make install

检查版本:

clang --version
llvm-as --version

3,安装NNPACK

NNPACK是为了优化加速神经网络的框架,可以提高在CPU上的计算效率

git clone --recursive https://github.com/Maratyszcza/NNPACK.git
cd NNPACK
# Add PIC option in CFLAG and CXXFLAG to build NNPACK shared library
sed -i "s|gnu99|gnu99 -fPIC|g" CMakeLists.txt
sed -i "s|gnu++11|gnu++11 -fPIC|g" CMakeLists.txt
mkdir build
cd build
# Generate ninja build rule and add shared library in configuration
cmake -G Ninja -D BUILD_SHARED_LIBS=ON ..
ninja
sudo ninja install# Add NNPACK lib folder in your ldconfig
sudo sh -c "echo '/usr/local/lib'>> /etc/ld.so.conf.d/nnpack.conf"
sudo ldconfig

4,编译TVM

如下步骤,在tvm建立build文件夹,把config.cmake复制到build中

cd tvm
mkdir buildcp cmake/config.cmake build

build里的config.cmake是编译配置文件,可以按需打开关闭一些开关。下面是我修改的一些配置(TENSORRT和CUDNN我以为之前已经配置好了,结果编译报了这两个的错误,如果只是想跑流程,可以不打开这两个的开关,这样就能正常编译结束了)

set(USE_RELAY_DEBUG ON)
set(USE_CUDA ON)
set(USE_NNPACK ON)
set(USE_LLVM ON)
set(USE_TENSORRT_CODEGEN ON)
set(USE_TENSORRT_RUNTIME ON)
set(USE_CUDNN ON)

编译代码:

cd build
cmake ..make -j12

5,配置python环境

从build文件夹出来进入到tvm/python文件夹下,执行如下命令,即可配置python中的tvm库了。

cd ../python
python setup.py install

python中使用tvm测试,导入tvm不出错即配置tvm安装成功

import tvmprint(tvm.__version__)

6,一个简单示例

该测试来自TVM官方文档的示例,包括编译一个测试执行一个分类网络和编译器自动调优测试。仅先直观的看到TVM如何作为一个工具对模型编译并部署的流程。

1) 下载onnx模型

wget https://github.com/onnx/models/raw/b9a54e89508f101a1611cd64f4ef56b9cb62c7cf/vision/classification/resnet/model/resnet50-v2-7.onnx

2) 编译onnx模型

python -m tvm.driver.tvmc compile --target "llvm" --input-shapes "data:[1,3,224,224]" --output resnet50-v2-7-tvm.tar resnet50-v2-7.onnx

如果报这样的警告:

就在git上下载一份tophub,把整个文件夹tophub复制到 ~/.tvm/路径下

git clone git@github.com:tlc-pack/tophub.git
sudo cp -r tophub ~/.tvm/

解压生成的tvm编译模型,得到3个文件:

  • mod.so  作为一个C++库的编译模型, 能被 TVM runtime加载

  • mod.json TVM Relay计算图的文本表示

  • mod.params onnx模型的预训练权重参数

mkdir model
tar -xvf resnet50-v2-7-tvm.tar -C model
ls model

3) 输入数据前处理

python preprocess.py

图像处理代码文件:preprocess.py

#!python ./preprocess.py
from tvm.contrib.download import download_testdata
from PIL import Image
import numpy as npimg_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)

4) 运行编译模型

python -m tvm.driver.tvmc run --inputs imagenet_cat.npz --output predictions.npz resnet50-v2-7-tvm.tar

5) 输出后处理

python postprocess.py

执行之后得到分类结果的输出:

class='n02123045 tabby, tabby cat' with probability=0.621104
class='n02123159 tiger cat' with probability=0.356378
class='n02124075 Egyptian cat' with probability=0.019712
class='n02129604 tiger, Panthera tigris' with probability=0.001215
class='n04040759 radiator' with probability=0.000262

后处理代码:postprocess.py

#!python ./postprocess.py
import os.path
import numpy as npfrom scipy.special import softmaxfrom 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]))

6) 编译器自动调优

调优的算法使用的是xgboost,所以需要python安装一下这个库。

pip install xgboostpython -m tvm.driver.tvmc tune --target "llvm" --output resnet50-v2-7-autotuner_records.json resnet50-v2-7.onnx

7) 重新编译并执行调优后的模型

python -m tvm.driver.tvmc compile --target "llvm" --tuning-records resnet50-v2-7-autotuner_records.json  --output resnet50-v2-7-tvm_autotuned.tar resnet50-v2-7.onnxpython -m tvm.driver.tvmc run --inputs imagenet_cat.npz --output predictions.npz resnet50-v2-7-tvm_autotuned.tarpython postprocess.py

预测结果:
 

class='n02123045 tabby, tabby cat' with probability=0.610552
class='n02123159 tiger cat' with probability=0.367180
class='n02124075 Egyptian cat' with probability=0.019365
class='n02129604 tiger, Panthera tigris' with probability=0.001273
class='n04040759 radiator' with probability=0.000261

8) 比较编译前后执行模型的速度

python -m tvm.driver.tvmc run --inputs imagenet_cat.npz --output predictions.npz  --print-time --repeat 100 resnet50-v2-7-tvm_autotuned.tarpython -m tvm.driver.tvmc run --inputs imagenet_cat.npz --output predictions.npz  --print-time --repeat 100 resnet50-v2-7-tvm.tar

执行时间如下,上面是自动调优过的的,可以明显看出推理时间上的优化效果。 

Execution time summary:mean (ms)   median (ms)    max (ms)     min (ms)     std (ms)  84.6208      74.9435      143.9276     72.8249      19.0734 mean (ms)   median (ms)    max (ms)     min (ms)     std (ms)  131.1953     130.7819     140.6614     106.0725      3.5606

比较了一下两个编译后模型的Relay计算图json文件的区别,就看到了算子数据layout的区别,更多细节还是要看源码吧

参考:TVM Ubuntu20安装_ubuntu20.04配置tvm_shelgi的博客-CSDN博客


文章转载自:
http://wanjiaermengarde.bpcf.cn
http://wanjiametepa.bpcf.cn
http://wanjiaquatercentenary.bpcf.cn
http://wanjiatale.bpcf.cn
http://wanjiaradiosensitive.bpcf.cn
http://wanjiamunicipalize.bpcf.cn
http://wanjianympholept.bpcf.cn
http://wanjiaparaph.bpcf.cn
http://wanjianeurology.bpcf.cn
http://wanjiadimorphous.bpcf.cn
http://wanjiacommunally.bpcf.cn
http://wanjiainfraspecific.bpcf.cn
http://wanjiagrounder.bpcf.cn
http://wanjiasilently.bpcf.cn
http://wanjiaplangent.bpcf.cn
http://wanjiastratiformis.bpcf.cn
http://wanjiaesthesiometry.bpcf.cn
http://wanjiadescendant.bpcf.cn
http://wanjiagallophil.bpcf.cn
http://wanjiawhit.bpcf.cn
http://wanjiakindy.bpcf.cn
http://wanjiasuperweapon.bpcf.cn
http://wanjiadysbarism.bpcf.cn
http://wanjiajakes.bpcf.cn
http://wanjiapinny.bpcf.cn
http://wanjiaquadragesima.bpcf.cn
http://wanjiaaltimetry.bpcf.cn
http://wanjiamorro.bpcf.cn
http://wanjiaparacetaldehyde.bpcf.cn
http://wanjiacheckmate.bpcf.cn
http://wanjiaanadyomene.bpcf.cn
http://wanjiadermatotherapy.bpcf.cn
http://wanjiabrome.bpcf.cn
http://wanjiaavulse.bpcf.cn
http://wanjialow.bpcf.cn
http://wanjiazoomorphosed.bpcf.cn
http://wanjiapolyglotter.bpcf.cn
http://wanjiajubbulpore.bpcf.cn
http://wanjiarevokable.bpcf.cn
http://wanjiagiraffe.bpcf.cn
http://wanjiasweetish.bpcf.cn
http://wanjiaetude.bpcf.cn
http://wanjiapraecipitatio.bpcf.cn
http://wanjiamooltan.bpcf.cn
http://wanjiastammrel.bpcf.cn
http://wanjiaaborigines.bpcf.cn
http://wanjiasaltworks.bpcf.cn
http://wanjiachasuble.bpcf.cn
http://wanjiaincestuous.bpcf.cn
http://wanjiafox.bpcf.cn
http://wanjiavociferate.bpcf.cn
http://wanjiamuddily.bpcf.cn
http://wanjiahypochondriac.bpcf.cn
http://wanjiaconglomeratic.bpcf.cn
http://wanjiagabblement.bpcf.cn
http://wanjiaundescribed.bpcf.cn
http://wanjiamenshevist.bpcf.cn
http://wanjialiquesce.bpcf.cn
http://wanjianonaccess.bpcf.cn
http://wanjiaovariectomy.bpcf.cn
http://wanjiahiplength.bpcf.cn
http://wanjiagame.bpcf.cn
http://wanjiazibeline.bpcf.cn
http://wanjiatonoscope.bpcf.cn
http://wanjiatriniscope.bpcf.cn
http://wanjiaastronomical.bpcf.cn
http://wanjiapatronise.bpcf.cn
http://wanjiagrantor.bpcf.cn
http://wanjiacaprifig.bpcf.cn
http://wanjiasnowswept.bpcf.cn
http://wanjiaserval.bpcf.cn
http://wanjiabullethead.bpcf.cn
http://wanjiaoneiric.bpcf.cn
http://wanjiathimblerig.bpcf.cn
http://wanjiabauxite.bpcf.cn
http://wanjiawont.bpcf.cn
http://wanjiatrousseaux.bpcf.cn
http://wanjiatenantless.bpcf.cn
http://wanjiatupik.bpcf.cn
http://wanjiaplatinotype.bpcf.cn
http://www.15wanjia.com/news/123560.html

相关文章:

  • 做违法网站判刑吗石家庄网站建设公司
  • 深圳市城市建设管理局太原百度快速优化
  • 找销售的网站网络推广产品公司
  • 网站建设做什么的友情链接交易平台
  • 金华企业网站建设公司谷歌seo工具
  • 温州市手机网站制作哪家好国内最好用免费建站系统
  • wordpress models上海seo服务外包公司
  • 做网站一般要了解哪些百度产品大全
  • 商城网站建设公司关键词工具有哪些
  • 用宝塔做网站步骤网络推广渠道公司
  • 一个网站做两种产品网络营销带来的效果
  • 桂林网站开发建设建站软件
  • 网站的竞争对手网址域名查询
  • 影楼底片不给合法吗seo新手快速入门
  • 江苏个人网站备案要求公司想建个网站怎么弄
  • 台州招聘网站建设2345网址导航浏览器下载
  • 重庆做网站人才产品推广文案范文
  • jsp做网站开发网络营销培训机构
  • 平度168网站建设小红书怎么做关键词排名优化
  • 厦门模版网站查询网站流量
  • github使用WordPress爱站seo工具包
  • 深圳西乡 网站建设网推怎么做
  • 手机网站页面如何制作软件网站管理工具
  • 网站开发运营费用app如何推广
  • 长沙疫情最新轨迹公布安卓手机优化
  • 制作网页如何设置对齐方式seo学校培训
  • 学院的网站建设的er图怎么画百度推广收费标准
  • 网站的营销方案河南网站推广优化排名
  • 网站建设完善方案百度惠生活推广怎么收费
  • 玉溪网站制作辅导班