Skip to content

Commit f7165a1

Browse files
authored
[microTVM] Fix tvmc tutorial (#14076)
This PR applies appropriate changes to make sure the CI fails if micro_tvmc.sh tutorial fails. This issue was captured in #14074. This PR also makes changes to avoid this breakage in bash script tutorials in future. In addition, this PR fixes the bug in running TVMC tutorial which happened due to renaming zephyr_board to board.
1 parent 74bcca2 commit f7165a1

File tree

4 files changed

+46
-36
lines changed

4 files changed

+46
-36
lines changed

docs/script_convert.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020

2121
BASH = "# bash"
2222
BASH_IGNORE = "# bash-ignore"
23-
BASH_MULTILINE_COMMENT_START = ": '"
24-
BASH_MULTILINE_COMMENT_END = "'"
23+
BASH_MULTILINE_COMMENT = "# bash-comment"
2524

2625

2726
def bash_to_python(src_path: pathlib.Path, dest_path: pathlib.Path):
@@ -33,6 +32,8 @@ def bash_to_python(src_path: pathlib.Path, dest_path: pathlib.Path):
3332
bash_detected = False
3433
bash_ignore_detected = False
3534
new_line_required = False
35+
bash_multiline_comment_detected = False
36+
3637
while line:
3738
line = line.strip("\n").strip("\r")
3839
if bash_detected:
@@ -42,7 +43,7 @@ def bash_to_python(src_path: pathlib.Path, dest_path: pathlib.Path):
4243
dest_f.write("\n")
4344
python_code = "# .. code-block:: bash\n#\n"
4445
for bash_line in bash_block:
45-
python_code += f"#\t {bash_line}\n"
46+
python_code += f"# \t {bash_line}\n"
4647
python_code += "#"
4748
dest_f.write(python_code)
4849

@@ -55,29 +56,34 @@ def bash_to_python(src_path: pathlib.Path, dest_path: pathlib.Path):
5556
elif bash_ignore_detected:
5657
if line == BASH_IGNORE:
5758
bash_ignore_detected = False
58-
new_line_required = True
5959
else:
6060
new_line_required = False
6161
pass
62+
elif bash_multiline_comment_detected:
63+
if line == BASH_MULTILINE_COMMENT:
64+
bash_multiline_comment_detected = False
65+
else:
66+
if line != "#":
67+
assert len(line) > 2, "Detected empty line."
68+
dest_f.write(line[2:])
69+
new_line_required = True
6270
else:
6371
if line == BASH:
6472
bash_detected = True
6573
elif line == BASH_IGNORE:
6674
bash_ignore_detected = True
67-
elif line in [BASH_MULTILINE_COMMENT_START, BASH_MULTILINE_COMMENT_END]:
68-
if new_line_required:
69-
dest_f.write("\n")
70-
dest_f.write('"""')
71-
new_line_required = True
75+
elif line == BASH_MULTILINE_COMMENT:
76+
bash_multiline_comment_detected = True
7277
else:
7378
if new_line_required:
7479
dest_f.write("\n")
7580
dest_f.write(f"{line}")
7681
new_line_required = True
7782

7883
line = src_f.readline()
79-
if new_line_required:
80-
dest_f.write("\n")
84+
if new_line_required:
85+
dest_f.write("\n")
86+
new_line_required = False
8187

8288

8389
def main():

gallery/how_to/work_with_microtvm/micro_tvmc.sh

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/bin/bash
12
# Licensed to the Apache Software Foundation (ASF) under one
23
# or more contributor license agreements. See the NOTICE file
34
# distributed with this work for additional information
@@ -15,18 +16,24 @@
1516
# specific language governing permissions and limitations
1617
# under the License.
1718

18-
: '
19-
.. _tutorial-micro-cli-tool:
20-
21-
1. microTVM CLI Tool
22-
====================
23-
**Author**: `Mehrdad Hessar <https://github.com/mehrdadh>`_
19+
# bash-ignore
20+
set -euxo pipefail
21+
# bash-ignore
2422

25-
This tutorial explains how to compile a tiny model for a micro device,
26-
build a program on Zephyr platform to execute this model, flash the program
27-
and run the model all using `tvmc micro` command.
28-
You need to install python and Zephyr dependencies before processing with this tutorial.
29-
'
23+
# bash-comment
24+
# """
25+
# .. _tutorial-micro-cli-tool:
26+
#
27+
# 1. microTVM CLI Tool
28+
# ====================
29+
# **Author**: `Mehrdad Hessar <https://github.com/mehrdadh>`_
30+
#
31+
# This tutorial explains how to compile a tiny model for a micro device,
32+
# build a program on Zephyr platform to execute this model, flash the program
33+
# and run the model all using `tvmc micro` command.
34+
# You need to install python and Zephyr dependencies before processing with this tutorial.
35+
# """
36+
# bash-comment
3037

3138
######################################################################
3239
#
@@ -126,7 +133,7 @@ tvmc micro create \
126133
project \
127134
model.tar \
128135
zephyr \
129-
--project-option project_type=host_driven zephyr_board=qemu_x86
136+
--project-option project_type=host_driven board=qemu_x86
130137
# bash
131138
# This will generate a ``Host-Driven`` Zephyr project for ``qemu_x86`` Zephyr board. In Host-Driven template project,
132139
# the Graph Executor will run on host and perform the model execution on Zephyr device by issuing commands to the

tests/python/ci/test_script_converter.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,7 @@
2828
from .test_utils import REPO_ROOT
2929

3030
sys.path.insert(0, str(REPO_ROOT / "docs"))
31-
from script_convert import (
32-
bash_to_python,
33-
BASH,
34-
BASH_IGNORE,
35-
BASH_MULTILINE_COMMENT_START,
36-
BASH_MULTILINE_COMMENT_END,
37-
)
31+
from script_convert import bash_to_python, BASH, BASH_IGNORE, BASH_MULTILINE_COMMENT
3832

3933
# pylint: enable=wrong-import-position,wrong-import-order
4034

@@ -57,7 +51,7 @@ def test_bash_cmd():
5751
with open(dest_path, "r") as dest_f:
5852
generated_cmd = dest_f.read()
5953

60-
expected_cmd = "# .. code-block:: bash\n" "#\n" "#\t tvmc\n" "#\n"
54+
expected_cmd = "# .. code-block:: bash\n" "#\n" "# \t tvmc\n" "#\n"
6155

6256
assert generated_cmd == expected_cmd
6357

@@ -126,7 +120,7 @@ def test_text_and_bash_command():
126120
with open(dest_path, "r") as dest_f:
127121
generated_cmd = dest_f.read()
128122

129-
expected_cmd = "# start\n" "# .. code-block:: bash\n" "#\n" "#\t tvmc\n" "#\n" "# end\n"
123+
expected_cmd = "# start\n" "# .. code-block:: bash\n" "#\n" "# \t tvmc\n" "#\n" "# end\n"
130124

131125
assert generated_cmd == expected_cmd
132126

@@ -158,18 +152,19 @@ def test_multiline_comment():
158152
dest_path = temp / "dest.py"
159153

160154
with open(src_path, "w") as src_f:
161-
src_f.write(BASH_MULTILINE_COMMENT_START)
155+
src_f.write(BASH_MULTILINE_COMMENT)
162156
src_f.write("\n")
163-
src_f.write("comment\n")
164-
src_f.write(BASH_MULTILINE_COMMENT_END)
157+
src_f.write('# """\n')
158+
src_f.write("# comment\n")
159+
src_f.write(BASH_MULTILINE_COMMENT)
165160
src_f.write("\n")
166161

167162
bash_to_python(src_path, dest_path)
168163

169164
with open(dest_path, "r") as dest_f:
170165
generated_cmd = dest_f.read()
171166

172-
expected_cmd = '"""\n' "comment\n" '"""\n'
167+
expected_cmd = '"""\ncomment\n'
173168

174169
assert generated_cmd == expected_cmd
175170

tests/scripts/setup-pytest-env.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,6 @@ function run_pytest() {
9292
if [ "$exit_code" -ne "0" ] && [ "$exit_code" -ne "5" ]; then
9393
pytest_errors+=("${suite_name}: $@")
9494
fi
95+
# To avoid overwriting.
96+
set -e
9597
}

0 commit comments

Comments
 (0)