Download - CMSC5743 Lab 06 TVM Tutorial-2 Materials
![Page 1: CMSC5743 Lab 06 TVM Tutorial-2 Materials](https://reader030.vdocuments.site/reader030/viewer/2022040420/6249710c8d252d4e712e3f74/html5/thumbnails/1.jpg)
CMSC5743 Lab 06TVM Tutorial-2 Materials
Yang BAIDepartment of Computer Science & EngineeringChinese University of Hong [email protected]
December 3, 2021
![Page 2: CMSC5743 Lab 06 TVM Tutorial-2 Materials](https://reader030.vdocuments.site/reader030/viewer/2022040420/6249710c8d252d4e712e3f74/html5/thumbnails/2.jpg)
1 TVM Installation
2 Compile PyTorch Model by TVM
3 Homework
Outline
2/19
![Page 3: CMSC5743 Lab 06 TVM Tutorial-2 Materials](https://reader030.vdocuments.site/reader030/viewer/2022040420/6249710c8d252d4e712e3f74/html5/thumbnails/3.jpg)
TVM Installation
![Page 4: CMSC5743 Lab 06 TVM Tutorial-2 Materials](https://reader030.vdocuments.site/reader030/viewer/2022040420/6249710c8d252d4e712e3f74/html5/thumbnails/4.jpg)
• Recommended System: MAC OS or Linux.
• LLVM 9.0+
• git clone –recursive https://github.com/apache/tvm tvm
• mkdir build
• cp cmake/config.cmake build
• open the LLVM option
• cmake ..
• make -j10
TVM Installation
4/19
![Page 5: CMSC5743 Lab 06 TVM Tutorial-2 Materials](https://reader030.vdocuments.site/reader030/viewer/2022040420/6249710c8d252d4e712e3f74/html5/thumbnails/5.jpg)
• vim ~/.bashrc or ~/.bash_profile
• export TVM_HOME=/Users/baiyang/Documents/Project/tvm
• export PYTHONPATH=$TVM_HOME/python:$PYTHONPATH
TVM Environment Setup
5/19
![Page 6: CMSC5743 Lab 06 TVM Tutorial-2 Materials](https://reader030.vdocuments.site/reader030/viewer/2022040420/6249710c8d252d4e712e3f74/html5/thumbnails/6.jpg)
• pip install torch==1.7.0
• pip install torchvision==0.8.1
PyTorch and PIL Installation
6/19
![Page 7: CMSC5743 Lab 06 TVM Tutorial-2 Materials](https://reader030.vdocuments.site/reader030/viewer/2022040420/6249710c8d252d4e712e3f74/html5/thumbnails/7.jpg)
• import tvm
• tvm.__version__
• from PIL import Image
• import torch
• import torchvision
Verify the correct installaltion
7/19
![Page 8: CMSC5743 Lab 06 TVM Tutorial-2 Materials](https://reader030.vdocuments.site/reader030/viewer/2022040420/6249710c8d252d4e712e3f74/html5/thumbnails/8.jpg)
Compile PyTorch Model by TVM
![Page 9: CMSC5743 Lab 06 TVM Tutorial-2 Materials](https://reader030.vdocuments.site/reader030/viewer/2022040420/6249710c8d252d4e712e3f74/html5/thumbnails/9.jpg)
• Load a pretrained PyTorch model
• Load a test image
• Import the graph to Relay
• Relay Build
• Execute the portable graph on TVM
• Look up synset name
Compile PyTorch Model by TVM
9/19
![Page 10: CMSC5743 Lab 06 TVM Tutorial-2 Materials](https://reader030.vdocuments.site/reader030/viewer/2022040420/6249710c8d252d4e712e3f74/html5/thumbnails/10.jpg)
TVM Overview
10/19
![Page 11: CMSC5743 Lab 06 TVM Tutorial-2 Materials](https://reader030.vdocuments.site/reader030/viewer/2022040420/6249710c8d252d4e712e3f74/html5/thumbnails/11.jpg)
• resnet18
• torchvision
Load a pretrained PyTorch model
11/19
![Page 12: CMSC5743 Lab 06 TVM Tutorial-2 Materials](https://reader030.vdocuments.site/reader030/viewer/2022040420/6249710c8d252d4e712e3f74/html5/thumbnails/12.jpg)
• img_url
• Preprocess the image and convert to tensor
Load a test image
12/19
![Page 13: CMSC5743 Lab 06 TVM Tutorial-2 Materials](https://reader030.vdocuments.site/reader030/viewer/2022040420/6249710c8d252d4e712e3f74/html5/thumbnails/13.jpg)
• relay.frontend.from_pytorch
• relay.frontend.from_tensorflow
• relay.frontend.from_mxnet
• relay.frontend.onnx
Import the graph to Relay
13/19
![Page 14: CMSC5743 Lab 06 TVM Tutorial-2 Materials](https://reader030.vdocuments.site/reader030/viewer/2022040420/6249710c8d252d4e712e3f74/html5/thumbnails/14.jpg)
• target
• target_host
• relay.build
Relay Build
14/19
![Page 15: CMSC5743 Lab 06 TVM Tutorial-2 Materials](https://reader030.vdocuments.site/reader030/viewer/2022040420/6249710c8d252d4e712e3f74/html5/thumbnails/15.jpg)
• Set inputs
• Execute
• Get outputs
Execute the portable graph on TVM
15/19
![Page 16: CMSC5743 Lab 06 TVM Tutorial-2 Materials](https://reader030.vdocuments.site/reader030/viewer/2022040420/6249710c8d252d4e712e3f74/html5/thumbnails/16.jpg)
• imagenet_synsets.txt
• Get top-1 result for TVM
• Get top-1 result for PyTorch
• Convert input to PyTorch variable and get PyTorch result for comparison
Look up synset name
16/19
![Page 17: CMSC5743 Lab 06 TVM Tutorial-2 Materials](https://reader030.vdocuments.site/reader030/viewer/2022040420/6249710c8d252d4e712e3f74/html5/thumbnails/17.jpg)
Homework
![Page 18: CMSC5743 Lab 06 TVM Tutorial-2 Materials](https://reader030.vdocuments.site/reader030/viewer/2022040420/6249710c8d252d4e712e3f74/html5/thumbnails/18.jpg)
• Change the img_path in your datapath and get the correct recognition
• Change the model_name in torchvision
• Record the final prediction of your change
Homwork
18/19
![Page 19: CMSC5743 Lab 06 TVM Tutorial-2 Materials](https://reader030.vdocuments.site/reader030/viewer/2022040420/6249710c8d252d4e712e3f74/html5/thumbnails/19.jpg)
THANK YOU!