Skip to content

Commit f3986a5

Browse files
alanmacdareusch
authored andcommitted
[microTVM] Add support for host-driven AoT Executor (apache#11044)
* Generate AOT Metadata when targeting C runtime and packed API. * Also copy metadata.h and metadata_base.h to standalone_crt. * add support for get_input_index as well as setting up get_input_info as unsupported * add support for tvm.aot_executor.create in C runtime * changes in-progress to unit tests * Include get_c_metadata in emitted function list * make CRT error codes generic for graph or AoT executor, fix AoT lib link order * add AoT executor creation and initializaion, as well as support for get_input_index() * add allocation of inputs, outputs, and pools; add get_input(), but shape encoded in metadata appears to be incorrect; * add support to test_aot_executor for get_input() * fix numpy array shape so that get_input() works properly * implement run(), get_output(), get_num_inputs(), and get_num_outputs(); test_aot_executor() is now passing; * fix up some issues from rebase with main * clean up logging and test_graph_executor() * lint clean-up * more lint clean-up * fix i386 build errors * first set of changes addressing PR feedback * more PR feedback: device pass-by-value, docstring entries, return variable name * add mangling of get_c_metadata() name to avoid function name collisions * only mangle get_c_metadata() when using C runtime * add static specifier to all kTvmgenMetadata variables to avoid namespace collisions * use TVM_IS_CPP_RUNTIME preprocessor define to deteremine whether or not to include metadata.h c++ code * add TVM_IS_CPP_RUNTIME define for cpptest * add TVM_IS_CPP_RUNTIME to apps/bundle_deploy * add TVM_IS_CPP_RUNTIME web/Makefile * update number of expected generated C files for AoT source files * break out metadata data structures into separate metadata_types.h header to avoid c/c++ issues and remove the need for the TVM_IS_CPP_RUNTIME define * remove TVM_IS_CPP_RUNTIME from web makefile * fix metadata.h include-order lint issue * correct error mask bits * address PR feedback * trigger build * trigger build * trigger build * trigger build * add alternate name for test_graph_executor() too see if it runs in CI * fix lint * revert alternate test code Co-authored-by: Andrew Reusch <areusch@gmail.com>
1 parent b12528f commit f3986a5

23 files changed

Lines changed: 905 additions & 118 deletions

File tree

cmake/modules/StandaloneCrt.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ if(USE_MICRO)
2828
"3rdparty/dlpack/include *.h -> include"
2929
"3rdparty/dmlc-core/include *.h -> include"
3030
"include/tvm/runtime c_*_api.h -> include/tvm/runtime"
31+
"include/tvm/runtime metadata_types.h -> include/tvm/runtime"
3132
"include/tvm/runtime/crt *.h -> include/tvm/runtime/crt"
3233
"src/runtime/crt Makefile -> ."
3334
"src/runtime/crt/include *.h -> include"
35+
"src/runtime/crt/aot_executor *.c -> src/runtime/crt/aot_executor"
36+
"src/runtime/crt/aot_executor_module *.c -> src/runtime/crt/aot_executor_module"
3437
"src/runtime/crt/common *.c -> src/runtime/crt/common"
3538
"src/runtime/crt/graph_executor *.c -> src/runtime/crt/graph_executor"
3639
"src/runtime/crt/graph_executor_module *.c -> src/runtime/crt/graph_executor_module"

include/tvm/runtime/c_runtime_api.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ TVM_DLL int TVMCbArgToReturn(TVMValue* value, int* code);
298298
* \param type_codes The type codes of the arguments
299299
* \param num_args Number of arguments.
300300
* \param ret The return value handle.
301-
* \param resource_handle The handle additional resouce handle from fron-end.
301+
* \param resource_handle The handle additional resouce handle from front-end.
302302
* \return 0 if success, -1 if failure happens, set error via TVMAPISetLastError.
303303
* \sa TVMCFuncSetReturn
304304
*/
@@ -307,7 +307,7 @@ typedef int (*TVMPackedCFunc)(TVMValue* args, int* type_codes, int num_args, TVM
307307

308308
/*!
309309
* \brief C callback to free the resource handle in C packed function.
310-
* \param resource_handle The handle additional resouce handle from fron-end.
310+
* \param resource_handle The handle additional resouce handle from front-end.
311311
*/
312312
typedef void (*TVMPackedCFuncFinalizer)(void* resource_handle);
313313

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
/*!
21+
* \file aot_executor.h
22+
* \brief AoT Executor
23+
*/
24+
#ifndef TVM_RUNTIME_CRT_AOT_EXECUTOR_H_
25+
#define TVM_RUNTIME_CRT_AOT_EXECUTOR_H_
26+
27+
#ifdef __cplusplus
28+
extern "C" {
29+
#endif
30+
31+
#include <dlpack/dlpack.h>
32+
#include <tvm/runtime/crt/internal/common/ndarray.h>
33+
#include <tvm/runtime/metadata_types.h>
34+
35+
typedef struct TVMMetadata TVMMetadata;
36+
37+
typedef struct TVMAotExecutor {
38+
/*! \brief The top-level metadata structure supplied by the generated code */
39+
const TVMMetadata* metadata;
40+
/*! \brief The code module that contains the compiled model */
41+
TVMModuleHandle module_handle;
42+
/*! \brief The device type */
43+
DLDevice device;
44+
/*! \brief List of allocated arguments, input(s), output(s), and pool(s)*/
45+
TVMNDArray* args;
46+
int64_t num_args;
47+
} TVMAotExecutor;
48+
49+
/*!
50+
* \brief Allocate a new AotExecutor with TVMPlatformMemoryAllocate and initialize it.
51+
*
52+
* \param module_handle TVM Module that exposes the functions to call.
53+
* \param device Runtime execution device, only supports device type kDLCPU, index 0.
54+
* \param executor Pointer which receives a pointer to the newly-created instance.
55+
* \param module_name TVM Module name prefix, typically "default".
56+
* \return 0 if successful.
57+
*/
58+
int TVMAotExecutor_Create(TVMModuleHandle module_handle, const DLDevice device,
59+
TVMAotExecutor** executor, const char* module_name);
60+
61+
/*!
62+
* \brief Release the AoT executor created by TVMAotExecutor_Create().
63+
*
64+
* \param executor Pointer to executor instance, created by TVMAotExecutor_Create().
65+
* \param device Runtime execution device, only supports device type kDLCPU, index 0.
66+
* \return 0 if successful.
67+
*/
68+
int TVMAotExecutor_Release(TVMAotExecutor* executor, const DLDevice device);
69+
70+
/*!
71+
* \brief Return the number of inputs.
72+
*
73+
* \param executor Pointer to executor instance, created by TVMAotExecutor_Create().
74+
* \return Number of inputs.
75+
*/
76+
int TVMAotExecutor_GetNumInputs(TVMAotExecutor* executor);
77+
78+
/*!
79+
* \brief Return the number of outputs.
80+
*
81+
* \param executor Pointer to executor instance, created by TVMAotExecutor_Create().
82+
* \return Number of outputs.
83+
*/
84+
int TVMAotExecutor_GetNumOutputs(TVMAotExecutor* executor);
85+
86+
/*!
87+
* \brief Return the input index of the specified input name
88+
*
89+
* \param executor Pointer to executor instance, created by TVMAotExecutor_Create().
90+
* \param name Input name for retrieving index.
91+
* \return Input index.
92+
*/
93+
int TVMAotExecutor_GetInputIndex(TVMAotExecutor* executor, const char* name);
94+
95+
/*!
96+
* \brief Run the generated program.
97+
*
98+
* \param executor Pointer to executor instance, created by TVMAotExecutor_Create().
99+
* \return 0 if successful.
100+
*/
101+
int TVMAotExecutor_Run(TVMAotExecutor* executor);
102+
103+
#ifdef __cplusplus
104+
} // extern "C"
105+
#endif
106+
107+
#endif // TVM_RUNTIME_CRT_AOT_EXECUTOR_H_
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
/*!
21+
* \file graph_executor.h
22+
* \brief Tiny AoT executor
23+
*/
24+
#ifndef TVM_RUNTIME_CRT_AOT_EXECUTOR_MODULE_H_
25+
#define TVM_RUNTIME_CRT_AOT_EXECUTOR_MODULE_H_
26+
27+
#ifdef __cplusplus
28+
extern "C" {
29+
#endif
30+
31+
#include <tvm/runtime/crt/error_codes.h>
32+
33+
/*!
34+
* \brief Register the "tvm.aot_executor.create" constructor PackedFunc.
35+
*/
36+
tvm_crt_error_t TVMAotExecutorModule_Register();
37+
38+
#ifdef __cplusplus
39+
} // extern "C"
40+
#endif
41+
42+
#endif // TVM_RUNTIME_CRT_AOT_EXECUTOR_MODULE_H_

include/tvm/runtime/crt/error_codes.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ typedef enum {
4242
kTvmErrorCategorySession = 4,
4343
kTvmErrorCategoryPlatform = 5,
4444
kTvmErrorCategoryGenerated = 6,
45-
kTvmErrorCategoryGraphExecutor = 7,
45+
kTvmErrorCategoryExecutor = 7,
4646
kTvmErrorCategoryFunctionCall = 8,
4747
kTvmErrorCategoryTimeEvaluator = 9,
4848
} tvm_crt_error_category_t;
@@ -84,10 +84,10 @@ typedef enum {
8484
// Common error codes returned from generated functions.
8585
kTvmErrorGeneratedInvalidStorageId = DEFINE_TVM_CRT_ERROR(kTvmErrorCategoryGenerated, 0),
8686

87-
// Graph executor
88-
kTvmErrorGraphModuleAlreadyCreated = DEFINE_TVM_CRT_ERROR(kTvmErrorCategoryGraphExecutor, 0),
89-
kTvmErrorGraphModuleBadContext = DEFINE_TVM_CRT_ERROR(kTvmErrorCategoryGraphExecutor, 1),
90-
kTvmErrorGraphModuleNoSuchInput = DEFINE_TVM_CRT_ERROR(kTvmErrorCategoryGraphExecutor, 2),
87+
// Graph or AoT executor
88+
kTvmErrorExecutorModuleAlreadyCreated = DEFINE_TVM_CRT_ERROR(kTvmErrorCategoryExecutor, 0),
89+
kTvmErrorExecutorModuleBadContext = DEFINE_TVM_CRT_ERROR(kTvmErrorCategoryExecutor, 1),
90+
kTvmErrorExecutorModuleNoSuchInput = DEFINE_TVM_CRT_ERROR(kTvmErrorCategoryExecutor, 2),
9191

9292
// Function Calls - common problems encountered calling functions.
9393
kTvmErrorFunctionCallNumArguments = DEFINE_TVM_CRT_ERROR(kTvmErrorCategoryFunctionCall, 0),
@@ -100,7 +100,7 @@ typedef enum {
100100

101101
// System errors are always negative integers; this mask indicates presence of a system error.
102102
// Cast tvm_crt_error_t to a signed integer to interpret the negative error code.
103-
kTvmErrorSystemErrorMask = (1 << (sizeof(int) * 4 - 1)),
103+
kTvmErrorSystemErrorMask = (1 << (sizeof(int) * 8 - 1)),
104104
} tvm_crt_error_t;
105105

106106
#ifdef __cplusplus

include/tvm/runtime/crt/graph_executor_module.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*!
21-
* \file graph_executor.h
21+
* \file graph_executor_module.h
2222
* \brief Tiny graph executor that can run graph containing only tvm PackedFunc.
2323
*/
2424
#ifndef TVM_RUNTIME_CRT_GRAPH_EXECUTOR_MODULE_H_

include/tvm/runtime/metadata.h

Lines changed: 6 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,19 @@
2424
#ifndef TVM_RUNTIME_METADATA_H_
2525
#define TVM_RUNTIME_METADATA_H_
2626

27-
#include <inttypes.h>
28-
#ifdef __cplusplus
29-
#include <memory>
30-
#include <string>
31-
#include <vector>
32-
#endif
3327
#include <tvm/runtime/c_runtime_api.h>
34-
#ifdef __cplusplus
3528
#include <tvm/runtime/metadata_base.h>
29+
#include <tvm/runtime/metadata_types.h>
30+
#include <tvm/runtime/object.h>
3631
#include <tvm/support/span.h>
37-
#endif
32+
33+
#include <memory>
34+
#include <string>
35+
#include <vector>
3836

3937
// Version number recorded in emitted artifacts for runtime checking.
4038
#define TVM_METADATA_VERSION 1
4139

42-
#ifdef __cplusplus
4340
namespace tvm {
4441
namespace runtime {
4542
namespace metadata {
@@ -52,59 +49,6 @@ static const constexpr int64_t kMetadataVersion = TVM_METADATA_VERSION;
5249
} // namespace runtime
5350
} // namespace tvm
5451

55-
extern "C" {
56-
#endif
57-
58-
/*!
59-
* \brief Top-level metadata structure. Holds all other metadata types.
60-
*/
61-
struct TVMMetadata {
62-
/*! \brief Version identifier for this metadata. */
63-
int64_t version;
64-
/*! \brief Inputs to the AOT run_model function.
65-
* The order of the elements is the same as in the arguments to run_model. That is to say,
66-
* this array specifies the first `num_inputs` arguments to run_model.
67-
*/
68-
const struct TVMTensorInfo* inputs;
69-
/*! \brief Number of elements in `inputs` array. */
70-
int64_t num_inputs;
71-
/*! \brief Outputs of the AOT run_model function.
72-
* The order of the elements is the same as in the arguments to run_model. That is to say,
73-
* this array specifies the last `num_outputs` arguments to run_model.
74-
*/
75-
const struct TVMTensorInfo* outputs;
76-
/*! \brief Number of elements in `outputs` array. */
77-
int64_t num_outputs;
78-
/*! \brief Memory Pools needed by the AOT main function.
79-
* The order of the elements is the same as in the arguments to run_model. That is to say,
80-
* this array specifies the last `num_pools` arguments to run_model.
81-
*/
82-
const struct TVMTensorInfo* pools;
83-
/*! \brief Number of elements in `pools` array. */
84-
int64_t num_pools;
85-
/*! \brief Name of the model, as passed to tvm.relay.build. */
86-
const char* mod_name;
87-
};
88-
89-
/*!
90-
* \brief Describes one tensor argument to `run_model`.
91-
* NOTE: while TIR allows for other types of arguments, such as scalars, the AOT run_model
92-
* function does not currently accept these. Therefore it's not possible to express those
93-
* in this metadata. A future patch may modify this.
94-
*/
95-
struct TVMTensorInfo {
96-
/*! \brief Name of the tensor, as specified in the Relay program. */
97-
const char* name;
98-
/*! \brief Shape of the tensor. */
99-
const int64_t* shape;
100-
/*! \brief Rank of this tensor. */
101-
int64_t num_shape;
102-
/*! \brief Data type of one element of this tensor. */
103-
DLDataType dtype;
104-
};
105-
#ifdef __cplusplus
106-
} // extern "C"
107-
#include <tvm/runtime/object.h>
10852
namespace tvm {
10953
namespace runtime {
11054
namespace metadata {
@@ -166,6 +110,5 @@ class TensorInfo : public MetadataBase {
166110
} // namespace metadata
167111
} // namespace runtime
168112
} // namespace tvm
169-
#endif // defined(__cplusplus)
170113

171114
#endif // TVM_RUNTIME_METADATA_H_

0 commit comments

Comments
 (0)