PandA-2024.02
Data Structures | Macros | Typedefs | Enumerations | Functions
c_runtime_api.h File Reference
#include <dlpack/dlpack.h>
#include <stdint.h>
#include <stddef.h>
Include dependency graph for c_runtime_api.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

union  TVMValue
 Union type of values being passed through API and function calls. More...
 
struct  TVMByteArray
 Byte array type used to pass in byte array When kBytes is used as data type. More...
 

Macros

#define TVM_WEAK   __attribute__((weak))
 
#define TVM_DLL   __attribute__((visibility("default")))
 
#define TVM_VERSION   "0.6.dev"
 

Typedefs

typedef int64_t tvm_index_t
 type of array index. More...
 
typedef DLDataType TVMType
 The data type used in TVM Runtime. More...
 
typedef DLContext TVMContext
 The Device information, abstract away common device types. More...
 
typedef DLTensor TVMArray
 The tensor array stucture to TVM API. More...
 
typedef TVMArrayTVMArrayHandle
 the array handle More...
 
typedef void * TVMModuleHandle
 Handle to TVM runtime modules. More...
 
typedef void * TVMFunctionHandle
 Handle to packed function handle. More...
 
typedef void * TVMRetValueHandle
 Handle to hold return value. More...
 
typedef void * TVMStreamHandle
 The stream that is specific to device can be NULL, which indicates the default one. More...
 
typedef void * TVMObjectHandle
 Handle to Object. More...
 
typedef int(* TVMPackedCFunc) (TVMValue *args, int *type_codes, int num_args, TVMRetValueHandle ret, void *resource_handle)
 C type of packed function. More...
 
typedef void(* TVMPackedCFuncFinalizer) (void *resource_handle)
 C callback to free the resource handle in C packed function. More...
 
typedef int(* TVMExtensionFuncDeclarer) (TVMFunctionHandle register_func_handle)
 Signature for extension function declarer. More...
 

Enumerations

enum  TVMDeviceExtType { kDLAOCL = 5, kDLSDAccel = 6, kOpenGL = 11, kDLMicroDev = 13 }
 Extension device types in TVM. More...
 
enum  TVMTypeCode {
  kHandle = 3U, kNull = 4U, kTVMType = 5U, kTVMContext = 6U,
  kArrayHandle = 7U, kNodeHandle = 8U, kModuleHandle = 9U, kFuncHandle = 10U,
  kStr = 11U, kBytes = 12U, kNDArrayContainer = 13U, kObjectCell = 14U,
  kExtBegin = 15U, kNNVMFirst = 16U, kNNVMLast = 20U, kExtReserveEnd = 64U,
  kExtEnd = 128U, kCustomBegin = 129U
}
 The type code in TVMType. More...
 

Functions

TVM_DLL void TVMAPISetLastError (const char *msg)
 Used for implementing C API function. Set last error message before return. More...
 
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 an error occurred, TVMGetLastError can be called to retrieve the error More...
 
TVM_DLL int TVMModLoadFromFile (const char *file_name, const char *format, TVMModuleHandle *out)
 Load module from file. More...
 
TVM_DLL int TVMModImport (TVMModuleHandle mod, TVMModuleHandle dep)
 Add dep to mod's dependency. This allows functions in this module to use modules. More...
 
TVM_DLL int TVMModGetFunction (TVMModuleHandle mod, const char *func_name, int query_imports, TVMFunctionHandle *out)
 Get function from the module. More...
 
TVM_DLL int TVMExtTypeFree (void *handle, int type_code)
 Free front-end extension type resource. More...
 
TVM_DLL int TVMModFree (TVMModuleHandle mod)
 Free the Module. More...
 
TVM_DLL int TVMFuncFree (TVMFunctionHandle func)
 Free the function when it is no longer needed. More...
 
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. More...
 
TVM_DLL int TVMCFuncSetReturn (TVMRetValueHandle ret, TVMValue *value, int *type_code, int num_ret)
 Set the return value of TVMPackedCFunc. More...
 
TVM_DLL int TVMCbArgToReturn (TVMValue *value, int code)
 Inplace translate callback argument value to return value. This is only needed for non-POD arguments. More...
 
