Skip to content

Commit

Permalink
[RUNTIME] Add interface header of runtime (#15)
Browse files Browse the repository at this point in the history
* [RUNTIME] Add interface header of runtime

* fix mac build
  • Loading branch information
tqchen authored Jan 16, 2017
1 parent 7f82912 commit 7e02523
Show file tree
Hide file tree
Showing 12 changed files with 328 additions and 141 deletions.
2 changes: 1 addition & 1 deletion HalideIR
Submodule HalideIR updated from 327810 to 6375e6
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export LDFLAGS = -pthread -lm
export CFLAGS = -std=c++11 -Wall -O2 -Wno-unknown-pragmas -funroll-loops\
export CFLAGS = -std=c++11 -Wall -O2\
-Iinclude -Idmlc-core/include -IHalideIR/src -fPIC

# specify tensor path
Expand Down
84 changes: 19 additions & 65 deletions include/tvm/c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,74 +6,28 @@
#ifndef TVM_C_API_H_
#define TVM_C_API_H_

#ifdef __cplusplus
#define TVM_EXTERN_C extern "C"
#else
#define TVM_EXTERN_C
#endif

/*! \brief TVM_DLL prefix for windows */
#ifdef _WIN32
#ifdef TVM_EXPORTS
#define TVM_DLL __declspec(dllexport)
#else
#define TVM_DLL __declspec(dllimport)
#endif
#else
#define TVM_DLL
#endif
#include "./c_runtime_api.h"

TVM_EXTERN_C {
/*! \brief handle to functions */
typedef void* FunctionHandle;
typedef void* APIFunctionHandle;
/*! \brief handle to node */
typedef void* NodeHandle;

/*!
* \brief union type for returning value of attributes
* Attribute type can be identified by id
*/
typedef union {
long v_long; // NOLINT(*)
double v_double;
const char* v_str;
NodeHandle v_handle;
} ArgVariant;

/*! \brief attribute types */
typedef enum {
kNull = 0,
kLong = 1,
kDouble = 2,
kStr = 3,
kNodeHandle = 4
} ArgVariantID;

/*!
* \brief return str message of the last error
* all function in this file will return 0 when success
* and -1 when an error occured,
* NNGetLastError can be called to retrieve the error
*
* this function is threadsafe and can be called by different thread
* \return error info
*/
TVM_DLL const char *TVMGetLastError(void);

/*!
* \brief List all the node function name
* \param out_size The number of functions
* \param out_array The array of function names.
*/
TVM_DLL int TVMListFunctionNames(int *out_size,
TVM_DLL int TVMListAPIFunctionNames(int *out_size,
const char*** out_array);
/*!
* \brief get function handle by name
* \param name The name of function
* \param handle The returning function handle
*/
TVM_DLL int TVMGetFunctionHandle(const char* name,
FunctionHandle *handle);
TVM_DLL int TVMGetAPIFunctionHandle(const char* name,
APIFunctionHandle *handle);

/*!
* \brief Get the detailed information about function.
Expand All @@ -88,14 +42,14 @@ TVM_DLL int TVMGetFunctionHandle(const char* name,
* \param return_type Return type of the function, if any.
* \return 0 when success, -1 when failure happens
*/
TVM_DLL int TVMGetFunctionInfo(FunctionHandle handle,
const char **real_name,
const char **description,
int *num_doc_args,
const char ***arg_names,
const char ***arg_type_infos,
const char ***arg_descriptions,
const char **return_type);
TVM_DLL int TVMGetAPIFunctionInfo(APIFunctionHandle handle,
const char **real_name,
const char **description,
int *num_doc_args,
const char ***arg_names,
const char ***arg_type_infos,
const char ***arg_descriptions,
const char **return_type);

/*!
* \brief Push an argument to the function calling stack.
Expand All @@ -104,8 +58,8 @@ TVM_DLL int TVMGetFunctionInfo(FunctionHandle handle,
* \param arg number of attributes
* \param type_id The typeid of attributes.
*/
TVM_DLL int TVMPushStack(ArgVariant arg,
int type_id);
TVM_DLL int TVMAPIPushStack(TVMArg arg,
int type_id);

/*!
* \brief call a function by using arguments in the stack.
Expand All @@ -115,9 +69,9 @@ TVM_DLL int TVMPushStack(ArgVariant arg,
* \param ret_val The return value.
* \param ret_typeid the type id of return value.
*/
TVM_DLL int TVMFunctionCall(FunctionHandle handle,
ArgVariant* ret_val,
int* ret_typeid);
TVM_DLL int TVMAPIFunctionCall(APIFunctionHandle handle,
TVMArg* ret_val,
int* ret_typeid);

/*!
* \brief free the node handle
Expand All @@ -135,7 +89,7 @@ TVM_DLL int TVMNodeFree(NodeHandle handle);
*/
TVM_DLL int TVMNodeGetAttr(NodeHandle handle,
const char* key,
ArgVariant* out_value,
TVMArg* out_value,
int* out_typeid,
int* out_success);

Expand Down
205 changes: 205 additions & 0 deletions include/tvm/c_runtime_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
/*!
* Copyright (c) 2016 by Contributors
* \file c_runtime_api.h
* \brief TVM runtime library.
*
* The philosophy of TVM project is to customize the compilation
* stage to generate code that can used by other projects transparently.
*
* So this is a minimum runtime code gluing, and some limited
* memory management code to enable quick testing.
*/
#ifndef TVM_C_RUNTIME_API_H_
#define TVM_C_RUNTIME_API_H_

#ifdef __cplusplus
#define TVM_EXTERN_C extern "C"
#else
#define TVM_EXTERN_C
#endif

/*! \brief TVM_DLL prefix for windows */
#ifdef _WIN32
#ifdef TVM_EXPORTS
#define TVM_DLL __declspec(dllexport)
#else
#define TVM_DLL __declspec(dllimport)
#endif
#else
#define TVM_DLL
#endif

#include <stdint.h>


TVM_EXTERN_C {
/*! \brief type of array index. */
typedef unsigned tvm_index_t;

/*!
* \brief union type for arguments and return values
* in both runtime API and TVM API calls
*/
typedef union {
long v_long; // NOLINT(*)
double v_double;
const char* v_str;
void* v_handle;
} TVMArg;

/*!
* \brief The type index in TVM.
*/
typedef enum {
kNull = 0,
kLong = 1,
kDouble = 2,
kStr = 3,
kNodeHandle = 4,
kArrayHandle = 5
} TVMArgTypeID;

/*!
* \brief The device type
*/
typedef enum {
/*! \brief CPU device */
kCPU = 1,
/*! \brief NVidia GPU device(CUDA) */
kGPU = 2,
/*! \brief opencl device */
KOpenCL = 4
} TVMDeviceMask;

/*!
* \brief The Device information, abstract away common device types.
*/
typedef struct {
/*! \brief The device type mask */
int dev_mask;
/*! \brief the device id */
int dev_id;
} TVMDevice;

/*! \brief The type code in TVMDataType */
typedef enum {
kInt = 0U,
kUInt = 1U,
kFloat = 2U
} TVMTypeCode;

/*!
* \brief the data type
* 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
*/
typedef struct {
/*! \brief type code, in TVMTypeCode */
uint8_t type_code;
/*! \brief number of bits of the type */
uint8_t bits;
/*! \brief number of lanes, */
uint16_t lanes;
} TVMDataType;

/*!
* \brief Data structure representing a n-dimensional array(tensor).
* This is used to pass data specification into TVM.
*/
typedef struct {
/*! \brief The data field pointer on specified device */
void* data;
/*! \brief The shape pointers of the array */
const tvm_index_t* shape;
/*!
* \brief The stride data about each dimension of the array, can be NULL
* When strides is NULL, it indicates that the array is empty.
*/
const tvm_index_t* strides;
/*! \brief number of dimensions of the array */
tvm_index_t ndim;
/*! \brief The data type flag */
TVMDataType dtype;
/*! \brief The device this array sits on */
TVMDevice device;
} TVMArray;

/*!
* \brief The stream that is specific to device
* can be NULL, which indicates the default one.
*/
typedef void* TVMStreamHandle;
/*!
* \brief Pointer to function handle that points to
* a generated TVM function.
*/
typedef void* TVMFunctionHandle;
/*! \brief the array handle */
typedef TVMArray* TVMArrayHandle;

/*!
* \brief return str message of the last error
* all function in this file will return 0 when success
* and -1 when an error occured,
* TVMGetLastError can be called to retrieve the error
*
* this function is threadsafe and can be called by different thread
* \return error info
*/
TVM_DLL const char *TVMGetLastError(void);

/*!
* \brief Allocate a nd-array's memory,
* including space of shape, of given spec.
*
* \param shape The shape of the array, the data content will be copied to out
* \param ndim The number of dimension of the array.
* \param dtype The array data type.
* \param device The device this array sits on.
* \param out The output handle.
* \return Whether the function is successful.
*/
TVM_DLL int TVMArrayAlloc(const tvm_index_t* shape,
tvm_index_t ndim,
int dtype,
TVMDevice device,
TVMArrayHandle* out);
/*!
* \brief Free the TVM Array.
* \param handle The array handle to be freed.
*/
TVM_DLL int TVMArrayFree(TVMArrayHandle handle);

/*!
* \brief Copy the array, both from and to must be valid during the copy.
* \param from The array to be copied from.
* \param to The target space.
* \param stream The stream where the copy happens, can be NULL.
*/
TVM_DLL int TVMArrayCopyFromTo(TVMArrayHandle from,
TVMArrayHandle to,
TVMStreamHandle stream);
/*!
* \brief Wait until all computations on stream completes.
* \param stream the stream to be synchronized.
*/
TVM_DLL int TVMSynchronize(TVMStreamHandle stream);

/*!
* \brief Launch a generated TVM function
* \param func function handle to be launched.
* \param args The arguments
* \param arg_type_ids The type id of the arguments
* \param num_args Number of arguments.
* \param stream The stream this function to be launched on.
*/
TVM_DLL int TVMLaunch(TVMFunctionHandle func,
TVMArg* args,
int* arg_type_ids,
int num_args,
TVMStreamHandle stream);
} // TVM_EXTERN_C

#endif // TVM_C_RUNTIME_API_H_
4 changes: 0 additions & 4 deletions python/tvm/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ def _load_lib():
# library instance of nnvm
_LIB = _load_lib()

# type definitions
FunctionHandle = ctypes.c_void_p
NodeHandle = ctypes.c_void_p

#----------------------------
# helper function definition
#----------------------------
Expand Down
Loading

0 comments on commit 7e02523

Please sign in to comment.