Skip to content

Commit

Permalink
[RUNTIME] Add Function, Unify TVMTypeCode and TVMArgTypeID
Browse files Browse the repository at this point in the history
  • Loading branch information
tqchen committed Jan 24, 2017
1 parent 4f1473f commit 3c49a68
Show file tree
Hide file tree
Showing 29 changed files with 684 additions and 359 deletions.
60 changes: 35 additions & 25 deletions include/tvm/c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,30 @@
#ifndef TVM_C_API_H_
#define TVM_C_API_H_

#include "./c_runtime_api.h"
#include "./runtime/c_runtime_api.h"

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

/*!
* \brief List all the node function name
* \param out_size The number of functions
* \param out_array The array of function names.
* \return 0 when success, -1 when failure happens
*/
TVM_DLL int TVMListAPIFunctionNames(int *out_size,
const char*** out_array);
TVM_DLL int TVMListAPIFuncNames(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
* \return 0 when success, -1 when failure happens
*/
TVM_DLL int TVMGetAPIFunctionHandle(const char* name,
APIFunctionHandle *handle);
TVM_DLL int TVMGetAPIFuncHandle(const char* name,
APIFuncHandle *handle);

/*!
* \brief Get the detailed information about function.
Expand All @@ -42,40 +44,45 @@ TVM_DLL int TVMGetAPIFunctionHandle(const char* name,
* \param return_type Return type of the function, if any.
* \return 0 when success, -1 when failure happens
*/
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);
TVM_DLL int TVMGetAPIFuncInfo(APIFuncHandle 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.
* If push fails, the stack will be reset to empty
*
* \param arg number of attributes
* \param type_id The typeid of attributes.
* \param arg The argument
* \param type_code The type_code of argument as in TVMTypeCode
* \return 0 when success, -1 when failure happens
* \note API calls always exchanges with type bits=64, lanes=1
*/
TVM_DLL int TVMAPIPushStack(TVMArg arg,
int type_id);
TVM_DLL int TVMAPIPushStack(TVMValue arg,
int type_code);

/*!
* \brief call a function by using arguments in the stack.
* The stack will be cleanup to empty after this call, whether the call is successful.
*
* \param handle The function handle
* \param ret_val The return value.
* \param ret_typeid the type id of return value.
* \param ret_type_code the type code of return value.
* \return 0 when success, -1 when failure happens
* \note API calls always exchanges with type bits=64, lanes=1
*/
TVM_DLL int TVMAPIFunctionCall(APIFunctionHandle handle,
TVMArg* ret_val,
int* ret_typeid);
TVM_DLL int TVMAPIFuncCall(APIFuncHandle handle,
TVMValue* ret_val,
int* ret_type_code);

/*!
* \brief free the node handle
* \param handle The node handle to be freed.
* \return 0 when success, -1 when failure happens
*/
TVM_DLL int TVMNodeFree(NodeHandle handle);

Expand All @@ -84,20 +91,23 @@ TVM_DLL int TVMNodeFree(NodeHandle handle);
* \param handle The node handle
* \param key The attribute name
* \param out_value The attribute value
* \param out_typeid The typeid of the attribute.
* \param out_type_code The type code of the attribute.
* \param out_success Whether get is successful.
* \return 0 when success, -1 when failure happens
* \note API calls always exchanges with type bits=64, lanes=1
*/
TVM_DLL int TVMNodeGetAttr(NodeHandle handle,
const char* key,
TVMArg* out_value,
int* out_typeid,
TVMValue* out_value,
int* out_type_code,
int* out_success);

/*!
* \brief get attributes names in the node.
* \param handle The node handle
* \param out_size The number of functions
* \param out_array The array of function names.
* \return 0 when success, -1 when failure happens
*/
TVM_DLL int TVMNodeListAttrNames(NodeHandle handle,
int *out_size,
Expand Down
5 changes: 5 additions & 0 deletions include/tvm/codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "./base.h"
#include "./expr.h"
#include "./module.h"
#include "./runtime/runtime.h"


namespace tvm {
/*! \brief namespace for lowlevel IR pass and codegen */
Expand Down Expand Up @@ -62,6 +64,9 @@ Array<Var> UndefinedVars(const LoweredFunc& f);
*/
Array<LoweredFunc> SplitHostDevice(LoweredFunc func);


runtime::PackedFunc BuildStackVM(LoweredFunc func);

} // namespace codegen
} // namespace tvm

Expand Down
8 changes: 8 additions & 0 deletions include/tvm/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ constexpr const char* tvm_array_get_field = "tvm_array_get_field";
* }
*/
constexpr const char* tvm_handle_is_null = "tvm_handle_is_null";
/*!
* \brief See pesudo code
*
* bool tvm_print(VType value) {
* LOG(INFO) << value;
* }
*/
constexpr const char* tvm_print = "tvm_print";