TVM_DLL int TVMFuncCreateFromCFunc (TVMPackedCFunc func, void *resource_handle, TVMPackedCFuncFinalizer fin, TVMFunctionHandle *out)
 Wrap a TVMPackedCFunc to become a FunctionHandle. More...
 
TVM_DLL int TVMFuncRegisterGlobal (const char *name, TVMFunctionHandle f, int override)
 Register the function to runtime's global table. More...
 
TVM_DLL int TVMFuncGetGlobal (const char *name, TVMFunctionHandle *out)
 Get a global function. More...
 
TVM_DLL int TVMFuncListGlobalNames (int *out_size, const char ***out_array)
 List all the globally registered function name. More...
 
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's memory, including space of shape, of given spec. More...
 
TVM_DLL int TVMArrayFree (TVMArrayHandle handle)
 Free the TVM Array. More...
 
TVM_DLL int TVMArrayCopyFromBytes (TVMArrayHandle handle, void *data, size_t nbytes)
 Copy array data from CPU byte array. More...
 
TVM_DLL int TVMArrayCopyToBytes (TVMArrayHandle handle, void *data, size_t nbytes)
 Copy array data to CPU byte array. More...
 
TVM_DLL int TVMArrayCopyFromTo (TVMArrayHandle from, TVMArrayHandle to, TVMStreamHandle stream)
 Copy the array, both from and to must be valid during the copy. More...
 
TVM_DLL int TVMArrayFromDLPack (DLManagedTensor *from, TVMArrayHandle *out)
 Produce an array from the DLManagedTensor that shares data memory with the DLManagedTensor. More...
 
TVM_DLL int TVMArrayToDLPack (TVMArrayHandle from, DLManagedTensor **out)
 Produce a DLMangedTensor from the array that shares data memory with the array. More...
 
TVM_DLL void TVMDLManagedTensorCallDeleter (DLManagedTensor *dltensor)
 Delete (free) a DLManagedTensor's data. More...
 
TVM_DLL int TVMStreamCreate (int device_type, int device_id, TVMStreamHandle *out)
 Create a new runtime stream. More...
 
TVM_DLL int TVMStreamFree (int device_type, int device_id, TVMStreamHandle stream)
 Free a created stream handle. More...
 
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 will use the setted stream handle. The specific type of stream is runtime device dependent. More...
 
TVM_DLL int TVMSynchronize (int device_type, int device_id, TVMStreamHandle stream)
 Wait until all computations on stream completes. More...
 
TVM_DLL int TVMStreamStreamSynchronize (int device_type, int device_id, TVMStreamHandle src, TVMStreamHandle dst)
 Synchronize two streams of execution. More...
 
TVM_DLL int TVMGetObjectTag (TVMObjectHandle obj, int *tag)
 Get the tag from an object. More...
 

Macro Definition Documentation

◆ TVM_DLL

#define TVM_DLL   __attribute__((visibility("default")))

Definition at line 59 of file c_runtime_api.h.

◆ TVM_VERSION

#define TVM_VERSION   "0.6.dev"

Definition at line 64 of file c_runtime_api.h.

◆ TVM_WEAK

#define TVM_WEAK   __attribute__((weak))

Definition at line 43 of file c_runtime_api.h.

Typedef Documentation

◆ tvm_index_t

typedef int64_t tvm_index_t

type of array index.

Definition at line 77 of file c_runtime_api.h.

◆ TVMArray

typedef DLTensor TVMArray

The tensor array stucture to TVM API.

Definition at line 142 of file c_runtime_api.h.

◆ TVMArrayHandle

the array handle

Definition at line 145 of file c_runtime_api.h.

◆ TVMContext

The Device information, abstract away common device types.

Definition at line 137 of file c_runtime_api.h.

◆ TVMExtensionFuncDeclarer

typedef int(* TVMExtensionFuncDeclarer) (TVMFunctionHandle register_func_handle)

Signature for extension function declarer.

TVM call this function to get the extension functions The declarer will call register_func to register function and their name.

Parameters
register_func_handleThe register function
Returns
0 if success, -1 if failure happens

Definition at line 354 of file c_runtime_api.h.

◆ TVMFunctionHandle

typedef void* TVMFunctionHandle

Handle to packed function handle.

Definition at line 172 of file c_runtime_api.h.

◆ TVMModuleHandle

typedef void* TVMModuleHandle

Handle to TVM runtime modules.

Definition at line 170 of file c_runtime_api.h.

