Skip to content

Commit 7e02523

Browse files
authored
[RUNTIME] Add interface header of runtime (#15)
* [RUNTIME] Add interface header of runtime * fix mac build
1 parent 7f82912 commit 7e02523

File tree

12 files changed

+328
-141
lines changed

12 files changed

+328
-141
lines changed

HalideIR

Submodule HalideIR updated from 3278103 to 6375e6b

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export LDFLAGS = -pthread -lm
2-
export CFLAGS = -std=c++11 -Wall -O2 -Wno-unknown-pragmas -funroll-loops\
2+
export CFLAGS = -std=c++11 -Wall -O2\
33
-Iinclude -Idmlc-core/include -IHalideIR/src -fPIC
44

55
# specify tensor path

include/tvm/c_api.h

Lines changed: 19 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,74 +6,28 @@
66
#ifndef TVM_C_API_H_
77
#define TVM_C_API_H_
88

9-
#ifdef __cplusplus
10-
#define TVM_EXTERN_C extern "C"
11-
#else
12-
#define TVM_EXTERN_C
13-
#endif
14-
15-
/*! \brief TVM_DLL prefix for windows */
16-
#ifdef _WIN32
17-
#ifdef TVM_EXPORTS
18-
#define TVM_DLL __declspec(dllexport)
19-
#else
20-
#define TVM_DLL __declspec(dllimport)
21-
#endif
22-
#else
23-
#define TVM_DLL
24-
#endif
9+
#include "./c_runtime_api.h"
2510

2611
TVM_EXTERN_C {
2712
/*! \brief handle to functions */
28-
typedef void* FunctionHandle;
13+
typedef void* APIFunctionHandle;
2914
/*! \brief handle to node */
3015
typedef void* NodeHandle;
3116

32-
/*!
33-
* \brief union type for returning value of attributes
34-
* Attribute type can be identified by id
35-
*/
36-
typedef union {
37-
long v_long; // NOLINT(*)
38-
double v_double;
39-
const char* v_str;
40-
NodeHandle v_handle;
41-
} ArgVariant;
42-
43-
/*! \brief attribute types */
44-
typedef enum {
45-
kNull = 0,
46-
kLong = 1,
47-
kDouble = 2,
48-
kStr = 3,
49-
kNodeHandle = 4
50-
} ArgVariantID;
51-
52-
/*!
53-
* \brief return str message of the last error
54-
* all function in this file will return 0 when success
55-
* and -1 when an error occured,
56-
* NNGetLastError can be called to retrieve the error
57-
*
58-
* this function is threadsafe and can be called by different thread
59-
* \return error info
60-
*/
61-
TVM_DLL const char *TVMGetLastError(void);
62-
6317
/*!
6418
* \brief List all the node function name
6519
* \param out_size The number of functions
6620
* \param out_array The array of function names.
6721
*/
68-
TVM_DLL int TVMListFunctionNames(int *out_size,
22+
TVM_DLL int TVMListAPIFunctionNames(int *out_size,
6923
const char*** out_array);
7024
/*!
7125
* \brief get function handle by name
7226
* \param name The name of function
7327
* \param handle The returning function handle
7428
*/
75-
TVM_DLL int TVMGetFunctionHandle(const char* name,
76-
FunctionHandle *handle);
29+
TVM_DLL int TVMGetAPIFunctionHandle(const char* name,
30+
APIFunctionHandle *handle);
7731

7832
/*!
7933
* \brief Get the detailed information about function.
@@ -88,14 +42,14 @@ TVM_DLL int TVMGetFunctionHandle(const char* name,
8842
* \param return_type Return type of the function, if any.
8943
* \return 0 when success, -1 when failure happens
9044
*/
91-
TVM_DLL int TVMGetFunctionInfo(FunctionHandle handle,
92-
const char **real_name,
93-
const char **description,
94-
int *num_doc_args,
95-
const char ***arg_names,
96-
const char ***arg_type_infos,
97-
const char ***arg_descriptions,
98-
const char **return_type);
45+
TVM_DLL int TVMGetAPIFunctionInfo(APIFunctionHandle handle,
46+
const char **real_name,
47+
const char **description,
48+
int *num_doc_args,
49+
const char ***arg_names,
50+
const char ***arg_type_infos,
51+
const char ***arg_descriptions,
52+
const char **return_type);
9953

10054
/*!
10155
* \brief Push an argument to the function calling stack.
@@ -104,8 +58,8 @@ TVM_DLL int TVMGetFunctionInfo(FunctionHandle handle,
10458
* \param arg number of attributes
10559
* \param type_id The typeid of attributes.
10660
*/
107-
TVM_DLL int TVMPushStack(ArgVariant arg,
108-
int type_id);
61+
TVM_DLL int TVMAPIPushStack(TVMArg arg,
62+
int type_id);
10963

11064
/*!
11165
* \brief call a function by using arguments in the stack.
@@ -115,9 +69,9 @@ TVM_DLL int TVMPushStack(ArgVariant arg,
11569
* \param ret_val The return value.
11670
* \param ret_typeid the type id of return value.
11771
*/
118-
TVM_DLL int TVMFunctionCall(FunctionHandle handle,
119-
ArgVariant* ret_val,
120-
int* ret_typeid);
72+
TVM_DLL int TVMAPIFunctionCall(APIFunctionHandle handle,
73+
TVMArg* ret_val,
74+
int* ret_typeid);
12175

12276
/*!
12377
* \brief free the node handle
@@ -135,7 +89,7 @@ TVM_DLL int TVMNodeFree(NodeHandle handle);
13589
*/
13690
TVM_DLL int TVMNodeGetAttr(NodeHandle handle,
13791
const char* key,
138-
ArgVariant* out_value,
92+
TVMArg* out_value,
13993
int* out_typeid,
14094
int* out_success);
14195

include/tvm/c_runtime_api.h

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/*!
2+
* Copyright (c) 2016 by Contributors
3+
* \file c_runtime_api.h
4+
* \brief TVM runtime library.
5+
*
6+
* The philosophy of TVM project is to customize the compilation
7+
* stage to generate code that can used by other projects transparently.
8+
*
9+
* So this is a minimum runtime code gluing, and some limited
10+
* memory management code to enable quick testing.
11+
*/
12+
#ifndef TVM_C_RUNTIME_API_H_
13+
#define TVM_C_RUNTIME_API_H_
14+
15+
#ifdef __cplusplus
16+
#define TVM_EXTERN_C extern "C"
17+
#else
18+
#define TVM_EXTERN_C
19+
#endif
20+
21+
/*! \brief TVM_DLL prefix for windows */
22+
#ifdef _WIN32
23+
#ifdef TVM_EXPORTS
24+
#define TVM_DLL __declspec(dllexport)
25+
#else
26+
#define TVM_DLL __declspec(dllimport)
27+
#endif
28+
#else
29+
#define TVM_DLL
30+
#endif
31+
32+
#include <stdint.h>
33+
34+
35+
TVM_EXTERN_C {
36+
/*! \brief type of array index. */
37+
typedef unsigned tvm_index_t;
38+
39+
/*!
40+
* \brief union type for arguments and return values
41+
* in both runtime API and TVM API calls
42+
*/
43+
typedef union {
44+
long v_long; // NOLINT(*)
45+
double v_double;
46+
const char* v_str;
47+
void* v_handle;
48+
} TVMArg;
49+
50+
/*!
51+
* \brief The type index in TVM.
52+
*/
53+
typedef enum {
54+
kNull = 0,
55+
kLong = 1,
56+
kDouble = 2,
57+
kStr = 3,
58+
kNodeHandle = 4,
59+
kArrayHandle = 5
60+
} TVMArgTypeID;
61+
62+
/*!
63+
* \brief The device type
64+
*/
65+
typedef enum {
66+
/*! \brief CPU device */
67+
kCPU = 1,
68+
/*! \brief NVidia GPU device(CUDA) */
69+
kGPU = 2,
70+
/*! \brief opencl device */
71+
KOpenCL = 4
72+
} TVMDeviceMask;
73+
74+
/*!
75+
* \brief The Device information, abstract away common device types.
76+
*/
77+
typedef struct {
78+
/*! \brief The device type mask */
79+
int dev_mask;
80+
/*! \brief the device id */
81+
int dev_id;
82+
} TVMDevice;
83+
84+
/*! \brief The type code in TVMDataType */
85+
typedef enum {
86+
kInt = 0U,
87+
kUInt = 1U,
88+
kFloat = 2U
89+
} TVMTypeCode;
90+
91+
/*!
92+
* \brief the data type
93+
* Examples
94+
* - float: type_code = 2, bits = 32, lanes=1
95+
* - float4(vectorized 4 float): type_code = 2, bits = 32, lanes=4
96+
* - int8: type_code = 0, bits = 8, lanes=1
97+
*/
98+
typedef struct {
99+
/*! \brief type code, in TVMTypeCode */
100+
uint8_t type_code;
101+
/*! \brief number of bits of the type */
102+
uint8_t bits;
103+
/*! \brief number of lanes, */
104+
uint16_t lanes;
105+
} TVMDataType;
106+
107+
/*!
108+
* \brief Data structure representing a n-dimensional array(tensor).
109+
* This is used to pass data specification into TVM.
110+
*/
111+
typedef struct {
112+
/*! \brief The data field pointer on specified device */
113+
void* data;
114+
/*! \brief The shape pointers of the array */
115+
const tvm_index_t* shape;
116+
/*!
117+
* \brief The stride data about each dimension of the array, can be NULL
118+
* When strides is NULL, it indicates that the array is empty.
119+
*/
120+
const tvm_index_t* strides;
121+
/*! \brief number of dimensions of the array */
122+
tvm_index_t ndim;
123+
/*! \brief The data type flag */
124+
TVMDataType dtype;
125+
/*! \brief The device this array sits on */
126+
TVMDevice device;
127+
} TVMArray;
128+
129+
/*!
130+
* \brief The stream that is specific to device
131+
* can be NULL, which indicates the default one.
132+
*/
133+
typedef void* TVMStreamHandle;
134+
/*!
135+
* \brief Pointer to function handle that points to
136+
* a generated TVM function.
137+
*/
138+
typedef void* TVMFunctionHandle;
139+
/*! \brief the array handle */
140+
typedef TVMArray* TVMArrayHandle;
141+
142+
/*!
143+
* \brief return str message of the last error
144+
* all function in this file will return 0 when success
145+
* and -1 when an error occured,
146+
* TVMGetLastError can be called to retrieve the error
147+
*
148+
* this function is threadsafe and can be called by different thread
149+
* \return error info
150+
*/
151+
TVM_DLL const char *TVMGetLastError(void);
152+
153+
/*!
154+
* \brief Allocate a nd-array's memory,
155+
* including space of shape, of given spec.
156+
*
157+
* \param shape The shape of the array, the data content will be copied to out
158+
* \param ndim The number of dimension of the array.
159+
* \param dtype The array data type.
160+
* \param device The device this array sits on.
161+
* \param out The output handle.
162+
* \return Whether the function is successful.
163+
*/
164+
TVM_DLL int TVMArrayAlloc(const tvm_index_t* shape,
165+
tvm_index_t ndim,
166+
int dtype,
167+
TVMDevice device,
168+
TVMArrayHandle* out);
169+
/*!
170+
* \brief Free the TVM Array.
171+
* \param handle The array handle to be freed.
172+
*/
173+
TVM_DLL int TVMArrayFree(TVMArrayHandle handle);
174+
175+
/*!
176+
* \brief Copy the array, both from and to must be valid during the copy.
177+
* \param from The array to be copied from.
178+
* \param to The target space.
179+
* \param stream The stream where the copy happens, can be NULL.
180+
*/
181+
TVM_DLL int TVMArrayCopyFromTo(TVMArrayHandle from,
182+
TVMArrayHandle to,
183+
TVMStreamHandle stream);
184+
/*!
185+
* \brief Wait until all computations on stream completes.
186+
* \param stream the stream to be synchronized.
187+
*/
188+
TVM_DLL int TVMSynchronize(TVMStreamHandle stream);
189+
190+
/*!
191+
* \brief Launch a generated TVM function
192+
* \param func function handle to be launched.
193+
* \param args The arguments
194+
* \param arg_type_ids The type id of the arguments
195+
* \param num_args Number of arguments.
196+
* \param stream The stream this function to be launched on.
197+
*/
198+
TVM_DLL int TVMLaunch(TVMFunctionHandle func,
199+
TVMArg* args,
200+
int* arg_type_ids,
201+
int num_args,
202+
TVMStreamHandle stream);
203+
} // TVM_EXTERN_C
204+
205+
#endif // TVM_C_RUNTIME_API_H_

python/tvm/_base.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ def _load_lib():
4141
# library instance of nnvm
4242
_LIB = _load_lib()
4343

44-
# type definitions
45-
FunctionHandle = ctypes.c_void_p
46-
NodeHandle = ctypes.c_void_p
47-
4844
#----------------------------
4945
# helper function definition
5046
#----------------------------

0 commit comments

Comments
 (0)