Skip to content

Commit

Permalink
[microTVM][tutorial] Add ENV variable to enable testing on physical h…
Browse files Browse the repository at this point in the history
…ardware (apache#9993)

* Add env variable to micro tflite tutorial

* Address @gromero comments

* address @areusch comment

* fix scope

* trigger

* trigger
  • Loading branch information
mehrdadh authored and sunggg committed Jan 28, 2022
1 parent f9e1ff8 commit fa00dc6
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions gallery/how_to/work_with_microtvm/micro_tflite.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,18 @@
# directory into a buffer

import os
import json
import tarfile
import pathlib
import tempfile
import numpy as np

import tvm
from tvm.contrib.download import download_testdata
from tvm import relay
import tvm.contrib.utils
from tvm.contrib.download import download_testdata

use_physical_hw = bool(os.getenv("TVM_MICRO_USE_HW"))
model_url = "https://people.linaro.org/~tom.gall/sine_model.tflite"
model_file = "sine_model.tflite"
model_path = download_testdata(model_url, model_file, module="data")
Expand Down Expand Up @@ -181,7 +187,7 @@
#
RUNTIME = tvm.relay.backend.Runtime("crt", {"system-lib": True})
TARGET = tvm.target.target.micro("host")
BOARD = "qemu_x86"

#
# Compiling for physical hardware
# When running on physical hardware, choose a TARGET and a BOARD that describe the hardware. The
Expand All @@ -190,8 +196,15 @@
# board but a couple of wirings and configs differ, it's necessary to select the "stm32f746g_disco"
# board to generated the right firmware image.
#
# TARGET = tvm.target.target.micro("stm32f746xx")
# BOARD = "nucleo_f746zg" # or "stm32f746g_disco#"

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_f746zg")
TARGET = tvm.target.target.micro(boards[BOARD]["model"])

#
# For some boards, Zephyr runs them emulated by default, using QEMU. For example, below is the
# TARGET and BOARD used to build a microTVM firmware for the mps2-an521 board. Since that board
Expand Down Expand Up @@ -237,15 +250,12 @@
# (:doc:`Model Library Format` </dev/model_library_format>`). This is a tarball with a standard layout:

# Get a temporary path where we can store the tarball (since this is running as a tutorial).
import tempfile

fd, model_library_format_tar_path = tempfile.mkstemp()
os.close(fd)
os.unlink(model_library_format_tar_path)
tvm.micro.export_model_library_format(module, model_library_format_tar_path)

import tarfile

with tarfile.open(model_library_format_tar_path, "r:*") as tar_f:
print("\n".join(f" - {m.name}" for m in tar_f.getmembers()))

Expand All @@ -264,9 +274,6 @@
# this lives in a file ``microtvm_api_server.py`` in the root directory). Let's use the example ``host``
# project in this tutorial, which simulates the device using a POSIX subprocess and pipes:

import subprocess
import pathlib

template_project_path = pathlib.Path(tvm.micro.get_microtvm_template_projects("crt"))
project_options = {} # You can use options to provide platform-specific options through TVM.

Expand All @@ -275,11 +282,12 @@
# For physical hardware, you can try out the Zephyr platform by using a different template project
# and options:
#
# template_project_path = pathlib.Path(tvm.micro.get_microtvm_template_projects("zephyr"))
# project_options = {"project_type": "host_driven", zephyr_board": "nucleo_f746zg"}}

if use_physical_hw:
template_project_path = pathlib.Path(tvm.micro.get_microtvm_template_projects("zephyr"))
project_options = {"project_type": "host_driven", "zephyr_board": BOARD}

# Create a temporary directory
import tvm.contrib.utils

temp_dir = tvm.contrib.utils.tempdir()
generated_project_dir = temp_dir / "generated-project"
Expand Down

0 comments on commit fa00dc6

Please sign in to comment.