Skip to content

Commit 0659c0c

Browse files
mehrdadhyongwww
authored andcommitted
[microTVM][Zephyr] Remove unnecessary use of generate_c_interface_header (apache#14091)
In standalone mode we can use the generated tvmgen_default.h in model/host/include instead of regenerating it using generate_c_interface_header. This PR also moves test_qemu_make_fail to test_zephyr since this test is not a standalone mode test.
1 parent 295b7b6 commit 0659c0c

File tree

5 files changed

+53
-75
lines changed

5 files changed

+53
-75
lines changed

apps/microtvm/zephyr/template_project/CMakeLists.txt.template

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ endforeach(crt_lib_name ${CRT_LIBS})
6767
zephyr_library_named(tvm_model)
6868
file(GLOB_RECURSE tvm_model_srcs model/codegen/host/src/*.c model/codegen/host/lib/*.o)
6969
target_sources(tvm_model PRIVATE ${tvm_model_srcs})
70-
target_include_directories(tvm_model PRIVATE ${CMAKE_SOURCE_DIR}/include crt_config crt/include ${cmsis_includes})
70+
target_include_directories(tvm_model PRIVATE ${CMAKE_SOURCE_DIR}/include crt_config crt/include model/codegen/host/include ${cmsis_includes})
7171
target_compile_options(tvm_model PRIVATE -Wno-unused-variable) # TVM-generated code tends to include lots of these.
7272
target_link_libraries(app PRIVATE tvm_model)
7373

@@ -82,4 +82,4 @@ endif()
8282

8383
file(GLOB_RECURSE app_srcs src/**.c src/**.cc)
8484
target_sources(app PRIVATE ${app_srcs} ${cmsis_lib_srcs})
85-
target_include_directories(app PRIVATE crt_config include ${CMAKE_SOURCE_DIR}/include crt/include ${cmsis_includes})
85+
target_include_directories(app PRIVATE crt_config include ${CMAKE_SOURCE_DIR}/include crt/include model/codegen/host/include ${cmsis_includes})

gallery/how_to/work_with_microtvm/micro_mlperftiny.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
from tvm.relay.backend import Executor, Runtime
6969
from tvm.contrib.download import download_testdata
7070
from tvm.micro import export_model_library_format
71-
from tvm.micro.model_library_format import generate_c_interface_header
7271
import tvm.micro.testing
7372
from tvm.micro.testing.utils import (
7473
create_header_file,
@@ -212,14 +211,6 @@
212211
extra_tar_file = extra_tar_dir / "extra.tar"
213212

214213
with tarfile.open(extra_tar_file, "w:gz") as tf:
215-
with tempfile.TemporaryDirectory() as tar_temp_dir:
216-
model_files_path = os.path.join(tar_temp_dir, "include")
217-
os.mkdir(model_files_path)
218-
header_path = generate_c_interface_header(
219-
module.libmod_name, [input_name], [output_name], [], {}, [], 0, model_files_path, {}, {}
220-
)
221-
tf.add(header_path, arcname=os.path.relpath(header_path, tar_temp_dir))
222-
223214
create_header_file(
224215
"output_data",
225216
np.zeros(

tests/micro/zephyr/test_zephyr.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import tvm.relay as relay
3131
from tvm.relay.backend import Executor, Runtime
3232
from tvm.relay.testing import byoc
33+
from tvm.micro.project_api import server
3334
from tvm.contrib import utils
3435
from tvm.micro.testing.utils import check_tune_log
3536

@@ -644,5 +645,55 @@ def test_debugging_enabled(workspace_dir):
644645
project.build()
645646

646647

648+
@tvm.testing.requires_micro
649+
@pytest.mark.skip_boards(["mps2_an521", "mps3_an547"])
650+
def test_qemu_make_fail(workspace_dir, board, microtvm_debug, serial_number):
651+
"""Testing QEMU make fail."""
652+
if not utils.ZEPHYR_BOARDS[board]["is_qemu"]:
653+
pytest.skip(msg="Only for QEMU targets.")
654+
655+
build_config = {"debug": microtvm_debug}
656+
shape = (10,)
657+
dtype = "float32"
658+
659+
# Construct Relay program.
660+
x = relay.var("x", relay.TensorType(shape=shape, dtype=dtype))
661+
xx = relay.multiply(x, x)
662+
z = relay.add(xx, relay.const(np.ones(shape=shape, dtype=dtype)))
663+
func = relay.Function([x], z)
664+
ir_mod = tvm.IRModule.from_expr(func)
665+
666+
target = tvm.micro.testing.get_target("zephyr", board)
667+
executor = Executor("aot")
668+
runtime = Runtime("crt", {"system-lib": True})
669+
with tvm.transform.PassContext(opt_level=3, config={"tir.disable_vectorize": True}):
670+
lowered = relay.build(ir_mod, target, executor=executor, runtime=runtime)
671+
672+
project_options = {
673+
"project_type": "host_driven",
674+
"verbose": bool(build_config.get("debug")),
675+
"board": board,
676+
}
677+
678+
sample = np.zeros(shape=shape, dtype=dtype)
679+
project = tvm.micro.generate_project(
680+
str(utils.TEMPLATE_PROJECT_DIR),
681+
lowered,
682+
workspace_dir / "project",
683+
project_options,
684+
)
685+
project.build()
686+
687+
file_path = workspace_dir / "project" / "build" / "build.ninja"
688+
assert file_path.is_file(), f"[{file_path}] does not exist."
689+
690+
# Remove a file to create make failure.
691+
os.remove(file_path)
692+
project.flash()
693+
with pytest.raises(server.JSONRPCError) as excinfo:
694+
project.transport().open()
695+
assert "QEMU setup failed" in str(excinfo.value)
696+
697+
647698
if __name__ == "__main__":
648699
tvm.testing.main()

tests/micro/zephyr/test_zephyr_aot_exec_standalone.py

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import tvm
2424
import tvm.testing
2525
import tvm.micro.testing
26-
from tvm.micro.project_api import server
2726
import tvm.relay as relay
2827
from tvm.relay.backend import Executor, Runtime
2928
from tvm.contrib.download import download_testdata
@@ -88,53 +87,5 @@ def test_tflite(workspace_dir, board, microtvm_debug, serial_number):
8887
assert result == 6
8988

9089

91-
@tvm.testing.requires_micro
92-
@pytest.mark.skip_boards(["mps2_an521", "mps3_an547"])
93-
def test_qemu_make_fail(workspace_dir, board, microtvm_debug, serial_number):
94-
"""Testing QEMU make fail."""
95-
if not utils.ZEPHYR_BOARDS[board]["is_qemu"]:
96-
pytest.skip(msg="Only for QEMU targets.")
97-
98-
build_config = {"debug": microtvm_debug}
99-
shape = (10,)
100-
dtype = "float32"
101-
102-
# Construct Relay program.
103-
x = relay.var("x", relay.TensorType(shape=shape, dtype=dtype))
104-
xx = relay.multiply(x, x)
105-
z = relay.add(xx, relay.const(np.ones(shape=shape, dtype=dtype)))
106-
func = relay.Function([x], z)
107-
ir_mod = tvm.IRModule.from_expr(func)
108-
109-
target = tvm.micro.testing.get_target("zephyr", board)
110-
executor = Executor("aot")
111-
runtime = Runtime("crt")
112-
with tvm.transform.PassContext(opt_level=3, config={"tir.disable_vectorize": True}):
113-
lowered = relay.build(ir_mod, target, executor=executor, runtime=runtime)
114-
115-
sample = np.zeros(shape=shape, dtype=dtype)
116-
project, project_dir = utils.generate_project(
117-
workspace_dir,
118-
board,
119-
lowered,
120-
build_config,
121-
sample,
122-
shape,
123-
dtype,
124-
False,
125-
serial_number,
126-
)
127-
128-
file_path = pathlib.Path(project_dir) / "build" / "build.ninja"
129-
assert file_path.is_file(), f"[{file_path}] does not exist."
130-
131-
# Remove a file to create make failure.
132-
os.remove(file_path)
133-
project.flash()
134-
with pytest.raises(server.JSONRPCError) as excinfo:
135-
project.transport().open()
136-
assert "QEMU setup failed" in str(excinfo.value)
137-
138-
13990
if __name__ == "__main__":
14091
tvm.testing.main()

tests/micro/zephyr/utils.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
import tvm.micro
3333
from tvm.micro import export_model_library_format
34-
from tvm.micro.model_library_format import generate_c_interface_header
3534
from tvm.micro.testing.utils import create_header_file
3635
from tvm.micro.testing.utils import (
3736
mlf_extract_workspace_size_bytes,
@@ -160,20 +159,6 @@ def generate_project(
160159
tf.add(
161160
model_files_path, arcname=os.path.relpath(model_files_path, tar_temp_dir)
162161
)
163-
header_path = generate_c_interface_header(
164-
lowered.libmod_name,
165-
["input_1"],
166-
["Identity"],
167-
[],
168-
{},
169-
[],
170-
0,
171-
model_files_path,
172-
{},
173-
{},
174-
)
175-
tf.add(header_path, arcname=os.path.relpath(header_path, tar_temp_dir))
176-
177162
create_header_file("input_data", sample, "include/tvm", tf)
178163
create_header_file(
179164
"output_data", np.zeros(shape=output_shape, dtype=output_type), "include/tvm", tf

0 commit comments

Comments
 (0)