PandA-2024.02
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 #ifndef TVM_RUNTIME_C_RUNTIME_API_H_
37 #define TVM_RUNTIME_C_RUNTIME_API_H_
38 
39 // Macros to do weak linking
40 #ifdef _MSC_VER
41 #define TVM_WEAK __declspec(selectany)
42 #else
43 #define TVM_WEAK __attribute__((weak))
44 #endif
45 
46 #ifdef __EMSCRIPTEN__
47 #include <emscripten/emscripten.h>
48 #define TVM_DLL EMSCRIPTEN_KEEPALIVE
49 #endif
50 
51 #ifndef TVM_DLL
52 #ifdef _WIN32
53 #ifdef TVM_EXPORTS
54 #define TVM_DLL __declspec(dllexport)
55 #else
56 #define TVM_DLL __declspec(dllimport)
57 #endif
58 #else
59 #define TVM_DLL __attribute__((visibility("default")))
60 #endif
61 #endif
62 
63 // TVM version
64 #define TVM_VERSION "0.6.dev"
65 
66 
67 // TVM Runtime is DLPack compatible.
68 #include <dlpack/dlpack.h>
69 
70 #ifdef __cplusplus
71 extern "C" {
72 #endif
73 #include <stdint.h>
74 #include <stddef.h>
75 
77 typedef int64_t tvm_index_t;
78 
80 typedef enum {
81  kDLAOCL = 5,
83  kOpenGL = 11,
85  // AddExtraTVMType which is not in DLPack here
87 
92 typedef enum {
93  // The type code of other types are compatible with DLPack.
94  // The next few fields are extension types
95  // that is used by TVM API calls.
96  kHandle = 3U,
97  kNull = 4U,
98  kTVMType = 5U,
104  kStr = 11U,
105  kBytes = 12U,
108  // Extension codes for other frameworks to integrate TVM PackedFunc.
109  // To make sure each framework's id do not conflict, use first and
110  // last sections to mark ranges.
111  // Open an issue at the repo if you need a section of code.
115  // The following section of code is used for non-reserved types.
117  kExtEnd = 128U,
118  // The rest of the space is used for custom, user-supplied datatypes
120 } TVMTypeCode;
121 
133 
138 
143 
145 typedef TVMArray* TVMArrayHandle;
146 
151 typedef union {
152  int64_t v_int64;
153  double v_float64;
154  void* v_handle;
155  const char* v_str;
156  TVMType v_type;
157  TVMContext v_ctx;
158 } TVMValue;
159 
164 typedef struct {
165  const char* data;
166  size_t size;
167 } TVMByteArray;
168 
170 typedef void* TVMModuleHandle;
172 typedef void* TVMFunctionHandle;
174 typedef void* TVMRetValueHandle;
179 typedef void* TVMStreamHandle;
181 typedef void* TVMObjectHandle;
182 
188 TVM_DLL void TVMAPISetLastError(const char* msg);
189 
199 TVM_DLL const char *TVMGetLastError(void);
210 TVM_DLL int TVMModLoadFromFile(const char* file_name,
211  const char* format,
212  TVMModuleHandle* out);
213 
222 TVM_DLL int TVMModImport(TVMModuleHandle mod,
223  TVMModuleHandle dep);
224 
233 TVM_DLL int TVMModGetFunction(TVMModuleHandle mod,
234  const char* func_name,
235  int query_imports,
236  TVMFunctionHandle *out);
237 
244 TVM_DLL int TVMExtTypeFree(void* handle, int type_code);
245 
257 TVM_DLL int TVMModFree(TVMModuleHandle mod);
258 
264 TVM_DLL int TVMFuncFree(TVMFunctionHandle func);
265 
286 TVM_DLL int TVMFuncCall(TVMFunctionHandle func,
287  TVMValue* arg_values,
288  int* type_codes,
289  int num_args,
290  TVMValue* ret_val,
291  int* ret_type_code);
292 
304 TVM_DLL int TVMCFuncSetReturn(TVMRetValueHandle ret,
305  TVMValue* value,
306  int* type_code,
307  int num_ret);
308 
319 TVM_DLL int TVMCbArgToReturn(TVMValue* value, int code);
320 
332 typedef int (*TVMPackedCFunc)(
333  TVMValue* args,
334  int* type_codes,
335  int num_args,
336  TVMRetValueHandle ret,
337  void* resource_handle);
338 
343 typedef void (*TVMPackedCFuncFinalizer)(void* resource_handle);
344 
354 typedef int (*TVMExtensionFuncDeclarer)(TVMFunctionHandle register_func_handle);
355 
368  void* resource_handle,
370  TVMFunctionHandle *out);
371 
382  const char* name, TVMFunctionHandle f, int override);
383 
393 TVM_DLL int TVMFuncGetGlobal(const char* name, TVMFunctionHandle* out);
394 
401 TVM_DLL int TVMFuncListGlobalNames(int* out_size,
402  const char*** out_array);
403 
404 // Array related apis for quick proptyping
419 TVM_DLL int TVMArrayAlloc(const tvm_index_t* shape,
420  int ndim,
421  int dtype_code,
422  int dtype_bits,
423  int dtype_lanes,
424  int device_type,
425  int device_id,
426  TVMArrayHandle* out);
427 
433 TVM_DLL int TVMArrayFree(TVMArrayHandle handle);
434 
442 TVM_DLL int TVMArrayCopyFromBytes(TVMArrayHandle handle,
443  void* data,
444  size_t nbytes);
445 
453 TVM_DLL int TVMArrayCopyToBytes(TVMArrayHandle handle,
454  void* data,
455  size_t nbytes);
456 
464 TVM_DLL int TVMArrayCopyFromTo(TVMArrayHandle from,
465  TVMArrayHandle to,
466  TVMStreamHandle stream);
467 
476  TVMArrayHandle* out);
477 
485 TVM_DLL int TVMArrayToDLPack(TVMArrayHandle from,
486  DLManagedTensor** out);
487 
493 
502 TVM_DLL int TVMStreamCreate(int device_type, int device_id, TVMStreamHandle* out);
503 
512 TVM_DLL int TVMStreamFree(int device_type, int device_id, TVMStreamHandle stream);
513 
525 TVM_DLL int TVMSetStream(int device_type, int device_id, TVMStreamHandle handle);
526 
535 TVM_DLL int TVMSynchronize(int device_type, int device_id, TVMStreamHandle stream);
536 
546 TVM_DLL int TVMStreamStreamSynchronize(int device_type,
547  int device_id,
548  TVMStreamHandle src,
549  TVMStreamHandle dst);
550 
558 TVM_DLL int TVMGetObjectTag(TVMObjectHandle obj, int* tag);
559 
560 #ifdef __cplusplus
561 } // TVM_EXTERN_C
562 #endif
563 #endif // TVM_RUNTIME_C_RUNTIME_API_H_
TVM_DLL const char * TVMGetLastError(void)
return str message of the last error all function in this file will return 0 when success and -1 when...
int64_t v_int64
DLDataType TVMType
The data type used in TVM Runtime.
int(* TVMPackedCFunc)(TVMValue *args, int *type_codes, int num_args, TVMRetValueHandle ret, void *resource_handle)
C type of packed function.
The common header of DLPack.
TVM_DLL int TVMModGetFunction(TVMModuleHandle mod, const char *func_name, int query_imports, TVMFunctionHandle *out)
Get function from the module.
void * TVMModuleHandle
Handle to TVM runtime modules.
TVMArray * TVMArrayHandle
the array handle
void * v_handle
TVM_DLL int TVMFuncRegisterGlobal(const char *name, TVMFunctionHandle f, int override)
Register the function to runtime&#39;s global table.
TVM_DLL int TVMStreamCreate(int device_type, int device_id, TVMStreamHandle *out)
Create a new runtime stream.
A Device context for Tensor and operator.
Definition: dlpack.h:69
TVMType v_type
TVM_DLL int TVMFuncCreateFromCFunc(TVMPackedCFunc func, void *resource_handle, TVMPackedCFuncFinalizer fin, TVMFunctionHandle *out)
Wrap a TVMPackedCFunc to become a FunctionHandle.
TVM_DLL int TVMArrayFromDLPack(DLManagedTensor *from, TVMArrayHandle *out)
Produce an array from the DLManagedTensor that shares data memory with the DLManagedTensor.
const char * v_str
TVM_DLL int TVMFuncListGlobalNames(int *out_size, const char ***out_array)
List all the globally registered function name.
TVM_DLL int TVMModImport(TVMModuleHandle mod, TVMModuleHandle dep)
Add dep to mod&#39;s dependency. This allows functions in this module to use modules. ...
Union type of values being passed through API and function calls.
TVM_DLL int TVMCFuncSetReturn(TVMRetValueHandle ret, TVMValue *value, int *type_code, int num_ret)
Set the return value of TVMPackedCFunc.
TVM_DLL int TVMModFree(TVMModuleHandle mod)
Free the Module.
TVM_DLL int TVMArrayCopyToBytes(TVMArrayHandle handle, void *data, size_t nbytes)
Copy array data to CPU byte array.
TVMContext v_ctx
const char * data
Byte array type used to pass in byte array When kBytes is used as data type.
void * TVMStreamHandle
The stream that is specific to device can be NULL, which indicates the default one.
TVM_DLL int TVMModLoadFromFile(const char *file_name, const char *format, TVMModuleHandle *out)
Load module from file.
TVM_DLL int TVMArrayCopyFromBytes(TVMArrayHandle handle, void *data, size_t nbytes)
Copy array data from CPU byte array.
TVM_DLL 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.
TVM_DLL int TVMStreamFree(int device_type, int device_id, TVMStreamHandle stream)
Free a created stream handle.
TVMDeviceExtType
Extension device types in TVM.
Definition: c_runtime_api.h:80
DLTensor TVMArray
The tensor array stucture to TVM API.
C Tensor object, manage memory of DLTensor. This data structure is intended to faciliate the borrowin...
Definition: dlpack.h:156
TVM_DLL int TVMArrayCopyFromTo(TVMArrayHandle from, TVMArrayHandle to, TVMStreamHandle stream)
Copy the array, both from and to must be valid during the copy.
TVM_DLL int TVMArrayToDLPack(TVMArrayHandle from, DLManagedTensor **out)
Produce a DLMangedTensor from the array that shares data memory with the array.
int(* TVMExtensionFuncDeclarer)(TVMFunctionHandle register_func_handle)
Signature for extension function declarer.
TVM_DLL int TVMFuncGetGlobal(const char *name, TVMFunctionHandle *out)
Get a global function.
void * TVMRetValueHandle
Handle to hold return value.
void * TVMFunctionHandle
Handle to packed function handle.
int64_t tvm_index_t
type of array index.
Definition: c_runtime_api.h:77
TVM_DLL int TVMGetObjectTag(TVMObjectHandle obj, int *tag)
Get the tag from an object.
DLContext TVMContext
The Device information, abstract away common device types.
TVM_DLL int TVMCbArgToReturn(TVMValue *value, int code)
Inplace translate callback argument value to return value. This is only needed for non-POD arguments...
TVMTypeCode
The type code in TVMType.
Definition: c_runtime_api.h:92
void * TVMObjectHandle
Handle to Object.
#define TVM_DLL
Definition: c_runtime_api.h:59
TVM_DLL int TVMFuncFree(TVMFunctionHandle func)
Free the function when it is no longer needed.
TVM_DLL int TVMExtTypeFree(void *handle, int type_code)
Free front-end extension type resource.
TVM_DLL int TVMStreamStreamSynchronize(int device_type, int device_id, TVMStreamHandle src, TVMStreamHandle dst)
Synchronize two streams of execution.
TVM_DLL int TVMSynchronize(int device_type, int device_id, TVMStreamHandle stream)
Wait until all computations on stream completes.
The data type the tensor can hold.
Definition: dlpack.h:93
TVM_DLL void TVMAPISetLastError(const char *msg)
Used for implementing C API function. Set last error message before return.
Plain C Tensor object, does not manage memory.
Definition: dlpack.h:111
TVM_DLL 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.
TVM_DLL 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...
TVM_DLL void TVMDLManagedTensorCallDeleter(DLManagedTensor *dltensor)
Delete (free) a DLManagedTensor&#39;s data.
double v_float64
TVM_DLL int TVMArrayFree(TVMArrayHandle handle)
Free the TVM Array.
void(* TVMPackedCFuncFinalizer)(void *resource_handle)
C callback to free the resource handle in C packed function.

Generated on Mon Feb 12 2024 13:02:50 for PandA-2024.02 by doxygen 1.8.13