◆ TVMObjectHandle

typedef void* TVMObjectHandle

Handle to Object.

Definition at line 181 of file c_runtime_api.h.

◆ TVMPackedCFunc

typedef int(* TVMPackedCFunc) (TVMValue *args, int *type_codes, int num_args, TVMRetValueHandle ret, void *resource_handle)

C type of packed function.

Parameters
argsThe arguments
type_codesThe type codes of the arguments
num_argsNumber of arguments.
retThe return value handle.
resource_handleThe handle additional resouce handle from fron-end.
Returns
0 if success, -1 if failure happens, set error via TVMAPISetLastError.
See also
TVMCFuncSetReturn

Definition at line 332 of file c_runtime_api.h.

◆ TVMPackedCFuncFinalizer

typedef void(* TVMPackedCFuncFinalizer) (void *resource_handle)

C callback to free the resource handle in C packed function.

Parameters
resource_handleThe handle additional resouce handle from fron-end.

Definition at line 343 of file c_runtime_api.h.

◆ TVMRetValueHandle

typedef void* TVMRetValueHandle

Handle to hold return value.

Definition at line 174 of file c_runtime_api.h.

◆ TVMStreamHandle

typedef void* TVMStreamHandle

The stream that is specific to device can be NULL, which indicates the default one.

Definition at line 179 of file c_runtime_api.h.

◆ TVMType

The data type used in TVM Runtime.

Examples

  • float: type_code = 2, bits = 32, lanes=1
  • float4(vectorized 4 float): type_code = 2, bits = 32, lanes=4
  • int8: type_code = 0, bits = 8, lanes=1
Note
Arguments TVM API function always takes bits=64 and lanes=1

Definition at line 132 of file c_runtime_api.h.

Enumeration Type Documentation

◆ TVMDeviceExtType

Extension device types in TVM.

Enumerator
kDLAOCL 
kDLSDAccel 
kOpenGL 
kDLMicroDev 

Definition at line 80 of file c_runtime_api.h.

◆ TVMTypeCode

The type code in TVMType.

Note
TVMType is used in two places.
Enumerator
kHandle 
kNull 
kTVMType 
kTVMContext 
kArrayHandle 
kNodeHandle 
kModuleHandle 
kFuncHandle 
kStr 
kBytes 
kNDArrayContainer 
kObjectCell 
kExtBegin 
kNNVMFirst 
kNNVMLast 
kExtReserveEnd 
kExtEnd 
kCustomBegin 

Definition at line 92 of file c_runtime_api.h.

Function Documentation

◆ TVMAPISetLastError()

TVM_DLL void TVMAPISetLastError ( const char *  msg)

Used for implementing C API function. Set last error message before return.

Parameters
msgThe error message to be set.

◆ TVMArrayAlloc()

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's memory, including space of shape, of given spec.

Parameters
shapeThe shape of the array, the data content will be copied to out
ndimThe number of dimension of the array.
dtype_codeThe type code of the dtype
dtype_bitsThe number of bits of dtype
dtype_lanesThe number of lanes in the dtype.
device_typeThe device type of context
device_idThe device id of context.
outThe output handle.
Returns
0 when success, -1 when failure happens

◆ TVMArrayCopyFromBytes()

TVM_DLL int TVMArrayCopyFromBytes ( TVMArrayHandle  handle,
void *  data,
size_t  nbytes 
)

Copy array data from CPU byte array.

Parameters
handleThe array handle.
datathe data pointer
nbytesThe number of bytes to copy.
Returns
0 when success, -1 when failure happens

◆ TVMArrayCopyFromTo()

TVM_DLL int TVMArrayCopyFromTo ( TVMArrayHandle  from,
TVMArrayHandle  to,
TVMStreamHandle  stream 
)

Copy the array, both from and to must be valid during the copy.

Parameters
fromThe array to be copied from.
toThe target space.
streamThe stream where the copy happens, can be NULL.
Returns
0 when success, -1 when failure happens

◆ TVMArrayCopyToBytes()

TVM_DLL int TVMArrayCopyToBytes ( TVMArrayHandle  handle,
void *  data,
size_t  nbytes 
)

Copy array data to CPU byte array.

Parameters
handleThe array handle.
datathe data pointer
nbytesThe number of bytes to copy.
Returns
0 when success, -1 when failure happens

