diff --git a/gallery/how_to/work_with_microtvm/micro_tflite.py b/gallery/how_to/work_with_microtvm/micro_tflite.py index bd70fc581c5c..3d871ba783ad 100644 --- a/gallery/how_to/work_with_microtvm/micro_tflite.py +++ b/gallery/how_to/work_with_microtvm/micro_tflite.py @@ -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") @@ -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 @@ -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 @@ -237,15 +250,12 @@ # (:doc:`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())) @@ -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. @@ -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"