From 71e4a0f9bedb8cec714e113535d2d943776708e2 Mon Sep 17 00:00:00 2001 From: Christopher Sidebottom Date: Fri, 8 Oct 2021 07:40:07 +0100 Subject: [PATCH] Migrate C Interface API Generation to C++ (#9106) Using the new name transformations added in #9088, the C interface API is now generated in C++ rather than in Python. This is intended to be a no-op for the actual users of this change and thus I've undone some of my overzealous sanitizing to match that expectation. Follow up PRs will clean up any remaining name transformation inconsistencies. Fixes #8792 --- python/tvm/micro/interface_api.py | 101 ----------- python/tvm/micro/model_library_format.py | 17 +- python/tvm/relay/backend/name_transforms.py | 12 ++ src/relay/backend/name_transforms.cc | 16 +- src/relay/backend/name_transforms.h | 11 ++ src/target/source/interface_c.cc | 137 +++++++++++++++ tests/cpp/name_transforms_test.cc | 12 +- tests/cpp/target/source/interface_c_test.cc | 184 ++++++++++++++++++++ tests/micro/zephyr/test_zephyr_aot.py | 2 +- tests/micro/zephyr/test_zephyr_armv7m.py | 2 +- tests/python/relay/aot/test_crt_aot.py | 4 +- tests/python/relay/test_name_transforms.py | 16 +- 12 files changed, 397 insertions(+), 117 deletions(-) delete mode 100644 python/tvm/micro/interface_api.py create mode 100644 src/target/source/interface_c.cc create mode 100644 tests/cpp/target/source/interface_c_test.cc diff --git a/python/tvm/micro/interface_api.py b/python/tvm/micro/interface_api.py deleted file mode 100644 index 5a4841f39f7cb..0000000000000 --- a/python/tvm/micro/interface_api.py +++ /dev/null @@ -1,101 +0,0 @@ -# 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. - -"""Defines functions for generating a C interface header""" - -# TODO: Currently the Interface API header is generated in Python but the source it references -# is generated in C++. These should be consolidated to generate both header and source in C++ -# and avoid re-implementing logic, such as name sanitising, in the two different languages. -# See https://github.com/apache/tvm/issues/8792 . - -import os -import re - -from tvm.relay.backend.utils import mangle_module_name - - -def _emit_brief(header_file, module_name, description): - header_file.write("/*!\n") - header_file.write(f' * \\brief {description} for TVM module "{module_name}" \n') - header_file.write(" */\n") - - -def generate_c_interface_header(module_name, inputs, outputs, output_path): - """Generates a C interface header for a given modules inputs and outputs - - Parameters - ---------- - module_name : str - Name of the module to be used in defining structs and naming the header - inputs : list[str] - List of module input names to be placed in generated structs - outputs : list[str] - List of module output names to be placed in generated structs - output_path : str - Path to the output folder to generate the header into - - Returns - ------- - str : - Name of the generated file. - """ - mangled_name = mangle_module_name(module_name) - metadata_header = os.path.join(output_path, f"{mangled_name}.h") - with open(metadata_header, "w") as header_file: - header_file.write( - f"#ifndef {mangled_name.upper()}_H_\n" - f"#define {mangled_name.upper()}_H_\n\n" - "#include \n\n" - "#ifdef __cplusplus\n" - 'extern "C" {\n' - "#endif\n\n" - ) - - _emit_brief(header_file, module_name, "Input tensor pointers") - header_file.write(f"struct {mangled_name}_inputs {{\n") - sanitized_names = [] - for input_name in inputs: - sanitized_input_name = re.sub(r"\W", "_", input_name) - if sanitized_input_name in sanitized_names: - raise ValueError(f"Sanitized input tensor name clash: {sanitized_input_name}") - sanitized_names.append(sanitized_input_name) - header_file.write(f" void* {sanitized_input_name};\n") - header_file.write("};\n\n") - - _emit_brief(header_file, module_name, "Output tensor pointers") - header_file.write(f"struct {mangled_name}_outputs {{\n") - for output_name in outputs: - header_file.write(f" void* {output_name};\n") - header_file.write("};\n\n") - - header_file.write( - "/*!\n" - f' * \\brief entrypoint function for TVM module "{module_name}"\n' - " * \\param inputs Input tensors for the module \n" - " * \\param outputs Output tensors for the module \n" - " */\n" - f"int32_t {mangled_name}_run(\n" - f" struct {mangled_name}_inputs* inputs,\n" - f" struct {mangled_name}_outputs* outputs\n" - ");\n" - ) - - header_file.write( - "\n#ifdef __cplusplus\n}\n#endif\n\n" f"#endif // {mangled_name.upper()}_H_\n" - ) - - return metadata_header diff --git a/python/tvm/micro/model_library_format.py b/python/tvm/micro/model_library_format.py index ed44a3336a521..f031acec0d76a 100644 --- a/python/tvm/micro/model_library_format.py +++ b/python/tvm/micro/model_library_format.py @@ -25,13 +25,14 @@ import tarfile import typing +import tvm from tvm.ir.type import TupleType from .._ffi import get_global_func -from .interface_api import generate_c_interface_header from ..contrib import utils from ..driver import build_module from ..runtime import ndarray as _nd from ..relay.backend import executor_factory +from ..relay.backend.name_transforms import to_c_variable_style, prefix_generated_name from ..relay import param_dict from ..tir import expr @@ -43,6 +44,20 @@ class UnsupportedInModelLibraryFormatError(Exception): """Raised when export_model_library_format does not support the given Module tree.""" +def generate_c_interface_header(module_name, inputs, outputs, include_path): + """Generate C Interface header to be included in MLF""" + mangled_name = to_c_variable_style(prefix_generated_name(module_name)) + metadata_header = os.path.join(include_path, f"{mangled_name}.h") + + interface_c_create = tvm._ffi.get_global_func("runtime.InterfaceCCreate") + interface_c_module = interface_c_create(module_name, inputs, outputs) + + with open(metadata_header, "w") as header_file: + header_file.write(interface_c_module.get_source()) + + return metadata_header + + def _populate_codegen_dir(mod, codegen_dir: str, module_name: str = None): """Populate the codegen sub-directory as part of a Model Library Format export. diff --git a/python/tvm/relay/backend/name_transforms.py b/python/tvm/relay/backend/name_transforms.py index 04a7a425bdf13..19208725a8b95 100644 --- a/python/tvm/relay/backend/name_transforms.py +++ b/python/tvm/relay/backend/name_transforms.py @@ -48,6 +48,18 @@ def to_c_variable_style(original_name: str): return _backend.ToCVariableStyle(original_name) +def to_c_constant_style(original_name: str): + """Transform a name to the C constant style assuming it is + appropriately constructed using the prefixing functions + + Parameters + ---------- + original_name : str + Original name to transform + """ + return _backend.ToCConstantStyle(original_name) + + def _preprocess_names(names: Union[List[str], str]): """Preprocesses name strings into format for C++ functions diff --git a/src/relay/backend/name_transforms.cc b/src/relay/backend/name_transforms.cc index a6d10a795cf7a..a2f24216ec245 100644 --- a/src/relay/backend/name_transforms.cc +++ b/src/relay/backend/name_transforms.cc @@ -62,6 +62,14 @@ std::string ToCVariableStyle(const std::string& original_name) { return variable_name; } +std::string ToCConstantStyle(const std::string& original_name) { + ICHECK_EQ(original_name.find("TVM"), 0) << "Constant not TVM prefixed"; + std::string constant_name = ToCVariableStyle(original_name); + + std::transform(constant_name.begin(), constant_name.end(), constant_name.begin(), ::toupper); + return constant_name; +} + std::string CombineNames(const Array& names) { std::stringstream combine_stream; ICHECK(!names.empty()) << "Name segments empty"; @@ -79,22 +87,16 @@ std::string CombineNames(const Array& names) { std::string SanitizeName(const std::string& name) { ICHECK(!name.empty()) << "Name is empty"; - auto multipleSeparators = [](char before, char after) { - return before == '_' && before == after; - }; auto isNotAlnum = [](char c) { return !std::isalnum(c); }; std::string sanitized_input = name; std::replace_if(sanitized_input.begin(), sanitized_input.end(), isNotAlnum, '_'); - sanitized_input.erase( - std::unique(sanitized_input.begin(), sanitized_input.end(), multipleSeparators), - sanitized_input.end()); - return sanitized_input; } TVM_REGISTER_GLOBAL("relay.backend.ToCFunctionStyle").set_body_typed(ToCFunctionStyle); TVM_REGISTER_GLOBAL("relay.backend.ToCVariableStyle").set_body_typed(ToCVariableStyle); +TVM_REGISTER_GLOBAL("relay.backend.ToCConstantStyle").set_body_typed(ToCConstantStyle); TVM_REGISTER_GLOBAL("relay.backend.PrefixName").set_body_typed(PrefixName); TVM_REGISTER_GLOBAL("relay.backend.PrefixGeneratedName").set_body_typed(PrefixGeneratedName); TVM_REGISTER_GLOBAL("relay.backend.SanitizeName").set_body_typed(SanitizeName); diff --git a/src/relay/backend/name_transforms.h b/src/relay/backend/name_transforms.h index 4c1fd3ae56fc5..a30ba6b10825d 100644 --- a/src/relay/backend/name_transforms.h +++ b/src/relay/backend/name_transforms.h @@ -35,6 +35,9 @@ * ToCVariableStyle(PrefixGeneratedName(CombineNames({"model", "Devices"}))) * // tvmgen_model_devices * + * ToCConstantStyle(PrefixGeneratedName(CombineNames({"model", "Devices"}))) + * // TVMGEN_MODEL_DEVICES + * */ #include @@ -68,6 +71,14 @@ std::string ToCFunctionStyle(const std::string& original_name); */ std::string ToCVariableStyle(const std::string& original_name); +/*! + * \brief Transform a name to the C constant style assuming it is + * appropriately constructed using the prefixing functions + * \param name Original name + * \return Transformed function in the C constant style + */ +std::string ToCConstantStyle(const std::string& original_name); + /*! * \brief Combine names together for use as a generated name * \param names Vector of strings to combine diff --git a/src/target/source/interface_c.cc b/src/target/source/interface_c.cc new file mode 100644 index 0000000000000..4089ccc7523ce --- /dev/null +++ b/src/target/source/interface_c.cc @@ -0,0 +1,137 @@ +/* + * 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. + */ + +/*! + * \file interface_c.cc + * \brief Generates a C interface header for a given modules inputs and outputs + */ + +#include +#include +#include +#include +#include + +#include + +#include "../../relay/backend/name_transforms.h" + +namespace tvm { +namespace codegen { + +using runtime::PackedFunc; +using namespace tvm::relay::backend; + +class InterfaceCNode : public runtime::ModuleNode { + public: + InterfaceCNode(std::string module_name, Array inputs, Array outputs) + : module_name_(module_name), inputs_(inputs), outputs_(outputs) {} + const char* type_key() const { return "h"; } + + std::string GetSource(const std::string& format) final { + std::stringstream code; + + EmitUpperHeaderGuard(code); + EmitBrief(code, "Input tensor pointers"); + EmitStruct(code, "inputs", inputs_); + EmitBrief(code, "Output tensor pointers"); + EmitStruct(code, "outputs", outputs_); + EmitRunFunction(code); + EmitLowerHeaderGuard(code); + + return code.str(); + } + + PackedFunc GetFunction(const std::string& name, const ObjectPtr& sptr_to_self) final { + return PackedFunc(nullptr); + } + + private: + void EmitUpperHeaderGuard(std::stringstream& code_stream) { + std::string header_guard_name = ToCConstantStyle(PrefixGeneratedName({module_name_, "H"})); + code_stream << "#ifndef " << header_guard_name << "_\n" + << "#define " << header_guard_name << "_\n" + << "#include \n\n" + << "#ifdef __cplusplus\n" + << "extern \"C\" {\n" + << "#endif\n\n"; + } + + void EmitLowerHeaderGuard(std::stringstream& code_stream) { + std::string header_guard_name = ToCConstantStyle(PrefixGeneratedName({module_name_, "H"})); + code_stream << "\n#ifdef __cplusplus\n" + << "}\n" + << "#endif\n\n" + << "#endif // " << header_guard_name << "_\n"; + } + + void EmitBrief(std::stringstream& code_stream, const std::string& description) { + code_stream << "/*!\n" + << " * \\brief " << description << " for TVM module \"" << module_name_ << "\" \n" + << " */\n"; + } + + void EmitStruct(std::stringstream& code_stream, const std::string& suffix, + Array properties) { + std::string struct_name = ToCVariableStyle(PrefixGeneratedName({module_name_, suffix})); + code_stream << "struct " << struct_name << " {\n"; + + std::vector sanitized_properties; + for (const String& property : properties) { + std::string sanitized_property = SanitizeName(property); + ICHECK(std::find(sanitized_properties.begin(), sanitized_properties.end(), + sanitized_property) == sanitized_properties.end()) + << "Sanitized input tensor name clash" << sanitized_property; + code_stream << " void* " << sanitized_property << ";\n"; + sanitized_properties.push_back(sanitized_property); + } + code_stream << "};\n\n"; + } + + void EmitRunFunction(std::stringstream& code_stream) { + std::string run_function = ToCVariableStyle(PrefixGeneratedName({module_name_, "run"})); + std::string inputs_struct = ToCVariableStyle(PrefixGeneratedName({module_name_, "inputs"})); + std::string outputs_struct = ToCVariableStyle(PrefixGeneratedName({module_name_, "outputs"})); + + code_stream << "/*!\n" + << " * \\brief entrypoint function for TVM module \"" << module_name_ << "\"\n" + << " * \\param inputs Input tensors for the module \n" + << " * \\param outputs Output tensors for the module \n" + << " */\n" + << "int32_t " << run_function << "(\n" + << " struct " << inputs_struct << "* inputs,\n" + << " struct " << outputs_struct << "* outputs\n" + << ");\n"; + } + + std::string module_name_; + Array inputs_; + Array outputs_; +}; + +runtime::Module InterfaceCCreate(std::string module_name, Array inputs, + Array outputs) { + auto n = make_object(module_name, inputs, outputs); + return runtime::Module(n); +} + +TVM_REGISTER_GLOBAL("runtime.InterfaceCCreate").set_body_typed(InterfaceCCreate); + +} // namespace codegen +} // namespace tvm diff --git a/tests/cpp/name_transforms_test.cc b/tests/cpp/name_transforms_test.cc index 9fc52e09dea80..09a5bbfb583aa 100644 --- a/tests/cpp/name_transforms_test.cc +++ b/tests/cpp/name_transforms_test.cc @@ -42,6 +42,14 @@ TEST(NameTransforms, ToCVariableStyle) { EXPECT_THROW(ToCVariableStyle(""), InternalError); } +TEST(NameTransforms, ToCConstantStyle) { + ASSERT_EQ(ToCConstantStyle("TVM_Woof"), "TVM_WOOF"); + ASSERT_EQ(ToCConstantStyle("TVM_woof"), "TVM_WOOF"); + ASSERT_EQ(ToCConstantStyle("TVM_woof_Woof"), "TVM_WOOF_WOOF"); + EXPECT_THROW(ToCConstantStyle("Cake_Bakery"), InternalError); // Incorrect prefix + EXPECT_THROW(ToCConstantStyle(""), InternalError); +} + TEST(NameTransforms, PrefixName) { ASSERT_EQ(PrefixName({"Woof"}), "TVM_Woof"); ASSERT_EQ(PrefixName({"woof"}), "TVM_woof"); @@ -71,10 +79,10 @@ TEST(NameTransforms, CombineNames) { } TEST(NameTransforms, SanitizeName) { - ASSERT_EQ(SanitizeName("+_+ "), "_"); + ASSERT_EQ(SanitizeName("+_+ "), "____"); ASSERT_EQ(SanitizeName("input+"), "input_"); ASSERT_EQ(SanitizeName("input-"), "input_"); - ASSERT_EQ(SanitizeName("input++"), "input_"); + ASSERT_EQ(SanitizeName("input++"), "input__"); ASSERT_EQ(SanitizeName("woof:1"), "woof_1"); EXPECT_THROW(SanitizeName(""), InternalError); } diff --git a/tests/cpp/target/source/interface_c_test.cc b/tests/cpp/target/source/interface_c_test.cc new file mode 100644 index 0000000000000..c53af43e9f696 --- /dev/null +++ b/tests/cpp/target/source/interface_c_test.cc @@ -0,0 +1,184 @@ +/* + * 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. + */ + +#include +#include +#include +#include +#include + +using ::testing::HasSubstr; + +namespace tvm { +namespace codegen { + +runtime::Module InterfaceCCreate(std::string module_name, Array inputs, + Array outputs); + +namespace { + +TEST(InterfaceAPI, ContainsHeaderGuards) { + std::stringstream upper_header_guard; + std::stringstream lower_header_guard; + + upper_header_guard << "#ifndef TVMGEN_ULTIMATE_CAT_SPOTTER_H_\n" + << "#define TVMGEN_ULTIMATE_CAT_SPOTTER_H_\n" + << "#include \n\n" + << "#ifdef __cplusplus\n" + << "extern \"C\" {\n" + << "#endif\n\n"; + + lower_header_guard << "\n#ifdef __cplusplus\n" + << "}\n" + << "#endif\n\n" + << "#endif // TVMGEN_ULTIMATE_CAT_SPOTTER_H_\n"; + + runtime::Module test_module = InterfaceCCreate("ultimate_cat_spotter", {"input"}, {"output"}); + std::string header_source = test_module->GetSource(); + + ASSERT_THAT(header_source, HasSubstr(upper_header_guard.str())); + ASSERT_THAT(header_source, HasSubstr(lower_header_guard.str())); +} + +TEST(InterfaceAPI, ContainsRunFunction) { + std::stringstream run_function; + + run_function << "/*!\n" + << " * \\brief entrypoint function for TVM module \"ultimate_cat_spotter\"\n" + << " * \\param inputs Input tensors for the module \n" + << " * \\param outputs Output tensors for the module \n" + << " */\n" + << "int32_t tvmgen_ultimate_cat_spotter_run(\n" + << " struct tvmgen_ultimate_cat_spotter_inputs* inputs,\n" + << " struct tvmgen_ultimate_cat_spotter_outputs* outputs\n" + << ");\n"; + + runtime::Module test_module = InterfaceCCreate("ultimate_cat_spotter", {"input"}, {"output"}); + std::string header_source = test_module->GetSource(); + + ASSERT_THAT(header_source, HasSubstr(run_function.str())); +} + +TEST(InterfaceAPI, ContainsInputStructSingle) { + std::stringstream input_struct; + + input_struct << "/*!\n" + << " * \\brief Input tensor pointers for TVM module \"ultimate_cat_spotter\" \n" + << " */\n" + << "struct tvmgen_ultimate_cat_spotter_inputs {\n" + << " void* input;\n" + << "};\n\n"; + + runtime::Module test_module = InterfaceCCreate("ultimate_cat_spotter", {"input"}, {"output"}); + std::string header_source = test_module->GetSource(); + + ASSERT_THAT(header_source, HasSubstr(input_struct.str())); +} + +TEST(InterfaceAPI, ContainsInputStructMany) { + std::stringstream input_struct; + + input_struct << "struct tvmgen_ultimate_cat_spotter_inputs {\n" + << " void* input1;\n" + << " void* input2;\n" + << "};\n\n"; + + runtime::Module test_module = + InterfaceCCreate("ultimate_cat_spotter", {"input1", "input2"}, {"output"}); + std::string header_source = test_module->GetSource(); + + ASSERT_THAT(header_source, HasSubstr(input_struct.str())); +} + +TEST(InterfaceAPI, ContainsInputStructSanitised) { + std::stringstream input_struct; + + input_struct << "struct tvmgen_ultimate_cat_spotter_inputs {\n" + << " void* input_1;\n" + << " void* input_2;\n" + << "};\n\n"; + + runtime::Module test_module = + InterfaceCCreate("ultimate_cat_spotter", {"input+1", "input+2"}, {"output"}); + std::string header_source = test_module->GetSource(); + + ASSERT_THAT(header_source, HasSubstr(input_struct.str())); +} + +TEST(InterfaceAPI, ContainsInputStructClash) { + runtime::Module test_module = + InterfaceCCreate("ultimate_cat_spotter", {"input+", "input-"}, {"output"}); + ASSERT_THROW(test_module->GetSource(), InternalError); +} + +TEST(InterfaceAPI, ContainsOutputStructSingle) { + std::stringstream output_struct; + + output_struct << "/*!\n" + << " * \\brief Output tensor pointers for TVM module \"ultimate_cat_spotter\" \n" + << " */\n" + << "struct tvmgen_ultimate_cat_spotter_outputs {\n" + << " void* output;\n" + << "};\n\n"; + + runtime::Module test_module = InterfaceCCreate("ultimate_cat_spotter", {"input"}, {"output"}); + std::string header_source = test_module->GetSource(); + + ASSERT_THAT(header_source, HasSubstr(output_struct.str())); +} + +TEST(InterfaceAPI, ContainsOutputStructMany) { + std::stringstream output_struct; + + output_struct << "struct tvmgen_ultimate_cat_spotter_outputs {\n" + << " void* output1;\n" + << " void* output2;\n" + << "};\n\n"; + + runtime::Module test_module = + InterfaceCCreate("ultimate_cat_spotter", {"input"}, {"output1", "output2"}); + std::string header_source = test_module->GetSource(); + + ASSERT_THAT(header_source, HasSubstr(output_struct.str())); +} + +TEST(InterfaceAPI, ContainsOutputStructSanitised) { + std::stringstream output_struct; + + output_struct << "struct tvmgen_ultimate_cat_spotter_outputs {\n" + << " void* output_1;\n" + << " void* output_2;\n" + << "};\n\n"; + + runtime::Module test_module = + InterfaceCCreate("ultimate_cat_spotter", {"input"}, {"output+1", "output-2"}); + std::string header_source = test_module->GetSource(); + + ASSERT_THAT(header_source, HasSubstr(output_struct.str())); +} + +TEST(InterfaceAPI, ContainsOutputStructClash) { + runtime::Module test_module = + InterfaceCCreate("ultimate_cat_spotter", {"input"}, {"output+", "output-"}); + ASSERT_THROW(test_module->GetSource(), InternalError); +} + +} // namespace +} // namespace codegen +} // namespace tvm diff --git a/tests/micro/zephyr/test_zephyr_aot.py b/tests/micro/zephyr/test_zephyr_aot.py index a8a7a99a34dd8..5bc665b748f65 100644 --- a/tests/micro/zephyr/test_zephyr_aot.py +++ b/tests/micro/zephyr/test_zephyr_aot.py @@ -32,7 +32,7 @@ import tvm.relay as relay from tvm.contrib.download import download_testdata -from tvm.micro.interface_api import generate_c_interface_header +from tvm.micro.model_library_format import generate_c_interface_header import test_utils diff --git a/tests/micro/zephyr/test_zephyr_armv7m.py b/tests/micro/zephyr/test_zephyr_armv7m.py index 350f7e2423041..972ffe2bda357 100644 --- a/tests/micro/zephyr/test_zephyr_armv7m.py +++ b/tests/micro/zephyr/test_zephyr_armv7m.py @@ -34,7 +34,7 @@ from tvm import relay from tvm.contrib.download import download_testdata -from tvm.micro.interface_api import generate_c_interface_header +from tvm.micro.model_library_format import generate_c_interface_header import conftest diff --git a/tests/python/relay/aot/test_crt_aot.py b/tests/python/relay/aot/test_crt_aot.py index d90c4217c4c3d..94ecaba280adc 100644 --- a/tests/python/relay/aot/test_crt_aot.py +++ b/tests/python/relay/aot/test_crt_aot.py @@ -22,7 +22,7 @@ import pytest import tvm -from tvm import relay +from tvm import relay, TVMError from tvm.ir.module import IRModule from tvm.relay import testing, transform from tvm.relay.testing import byoc @@ -613,7 +613,7 @@ def test_name_sanitiser_name_clash(): inputs = {"input::-1": x_data, "input::-2": y_data, "input:--2": t_data} output_list = generate_ref_data(func, inputs) - with pytest.raises(ValueError, match="Sanitized input tensor name clash"): + with pytest.raises(TVMError, match="Sanitized input tensor name clash"): compile_and_run( AOTTestModel(module=IRModule.from_expr(func), inputs=inputs, outputs=output_list), test_runner, diff --git a/tests/python/relay/test_name_transforms.py b/tests/python/relay/test_name_transforms.py index c4a7d6c4477c0..1c3435a6cc855 100644 --- a/tests/python/relay/test_name_transforms.py +++ b/tests/python/relay/test_name_transforms.py @@ -19,6 +19,7 @@ from tvm.relay.backend.name_transforms import ( to_c_function_style, to_c_variable_style, + to_c_constant_style, prefix_name, prefix_generated_name, sanitize_name, @@ -51,6 +52,17 @@ def test_to_c_variable_style(): to_c_variable_style("") +def test_to_c_constant_style(): + assert to_c_constant_style("TVM_Woof") == "TVM_WOOF" + assert to_c_constant_style("TVM_woof") == "TVM_WOOF" + assert to_c_constant_style("TVM_woof_Woof") == "TVM_WOOF_WOOF" + + with pytest.raises(TVMError, match="Constant not TVM prefixed"): + to_c_constant_style("Cake_Bakery") + with pytest.raises(TVMError): + to_c_constant_style("") + + def test_prefix_name(): assert prefix_name("Woof") == "TVM_Woof" assert prefix_name(["Woof"]) == "TVM_Woof" @@ -81,10 +93,10 @@ def test_prefix_generated_name(): def test_sanitize_name(): - assert sanitize_name("+_+ ") == "_" + assert sanitize_name("+_+ ") == "____" assert sanitize_name("input+") == "input_" assert sanitize_name("input-") == "input_" - assert sanitize_name("input++") == "input_" + assert sanitize_name("input++") == "input__" assert sanitize_name("woof:1") == "woof_1" with pytest.raises(TVMError, match="Name is empty"):