You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2022/07/26 17:58:47 UTC

[GitHub] [tvm] mehrdadh commented on a diff in pull request #12182: [microTVM][tutorial] AOT host-driven tutorial with TFLite model

mehrdadh commented on code in PR #12182:
URL: https://github.com/apache/tvm/pull/12182#discussion_r930240740


##########
gallery/how_to/work_with_microtvm/micro_aot.py:
##########
@@ -0,0 +1,162 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""
+.. _tutorial-micro-AoT:
+
+microTVM Host-Driven AoT
+===========================
+**Authors**:
+`Mehrdad Hessar <https://github.com/mehrdadh>`_,
+`Alan MacDonald <https://github.com/alanmacd>`_
+
+This tutorial is showcasing microTVM host-driven AoT compilation with
+a TFLite model. This tutorial can be executed on a X86 CPU using C runtime (CRT)
+or on Zephyr plarform on a microcontroller that supports Zephyr platform.

Review Comment:
   done



##########
gallery/how_to/work_with_microtvm/micro_aot.py:
##########
@@ -0,0 +1,162 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""
+.. _tutorial-micro-AoT:
+
+microTVM Host-Driven AoT
+===========================
+**Authors**:
+`Mehrdad Hessar <https://github.com/mehrdadh>`_,
+`Alan MacDonald <https://github.com/alanmacd>`_
+
+This tutorial is showcasing microTVM host-driven AoT compilation with
+a TFLite model. This tutorial can be executed on a X86 CPU using C runtime (CRT)
+or on Zephyr plarform on a microcontroller that supports Zephyr platform.
+"""
+
+import numpy as np
+import pathlib
+import json
+import os
+
+import tvm
+from tvm import relay
+from tvm.relay.backend import Executor, Runtime
+from tvm.contrib.download import download_testdata
+
+######################################################################
+# Import a TFLite model
+# ---------------------
+#
+# To begin with, download and import a TFLite model from TinyMLPerf models.

Review Comment:
   I added comment about the origin of the model and fixed MLPerf Tiny name.



##########
gallery/how_to/work_with_microtvm/micro_aot.py:
##########
@@ -0,0 +1,162 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""
+.. _tutorial-micro-AoT:
+
+microTVM Host-Driven AoT
+===========================
+**Authors**:
+`Mehrdad Hessar <https://github.com/mehrdadh>`_,
+`Alan MacDonald <https://github.com/alanmacd>`_
+
+This tutorial is showcasing microTVM host-driven AoT compilation with

Review Comment:
   added few comments, please take another look.



##########
gallery/how_to/work_with_microtvm/micro_aot.py:
##########
@@ -0,0 +1,168 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""
+.. _tutorial-micro-AoT:
+
+microTVM Host-Driven AoT
+===========================
+**Authors**:
+`Mehrdad Hessar <https://github.com/mehrdadh>`_,
+`Alan MacDonald <https://github.com/alanmacd>`_
+
+This tutorial is showcasing microTVM host-driven AoT compilation with
+a TFLite model. This tutorial can be executed on a X86 CPU using C runtime (CRT)

Review Comment:
   done.



##########
gallery/how_to/work_with_microtvm/micro_aot.py:
##########
@@ -0,0 +1,168 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""
+.. _tutorial-micro-AoT:
+
+microTVM Host-Driven AoT
+===========================
+**Authors**:
+`Mehrdad Hessar <https://github.com/mehrdadh>`_,
+`Alan MacDonald <https://github.com/alanmacd>`_
+
+This tutorial is showcasing microTVM host-driven AoT compilation with
+a TFLite model. This tutorial can be executed on a X86 CPU using C runtime (CRT)
+or on Zephyr plarform on a microcontroller that supports Zephyr platform.
+"""
+
+# sphinx_gallery_start_ignore
+from tvm import testing
+
+testing.utils.install_request_hook(depth=3)
+# sphinx_gallery_end_ignore
+
+import numpy as np
+import pathlib
+import json
+import os
+
+import tvm
+from tvm import relay
+from tvm.relay.backend import Executor, Runtime
+from tvm.contrib.download import download_testdata
+
+######################################################################
+# Import a TFLite model
+# ---------------------
+#
+# To begin with, download and import a TFLite model from TinyMLPerf models.
+#
+# **Note:** By default this tutorial runs on X86 CPU using CRT, if you would like to run on Zephyr platform
+# you need to export `TVM_MICRO_USE_HW` environment variable.
+#
+use_physical_hw = bool(os.getenv("TVM_MICRO_USE_HW"))
+MODEL_URL = "https://github.com/tlc-pack/web-data/raw/main/testdata/microTVM/model/keyword_spotting_quant.tflite"
+MODEL_PATH = download_testdata(MODEL_URL, "keyword_spotting_quant.tflite", module="model")
+SAMPLE_URL = "https://github.com/tlc-pack/web-data/raw/main/testdata/microTVM/data/keyword_spotting_int8_6.pyc.npy"
+SAMPLE_PATH = download_testdata(SAMPLE_URL, "keyword_spotting_int8_6.pyc.npy", module="data")
+
+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)
+
+input_shape = (1, 49, 10, 1)
+INPUT_NAME = "input_1"
+relay_mod, params = relay.frontend.from_tflite(
+    tflite_model, shape_dict={INPUT_NAME: input_shape}, dtype_dict={INPUT_NAME: "int8"}
+)
+
+######################################################################
+# Defining the target
+# -------------------
+#
+# Now we need to define the target, runtime and executor. In this tutorial, we focused on
+# using AOT host driven executor. We use the host micro target which is for running a model

Review Comment:
   added



##########
gallery/how_to/work_with_microtvm/micro_aot.py:
##########
@@ -0,0 +1,168 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""
+.. _tutorial-micro-AoT:
+
+microTVM Host-Driven AoT
+===========================
+**Authors**:
+`Mehrdad Hessar <https://github.com/mehrdadh>`_,
+`Alan MacDonald <https://github.com/alanmacd>`_
+
+This tutorial is showcasing microTVM host-driven AoT compilation with
+a TFLite model. This tutorial can be executed on a X86 CPU using C runtime (CRT)
+or on Zephyr plarform on a microcontroller that supports Zephyr platform.
+"""
+
+# sphinx_gallery_start_ignore
+from tvm import testing
+
+testing.utils.install_request_hook(depth=3)
+# sphinx_gallery_end_ignore
+
+import numpy as np
+import pathlib
+import json
+import os
+
+import tvm
+from tvm import relay
+from tvm.relay.backend import Executor, Runtime
+from tvm.contrib.download import download_testdata
+
+######################################################################
+# Import a TFLite model
+# ---------------------
+#
+# To begin with, download and import a TFLite model from TinyMLPerf models.
+#
+# **Note:** By default this tutorial runs on X86 CPU using CRT, if you would like to run on Zephyr platform
+# you need to export `TVM_MICRO_USE_HW` environment variable.
+#
+use_physical_hw = bool(os.getenv("TVM_MICRO_USE_HW"))

Review Comment:
   added that.



-- 
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: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org