From 68ac9e34c0122e52413b98ef99eef3c609494748 Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Wed, 19 Jan 2022 14:56:44 -0800 Subject: [PATCH 1/6] Add env variable to micro tflite tutorial --- .../how_to/work_with_microtvm/micro_tflite.py | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/gallery/how_to/work_with_microtvm/micro_tflite.py b/gallery/how_to/work_with_microtvm/micro_tflite.py index bd70fc581c5c..389e4652f29c 100644 --- a/gallery/how_to/work_with_microtvm/micro_tflite.py +++ b/gallery/how_to/work_with_microtvm/micro_tflite.py @@ -129,6 +129,7 @@ from tvm.contrib.download import download_testdata from tvm import relay +use_physical_hw = True if os.getenv("TVM_MICRO_USE_HW") else False 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 +182,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 +191,23 @@ # 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#" + +import json +import pathlib + +if use_physical_hw: + if os.getenv("TVM_MICRO_BOARD"): + BOARD = os.getenv("TVM_MICRO_BOARD") + with open( + pathlib.Path(tvm.micro.get_microtvm_template_projects("zephyr")) / "boards.json" + ) as f: + board_properties = json.load(f) + boards_model = {board: info["model"] for board, info in board_properties.items()} + TARGET = tvm.target.target.micro(boards_model[BOARD]) + else: + BOARD = "nucleo_f746zg" + TARGET = tvm.target.target.micro("stm32f746xx") + # # 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 @@ -264,7 +280,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")) @@ -275,8 +290,10 @@ # 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 From 99446a2815191dbd926bc1784bded1d651eae3b2 Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Mon, 24 Jan 2022 09:32:21 -0800 Subject: [PATCH 2/6] Address @gromero comments --- .../how_to/work_with_microtvm/micro_tflite.py | 33 +++++++------------ 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/gallery/how_to/work_with_microtvm/micro_tflite.py b/gallery/how_to/work_with_microtvm/micro_tflite.py index 389e4652f29c..a7b7e285b0ca 100644 --- a/gallery/how_to/work_with_microtvm/micro_tflite.py +++ b/gallery/how_to/work_with_microtvm/micro_tflite.py @@ -123,11 +123,16 @@ # 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 = True if os.getenv("TVM_MICRO_USE_HW") else False model_url = "https://people.linaro.org/~tom.gall/sine_model.tflite" @@ -192,21 +197,13 @@ # board to generated the right firmware image. # -import json -import pathlib +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"]) -if use_physical_hw: - if os.getenv("TVM_MICRO_BOARD"): - BOARD = os.getenv("TVM_MICRO_BOARD") - with open( - pathlib.Path(tvm.micro.get_microtvm_template_projects("zephyr")) / "boards.json" - ) as f: - board_properties = json.load(f) - boards_model = {board: info["model"] for board, info in board_properties.items()} - TARGET = tvm.target.target.micro(boards_model[BOARD]) - else: - BOARD = "nucleo_f746zg" - TARGET = tvm.target.target.micro("stm32f746xx") # # For some boards, Zephyr runs them emulated by default, using QEMU. For example, below is the @@ -253,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())) @@ -280,8 +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 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. @@ -296,7 +288,6 @@ 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" From 5d5f97999fcb60c4b6c091414af656406f37c4c9 Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Mon, 24 Jan 2022 10:05:51 -0800 Subject: [PATCH 3/6] address @areusch comment --- gallery/how_to/work_with_microtvm/micro_tflite.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gallery/how_to/work_with_microtvm/micro_tflite.py b/gallery/how_to/work_with_microtvm/micro_tflite.py index a7b7e285b0ca..feb037d5f3c1 100644 --- a/gallery/how_to/work_with_microtvm/micro_tflite.py +++ b/gallery/how_to/work_with_microtvm/micro_tflite.py @@ -134,7 +134,7 @@ import tvm.contrib.utils from tvm.contrib.download import download_testdata -use_physical_hw = True if os.getenv("TVM_MICRO_USE_HW") else False +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") From 241c4386ed6347bc7b220ed258beb91b912707be Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Mon, 24 Jan 2022 10:15:58 -0800 Subject: [PATCH 4/6] fix scope --- gallery/how_to/work_with_microtvm/micro_tflite.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/gallery/how_to/work_with_microtvm/micro_tflite.py b/gallery/how_to/work_with_microtvm/micro_tflite.py index feb037d5f3c1..3d871ba783ad 100644 --- a/gallery/how_to/work_with_microtvm/micro_tflite.py +++ b/gallery/how_to/work_with_microtvm/micro_tflite.py @@ -197,13 +197,13 @@ # board to generated the right firmware image. # -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"]) +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 From 2f1adcfeb0f3fc828f34945aff5d1f3a65f336d7 Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Mon, 24 Jan 2022 17:05:28 -0800 Subject: [PATCH 5/6] trigger From f3f09bc5717fe2440a2636d0e1f59f51e8cfa832 Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Tue, 25 Jan 2022 17:07:03 -0800 Subject: [PATCH 6/6] trigger