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] Add support for host-driven AoT Executor #11044

Merged
merged 45 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
1f076ad
Generate AOT Metadata when targeting C runtime and packed API.
areusch Mar 9, 2022
92aad23
Also copy metadata.h and metadata_base.h to standalone_crt.
areusch Mar 9, 2022
8d5b266
add support for get_input_index as well as setting up get_input_info …
alanmacd Mar 10, 2022
5425e46
add support for tvm.aot_executor.create in C runtime
alanmacd Mar 17, 2022
6ddb5c2
changes in-progress to unit tests
alanmacd Mar 17, 2022
a7e05a5
Include get_c_metadata in emitted function list
areusch Apr 4, 2022
fd8fc0d
make CRT error codes generic for graph or AoT executor, fix AoT lib l…
alanmacd Apr 5, 2022
3bad0f9
add AoT executor creation and initializaion, as well as support for g…
alanmacd Apr 6, 2022
db82d35
add allocation of inputs, outputs, and pools; add get_input(), but sh…
alanmacd Apr 8, 2022
9786e6b
add support to test_aot_executor for get_input()
alanmacd Apr 8, 2022
75a2e28
fix numpy array shape so that get_input() works properly
alanmacd Apr 8, 2022
90f3152
implement run(), get_output(), get_num_inputs(), and get_num_outputs(…
alanmacd Apr 9, 2022
c8e9964
fix up some issues from rebase with main
alanmacd Apr 15, 2022
6f389bb
clean up logging and test_graph_executor()
alanmacd Apr 15, 2022
ae73fa6
Merge branch 'main' into host-driven-aot-executor-merge-main
alanmacd Apr 16, 2022
2c16c63
lint clean-up
alanmacd Apr 18, 2022
2f54090
more lint clean-up
alanmacd Apr 18, 2022
94294d3
fix i386 build errors
alanmacd Apr 20, 2022
4e2978f
Merge branch 'main' into host-driven-aot-executor
alanmacd Apr 20, 2022
f11400e
first set of changes addressing PR feedback
alanmacd Apr 21, 2022
0723196
more PR feedback: device pass-by-value, docstring entries, return var…
alanmacd Apr 21, 2022
a7c07e4
add mangling of get_c_metadata() name to avoid function name collisions
alanmacd Apr 26, 2022
797f9a0
only mangle get_c_metadata() when using C runtime
alanmacd Apr 27, 2022
57ad55c
add static specifier to all kTvmgenMetadata variables to avoid namesp…
alanmacd Apr 27, 2022
31014d0
Merge branch 'main' into host-driven-aot-executor
alanmacd Apr 28, 2022
ae79dc6
use TVM_IS_CPP_RUNTIME preprocessor define to deteremine whether or n…
alanmacd Apr 29, 2022
c40e6ad
add TVM_IS_CPP_RUNTIME define for cpptest
alanmacd Apr 29, 2022
2290e55
add TVM_IS_CPP_RUNTIME to apps/bundle_deploy
alanmacd Apr 29, 2022
fcdbb6d
add TVM_IS_CPP_RUNTIME web/Makefile
alanmacd Apr 29, 2022
1d9545b
update number of expected generated C files for AoT source files
alanmacd May 2, 2022
9b152f1
break out metadata data structures into separate metadata_types.h hea…
alanmacd May 3, 2022
8185074
remove TVM_IS_CPP_RUNTIME from web makefile
alanmacd May 3, 2022
8ff68f3
fix metadata.h include-order lint issue
alanmacd May 3, 2022
b68bd3a
correct error mask bits
alanmacd May 3, 2022
370c272
address PR feedback
alanmacd May 4, 2022
ab30758
Merge branch 'main' into host-driven-aot-executor
alanmacd May 4, 2022
d3dae0f
Merge branch 'main' into host-driven-aot-executor
alanmacd May 5, 2022
778ec49
Merge branch 'main' into host-driven-aot-executor
alanmacd May 6, 2022
49b4f15
trigger build
alanmacd May 7, 2022
f22f274
trigger build
alanmacd May 8, 2022
bdc02d1
trigger build
alanmacd May 8, 2022
7b744a4
trigger build
alanmacd May 9, 2022
e7f5080
add alternate name for test_graph_executor() too see if it runs in CI
alanmacd May 9, 2022
d566feb
fix lint
alanmacd May 9, 2022
98eed70
revert alternate test code
alanmacd May 9, 2022
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
3 changes: 3 additions & 0 deletions cmake/modules/StandaloneCrt.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ if(USE_MICRO)
"3rdparty/dlpack/include *.h -> include"
"3rdparty/dmlc-core/include *.h -> include"
"include/tvm/runtime c_*_api.h -> include/tvm/runtime"
"include/tvm/runtime metadata*.h -> include/tvm/runtime"
alanmacd marked this conversation as resolved.
Show resolved Hide resolved
"include/tvm/runtime/crt *.h -> include/tvm/runtime/crt"
"src/runtime/crt Makefile -> ."
"src/runtime/crt/include *.h -> include"
"src/runtime/crt/aot_executor *.c -> src/runtime/crt/aot_executor"
"src/runtime/crt/aot_executor_module *.c -> src/runtime/crt/aot_executor_module"
"src/runtime/crt/common *.c -> src/runtime/crt/common"
"src/runtime/crt/graph_executor *.c -> src/runtime/crt/graph_executor"
"src/runtime/crt/graph_executor_module *.c -> src/runtime/crt/graph_executor_module"
Expand Down
4 changes: 2 additions & 2 deletions include/tvm/runtime/c_runtime_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ TVM_DLL int TVMCbArgToReturn(TVMValue* value, int* code);
* \param type_codes The type codes of the arguments
* \param num_args Number of arguments.
* \param ret The return value handle.
* \param resource_handle The handle additional resouce handle from fron-end.
* \param resource_handle The handle additional resouce handle from front-end.
* \return 0 if success, -1 if failure happens, set error via TVMAPISetLastError.
* \sa TVMCFuncSetReturn
*/
Expand All @@ -307,7 +307,7 @@ typedef int (*TVMPackedCFunc)(TVMValue* args, int* type_codes, int num_args, TVM

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

Expand Down
107 changes: 107 additions & 0 deletions include/tvm/runtime/crt/aot_executor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* 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 aot_executor.h
* \brief AoT Executor
*/
#ifndef TVM_RUNTIME_CRT_AOT_EXECUTOR_H_
#define TVM_RUNTIME_CRT_AOT_EXECUTOR_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <dlpack/dlpack.h>
#include <tvm/runtime/crt/internal/common/ndarray.h>
#include <tvm/runtime/metadata_types.h>

typedef struct TVMMetadata TVMMetadata;

typedef struct TVMAotExecutor {
/*! \brief The top-level metadata structure supplied by the generated code */
TVMMetadata* metadata;
alanmacd marked this conversation as resolved.
Show resolved Hide resolved
/*! \brief The code module that contains the compiled model */
TVMModuleHandle module_handle;
/*! \brief The device type */
DLDevice device;
/*! \brief List of allocated arguments, input(s), output(s), and pool(s)*/
TVMNDArray* args;
int64_t num_args;
} TVMAotExecutor;

/*!
* \brief Allocate a new AotExecutor with TVMPlatformMemoryAllocate and initialize it.
*
* \param module_handle TVM Module that exposes the functions to call.
* \param device Runtime execution device, only supports device type kDLCPU, index 0.
* \param executor Pointer which receives a pointer to the newly-created instance.
* \param module_name TVM Module name prefix, typically "default".
* \return 0 if successful.
*/
int TVMAotExecutor_Create(TVMModuleHandle module_handle, const DLDevice device,
TVMAotExecutor** executor, const char* module_name);

/*!
* \brief Release the AoT executor created by TVMAotExecutor_Create().
*
* \param executor Pointer to executor instance, created by TVMAotExecutor_Create().
* \param device Runtime execution device, only supports device type kDLCPU, index 0.
* \return 0 if successful.
*/
int TVMAotExecutor_Release(TVMAotExecutor* executor, const DLDevice device);
alanmacd marked this conversation as resolved.
Show resolved Hide resolved

/*!
* \brief Return the number of inputs.
*
* \param executor Pointer to executor instance, created by TVMAotExecutor_Create().
* \return Number of inputs.
*/
int TVMAotExecutor_GetNumInputs(TVMAotExecutor* executor);

/*!
* \brief Return the number of outputs.
*
* \param executor Pointer to executor instance, created by TVMAotExecutor_Create().
* \return Number of outputs.
*/
int TVMAotExecutor_GetNumOutputs(TVMAotExecutor* executor);

/*!
* \brief Return the input index of the specified input name
*
* \param executor Pointer to executor instance, created by TVMAotExecutor_Create().
* \param name Input name for retrieving index.
* \return Input index.
*/
int TVMAotExecutor_GetInputIndex(TVMAotExecutor* executor, const char* name);

/*!
* \brief Run the generated program.
*
* \param executor Pointer to executor instance, created by TVMAotExecutor_Create().
* \return 0 if successful.
*/
int TVMAotExecutor_Run(TVMAotExecutor* executor);

#ifdef __cplusplus
} // extern "C"
#endif

#endif // TVM_RUNTIME_CRT_AOT_EXECUTOR_H_
42 changes: 42 additions & 0 deletions include/tvm/runtime/crt/aot_executor_module.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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 graph_executor.h
* \brief Tiny AoT executor
*/
#ifndef TVM_RUNTIME_CRT_AOT_EXECUTOR_MODULE_H_
#define TVM_RUNTIME_CRT_AOT_EXECUTOR_MODULE_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <tvm/runtime/crt/error_codes.h>

/*!
* \brief Register the "tvm.aot_executor.create" constructor PackedFunc.
*/
tvm_crt_error_t TVMAotExecutorModule_Register();

#ifdef __cplusplus
} // extern "C"
#endif

