Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[microTVM] Remove Arduino aot code #8869

Merged
merged 4 commits into from
Sep 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/microtvm/arduino/example_project/src/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
#include "model.h"

#include "Arduino.h"
#include "standalone_crt/include/dlpack/dlpack.h"
#include "standalone_crt/include/tvm/runtime/crt/stack_allocator.h"

// AOT memory array
static uint8_t g_aot_memory[WORKSPACE_SIZE];
extern tvm_model_t tvmgen_default_network;
tvm_workspace_t app_workspace;

// Blink code for debugging purposes
Expand Down
2 changes: 2 additions & 0 deletions apps/microtvm/arduino/host_driven/src/model_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* under the License.
*/

#include "standalone_crt/include/dlpack/dlpack.h"
#include "standalone_crt/include/tvm/runtime/crt/error_codes.h"
#include "stdarg.h"

// Blink code for debugging purposes
Expand Down
8 changes: 5 additions & 3 deletions apps/microtvm/arduino/template_project/microtvm_api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ def build(self, options):
compile_cmd.append("--verbose")

# Specify project to compile
subprocess.run(compile_cmd)
subprocess.run(compile_cmd, check=True)

BOARD_LIST_HEADERS = ("Port", "Type", "Board Name", "FQBN", "Core")

Expand Down Expand Up @@ -402,7 +402,9 @@ def _parse_boards_tabular_str(self, tabular_str):

def _auto_detect_port(self, options):
list_cmd = [options["arduino_cli_cmd"], "board", "list"]
list_cmd_output = subprocess.run(list_cmd, stdout=subprocess.PIPE).stdout.decode("utf-8")
list_cmd_output = subprocess.run(
list_cmd, check=True, stdout=subprocess.PIPE
).stdout.decode("utf-8")

desired_fqbn = self._get_fqbn(options)
for line in self._parse_boards_tabular_str(list_cmd_output):
Expand Down Expand Up @@ -439,7 +441,7 @@ def flash(self, options):
if options.get("verbose"):
upload_cmd.append("--verbose")

subprocess.run(upload_cmd)
subprocess.run(upload_cmd, check=True)

def open_transport(self, options):
# Zephyr example doesn't throw an error in this case
Expand Down
40 changes: 40 additions & 0 deletions tests/micro/arduino/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import pytest
import tvm.target.target
from tvm import micro, relay

# The models that should pass this configuration. Maps a short, identifying platform string to
# (model, zephyr_board).
Expand Down Expand Up @@ -121,3 +122,42 @@ def make_workspace_dir(test_name, platform):
t = tvm.contrib.utils.tempdir(board_workspace)
# time.sleep(200)
return t


def make_kws_project(platform, arduino_cli_cmd, tvm_debug, workspace_dir):
this_dir = pathlib.Path(__file__).parent
model, arduino_board = PLATFORMS[platform]
build_config = {"debug": tvm_debug}

with open(this_dir.parent / "testdata" / "kws" / "yes_no.tflite", "rb") as f:
tflite_model_buf = f.read()

# TFLite.Model.Model has changed to TFLite.Model from 1.14 to 2.1
try:
import tflite.Model

tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0)
except AttributeError:
import tflite

tflite_model = tflite.Model.GetRootAsModel(tflite_model_buf, 0)

mod, params = relay.frontend.from_tflite(tflite_model)
target = tvm.target.target.micro(
model, options=["--link-params=1", "--unpacked-api=1", "--executor=aot"]
)

with tvm.transform.PassContext(opt_level=3, config={"tir.disable_vectorize": True}):
mod = relay.build(mod, target, params=params)

return tvm.micro.generate_project(
str(TEMPLATE_PROJECT_DIR),
mod,
workspace_dir / "project",
{
"arduino_board": arduino_board,
"arduino_cli_cmd": arduino_cli_cmd,
"project_type": "example_project",
"verbose": bool(build_config.get("debug")),
},
)
52 changes: 52 additions & 0 deletions tests/micro/arduino/test_arduino_error_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# 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.

import pathlib
import re
import sys

import pytest

import conftest
from tvm.micro.project_api.server import ServerError


# A new project and workspace dir is created for EVERY test
guberti marked this conversation as resolved.
Show resolved Hide resolved
@pytest.fixture
def workspace_dir(request, platform):
return conftest.make_workspace_dir("arduino_error_detection", platform)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given these fixtures are only used as part of the tests in this folder, and conftest is meant for auto-registration or pytest configuration rather than to hold helpers and utils, could these be either moved fully into conftest as fixtures or the underlying function moved into a more suitably named file?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The scope fixture of workspace_dir varies from test to test (for example, test_arduino_workflowhas it at module scope), and I suspect tests with more complexworkspace_dirsetups will be added in the future. For that reason, I'd prefer to defineworkspace_dirindividually in each test, but use the helper functionmake_workspace_dir` to avoid code reuse. I've also added a comment to each of these functions to clarify it.

