Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[microTVM][tutorial] Add ENV variable to enable testing on physical hardware #9993

Merged
merged 6 commits into from
Jan 26, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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#"

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"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mehrdadh This is now out of use_physical_hw scope, so it will overwrite the previous TARGET value. Is that intentional?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, that was incorrect. fixed it.
Thanks!



#
# 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