Skip to content

Commit

Permalink
[microTVM] Update tutorials (#13845)
Browse files Browse the repository at this point in the history
This PR updates microTVM tutorials to use updated APIs.
It also adds an ordering to the tutorials that are useful for first time users.
RVM tutorial is also removed as it is not supported anymore.
  • Loading branch information
mehrdadh authored Jan 27, 2023
1 parent 95fa223 commit c2cc019
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 272 deletions.
12 changes: 6 additions & 6 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,15 +511,15 @@ def jupyter_notebook(script_blocks, gallery_conf, target_dir, real_func):
"use_pass_instrument.py",
"bring_your_own_datatypes.py",
],
"micro": [
"micro_train.py",
"micro_autotune.py",
"micro_reference_vm.py",
"micro_tflite.py",
"micro_ethosu.py",
"work_with_microtvm": [
"micro_tvmc.py",
"micro_tflite.py",
"micro_aot.py",
"micro_pytorch.py",
"micro_train.py",
"micro_autotune.py",
"micro_ethosu.py",
"micro_mlperftiny.py",
],
}

Expand Down
11 changes: 5 additions & 6 deletions docs/topic/microtvm/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,12 @@ Getting Started with microTVM
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Before working with microTVM, we recommend you have a supported development board. Then, follow these
tutorials to get started with microTVM:
tutorials to get started with microTVM. Tutorials are in the order that could help developers to learn
more as they follow through them. Here is a list of tutorials that you can start with:

1. :ref:`Start the microTVM Reference VM <tutorial-micro-reference-vm>`. The microTVM tutorials
depend on Zephyr and on a compiler toolchain for your hardware. The reference VM is a convenient
way to install those dependencies.
2. Try the :ref:`microTVM with TFLite Tutorial <microTVM-with-TFLite>`.
3. Try running a more complex `CIFAR10-CNN model <https://github.com/areusch/microtvm-blogpost-eval>`_.
1. Try :ref:`microTVM CLI Tool <tutorial-micro-cli-tool>`.
2. Try the :ref:`microTVM TFLite Tutorial <tutorial_micro_tflite>`.
3. Try running a more complex tutorial: :ref:`Creating Your MLPerfTiny Submission with microTVM <tutorial-micro-mlperftiny>`.


How microTVM Works
Expand Down
17 changes: 7 additions & 10 deletions gallery/how_to/work_with_microtvm/micro_aot.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
# specific language governing permissions and limitations
# under the License.
"""
.. _tutorial-micro-AoT:
.. _tutorial-micro-aot:
microTVM Host-Driven AoT
===========================
3. microTVM Ahead-of-Time (AOT) Compilation
===========================================
**Authors**:
`Mehrdad Hessar <https://github.com/mehrdadh>`_,
`Alan MacDonald <https://github.com/alanmacd>`_
Expand Down Expand Up @@ -59,6 +59,7 @@

import tvm
from tvm import relay
import tvm.micro.testing
from tvm.relay.backend import Executor, Runtime
from tvm.contrib.download import download_testdata

Expand Down Expand Up @@ -102,27 +103,23 @@
# using AOT host driven executor. We use the host micro target which is for running a model
# on x86 CPU using CRT runtime or running a model with Zephyr platform on qemu_x86 simulator
# board. In the case of a physical microcontroller, we get the target model for the physical
# board (E.g. nucleo_l4r5zi) and pass it to `tvm.target.target.micro` to create a full
# micro target.
# board (E.g. nucleo_l4r5zi) and change `BOARD` to supported Zephyr board.
#

# Use the C runtime (crt) and enable static linking by setting system-lib to True
RUNTIME = Runtime("crt", {"system-lib": True})

# Simulate a microcontroller on the host machine. Uses the main() from `src/runtime/crt/host/main.cc`.
# To use physical hardware, replace "host" with something matching your hardware.
TARGET = tvm.target.target.micro("host")
TARGET = tvm.micro.testing.get_target("crt")

# Use the AOT executor rather than graph or vm executors. Don't use unpacked API or C calling style.
EXECUTOR = Executor("aot")

if use_physical_hw:
boards_file = pathlib.Path(tvm.micro.get_microtvm_template_projects("zephyr")) / "boards.json"
with open(boards_file) as f:
boards = json.load(f)
BOARD = os.getenv("TVM_MICRO_BOARD", default="nucleo_l4r5zi")
SERIAL = os.getenv("TVM_MICRO_SERIAL", default=None)
TARGET = tvm.target.target.micro(boards[BOARD]["model"])
TARGET = tvm.micro.testing.get_target("zephyr", BOARD)

######################################################################
# Compile the model
Expand Down
13 changes: 5 additions & 8 deletions gallery/how_to/work_with_microtvm/micro_autotune.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"""
.. _tutorial-micro-autotune:
Autotuning with microTVM
=========================
6. Model Tuning with microTVM
=============================
**Authors**:
`Andrew Reusch <https://github.com/areusch>`_,
`Mehrdad Hessar <https://github.com/mehrdadh>`_
Expand Down Expand Up @@ -55,6 +55,7 @@

