cmsc5743 lab 06 tvm tutorial-2 materials

19
CMSC5743 Lab 06 TVM Tutorial-2 Materials Yang BAI Department of Computer Science & Engineering Chinese University of Hong Kong [email protected] December 3, 2021

Upload: others

Post on 03-Apr-2022

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CMSC5743 Lab 06 TVM Tutorial-2 Materials

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

1 TVM Installation

2 Compile PyTorch Model by TVM

3 Homework

Outline

2/19

Page 3: CMSC5743 Lab 06 TVM Tutorial-2 Materials

TVM Installation

Page 4: CMSC5743 Lab 06 TVM Tutorial-2 Materials

• 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

• 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

• 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

• 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

Compile PyTorch Model by TVM

Page 9: CMSC5743 Lab 06 TVM Tutorial-2 Materials

• 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

TVM Overview

10/19

Page 11: CMSC5743 Lab 06 TVM Tutorial-2 Materials

• resnet18

• torchvision

Load a pretrained PyTorch model

11/19

Page 12: CMSC5743 Lab 06 TVM Tutorial-2 Materials

• img_url

• Preprocess the image and convert to tensor

Load a test image

12/19

Page 13: CMSC5743 Lab 06 TVM Tutorial-2 Materials

• 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

• target

• target_host

• relay.build

Relay Build

14/19

Page 15: CMSC5743 Lab 06 TVM Tutorial-2 Materials

• Set inputs

• Execute

• Get outputs

Execute the portable graph on TVM

15/19

Page 16: CMSC5743 Lab 06 TVM Tutorial-2 Materials

• 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

Homework

Page 18: CMSC5743 Lab 06 TVM Tutorial-2 Materials

• 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

THANK YOU!