You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by me...@apache.org on 2023/01/12 22:48:53 UTC

[tvm] branch micro/zephyr_add_model_usmp created (now 6f4781d5fa)

This is an automated email from the ASF dual-hosted git repository.

mehrdadh pushed a change to branch micro/zephyr_add_model_usmp
in repository https://gitbox.apache.org/repos/asf/tvm.git


      at 6f4781d5fa added test

This branch includes the following new commits:

     new 6f4781d5fa added test

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[tvm] 01/01: added test

Posted by me...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

mehrdadh pushed a commit to branch micro/zephyr_add_model_usmp
in repository https://gitbox.apache.org/repos/asf/tvm.git

commit 6f4781d5fadf57335afee82e800a958a1d6b1f8f
Author: Mehrdad Hessar <mh...@octoml.ai>
AuthorDate: Thu Jan 12 14:48:32 2023 -0800

    added test
---
 python/tvm/micro/testing/evaluation.py     | 18 +++++---
 tests/micro/zephyr/test_zephyr_aot_exec.py | 73 ++++++++++++++++++++++++++++++
 2 files changed, 84 insertions(+), 7 deletions(-)

diff --git a/python/tvm/micro/testing/evaluation.py b/python/tvm/micro/testing/evaluation.py
index c16b97f61d..cab3952a5f 100644
--- a/python/tvm/micro/testing/evaluation.py
+++ b/python/tvm/micro/testing/evaluation.py
@@ -91,17 +91,18 @@ def tune_model(
 
 
 def create_aot_session(
-    platform,
-    board,
-    target,
+    platform: str,
+    board: str,
+    target: tvm.target.Target,
     mod,
     params,
     build_dir=Path(tempfile.mkdtemp()),
     tune_logs=None,
     timeout_override=None,
-    use_cmsis_nn=False,
-    project_options=None,
-    use_existing=False,
+    use_cmsis_nn: bool = False,
+    project_options: dict = None,
+    use_existing: bool = False,
+    enable_usmp: bool = False,
 ):
     """AOT-compiles and uploads a model to a microcontroller, and returns the RPC session"""
 
@@ -109,7 +110,10 @@ def create_aot_session(
     crt_runtime = tvm.relay.backend.Runtime("crt", {"system-lib": True})
 
     with ExitStack() as stack:
-        config = {"tir.disable_vectorize": True}
+        config = {
+            "tir.disable_vectorize": True,
+            "tir.usmp.enable": enable_usmp,
+        }
         if use_cmsis_nn:
             config["relay.ext.cmsisnn.options"] = {"mcpu": target.mcpu}
         stack.enter_context(tvm.transform.PassContext(opt_level=3, config=config))
diff --git a/tests/micro/zephyr/test_zephyr_aot_exec.py b/tests/micro/zephyr/test_zephyr_aot_exec.py
index a67cf0830a..fd61316bf9 100644
--- a/tests/micro/zephyr/test_zephyr_aot_exec.py
+++ b/tests/micro/zephyr/test_zephyr_aot_exec.py
@@ -22,6 +22,9 @@ import tvm.testing
 import tvm.relay as relay
 from tvm.relay.backend import Executor, Runtime
 from tvm.contrib import utils
+from tvm.contrib.download import download_testdata
+from tvm.micro.testing import create_aot_session, predict_labels_aot
+from tvm.micro.project_api import server
 
 from . import utils
 
@@ -150,5 +153,75 @@ def test_aot_executor(workspace_dir, board, microtvm_debug, use_fvp, serial_numb
         do_test()
 
 
+enable_usmp = tvm.testing.parameter(False, True)
+
+
+@tvm.testing.requires_micro
+@pytest.mark.skip_boards(["mps2_an521"])
+def test_model(workspace_dir, board, microtvm_debug, use_fvp, enable_usmp, serial_number):
+    model_url = "https://github.com/tlc-pack/web-data/raw/93ed36f3bef8e23318b516919fdc89cd6eab6aef/testdata/microTVM/model/visual_wake_word_quant.tflite"
+    model_path = download_testdata(model_url, "vww_96_int8.tflite", module="model")
+    input_shape = (1, 96, 96, 3)
+
+    # Import TFLite model
+    tflite_model_buf = open(model_path, "rb").read()
+    try:
+        import tflite
+
+        tflite_model = tflite.Model.GetRootAsModel(tflite_model_buf, 0)
+    except AttributeError:
+        import tflite.Model
+
+        tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0)
+
+    relay_mod, params = relay.frontend.from_tflite(
+        tflite_model, shape_dict={"input_1_int8": input_shape}, dtype_dict={"input_1_int8": "int8"}
+    )
+
+    sample_url = "https://github.com/tlc-pack/web-data/raw/93ed36f3bef8e23318b516919fdc89cd6eab6aef/testdata/microTVM/data/visual_wake_word_int8_1.npy"
+    sample_path = download_testdata(sample_url, "visual_wake_word_int8_1.npy", module="data")
+    sample = [np.load(sample_path)]
+
+    if board == "nrf5340dk_nrf5340_cpuapp":
+        config_main_stack_size = 4000
+    elif board == "nucleo_f746zg":
+        config_main_stack_size = 4000
+    elif board == "nucleo_l4r5zi":
+        config_main_stack_size = 4000
+    elif board == "stm32f746g_disco":
+        config_main_stack_size = 3000
+    elif board == "qemu_x86":
+        config_main_stack_size = 4000
+
+    project_options = {"serial_number": serial_number}
+
+    if config_main_stack_size:
+        project_options["config_main_stack_size"] = config_main_stack_size
+
+    target = tvm.micro.testing.get_target("zephyr", board)
+    with create_aot_session(
+        "zephyr",
+        board,
+        target,
+        relay_mod,
+        params,
+        build_dir=workspace_dir,
+        # # The longest models take ~5 seconds to infer, but running them
+        # # ten times (with NUM_TESTING_RUNS_PER_SAMPLE) makes that 50
+        timeout_override=server.TransportTimeouts(
+            session_start_retry_timeout_sec=300,
+            session_start_timeout_sec=150,
+            session_established_timeout_sec=150,
+        ),
+        project_options=project_options,
+        enable_usmp=enable_usmp,
+    ) as session:
+        aot_executor = tvm.runtime.executor.aot_executor.AotModule(session.create_aot_executor())
+        predicted_labels, _ = zip(
+            *predict_labels_aot(session, aot_executor, sample, runs_per_sample=1)
+        )
+        assert predicted_labels[0] == 1
+
+
 if __name__ == "__main__":
     tvm.testing.main()