◆ TVMArrayFree()

TVM_DLL int TVMArrayFree ( TVMArrayHandle  handle)

Free the TVM Array.

Parameters
handleThe array handle to be freed.
Returns
0 when success, -1 when failure happens

◆ TVMArrayFromDLPack()

TVM_DLL int TVMArrayFromDLPack ( DLManagedTensor from,
TVMArrayHandle out 
)

Produce an array from the DLManagedTensor that shares data memory with the DLManagedTensor.

Parameters
fromThe source DLManagedTensor.
outThe output array handle.
Returns
0 when success, -1 when failure happens

◆ TVMArrayToDLPack()

TVM_DLL int TVMArrayToDLPack ( TVMArrayHandle  from,
DLManagedTensor **  out 
)

Produce a DLMangedTensor from the array that shares data memory with the array.

Parameters
fromThe source array.
outThe DLManagedTensor handle.
Returns
0 when success, -1 when failure happens

◆ TVMCbArgToReturn()

TVM_DLL int TVMCbArgToReturn ( TVMValue value,
int  code 
)

Inplace translate callback argument value to return value. This is only needed for non-POD arguments.

Parameters
valueThe value to be translated.
codeThe type code to be translated.
Note
This function will do a shallow copy when necessary.
Returns
0 when success, -1 when failure happens.

◆ TVMCFuncSetReturn()

TVM_DLL int TVMCFuncSetReturn ( TVMRetValueHandle  ret,
TVMValue value,
int *  type_code,
int  num_ret 
)

Set the return value of TVMPackedCFunc.

This function is called by TVMPackedCFunc to set the return value. When this function is not called, the function returns null by default.

Parameters
retThe return value handle, pass by ret in TVMPackedCFunc
valueThe value to be returned.
type_codeThe type of the value to be returned.
num_retNumber of return values, for now only 1 is supported.

◆ TVMDLManagedTensorCallDeleter()

TVM_DLL void TVMDLManagedTensorCallDeleter ( DLManagedTensor dltensor)

Delete (free) a DLManagedTensor's data.

Parameters
dltensorPointer to the DLManagedTensor.

◆ TVMExtTypeFree()

TVM_DLL int TVMExtTypeFree ( void *  handle,
int  type_code 
)

Free front-end extension type resource.

Parameters
handleThe extension handle.
type_codeThe type of of the extension type.
Returns
0 when success, -1 when failure happens

◆ TVMFuncCall()

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.

Parameters
funcnode handle of the function.
arg_valuesThe arguments
type_codesThe type codes of the arguments
num_argsNumber of arguments.
ret_valThe return value.
ret_type_codethe type code of return value.
Returns
0 when success, -1 when failure happens
Note
TVM calls always exchanges with type bits=64, lanes=1
API calls always exchanges with type bits=64, lanes=1 If API call returns container handles (e.g. FunctionHandle) these handles should be managed by the front-end. The front-end need to call free function (e.g. TVMFuncFree) to free these handles.

◆ TVMFuncCreateFromCFunc()

TVM_DLL int TVMFuncCreateFromCFunc ( TVMPackedCFunc  func,
void *  resource_handle,
TVMPackedCFuncFinalizer  fin,
TVMFunctionHandle out 
)

Wrap a TVMPackedCFunc to become a FunctionHandle.

The resource_handle will be managed by TVM API, until the function is no longer used.

Parameters
funcThe packed C function.
resource_handleThe resource handle from front-end, can be NULL.
finThe finalizer on resource handle when the FunctionHandle get freed, can be NULL
outthe result function handle.
Returns
0 when success, -1 when failure happens

◆ TVMFuncFree()

TVM_DLL int TVMFuncFree ( TVMFunctionHandle  func)

Free the function when it is no longer needed.

Parameters
funcThe function handle
Returns
0 when success, -1 when failure happens

◆ TVMFuncGetGlobal()

TVM_DLL int TVMFuncGetGlobal ( const char *  name,
TVMFunctionHandle out 
)

Get a global function.

Parameters
nameThe name of the function.
outthe result function pointer, NULL if it does not exist.
Note
The function handle of global function is managed by TVM runtime, So TVMFuncFree is should not be called when it get deleted.

◆ TVMFuncListGlobalNames()

