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 2022/11/08 21:30:27 UTC

[tvm] branch main updated: [microTVM][CRT] Add memory size as project option (#13313)

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

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


The following commit(s) were added to refs/heads/main by this push:
     new 16bb1a6c2e [microTVM][CRT] Add memory size as project option (#13313)
16bb1a6c2e is described below

commit 16bb1a6c2ee7a8f68f15ab8710588c75880e0dd5
Author: Mehrdad Hessar <mh...@octoml.ai>
AuthorDate: Tue Nov 8 15:30:14 2022 -0600

    [microTVM][CRT] Add memory size as project option (#13313)
    
    * Add memory size as project option
    
    * cleanup
    
    * address comments
    
    * address comments
---
 cmake/modules/StandaloneCrt.cmake                  |  2 +-
 .../crt/host/{Makefile => Makefile.template}       |  3 +-
 src/runtime/crt/host/main.cc                       |  2 +-
 src/runtime/crt/host/microtvm_api_server.py        | 44 ++++++++++++++++++++--
 tests/lint/check_file_type.py                      |  2 +
 5 files changed, 47 insertions(+), 6 deletions(-)

diff --git a/cmake/modules/StandaloneCrt.cmake b/cmake/modules/StandaloneCrt.cmake
index 5703058d3c..8c25cf48df 100644
--- a/cmake/modules/StandaloneCrt.cmake
+++ b/cmake/modules/StandaloneCrt.cmake
@@ -62,7 +62,7 @@ else()
          "src/runtime/crt/graph_executor_module *.c -> src/runtime/crt/graph_executor_module"
          "src/runtime/crt/host *.cc -> template/host"
          "src/runtime/crt/host *.py -> template/host"
-         "src/runtime/crt/host Makefile -> template/host"
+         "src/runtime/crt/host Makefile.template -> template/host"
          "src/runtime/crt/memory *.c -> src/runtime/crt/memory"
          "src/runtime/crt/microtvm_rpc_common *.cc -> src/runtime/crt/microtvm_rpc_common"
          "src/runtime/crt/microtvm_rpc_server *.cc -> src/runtime/crt/microtvm_rpc_server"
diff --git a/src/runtime/crt/host/Makefile b/src/runtime/crt/host/Makefile.template
similarity index 96%
rename from src/runtime/crt/host/Makefile
rename to src/runtime/crt/host/Makefile.template
index ea2966045b..a8e725ade2 100644
--- a/src/runtime/crt/host/Makefile
+++ b/src/runtime/crt/host/Makefile.template
@@ -16,8 +16,9 @@
 # under the License.
 
 INCLUDES ?= -isystem crt/include -Icrt_config
+MEMORY_SIZE_BYTES := <MEMORY_SIZE_BYTES>
 CFLAGS ?= -Werror -Wall
-CXXFLAGS ?= -Werror -Wall -std=c++11 -DTVM_HOST_USE_GRAPH_EXECUTOR_MODULE
+CXXFLAGS ?= -Werror -Wall -std=c++11 -DTVM_HOST_USE_GRAPH_EXECUTOR_MODULE -DMEMORY_SIZE_BYTES=$(MEMORY_SIZE_BYTES)
 LDFLAGS ?= -Werror -Wall
 
 # Codegen produces spurious lines like: int32_t arg2_code = ((int32_t*)arg_type_ids)[(2)];
diff --git a/src/runtime/crt/host/main.cc b/src/runtime/crt/host/main.cc
index d8fa95fe23..e9f6813f9b 100644
--- a/src/runtime/crt/host/main.cc
+++ b/src/runtime/crt/host/main.cc
@@ -112,7 +112,7 @@ tvm_crt_error_t TVMPlatformGenerateRandom(uint8_t* buffer, size_t num_bytes) {
 }
 }
 
-uint8_t memory[2048 * 1024];
+uint8_t memory[MEMORY_SIZE_BYTES];
 
 static char** g_argv = NULL;
 
diff --git a/src/runtime/crt/host/microtvm_api_server.py b/src/runtime/crt/host/microtvm_api_server.py
index 925dc4ea75..d8b35660e4 100644
--- a/src/runtime/crt/host/microtvm_api_server.py
+++ b/src/runtime/crt/host/microtvm_api_server.py
@@ -24,6 +24,8 @@ import shutil
 import subprocess
 import tarfile
 import time
+import re
+
 from tvm.micro.project_api import server
 
 
@@ -35,6 +37,10 @@ MODEL_LIBRARY_FORMAT_RELPATH = "model.tar"
 
 IS_TEMPLATE = not os.path.exists(os.path.join(PROJECT_DIR, MODEL_LIBRARY_FORMAT_RELPATH))
 
+MEMORY_SIZE_BYTES = 2 * 1024 * 1024
+
+MAKEFILE_FILENAME = "Makefile"
+
 
 class Handler(server.ProjectAPIHandler):
 
@@ -57,7 +63,14 @@ class Handler(server.ProjectAPIHandler):
                     optional=["build"],
                     type="bool",
                     help="Run make with verbose output",
-                )
+                ),
+                server.ProjectOption(
+                    "memory_size_bytes",
+                    optional=["generate_project"],
+                    type="int",
+                    default=MEMORY_SIZE_BYTES,
+                    help="Sets the value of MEMORY_SIZE_BYTES.",
+                ),
             ],
         )
 