#endif // TVM_RUNTIME_CRT_AOT_EXECUTOR_MODULE_H_
12 changes: 6 additions & 6 deletions include/tvm/runtime/crt/error_codes.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ typedef enum {
kTvmErrorCategorySession = 4,
kTvmErrorCategoryPlatform = 5,
kTvmErrorCategoryGenerated = 6,
kTvmErrorCategoryGraphExecutor = 7,
kTvmErrorCategoryExecutor = 7,
kTvmErrorCategoryFunctionCall = 8,
kTvmErrorCategoryTimeEvaluator = 9,
} tvm_crt_error_category_t;
Expand Down Expand Up @@ -84,10 +84,10 @@ typedef enum {
// Common error codes returned from generated functions.
kTvmErrorGeneratedInvalidStorageId = DEFINE_TVM_CRT_ERROR(kTvmErrorCategoryGenerated, 0),

// Graph executor
kTvmErrorGraphModuleAlreadyCreated = DEFINE_TVM_CRT_ERROR(kTvmErrorCategoryGraphExecutor, 0),
kTvmErrorGraphModuleBadContext = DEFINE_TVM_CRT_ERROR(kTvmErrorCategoryGraphExecutor, 1),
kTvmErrorGraphModuleNoSuchInput = DEFINE_TVM_CRT_ERROR(kTvmErrorCategoryGraphExecutor, 2),
// Graph or AoT executor
kTvmErrorExecutorModuleAlreadyCreated = DEFINE_TVM_CRT_ERROR(kTvmErrorCategoryExecutor, 0),
kTvmErrorExecutorModuleBadContext = DEFINE_TVM_CRT_ERROR(kTvmErrorCategoryExecutor, 1),
kTvmErrorExecutorModuleNoSuchInput = DEFINE_TVM_CRT_ERROR(kTvmErrorCategoryExecutor, 2),

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

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

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion include/tvm/runtime/crt/graph_executor_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*!
* \file graph_executor.h
* \file graph_executor_module.h
* \brief Tiny graph executor that can run graph containing only tvm PackedFunc.
*/
#ifndef TVM_RUNTIME_CRT_GRAPH_EXECUTOR_MODULE_H_
Expand Down
69 changes: 6 additions & 63 deletions include/tvm/runtime/metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,19 @@
#ifndef TVM_RUNTIME_METADATA_H_
#define TVM_RUNTIME_METADATA_H_

#include <inttypes.h>
#ifdef __cplusplus
#include <memory>
#include <string>
#include <vector>
#endif
#include <tvm/runtime/c_runtime_api.h>
#ifdef __cplusplus
#include <tvm/runtime/metadata_base.h>
#include <tvm/runtime/metadata_types.h>
#include <tvm/runtime/object.h>
#include <tvm/support/span.h>
#endif

#include <memory>
#include <string>
#include <vector>

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

#ifdef __cplusplus
namespace tvm {
namespace runtime {
namespace metadata {
Expand All @@ -52,59 +49,6 @@ static const constexpr int64_t kMetadataVersion = TVM_METADATA_VERSION;
} // namespace runtime
} // namespace tvm

extern "C" {
#endif

/*!
* \brief Top-level metadata structure. Holds all other metadata types.
*/
struct TVMMetadata {
/*! \brief Version identifier for this metadata. */
int64_t version;
/*! \brief Inputs to the AOT run_model function.
* The order of the elements is the same as in the arguments to run_model. That is to say,
* this array specifies the first `num_inputs` arguments to run_model.
*/
const struct TVMTensorInfo* inputs;
/*! \brief Number of elements in `inputs` array. */
int64_t num_inputs;
/*! \brief Outputs of the AOT run_model function.
* The order of the elements is the same as in the arguments to run_model. That is to say,
* this array specifies the last `num_outputs` arguments to run_model.
*/
const struct TVMTensorInfo* outputs;
/*! \brief Number of elements in `outputs` array. */
int64_t num_outputs;
/*! \brief Memory Pools needed by the AOT main function.
* The order of the elements is the same as in the arguments to run_model. That is to say,
* this array specifies the last `num_pools` arguments to run_model.
*/
const struct TVMTensorInfo* pools;
/*! \brief Number of elements in `pools` array. */
int64_t num_pools;
/*! \brief Name of the model, as passed to tvm.relay.build. */
const char* mod_name;
};

/*!
* \brief Describes one tensor argument to `run_model`.
* NOTE: while TIR allows for other types of arguments, such as scalars, the AOT run_model
* function does not currently accept these. Therefore it's not possible to express those
* in this metadata. A future patch may modify this.
*/
struct TVMTensorInfo {
/*! \brief Name of the tensor, as specified in the Relay program. */
const char* name;
/*! \brief Shape of the tensor. */
const int64_t* shape;
/*! \brief Rank of this tensor. */
int64_t num_shape;
/*! \brief Data type of one element of this tensor. */
DLDataType dtype;
};
#ifdef __cplusplus
} // extern "C"
#include <tvm/runtime/object.h>
namespace tvm {
namespace runtime {
namespace metadata {
Expand Down Expand Up @@ -166,6 +110,5 @@ class TensorInfo : public MetadataBase {
} // namespace metadata
} // namespace runtime
} // namespace tvm
#endif // defined(__cplusplus)

#endif // TVM_RUNTIME_METADATA_H_
Loading