|
| 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_ |
0 commit comments