That said, if you feel strongly I'm also open to other solutions, like defining multiple kinds of workspace dirs in conftest (e.g. a module_scope_workspace_dir, per_function_workspace_dir, etc.).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Argh, I feel like I could've been clearer here, my apologies. My suggestion is not to have general functions in conftest.py but rather in a different file such as a workspace.py to include from the test. This means only pytest-specific things end up in conftest.py, I have no issue with fixtures local to the test files which then use those functions.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that makes more sense. I like the idea of moving non-pytest code into a workspace.py, but I think that might be OOS for this PR. Definitely worth addressing in a follow up though!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, it'd be good to have the move isolated in another PR 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i agree with @Mousius sentiment--let's place test_utils in a file called test_utils or similar. can be a follow-up PR.



@pytest.fixture
def project(platform, arduino_cli_cmd, tvm_debug, workspace_dir):
return conftest.make_kws_project(platform, arduino_cli_cmd, tvm_debug, workspace_dir)


def test_blank_project_compiles(workspace_dir, project):
project.build()


# Add a bug (an extra curly brace) and make sure the project doesn't compile
def test_bugged_project_compile_fails(workspace_dir, project):
with open(workspace_dir / "project" / "project.ino", "a") as main_file:
main_file.write("}\n")
with pytest.raises(ServerError):
project.build()


if __name__ == "__main__":
sys.exit(pytest.main([__file__] + sys.argv[1:]))
4 changes: 2 additions & 2 deletions tests/micro/arduino/test_arduino_rpc_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
import conftest


# We'll make a new workspace for each test
@pytest.fixture(scope="function")
# # A new project and workspace dir is created for EVERY test
guberti marked this conversation as resolved.
Show resolved Hide resolved
@pytest.fixture
def workspace_dir(platform):
return conftest.make_workspace_dir("arduino_rpc_server", platform)

Expand Down
46 changes: 3 additions & 43 deletions tests/micro/arduino/test_arduino_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import sys

import pytest
import tvm
from tvm import micro, relay

import conftest

Expand All @@ -38,7 +36,8 @@
"""


# Since these tests are sequential, we'll use the same project for all tests
# Since these tests are sequential, we'll use the same project/workspace
# directory for all tests in this file
@pytest.fixture(scope="module")
def workspace_dir(request, platform):
return conftest.make_workspace_dir("arduino_workflow", platform)
Expand All @@ -49,49 +48,10 @@ def project_dir(workspace_dir):
return workspace_dir / "project"


def _generate_project(arduino_board, arduino_cli_cmd, workspace_dir, mod, build_config):
return tvm.micro.generate_project(
str(conftest.TEMPLATE_PROJECT_DIR),
mod,
workspace_dir / "project",
{
"arduino_board": arduino_board,
"arduino_cli_cmd": arduino_cli_cmd,
"project_type": "example_project",
"verbose": bool(build_config.get("debug")),
},
)


# We MUST pass workspace_dir, not project_dir, or the workspace will be dereferenced too soon
@pytest.fixture(scope="module")
def project(platform, arduino_cli_cmd, tvm_debug, workspace_dir):
this_dir = pathlib.Path(__file__).parent
model, arduino_board = conftest.PLATFORMS[platform]
build_config = {"debug": tvm_debug}

with open(this_dir.parent / "testdata" / "kws" / "yes_no.tflite", "rb") as f:
tflite_model_buf = f.read()

# TFLite.Model.Model has changed to TFLite.Model from 1.14 to 2.1
try:
import tflite.Model

tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0)
except AttributeError:
import tflite

tflite_model = tflite.Model.GetRootAsModel(tflite_model_buf, 0)

mod, params = relay.frontend.from_tflite(tflite_model)
target = tvm.target.target.micro(
model, options=["--link-params=1", "--unpacked-api=1", "--executor=aot"]
)

with tvm.transform.PassContext(opt_level=3, config={"tir.disable_vectorize": True}):
mod = relay.build(mod, target, params=params)

return _generate_project(arduino_board, arduino_cli_cmd, workspace_dir, mod, build_config)
return conftest.make_kws_project(platform, arduino_cli_cmd, tvm_debug, workspace_dir)


def _get_directory_elements(directory):
Expand Down