/*! \brief The field id of each field in array */
enum TVMArrayFieldKind {
Expand Down
129 changes: 56 additions & 73 deletions include/tvm/c_runtime_api.h → include/tvm/runtime/c_runtime_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
* 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_
#ifndef TVM_RUNTIME_C_RUNTIME_API_H_
#define TVM_RUNTIME_C_RUNTIME_API_H_

#ifdef __cplusplus
#define TVM_EXTERN_C extern "C"
Expand Down Expand Up @@ -38,27 +38,51 @@ TVM_EXTERN_C {
typedef uint32_t tvm_index_t;

/*!
* \brief union type for arguments and return values
* in both runtime API and TVM API calls
* \brief Union type of values
* being passed through API and function calls.
*/
typedef union {
long v_long; // NOLINT(*)
double v_double;
const char* v_str;
int64_t v_int64;
double v_float64;
void* v_handle;
} TVMArg;
const char* v_str;
} TVMValue;

/*!
* \brief The type index in TVM.
* \brief The type code in TVMType
* \note TVMType is used in two places.
*/
typedef enum {
kNull = 0,
kLong = 1,
kDouble = 2,
kStr = 3,
kNodeHandle = 4,
kArrayHandle = 5
} TVMArgTypeID;
kInt = 0U,
kUInt = 1U,
kFloat = 2U,
kHandle = 3U,
// The next few fields are extension types
// that is used by TVM API calls.
kNull = 4U,
kNodeHandle = 5U,
kStr = 6U,
kFuncHandle = 7U
} TVMTypeCode;

/*!
* \brief 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
*/
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;
} TVMType;

/*!
* \brief The device type
Expand All @@ -82,29 +106,6 @@ typedef struct {
int dev_id;
} TVMContext;

/*! \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.
Expand All @@ -122,7 +123,7 @@ typedef struct {
/*! \brief number of dimensions of the array */
tvm_index_t ndim;
/*! \brief The data type flag */
TVMDataType dtype;
TVMType dtype;
/*! \brief The device context this array sits on */
TVMContext ctx;
} TVMArray;
Expand Down Expand Up @@ -191,7 +192,7 @@ TVM_DLL int TVMContextEnabled(TVMContext ctx,
*/
TVM_DLL int TVMArrayAlloc(const tvm_index_t* shape,
tvm_index_t ndim,
TVMDataType dtype,
TVMType dtype,
TVMContext ctx,
TVMArrayHandle* out);
/*!
Expand All @@ -217,45 +218,27 @@ TVM_DLL int TVMArrayCopyFromTo(TVMArrayHandle from,
TVM_DLL int TVMSynchronize(TVMContext ctx, TVMStreamHandle stream);

/*!
* \brief TVM Function API: Get resource requirement
*
* By default TVM function try not to do internal allocations.
* Instead, TVMFuncRequirement can be called, given the input arguments.
*
* \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 out_workspace_size The workspace size needed to launch this function.
* \param out_workspace_align The alignment requirement of workspace.
*
* \note The data pointer in the arrays is not used by requirement.
* \brief Free the function when it is no longer needed.
* \param func The function handle
* \return whether
*/
TVM_DLL int TVMFuncRequirement(TVMFunctionHandle func,
TVMArg* args,
int* arg_type_ids,
int num_args,
size_t* out_workspace_size,
size_t* out_workspace_align);
TVM_DLL int TVMFuncFree(TVMFunctionHandle func);

/*!
* \brief TVM Function API: Launch generated function.
* \brief Call a function whose parameters are all packed.
*
* \param func function handle to be launched.
* \param func node handle of the function.
* \param args The arguments
* \param arg_type_ids The type id of the arguments
* \param type_codes The type codes of the arguments
* \param num_args Number of arguments.
* \param stream The stream this function to be launched on.
* \param workspace Additional workspace used to launch this function.
*
* \sa TVMFuncRequirement
* \return 0 when success, -1 when failure happens
* \note TVM calls always exchanges with type bits=64, lanes=1
*/
TVM_DLL int TVMFuncLaunch(TVMFunctionHandle func,
TVMArg* args,
int* arg_type_ids,
int num_args,
TVMStreamHandle stream,
TVMArrayHandle workspace);
TVM_DLL int TVMFuncCall(TVMFunctionHandle func,
TVMValue* args,
int* type_codes,
int num_args);
} // TVM_EXTERN_C

#endif // TVM_C_RUNTIME_API_H_
#endif // TVM_RUNTIME_C_RUNTIME_API_H_
Loading

0 comments on commit 3c49a68

Please sign in to comment.