gromero commented on a change in pull request #9993:
URL: https://github.com/apache/tvm/pull/9993#discussion_r790801755



##########
File path: gallery/how_to/work_with_microtvm/micro_tflite.py
##########
@@ -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()}

Review comment:
       It's not necessary to build a list comprehension here out of the the 
json board file, you can use the json file directly, like:
   
   ```
   TARGET = tvm.target.target.micro(boards[BOARD]["model"])
   ```
   
   `os.getenv()` also supports setting a default in case `TVM_MICRO_BOARD` is 
not set in ENV, which can simply the code a bit for the tutorial.
   
   `pathlib` is also being imported twice regardless if the 
`use_physical_hw=True` or not. I  also would move these imports to the 
beginning of the .py. 
   
   Suggested change here:
   
   ```
   diff --git a/gallery/how_to/work_with_microtvm/micro_tflite.py 
b/gallery/how_to/work_with_microtvm/micro_tflite.py
   index 389e4652f..c8b8bf877 100644
   --- a/gallery/how_to/work_with_microtvm/micro_tflite.py
   +++ b/gallery/how_to/work_with_microtvm/micro_tflite.py
   @@ -122,8 +122,10 @@ model with Relay.
    # Load the pretrained TFLite model from a file in your current
    # directory into a buffer
    
   -import os
   +import json
    import numpy as np
   +import os
   +import pathlib
    
    import tvm
    from tvm.contrib.download import download_testdata
   @@ -192,21 +194,14 @@ TARGET = tvm.target.target.micro("host")
    #  board to generated the right firmware image.
    #
    
   -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")
   +    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
   @@ -280,8 +275,6 @@ os.unlink(model_library_format_tar_path)
    # 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.
    
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to