Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4k
Introduce --interface-api={c,packed} parameter#8280
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
7348419a019367babb2e2f8e6e43c6997034f5fa2a1481471File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -32,6 +32,7 @@ | ||
| #include "input_data.h" | ||
| #include "output_data.h" | ||
| #include "tvmgen_default.h" | ||
| #include "zephyr_uart.h" | ||
| #ifdef CONFIG_ARCH_POSIX | ||
| @@ -194,18 +195,18 @@ void main(void) { | ||
| } | ||
| TVMLogf("Zephyr AOT Runtime\n"); | ||
| void* inputs[1] = { | ||
| input_data, | ||
| struct tvmgen_default_inputs inputs = { | ||
| .input_1 = input_data, | ||
| }; | ||
| void* outputs[1] = { | ||
| output_data, | ||
| struct tvmgen_default_outputs outputs = { | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TvmGenDefaultOutputs | ||
| .output = output_data, | ||
| }; | ||
| StackMemoryManager_Init(&app_workspace, g_aot_memory, WORKSPACE_SIZE); | ||
| double elapsed_time = 0; | ||
| TVMPlatformTimerStart(); | ||
| int ret_val = tvm_runtime_run(&tvmgen_default_network, inputs, outputs); | ||
| int ret_val = tvmgen_default_run(&inputs, &outputs); | ||
| TVMPlatformTimerStop(&elapsed_time); | ||
| if (ret_val != 0) { | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -230,8 +230,10 @@ constexpr const char* tvm_module_main = "__tvm_main__"; | ||
| constexpr const char* tvm_param_prefix = "__tvm_param__"; | ||
| /*! \brief A PackedFunc that looks up linked parameters by storage_id. */ | ||
| constexpr const char* tvm_lookup_linked_param = "_lookup_linked_param"; | ||
| /*! \brief The main AOT executor function */ | ||
| /*! \brief The main AOT executor function generated from TIR */ | ||
| constexpr const char* tvm_run_func_suffix = "run_model"; | ||
Mousius marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| /*! \brief Model entrypoint generated as an interface to the AOT function outside of TIR */ | ||
| constexpr const char* tvm_entrypoint_suffix = "run"; | ||
| } // namespace symbol | ||
| // implementations of inline functions. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| # 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""" | ||
| import os | ||
| 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 | ||
| """ | ||
| 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( | ||
| "#include <stdint.h>\n" | ||
| f"#ifndef {mangled_name.upper()}_H_\n" | ||
| f"#define {mangled_name.upper()}_H_\n" | ||
| ) | ||
| _emit_brief(header_file, module_name, "Input tensor pointers") | ||
| header_file.write(f"struct {mangled_name}_inputs {{\n") | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you add struct-level \brief documentation here? MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added an example of the emitted header to the PR, the Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since memory and devices was removed, i think what you have is fine now. it's pretty hard to confuse "inputs" and "outputs," since they are core concepts of the model (perhaps parameters may be confusing--are they inputs?). when we add "memory" and "devices," i'd like us to be explicit about which memory is intended to be there, where the user might expect to configure this in TVM, and which memory is not going to be there. likewise with devices--what should be expected, where should it get configured, and what should a user fill in here? but ok to merge now. | ||
| for input_name in inputs: | ||
| header_file.write(f" void* {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(f"#endif // {mangled_name.upper()}_H_\n") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -192,25 +192,26 @@ class CSourceCrtMetadataModuleNode : public runtime::ModuleNode { | ||
| << "}\n"; | ||
| } | ||
| void GenerateEntrypointForUnpackedAPI(const std::string& run_func) { | ||
| void GenerateEntrypointForUnpackedAPI(const std::string& entrypoint_name, | ||
| const std::string& run_func) { | ||
| code_ << "TVM_DLL int32_t " << run_func << "("; | ||
| int total_args = (metadata_->num_inputs + metadata_->num_outputs); | ||
| for (int i = 0; i < total_args; ++i) { | ||
| code_ << "arg" << i; | ||
| unsigned int total_args = (metadata_->inputs.size() + metadata_->num_outputs); | ||
| for (unsigned int i = 0; i < total_args; ++i) { | ||
| code_ << "void* arg" << i; | ||
| if (i + 1 != total_args) { | ||
| code_ << ","; | ||
| } | ||
| } | ||
| code_ << ");\n"; | ||
| code_ << "static int32_t " << ::tvm::runtime::symbol::tvm_module_main; | ||
| code_ << "int32_t " << entrypoint_name; | ||
| code_ << "(void* args, void* type_code, int num_args, void* out_value, void* " | ||
| "out_type_code, void* resource_handle) {\n"; | ||
| code_ << "return " << run_func << "("; | ||
| for (int i = 0; i < metadata_->num_inputs; ++i) { | ||
| for (unsigned int i = 0; i < metadata_->inputs.size(); ++i) { | ||
| code_ << "((DLTensor*)(((TVMValue*)args)[" << i << "].v_handle))[0].data,"; | ||
| } | ||
| for (int i = 0; i < metadata_->num_outputs; ++i) { | ||
| int j = metadata_->num_inputs + i; | ||
| int j = metadata_->inputs.size() + i; | ||
| code_ << "((DLTensor*)(((TVMValue*)args)[" << j << "].v_handle))[0].data"; | ||
| if (i + 1 != metadata_->num_outputs) { | ||
| code_ << ","; | ||
| @@ -220,37 +221,83 @@ class CSourceCrtMetadataModuleNode : public runtime::ModuleNode { | ||
| code_ << "}\n"; | ||
| } | ||
| void GenerateEntrypointForPackedAPI(const std::string& run_func) { | ||
| void GenerateEntrypointForPackedAPI(const std::string& entrypoint_name, | ||
| const std::string& run_func) { | ||
| code_ << "TVM_DLL int32_t " << run_func; | ||
| code_ << "(void* args, void* type_code, int num_args, void* out_value, void* " | ||
| "out_type_code, void* resource_handle);\n"; | ||
| code_ << "static int32_t " << ::tvm::runtime::symbol::tvm_module_main; | ||
| code_ << "int32_t " << entrypoint_name; | ||
| code_ << "(void* args, void* type_code, int num_args, void* out_value, void* " | ||
| "out_type_code, void* resource_handle) {\n"; | ||
| code_ << "return " << run_func; | ||
| code_ << "(args, type_code, num_args, out_value, out_type_code, resource_handle);\n"; | ||
| code_ << "}\n"; | ||
| } | ||
| void GenerateCInterfaceEntrypoint(const std::string& entrypoint_name, const std::string& run_func, | ||
| const std::string& mod_name) { | ||
| code_ << "#include <" << mod_name << ".h>\n"; | ||
Mousius marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| code_ << "TVM_DLL int32_t " << run_func << "("; | ||
| unsigned int total_args = (metadata_->inputs.size() + metadata_->num_outputs); | ||
| for (unsigned int i = 0; i < total_args; ++i) { | ||
| code_ << "void* arg" << i; | ||
| if (i + 1 != total_args) { | ||
| code_ << ","; | ||
| } | ||
| } | ||
| code_ << ");\n"; | ||
| code_ << "int32_t " << entrypoint_name << "("; | ||
| code_ << "struct " << runtime::get_name_mangled(mod_name, "inputs") << "* inputs," | ||
| << "struct " << runtime::get_name_mangled(mod_name, "outputs") << "* outputs" | ||
| << ") {"; | ||
| code_ << "return " << run_func << "("; | ||
| for (const auto& input : metadata_->inputs) { | ||
| code_ << "inputs->" << input << ","; | ||
| } | ||
| if (metadata_->num_outputs == 1) { | ||
| code_ << "outputs->output"; | ||
| } else { | ||
| for (int i = 0; i < metadata_->num_outputs; ++i) { | ||
| code_ << "outputs->output" << i; | ||
| if (i + 1 != metadata_->num_outputs) { | ||
| code_ << ","; | ||
| } | ||
| } | ||
| } | ||
| code_ << ");\n"; | ||
| code_ << "}\n"; | ||
| } | ||
| void GenerateAOTDescriptor() { | ||
| const std::string run_func = ::tvm::runtime::symbol::tvm_run_func_suffix; | ||
| const std::string run_func_mangled = runtime::get_name_mangled(metadata_->mod_name, run_func); | ||
| const std::string run_func_suffix = ::tvm::runtime::symbol::tvm_run_func_suffix; | ||
| const std::string tvm_entrypoint_suffix = ::tvm::runtime::symbol::tvm_entrypoint_suffix; | ||
| const std::string run_func_mangled = | ||
| runtime::get_name_mangled(metadata_->mod_name, run_func_suffix); | ||
| const std::string entrypoint_mangled = | ||
| runtime::get_name_mangled(metadata_->mod_name, tvm_entrypoint_suffix); | ||
| const std::string network_mangled = runtime::get_name_mangled(metadata_->mod_name, "network"); | ||
| code_ << "#include \"tvm/runtime/crt/internal/aot_executor/aot_executor.h\"\n"; | ||
| auto unpacked_api = target_->GetAttr<Bool>("unpacked-api").value_or(Bool(false)); | ||
| auto interface_api = target_->GetAttr<String>("interface-api").value_or(String("packed")); | ||
| code_ << "#include \"tvm/runtime/c_runtime_api.h\"\n"; | ||
| code_ << "#ifdef __cplusplus\n"; | ||
| code_ << "extern \"C\"\n"; | ||
| code_ << "extern \"C\" {\n"; | ||
| code_ << "#endif\n"; | ||
| if (target_->GetAttr<Bool>("unpacked-api").value_or(Bool(false))) { | ||
| GenerateEntrypointForUnpackedAPI(run_func_mangled); | ||
| if (unpacked_api) { | ||
| if (interface_api == "c") { | ||
| GenerateCInterfaceEntrypoint(entrypoint_mangled, run_func_mangled, metadata_->mod_name); | ||
| } else { | ||
| GenerateEntrypointForUnpackedAPI(entrypoint_mangled, run_func_mangled); | ||
| } | ||
| } else { | ||
| GenerateEntrypointForPackedAPI(run_func_mangled); | ||
| ICHECK_EQ(interface_api, "packed") << "Packed interface required for packed operators"; | ||
Mousius marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| GenerateEntrypointForPackedAPI(entrypoint_mangled, run_func_mangled); | ||
| } | ||
| code_ << "const tvm_model_t " << network_mangled << " = {\n" | ||
| << " .run_func = &" << ::tvm::runtime::symbol::tvm_module_main << ",\n" | ||
| << " .num_input_tensors = " << metadata_->num_inputs << ",\n" | ||
| << " .num_output_tensors = " << metadata_->num_outputs << ", \n" | ||
| << "};\n"; | ||
| code_ << "#ifdef __cplusplus\n"; | ||
| code_ << "}\n"; | ||
| code_ << "#endif\n"; | ||
| } | ||
| void CreateSource() { | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TvmGenDefaultInputs