@@ -67,6 +80,27 @@ class Handler(server.ProjectAPIHandler):
     # The build target given to make
     BUILD_TARGET = "build/main"
 
+    def _populate_makefile(
+        self,
+        makefile_template_path: pathlib.Path,
+        makefile_path: pathlib.Path,
+        memory_size: int,
+    ):
+        """Generate Makefile from template."""
+        flags = {
+            "MEMORY_SIZE_BYTES": str(memory_size),
+        }
+
+        regex = re.compile(r"([A-Z_]+) := (<[A-Z_]+>)")
+        with open(makefile_path, "w") as makefile_f:
+            with open(makefile_template_path, "r") as makefile_template_f:
+                for line in makefile_template_f:
+                    m = regex.match(line)
+                    if m:
+                        var, token = m.groups()
+                        line = line.replace(token, flags[var])
+                    makefile_f.write(line)
+
     def generate_project(self, model_library_format_path, standalone_crt_dir, project_dir, options):
         # Make project directory.
         project_dir.mkdir(parents=True)
@@ -97,8 +131,12 @@ class Handler(server.ProjectAPIHandler):
             else:
                 shutil.copy2(src_path, dst_path)
 
-        # Populate Makefile.
-        shutil.copy2(pathlib.Path(__file__).parent / "Makefile", project_dir / "Makefile")
+        # Populate Makefile
+        self._populate_makefile(
+            pathlib.Path(__file__).parent / f"{MAKEFILE_FILENAME}.template",
+            project_dir / MAKEFILE_FILENAME,
+            options.get("memory_size_bytes", MEMORY_SIZE_BYTES),
+        )
 
         # Populate crt-config.h
         crt_config_dir = project_dir / "crt_config"
diff --git a/tests/lint/check_file_type.py b/tests/lint/check_file_type.py
index 527c797547..2b8b61c413 100644
--- a/tests/lint/check_file_type.py
+++ b/tests/lint/check_file_type.py
@@ -151,6 +151,8 @@ ALLOW_SPECIFIC_FILE = {
     "apps/microtvm/zephyr/template_project/app-overlay/nucleo_l4r5zi.overlay",
     # microTVM Arduino runtime
     "apps/microtvm/arduino/template_project/Makefile.template",
+    # microTVM CRT
+    "src/runtime/crt/host/Makefile.template",
     # microTVM Virtual Machines
     "apps/microtvm/poetry.lock",
     "apps/microtvm/reference-vm/Vagrantfile",