Skip to content

Commit 1f2dbea

Browse files
committed
[API/JIT] Enable registerable global function, introduce StackVM intepreter
1 parent 01a7ce0 commit 1f2dbea

21 files changed

+1712
-52
lines changed

include/tvm/codegen.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "./base.h"
1111
#include "./expr.h"
1212
#include "./module.h"
13-
#include "./runtime/runtime.h"
13+
#include "./runtime/packed_func.h"
1414

1515

1616
namespace tvm {

include/tvm/ir.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,13 @@ constexpr const char* tvm_handle_is_null = "tvm_handle_is_null";
8181
/*!
8282
* \brief See pesudo code
8383
*
84-
* bool tvm_print(VType value) {
85-
* LOG(INFO) << value;
84+
* int tvm_call_global(name, TVMValue* args) {
85+
* PackedFunc f = PackedFunc::GetGlobal(name);
86+
* f (args, type_code_of(args), len(args));
87+
* return 0;
8688
* }
8789
*/
88-
constexpr const char* tvm_print = "tvm_print";
90+
constexpr const char* tvm_call_global = "tvm_call_global";
8991

9092
/*! \brief The field id of each field in array */
9193
enum TVMArrayFieldKind {

include/tvm/module.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ namespace tvm {
2020
// Internal node container of lowered function.
2121
class LoweredFuncNode;
2222

23-
// Internal node container of module.
24-
class ModuleNode;
25-
2623
/*!
2724
* \brief LoweredFunc represents function after lowering.
2825
* This is the final IR representation before codegen.

include/tvm/runtime/c_runtime_api.h

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ TVM_DLL const char *TVMGetLastError(void);
161161
* \param option_vals Additional option values to pass
162162
* \param num_options Number of options to be passed into it.
163163
* \param out_code 1: success, 0: already initialized
164-
* \return Whether the function is successful.
164+
* \return 0 when success, -1 when failure happens
165165
*/
166166
TVM_DLL int TVMDeviceInit(int dev_mask,
167167
const char** option_keys,
@@ -188,7 +188,7 @@ TVM_DLL int TVMContextEnabled(TVMContext ctx,
188188
* \param dtype The array data type.
189189
* \param ctx The ctx this array sits on.
190190
* \param out The output handle.
191-
* \return Whether the function is successful.
191+
* \return 0 when success, -1 when failure happens
192192
*/
193193
TVM_DLL int TVMArrayAlloc(const tvm_index_t* shape,
194194
tvm_index_t ndim,
@@ -198,6 +198,7 @@ TVM_DLL int TVMArrayAlloc(const tvm_index_t* shape,
198198
/*!
199199
* \brief Free the TVM Array.
200200
* \param handle The array handle to be freed.
201+
* \return 0 when success, -1 when failure happens
201202
*/
202203
TVM_DLL int TVMArrayFree(TVMArrayHandle handle);
203204

@@ -206,6 +207,7 @@ TVM_DLL int TVMArrayFree(TVMArrayHandle handle);
206207
* \param from The array to be copied from.
207208
* \param to The target space.
208209
* \param stream The stream where the copy happens, can be NULL.
210+
* \return 0 when success, -1 when failure happens
209211
*/
210212
TVM_DLL int TVMArrayCopyFromTo(TVMArrayHandle from,
211213
TVMArrayHandle to,
@@ -214,13 +216,14 @@ TVM_DLL int TVMArrayCopyFromTo(TVMArrayHandle from,
214216
* \brief Wait until all computations on stream completes.
215217
* \param ctx The ctx to be synchronized.
216218
* \param stream The stream to be synchronized.
219+
* \return 0 when success, -1 when failure happens
217220
*/
218221
TVM_DLL int TVMSynchronize(TVMContext ctx, TVMStreamHandle stream);
219222

220223
/*!
221224
* \brief Free the function when it is no longer needed.
222225
* \param func The function handle
223-
* \return whether
226+
* \return 0 when success, -1 when failure happens
224227
*/
225228
TVM_DLL int TVMFuncFree(TVMFunctionHandle func);
226229

@@ -239,6 +242,57 @@ TVM_DLL int TVMFuncCall(TVMFunctionHandle func,
239242
TVMValue* args,
240243
int* type_codes,
241244
int num_args);
245+
246+
/*!
247+
* \brief C type of packed function.
248+
*
249+
* \param args The arguments
250+
* \param type_codes The type codes of the arguments
251+
* \param num_args Number of arguments.
252+
* \param resource_handle The handle additional resouce handle from fron-end.
253+
*/
254+
typedef void (*TVMPackedCFunc)(
255+
TVMValue* args, int* type_codes, int num_args, void* resource_handle);
256+
257+
/*!
258+
* \brief C callback to free the resource handle in C packed function.
259+
* \param resource_handle The handle additional resouce handle from fron-end.
260+
*/
261+
typedef void (*TVMPackedCFuncFinalizer)(void* resource_handle);
262+
263+
/*!
264+
* \brief Wrap a TVMPackedCFunc to become a FunctionHandle.
265+
*
266+
* The resource_handle will be managed by TVM API, until the function is no longer used.
267+
*
268+
* \param func The packed C function.
269+
* \param resource_handle The resource handle from front-end, can be NULL.
270+
* \param fin The finalizer on resource handle when the FunctionHandle get freed, can be NULL
271+
* \param out the result function handle.
272+
* \return 0 when success, -1 when failure happens
273+
*/
274+
TVM_DLL int TVMFuncCreateFromCFunc(TVMPackedCFunc func,
275+
void* resource_handle,
276+
TVMPackedCFuncFinalizer fin,
277+
TVMFunctionHandle *out);
278+
279+
/*!
280+
* \brief Register the function to runtime's global table.
281+
*
282+
* The registered function then can be pulled by the backend by the name.
283+
*
284+
* \param name The name of the function.
285+
* \param f The function to be registered.
286+
*/
287+
TVM_DLL int TVMFuncRegisterGlobal(const char* name, TVMFunctionHandle f);
288+
289+
/*!
290+
* \brief Get a global function.
291+
*
292+
* \param name The name of the function.
293+
* \param out the result function pointer.
294+
*/
295+
TVM_DLL int TVMFuncGetGlobal(const char* name, TVMFunctionHandle* out);
242296
} // TVM_EXTERN_C
243297

244298
#endif // TVM_RUNTIME_C_RUNTIME_API_H_

include/tvm/runtime/runtime.h renamed to include/tvm/runtime/packed_func.h

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,43 @@
11
/*!
22
* Copyright (c) 2016 by Contributors
3-
* \file runtime.h
3+
* \file packed_func.h
44
* \brief Runtime related c++ class.
55
*/
6-
#ifndef TVM_RUNTIME_RUNTIME_H_
7-
#define TVM_RUNTIME_RUNTIME_H_
6+
#ifndef TVM_RUNTIME_PACKED_FUNC_H_
7+
#define TVM_RUNTIME_PACKED_FUNC_H_
88

99
#include <functional>
1010
#include <tuple>
11+
#include <vector>
12+
#include <string>
1113
#include "./c_runtime_api.h"
1214

1315
namespace tvm {
1416
namespace runtime {
1517

1618
/*!
17-
* \brief Packed function is a runtime function
18-
* whose argument type_codes are erased by packed format.
19+
* \brief Packed function is a type-erased function.
20+
* The arguments are passed by packed format.
1921
*
20-
* This is an useful unified interface to call generated functions.
22+
* This is an useful unified interface to call generated functions,
23+
* It is the unified function function type of TVM.
24+
* It corresponds to TVMFunctionHandle in C runtime API.
2125
*/
2226
class PackedFunc {
2327
public:
2428
/*! \brief The internal std::function */
2529
using FType = std::function<void(const TVMValue* args, const int* type_codes, int num_args)>;
30+
/*! \brief default constructor */
2631
PackedFunc() {}
32+
/*!
33+
* \brief constructing a packed function from a std::function.
34+
* \param body the internal container of packed function.
35+
*/
2736
explicit PackedFunc(FType body) : body_(body) {}
2837
/*!
29-
* \brief invoke the packed function by directly passing in arguments.
38+
* \brief Call packed function by directly passing in unpacked format.
3039
* \param args Arguments to be passed.
3140
* \tparam Args arguments to be passed.
32-
* \return The first return value.
3341
*/
3442
template<typename... Args>
3543
inline void operator()(Args&& ...args) const;
@@ -41,9 +49,25 @@ class PackedFunc {
4149
*/
4250
inline void CallPacked(const TVMValue* args, const int* type_codes, int num_args) const;
4351
/*! \return the internal body function */
44-
inline FType body() const {
45-
return body_;
46-
}
52+
inline FType body() const;
53+
/*!
54+
* \brief Register f as into global function table
55+
* \param name The name of the function.
56+
* \param f The function to be registered.
57+
* \return Reference to the registered function.
58+
* \note The returned reference is valid until the end of the program
59+
*/
60+
static const PackedFunc& RegisterGlobal(const std::string& name, PackedFunc f);
61+
/*!
62+
* \brief Get the global function by name.
63+
* \param name The name of the function.
64+
* \return reference to the registered function.
65+
*/
66+
static const PackedFunc& GetGlobal(const std::string& name);
67+
/*!
68+
* \brief Get the names of currently registered global function.
69+
*/
70+
static std::vector<std::string> ListGlobalNames();
4771

4872
private:
4973
/*! \brief internal container of packed function */
@@ -56,6 +80,10 @@ inline void PackedFunc::CallPacked(
5680
body_(args, type_codes, num_args);
5781
}
5882

83+
inline PackedFunc::FType PackedFunc::body() const {
84+
return body_;
85+
}
86+
5987
template<bool stop, std::size_t I, typename F, typename ...Args>
6088
struct for_each_dispatcher_ {
6189
static inline void run(const std::tuple<Args...>& args, F f) {
@@ -124,4 +152,4 @@ inline void PackedFunc::operator()(Args&& ...args) const {
124152
}
125153
} // namespace runtime
126154
} // namespace tvm
127-
#endif // TVM_RUNTIME_RUNTIME_H_
155+
#endif // TVM_RUNTIME_PACKED_FUNC_H_

0 commit comments

Comments
 (0)