TVM_DLL int TVMFuncListGlobalNames ( int *  out_size,
const char ***  out_array 
)

List all the globally registered function name.

Parameters
out_sizeThe number of functions
out_arrayThe array of function names.
Returns
0 when success, -1 when failure happens

◆ TVMFuncRegisterGlobal()

TVM_DLL int TVMFuncRegisterGlobal ( const char *  name,
TVMFunctionHandle  f,
int  override 
)

Register the function to runtime's global table.

The registered function then can be pulled by the backend by the name.

Parameters
nameThe name of the function.
fThe function to be registered.
overrideWhether allow override already registered function.

◆ TVMGetLastError()

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 an error occurred, TVMGetLastError can be called to retrieve the error

this function is threadsafe and can be called by different thread

Returns
error info

◆ TVMGetObjectTag()

TVM_DLL int TVMGetObjectTag ( TVMObjectHandle  obj,
int *  tag 
)

Get the tag from an object.

Parameters
objThe object handle.
tagThe tag of object.
Returns
0 when success, -1 when failure happens

◆ TVMModFree()

TVM_DLL int TVMModFree ( TVMModuleHandle  mod)

Free the Module.

Parameters
modThe module to be freed.
Note
This may not free up the module's resources. If there is active TVMFunctionHandle uses the module Or if this module is imported by another active module.

The all functions remains valid until TVMFuncFree is called.

Returns
0 when success, -1 when failure happens

◆ TVMModGetFunction()

TVM_DLL int TVMModGetFunction ( TVMModuleHandle  mod,
const char *  func_name,
int  query_imports,
TVMFunctionHandle out 
)

Get function from the module.

Parameters
modThe module handle.
func_nameThe name of the function.
query_importsWhether to query imported modules
outThe result function, can be NULL if it is not available.
Returns
0 when no error is thrown, -1 when failure happens

◆ TVMModImport()

TVM_DLL int TVMModImport ( TVMModuleHandle  mod,
TVMModuleHandle  dep 
)

Add dep to mod's dependency. This allows functions in this module to use modules.

Parameters
modThe module handle.
depThe dependent module to be imported.
Returns
0 when success, -1 when failure happens

◆ TVMModLoadFromFile()

TVM_DLL int TVMModLoadFromFile ( const char *  file_name,
const char *  format,
TVMModuleHandle out 
)

Load module from file.

Parameters
file_nameThe file name to load the module from.
formatThe format of the module.
outThe result module
Returns
0 when success, -1 when failure happens
Note
The resulting module do not contain import relation. It can be reconstructed by TVMModImport.

◆ TVMSetStream()

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 will use the setted stream handle. The specific type of stream is runtime device dependent.

Parameters
device_typeThe device type of context
device_idThe device id of context.
handleThe stream handle.
Returns
0 when success, -1 when failure happens

◆ TVMStreamCreate()

TVM_DLL int TVMStreamCreate ( int  device_type,
int  device_id,
TVMStreamHandle out 
)

Create a new runtime stream.

Parameters
device_typeThe device type of context
device_idThe device id of context
outThe new stream handle
Returns
0 when success, -1 when failure happens

◆ TVMStreamFree()

TVM_DLL int TVMStreamFree ( int  device_type,
int  device_id,
TVMStreamHandle  stream 
)

Free a created stream handle.

Parameters
device_typeThe device type of context
device_idThe device id of context
streamThe stream to be freed
Returns
0 when success, -1 when failure happens

◆ TVMStreamStreamSynchronize()

TVM_DLL int TVMStreamStreamSynchronize ( int  device_type,
int  device_id,
TVMStreamHandle  src,
TVMStreamHandle  dst 
)

Synchronize two streams of execution.

Parameters
device_typeThe device type of context
device_idThe device id of context
srcThe source stream to synchronize.
dstThe destination stream to synchronize.
Returns
0 when success, -1 when failure happens

◆ TVMSynchronize()

TVM_DLL int TVMSynchronize ( int  device_type,
int  device_id,
TVMStreamHandle  stream 
)

Wait until all computations on stream completes.

Parameters
device_typeThe device type of context
device_idThe device id of context.
streamThe stream to be synchronized.
Returns
0 when success, -1 when failure happens

Generated on Mon Feb 12 2024 13:03:11 for PandA-2024.02 by doxygen 1.8.13