tvm
c_runtime_api.h
Go to the documentation of this file.
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 tvm/runtime/c_runtime_api.h
22  * \brief TVM runtime library.
23  *
24  * The philosophy of TVM project is to customize the compilation
25  * stage to generate code that can used by other projects transparently.
26  * So this is a minimum runtime code gluing, and some limited
27  * memory management code to enable quick testing.
28  *
29  * The runtime API is independent from TVM compilation stack and can
30  * be linked via libtvm_runtime.
31  *
32  * The common flow is:
33  * - Use TVMFuncListGlobalNames to get global function name
34  * - Use TVMFuncCall to call these functions.
35  *
36  * Possible return values of the API functions:
37  * * 0: success
38  * * -1: the error can be retrieved through TVMGetLastError.
39  * * -2: a frontend error occurred and recorded in the frontend.
40  */
41 #ifndef TVM_RUNTIME_C_RUNTIME_API_H_
42 #define TVM_RUNTIME_C_RUNTIME_API_H_
43 
44 // Macros to do weak linking
45 #ifdef _MSC_VER
46 #define TVM_WEAK __declspec(selectany)
47 #else
48 #define TVM_WEAK __attribute__((weak))
49 #endif
50 
51 #ifdef __EMSCRIPTEN__
52 #include <emscripten/emscripten.h>
53 #define TVM_DLL EMSCRIPTEN_KEEPALIVE
54 #endif
55 
56 #ifndef TVM_DLL
57 #ifdef _WIN32
58 #ifdef TVM_EXPORTS
59 #define TVM_DLL __declspec(dllexport)
60 #else
61 #define TVM_DLL __declspec(dllimport)
62 #endif
63 #else
64 #define TVM_DLL __attribute__((visibility("default")))
65 #endif
66 #endif
67 
68 // TVM version
69 #define TVM_VERSION "0.10.0"
70 
71 // TVM Runtime is DLPack compatible.
72 #include <dlpack/dlpack.h>
73 
74 #ifdef __cplusplus
75 extern "C" {
76 #endif
77 #include <stddef.h>
78 #include <stdint.h>
79 
81 typedef int64_t tvm_index_t;
82 
84 typedef enum {
85  kDLAOCL = 5,
87  kOpenGL = 11,
89  kDLHexagon = 14,
90  kDLWebGPU = 15
91  // AddExtraTVMType which is not in DLPack here
93 
110 typedef enum {
111  kTVMArgInt = kDLInt,
112  kTVMArgFloat = kDLFloat,
116  kDLDevice = 6U,
121  kTVMStr = 11U,
122  kTVMBytes = 12U,
125  // Extension codes for other frameworks to integrate TVM PackedFunc.
126  // To make sure each framework's id do not conflict, use first and
127  // last sections to mark ranges.
128  // Open an issue at the repo if you need a section of code.
132  // The following section of code is used for non-reserved types.
134  kTVMExtEnd = 128U,
136 
138 typedef DLTensor* TVMArrayHandle;
139 
144 typedef union {
145  int64_t v_int64;
146  double v_float64;
147  void* v_handle;
148  const char* v_str;
149  DLDataType v_type;
150  DLDevice v_device;
151 } TVMValue;
152 
157 typedef struct {
158  const char* data;
159  size_t size;
160 } TVMByteArray;
161 
163 typedef void* TVMModuleHandle;
165 typedef void* TVMFunctionHandle;
167 typedef void* TVMRetValueHandle;
172 typedef void* TVMStreamHandle;
174 typedef void* TVMObjectHandle;
175 
181 TVM_DLL void TVMAPISetLastError(const char* msg);
182 
192 TVM_DLL const char* TVMGetLastError(void);
203 TVM_DLL int TVMModLoadFromFile(const char* file_name, const char* format, TVMModuleHandle* out);
204 
213 TVM_DLL int TVMModImport(TVMModuleHandle mod, TVMModuleHandle dep);
214 
223 TVM_DLL int TVMModGetFunction(TVMModuleHandle mod, const char* func_name, int query_imports,
224  TVMFunctionHandle* out);
225 
237 TVM_DLL int TVMModFree(TVMModuleHandle mod);
238 
244 TVM_DLL int TVMFuncFree(TVMFunctionHandle func);
245 
266 TVM_DLL int TVMFuncCall(TVMFunctionHandle func, TVMValue* arg_values, int* type_codes, int num_args,
267  TVMValue* ret_val, int* ret_type_code);
268 
280 TVM_DLL int TVMCFuncSetReturn(TVMRetValueHandle ret, TVMValue* value, int* type_code, int num_ret);
281 
292 TVM_DLL int TVMCbArgToReturn(TVMValue* value, int* code);
293 
305 typedef int (*TVMPackedCFunc)(TVMValue* args, int* type_codes, int num_args, TVMRetValueHandle ret,
306  void* resource_handle);
307 
312 typedef void (*TVMPackedCFuncFinalizer)(void* resource_handle);
313 
323 typedef int (*TVMExtensionFuncDeclarer)(TVMFunctionHandle register_func_handle);
324 
336 TVM_DLL int TVMFuncCreateFromCFunc(TVMPackedCFunc func, void* resource_handle,
337  TVMPackedCFuncFinalizer fin, TVMFunctionHandle* out);
338 
348 TVM_DLL int TVMFuncRegisterGlobal(const char* name, TVMFunctionHandle f, int override);
349 
359 TVM_DLL int TVMFuncGetGlobal(const char* name, TVMFunctionHandle* out);
360 
367 TVM_DLL int TVMFuncListGlobalNames(int* out_size, const char*** out_array);
368 
373 TVM_DLL int TVMFuncRemoveGlobal(const char* name);
374 
375 // Array related apis for quick proptyping
390 TVM_DLL int TVMArrayAlloc(const tvm_index_t* shape, int ndim, int dtype_code, int dtype_bits,
391  int dtype_lanes, int device_type, int device_id, TVMArrayHandle* out);
392 
398 TVM_DLL int TVMArrayFree(TVMArrayHandle handle);
399 
407 TVM_DLL int TVMArrayCopyFromBytes(TVMArrayHandle handle, void* data, size_t nbytes);
408 
416 TVM_DLL int TVMArrayCopyToBytes(TVMArrayHandle handle, void* data, size_t nbytes);
417 
425 TVM_DLL int TVMArrayCopyFromTo(TVMArrayHandle from, TVMArrayHandle to, TVMStreamHandle stream);
426 
434 TVM_DLL int TVMArrayFromDLPack(DLManagedTensor* from, TVMArrayHandle* out);
435 
443 TVM_DLL int TVMArrayToDLPack(TVMArrayHandle from, DLManagedTensor** out);
444 
449 TVM_DLL void TVMDLManagedTensorCallDeleter(DLManagedTensor* dltensor);
450 
459 TVM_DLL int TVMStreamCreate(int device_type, int device_id, TVMStreamHandle* out);
460 
469 TVM_DLL int TVMStreamFree(int device_type, int device_id, TVMStreamHandle stream);
470 
482 TVM_DLL int TVMSetStream(int device_type, int device_id, TVMStreamHandle handle);
483 
492 TVM_DLL int TVMSynchronize(int device_type, int device_id, TVMStreamHandle stream);
493 
503 TVM_DLL int TVMStreamStreamSynchronize(int device_type, int device_id, TVMStreamHandle src,
504  TVMStreamHandle dst);
505 
513 TVM_DLL int TVMObjectGetTypeIndex(TVMObjectHandle obj, unsigned* out_tindex);
514 
521 TVM_DLL int TVMObjectTypeKey2Index(const char* type_key, unsigned* out_tindex);
522 
529 TVM_DLL int TVMObjectTypeIndex2Key(unsigned tindex, char** out_type_key);
530 
538 TVM_DLL int TVMObjectRetain(TVMObjectHandle obj);
539 
548 TVM_DLL int TVMObjectFree(TVMObjectHandle obj);
549 
555 TVM_DLL int TVMByteArrayFree(TVMByteArray* arr);
556 
567 TVM_DLL int TVMDeviceAllocDataSpace(DLDevice dev, size_t nbytes, size_t alignment,
568  DLDataType type_hint, void** out_data);
569 
583 TVM_DLL int TVMDeviceAllocDataSpaceWithScope(DLDevice dev, int ndim, const int64_t* shape,
584  DLDataType dtype, const char* mem_scope,
585  void** out_data);
586 
593 TVM_DLL int TVMDeviceFreeDataSpace(DLDevice dev, void* ptr);
594 
604 TVM_DLL int TVMDeviceCopyDataFromTo(DLTensor* from, DLTensor* to, TVMStreamHandle stream);
605 
613 TVM_DLL int TVMObjectDerivedFrom(uint32_t child_type_index, uint32_t parent_type_index,
614  int* is_derived);
615 
616 #ifdef __cplusplus
617 } // TVM_EXTERN_C
618 #endif
619 #endif // TVM_RUNTIME_C_RUNTIME_API_H_
Definition: c_runtime_api.h:130
int64_t v_int64
Definition: c_runtime_api.h:145
int(* TVMPackedCFunc)(TVMValue *args, int *type_codes, int num_args, TVMRetValueHandle ret, void *resource_handle)
C type of packed function.
Definition: c_runtime_api.h:305
void TVMAPISetLastError(const char *msg)
Used for implementing C API function. Set last error message before return.
constexpr const char * device_type
The device type.
Definition: stmt.h:1392
int TVMFuncFree(TVMFunctionHandle func)
Free the function when it is no longer needed.
int TVMArrayFree(TVMArrayHandle handle)
Free the TVM Array.
Definition: c_runtime_api.h:86
void * TVMModuleHandle
Handle to TVM runtime modules.
Definition: c_runtime_api.h:163
int TVMModImport(TVMModuleHandle mod, TVMModuleHandle dep)
Add dep to mod&#39;s dependency. This allows functions in this module to use modules. ...
void * v_handle
Definition: c_runtime_api.h:147
int TVMDeviceAllocDataSpaceWithScope(DLDevice dev, int ndim, const int64_t *shape, DLDataType dtype, const char *mem_scope, void **out_data)
Allocate a data space on device with special memory scope.
Definition: c_runtime_api.h:124
int TVMFuncListGlobalNames(int *out_size, const char ***out_array)
List all the globally registered function name.
Definition: c_runtime_api.h:111
Definition: c_runtime_api.h:119
const char * v_str
Definition: c_runtime_api.h:148
int TVMFuncCreateFromCFunc(TVMPackedCFunc func, void *resource_handle, TVMPackedCFuncFinalizer fin, TVMFunctionHandle *out)
Wrap a TVMPackedCFunc to become a FunctionHandle.
DLTensor * TVMArrayHandle
the array handle
Definition: c_runtime_api.h:138
Definition: c_runtime_api.h:115
int TVMArrayCopyToBytes(TVMArrayHandle handle, void *data, size_t nbytes)
Copy array data to CPU byte array.
int TVMModLoadFromFile(const char *file_name, const char *format, TVMModuleHandle *out)
Load module from file.
Definition: c_runtime_api.h:120
size_t size
Definition: c_runtime_api.h:159
Definition: c_runtime_api.h:85
Union type of values being passed through API and function calls.
Definition: c_runtime_api.h:144
Definition: c_runtime_api.h:118
const char * data
Definition: c_runtime_api.h:158
const char * TVMGetLastError(void)
return str message of the last error all function in this file will return 0 when success and nonzero...
Byte array type used to pass in byte array When kTVMBytes is used as data type.
Definition: c_runtime_api.h:157
void * TVMStreamHandle
The stream that is specific to device can be NULL, which indicates the default one.
Definition: c_runtime_api.h:172
int TVMCFuncSetReturn(TVMRetValueHandle ret, TVMValue *value, int *type_code, int num_ret)
Set the return value of TVMPackedCFunc.
Definition: c_runtime_api.h:90
int TVMFuncCall(TVMFunctionHandle func, TVMValue *arg_values, int *type_codes, int num_args, TVMValue *ret_val, int *ret_type_code)
Call a Packed TVM Function.
int TVMStreamStreamSynchronize(int device_type, int device_id, TVMStreamHandle src, TVMStreamHandle dst)
Synchronize two streams of execution.
int TVMCbArgToReturn(TVMValue *value, int *code)
Inplace translate callback argument value to return value. This is only needed for non-POD arguments...
constexpr const char * device_id
The allocation device for global malloc in host.
Definition: stmt.h:1390
int TVMFuncRemoveGlobal(const char *name)
Remove a global function.
Definition: c_runtime_api.h:89
int TVMFuncGetGlobal(const char *name, TVMFunctionHandle *out)
Get a global function.
Definition: c_runtime_api.h:134
TVMDeviceExtType
Extension device types in TVM.
Definition: c_runtime_api.h:84
int TVMSynchronize(int device_type, int device_id, TVMStreamHandle stream)
Wait until all computations on stream completes.
int TVMModFree(TVMModuleHandle mod)
Free the Module.
int TVMByteArrayFree(TVMByteArray *arr)
Free a TVMByteArray returned from TVMFuncCall, and associated memory.
void TVMDLManagedTensorCallDeleter(DLManagedTensor *dltensor)
Delete (free) a DLManagedTensor&#39;s data.
int TVMModGetFunction(TVMModuleHandle mod, const char *func_name, int query_imports, TVMFunctionHandle *out)
Get function from the module.
int TVMObjectFree(TVMObjectHandle obj)
Free the object.
Definition: c_runtime_api.h:88
Definition: c_runtime_api.h:87
TVMArgTypeCode
The type code in used and only used in TVM FFI for argument passing.
Definition: c_runtime_api.h:110
Definition: c_runtime_api.h:123
int TVMStreamFree(int device_type, int device_id, TVMStreamHandle stream)
Free a created stream handle.
int(* TVMExtensionFuncDeclarer)(TVMFunctionHandle register_func_handle)
Signature for extension function declarer.
Definition: c_runtime_api.h:323
DLDevice v_device
Definition: c_runtime_api.h:150
int TVMDeviceAllocDataSpace(DLDevice dev, size_t nbytes, size_t alignment, DLDataType type_hint, void **out_data)
Allocate a data space on device.
int TVMArrayCopyFromTo(TVMArrayHandle from, TVMArrayHandle to, TVMStreamHandle stream)
Copy the array, both from and to must be valid during the copy.
int TVMArrayToDLPack(TVMArrayHandle from, DLManagedTensor **out)
Produce a DLMangedTensor from the array that shares data memory with the array.
void * TVMRetValueHandle
Handle to hold return value.
Definition: c_runtime_api.h:167
void * TVMFunctionHandle
Handle to packed function handle.
Definition: c_runtime_api.h:165
Tensor shape(const Tensor &src, DataType dtype, const std::string name="T_shape", const std::string tag=kInjective)
Get the shape of input tensor.
Definition: transform.h:1758
int64_t tvm_index_t
type of array index.
Definition: c_runtime_api.h:81
Definition: c_runtime_api.h:113
int TVMObjectRetain(TVMObjectHandle obj)
Increase the reference count of an object.
DLDataType v_type
Definition: c_runtime_api.h:149
int TVMObjectDerivedFrom(uint32_t child_type_index, uint32_t parent_type_index, int *is_derived)
Check that an object is derived from another.
int TVMObjectTypeIndex2Key(unsigned tindex, char **out_type_key)
Convert type index to type key.
int TVMStreamCreate(int device_type, int device_id, TVMStreamHandle *out)
Create a new runtime stream.
Definition: c_runtime_api.h:129
int TVMFuncRegisterGlobal(const char *name, TVMFunctionHandle f, int override)
Register the function to runtime&#39;s global table.
Definition: c_runtime_api.h:112
void * TVMObjectHandle
Handle to Object.
Definition: c_runtime_api.h:174
Definition: c_runtime_api.h:114
int TVMObjectTypeKey2Index(const char *type_key, unsigned *out_tindex)
Convert type key to type index.
Definition: c_runtime_api.h:131
int TVMObjectGetTypeIndex(TVMObjectHandle obj, unsigned *out_tindex)
Get the type_index from an object.
int TVMSetStream(int device_type, int device_id, TVMStreamHandle handle)
Set the runtime stream of current thread to be stream. The subsequent calls to the same device_type w...
int TVMArrayAlloc(const tvm_index_t *shape, int ndim, int dtype_code, int dtype_bits, int dtype_lanes, int device_type, int device_id, TVMArrayHandle *out)
Allocate a nd-array&#39;s memory, including space of shape, of given spec.
int TVMArrayCopyFromBytes(TVMArrayHandle handle, void *data, size_t nbytes)
Copy array data from CPU byte array.
tvm::PrimExpr mod(const tvm::PrimExpr &a, const tvm::PrimExpr &b)
Definition: broadcast.h:290
int TVMArrayFromDLPack(DLManagedTensor *from, TVMArrayHandle *out)
Produce an array from the DLManagedTensor that shares data memory with the DLManagedTensor.
Definition: c_runtime_api.h:116
const Op & ret()
Return value.
Definition: c_runtime_api.h:122
int TVMDeviceCopyDataFromTo(DLTensor *from, DLTensor *to, TVMStreamHandle stream)
Copy data from one place to another.
int TVMDeviceFreeDataSpace(DLDevice dev, void *ptr)
Free a data space on device.
Definition: c_runtime_api.h:121
Definition: c_runtime_api.h:133
double v_float64
Definition: c_runtime_api.h:146
Definition: c_runtime_api.h:117
void(* TVMPackedCFuncFinalizer)(void *resource_handle)
C callback to free the resource handle in C packed function.
Definition: c_runtime_api.h:312