import tvm
from tvm.relay.backend import Runtime
import tvm.micro.testing

####################
# Defining the model
Expand Down Expand Up @@ -102,20 +103,16 @@
#

RUNTIME = Runtime("crt", {"system-lib": True})
TARGET = tvm.target.target.micro("host")
TARGET = tvm.micro.testing.get_target("crt")

# Compiling for physical hardware
# --------------------------------------------------------------------------
# When running on physical hardware, choose a TARGET and a BOARD that describe the hardware. The
# STM32L4R5ZI Nucleo target and board is chosen in the example below.
if use_physical_hw:
boards_file = pathlib.Path(tvm.micro.get_microtvm_template_projects("zephyr")) / "boards.json"
with open(boards_file) as f:
boards = json.load(f)

BOARD = os.getenv("TVM_MICRO_BOARD", default="nucleo_l4r5zi")
SERIAL = os.getenv("TVM_MICRO_SERIAL", default=None)
TARGET = tvm.target.target.micro(boards[BOARD]["model"])
TARGET = tvm.micro.testing.get_target("zephyr", BOARD)


#########################
Expand Down
6 changes: 4 additions & 2 deletions gallery/how_to/work_with_microtvm/micro_ethosu.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
# specific language governing permissions and limitations
# under the License.
"""
Running TVM on bare metal Arm(R) Cortex(R)-M55 CPU and Ethos(TM)-U55 NPU with CMSIS-NN
======================================================================================
.. _tutorial-micro-ethosu:
7. Running TVM on bare metal Arm(R) Cortex(R)-M55 CPU and Ethos(TM)-U55 NPU with CMSIS-NN
=========================================================================================
**Author**:
`Grant Watson <https://github.com/grant-arm>`_
Expand Down
7 changes: 4 additions & 3 deletions gallery/how_to/work_with_microtvm/micro_mlperftiny.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
# specific language governing permissions and limitations
# under the License.
"""
.. _tutorial-micro-MLPerfTiny:
.. _tutorial-micro-mlperftiny:
Creating Your MLPerfTiny Submission with microTVM
=================================================
8. Creating Your MLPerfTiny Submission with microTVM
====================================================
**Authors**:
`Mehrdad Hessar <https://github.com/mehrdadh>`_
Expand Down Expand Up @@ -69,6 +69,7 @@
from tvm.contrib.download import download_testdata
from tvm.micro import export_model_library_format
from tvm.micro.model_library_format import generate_c_interface_header
import tvm.micro.testing
from tvm.micro.testing.utils import (
create_header_file,
mlf_extract_workspace_size_bytes,
Expand Down
18 changes: 10 additions & 8 deletions gallery/how_to/work_with_microtvm/micro_pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
# specific language governing permissions and limitations
# under the License.
"""
.. _tutorial-micro-Pytorch:
.. _tutorial-micro-pytorch:
microTVM PyTorch Tutorial
===========================
4. microTVM PyTorch Tutorial
============================
**Authors**:
`Mehrdad Hessar <https://github.com/mehrdadh>`_
Expand Down Expand Up @@ -46,6 +46,7 @@
from tvm import relay
from tvm.contrib.download import download_testdata
from tvm.relay.backend import Executor
import tvm.micro.testing

##################################
# Load a pre-trained PyTorch model
Expand Down Expand Up @@ -91,13 +92,14 @@
# and we use `host` micro target. Using this setup, TVM compiles the model
# for C runtime which can run on a x86 CPU machine with the same flow that
# would run on a physical microcontroller.
# CRT Uses the main() from `src/runtime/crt/host/main.cc`
# To use physical hardware, replace `board` with another physical micro target, e.g. `nrf5340dk_nrf5340_cpuapp`
# or `mps2_an521` and change the platform type to Zephyr.
# See more target examples in :ref:`Training Vision Models for microTVM on Arduino <tutorial-micro-train-arduino>`
# and :ref:`microTVM TFLite Tutorial<tutorial_micro_tflite>`.
#


# Simulate a microcontroller on the host machine. Uses the main() from `src/runtime/crt/host/main.cc`
# To use physical hardware, replace "host" with another physical micro target, e.g. `nrf52840`
# or `mps2_an521`. See more more target examples in micro_train.py and micro_tflite.py tutorials.
target = tvm.target.target.micro("host")
target = tvm.micro.testing.get_target(platform="crt", board=None)

# Use the C runtime (crt) and enable static linking by setting system-lib to True
runtime = tvm.relay.backend.Runtime("crt", {"system-lib": True})
Expand Down
159 changes: 0 additions & 159 deletions gallery/how_to/work_with_microtvm/micro_reference_vm.py

This file was deleted.

Loading

0 comments on commit c2cc019

Please sign in to comment.