forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce --interface-api={c,packed} parameter (apache#8280)
* Introduce --interface-api={c,packed} parameter This introduces structures generated to provide a documented and stable user friendly interface to a TVM generated model, as can be seen in the AOT demo application: ``` struct tvmgen_default_inputs inputs = { .input_1 = input_data, }; struct tvmgen_default_outputs outputs = { .output = output_data, }; int ret_val = tvmgen_default_run(&inputs, &outputs, NULL, NULL); ``` To facilitate this, some other changes are included: * Removed dependency on `aot_executor.{c,h}` in tests, pending the discussion in the interface RFC as to whether we keep them. * Moved creation of test DLTensor's into the AOT test utils, in future this can be replaced by loading via the Python API or otherwise * Introduce `parametrize_aot_options` which can be used to test permutations of AOT which work together - for now this filters C interface and packed operators * Updated demo application to generate the header for demonstration purposes, we should consider porting the demo application to Model Library Format and using the toolchain in the Zephyr App via CMake instead? This patch builds upon the improvements @giuseros made to AOT testing and name mangling from apache#8014 * Tweak metadata variable description and MLF target loop * Remove direct usage of `relay::Var` in meta_data.h This looks like the only place that could be causing the Windows CI failures, so trying removing the additional header in meta_data.h * Linting fix * Post-rebase files fixing These tests were somehow transmuted in transit, I've updated them to the most recent variant of the test helpers. * Strip back interface API to just inputs and outputs This removes any speculative structures from the generated code and cleans up some of the documentation. * Add header guards and tweak documentation
- Loading branch information
Showing
13 changed files
with
637 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.