Skip to content

Commit

Permalink
1. update install.md
Browse files Browse the repository at this point in the history
    2. using the NamedParam for operator param definition
       so that the parameter can be retrievaled by those APIs in applications

        get_node_param_int()/get_node_param_float()/get_node_param_generic()


Former-commit-id: 8659f3b
  • Loading branch information
cyberfire committed Jun 26, 2018
1 parent a652e79 commit 571f350
Show file tree
Hide file tree
Showing 41 changed files with 832 additions and 541 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ protobuf/
OpenBLAS/
protobuf_lib/
sysroot/
android_config.txt
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ execute_process( COMMAND git rev-parse HEAD
STRING(STRIP ${git_commit_id} stripped_commit_id)
set(GIT_COMMIT_ID -DGIT_COMMIT_ID="0x${stripped_commit_id}")

message("GIT COMMIT ID: " 0x${stripped_commit_id})

if (CONFIG_ARCH_ARM64)
add_definitions(-DCONFIG_ARCH_ARM64=1)
endif()
Expand Down
28 changes: 20 additions & 8 deletions cmake/executor.cmake
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
include_directories(executor/include executor/operator/include)

FILE(GLOB_RECURSE COMMON_LIB_CPP_SRCS executor/engine/*.cpp executor/lib/*.cpp executor/plugin/*.cpp)
FILE(GLOB COMMON_CPP_SRCS executor/operator/common/*.cpp executor/operator/common/fused/*.cpp)
if(CONFIG_ARCH_BLAS)
FILE(GLOB_RECURSE COMMON_LIB_CPP_SRCS executor/engine/*.cpp executor/lib/*.cpp executor/plugin/*.cpp executor/operator/common/*.cpp )
else()
FILE(GLOB_RECURSE COMMON_LIB_CPP_SRCS executor/engine/*.cpp executor/lib/*.cpp executor/plugin/*.cpp)
FILE(GLOB COMMON_CPP_SRCS executor/operator/common/*.cpp executor/operator/common/fused/*.cpp)
list(APPEND COMMON_LIB_CPP_SRCS ${COMMON_CPP_SRCS})
FILE(GLOB COMMON_BLAS_SRCS executor/operator/common/blas/*.cpp)
list(APPEND COMMON_CPP_SRCS ${COMMON_BLAS_SRCS})
endif()

list(APPEND TENGINE_LIB_SRCS ${COMMON_LIB_CPP_SRCS})
list(APPEND TENGINE_LIB_SRCS ${COMMON_CPP_SRCS})

include_directories(driver/cpu)

Expand All @@ -24,14 +25,25 @@ endif()

# Now, handle the .S file
if(CONFIG_ARCH_ARM64)
FILE(GLOB_RECURSE ARCH_LIB_CPP_SRCS executor/operator/arm64/*.cpp)
FILE(GLOB_RECURSE ARCH64_LIB_CPP_SRCS executor/operator/arm64/*.cpp)
include_directories(executor/operator/arm64/include)

FOREACH(file ${ARCH_LIB_CPP_SRCS})
list(APPEND TENGINE_LIB_SRCS ${file})
FOREACH(file ${ARCH64_LIB_CPP_SRCS})
set(ACL_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}/executor/operator/arm64/conv/conv_2d_acl")
STRING(REGEX MATCH ${ACL_PREFIX} skip_file2 ${file})

if( NOT skip_file2)
list(APPEND ARCH_LIB_CPP_SRCS ${file})
endif()

endforeach()
endif()


list(APPEND TENGINE_LIB_SRCS ${ARCH_LIB_CPP_SRCS})

# Now, handle the .S file

if( CONFIG_ARCH_ARM64)

set(src_path executor/operator/arm64)
Expand Down
66 changes: 65 additions & 1 deletion core/include/operator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class Operator: public BaseObject {
virtual void ParseParam(void) {};
virtual bool ParamFromStaticOp(StaticOp * s_op) {return true;}

virtual bool GetParamItem(const char * param_name, const std::type_info * type_info, void * val) { return false;}
virtual bool SetParamItem(const char * param_name, const std::type_info * type_info, const void * val) { return false;}

virtual any GetDefParam(void)
{
return any();
Expand Down Expand Up @@ -127,6 +130,7 @@ class Operator: public BaseObject {
return *this;
}


const std::string& GetDoc(void) const { return doc_;}

int GetInputNum(void) const { return inputs_.size();}
Expand Down Expand Up @@ -233,9 +237,59 @@ class OperatorWithParam: public Operator {

}

/* a complicated one, now */
void ParsePredefinedParam(P& param, Operator * op)
{
auto map=param.GetItemMap();

auto ir=map.begin();
auto end=map.end();

while(ir!=end)
{
if(!op->ExistAttr(ir->first))
{
ir++;
continue;
}

const any& data=op->GetAttr(ir->first);

if(param.SetItemFromAny(ir->first,data))
{
ir++;
continue;
}

//type mismatch
//possible reason:
// 1. require float, while input is int
// 2. require const char *, while input is std::string

// otherwise, failed

const std::type_info & data_type=data.type();

if(data_type==typeid(std::string))
{
const std::string& str=any_cast<std::string>(data);

param.SetItemVal(ir->first,&typeid(const char *),str.c_str());
}
else if(data_type==typeid(int))
{
float f=(float)any_cast<int>(data);
param.SetItemVal(ir->first,&typeid(float),&f);
}

ir++;
}
}


virtual void ParseParam(P& param, Operator * op)
{
P::Parse(param,op);
ParsePredefinedParam(param,op);
}


Expand All @@ -255,6 +309,16 @@ class OperatorWithParam: public Operator {
return param;
}

bool GetParamItem(const char * param_name, const std::type_info * type_info, void * val) override
{
return param_.GetItemVal(param_name,type_info,val);
}

bool SetParamItem(const char * param_name, const std::type_info * type_info, const void * val) override
{
return param_.SetItemVal(param_name,type_info,val);
}

protected:

P param_;
Expand Down
159 changes: 123 additions & 36 deletions core/include/parameter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,46 +26,133 @@


#include <functional>
#include <unordered_map>

#include "base_object.hpp"
#include "any.hpp"

namespace TEngine {


using entry_parser_t=std::function<void(BaseObject&)>;

template <typename T>
bool ConvertSpecialAny(T& entry, const std::type_info & info, any& data);


#define DECLARE_PARSER_STRUCTURE(param) \
static void Parse(param& param_obj, BaseObject * p_obj)\


#define DECLARE_PARSER_ENTRY(entry) \
{\
typedef decltype(param_obj.entry) type0;\
any& content=(*p_obj)[#entry];\
if(typeid(type0)== content.type()) \
param_obj.entry=any_cast<type0>(content); \
else\
{\
if(!ConvertSpecialAny(param_obj.entry,content.type(),content))\
std::cerr<<"cannot parser entry: "<<#entry<<std::endl;\
}\
}

#define DECLARE_CUSTOM_PARSER_ENTRY(entry) \
{\
typedef decltype(param_obj.entry) type0;\
any& content=(*p_obj)[#entry];\
if(typeid(type0)== content.type()) \
param_obj.entry=any_cast<type0>(content); \
else\
{\
std::cerr<<"cannot parser entry: "<<#entry<<std::endl;\
}\
}
struct NamedParam {

using item_cpy_t=void(*)(void *,const void *);
using item_set_any=void (*)(void *,const any &);

struct ItemInfo {
item_cpy_t cpy_func;
item_set_any cpy_any;
const std::type_info * type_info;
int data;
};

ItemInfo * FindItem(const std::string& name, const std::type_info * item_type)
{
if(item_map_.count(name)==0)
return nullptr;

ItemInfo& entry=item_map_.at(name);

if(*item_type!= *entry.type_info)
{
//printf("requested: %s recorded:%s\n",item_type->name(),entry.type_info->name());
return nullptr;
}

return &entry;
}

bool GetItemVal(const std::string& name, const std::type_info * val_type, void * val)
{
ItemInfo * entry=FindItem(name,val_type);

if(entry==nullptr)
return false;

entry->cpy_func(val,(char *)this+entry->data);

return true;
}

bool SetItemVal(const std::string& name, const std::type_info * val_type, const void * val)
{
ItemInfo * entry=FindItem(name,val_type);

if(entry==nullptr)
return false;


entry->cpy_func((char *)this+entry->data,val);

return true;
}

bool SetItemCompatibleAny(const std::string& name, const any& n)
{
if(item_map_.count(name)==0)
return false;

ItemInfo& entry=item_map_.at(name);
const std::type_info * item_type=entry.type_info;
const std::type_info& any_type=n.type();

/* several special cases */
if(*item_type== typeid(const char *) && any_type==typeid(std::string))
{
const char ** ptr=(const char **)((char *) this+entry.data);
const std::string& str=any_cast<std::string>(n);

ptr[0]=str.c_str(); //unsafe, since any may be destroyed soon

return true;
}

if(*item_type==typeid(std::string) && any_type==typeid(const char *))
{
std::string * p_str=(std::string*)((char *)this+entry.data);
const char * ptr=any_cast<const char *>(n);

*p_str=ptr;

return true;
}

return false;

}

bool SetItemFromAny(const std::string& name, const any& n)
{

ItemInfo * entry=FindItem(name,&n.type());

if(entry==nullptr)
return SetItemCompatibleAny(name,n);

entry->cpy_any((char*)this+entry->data,n);

return true;
}

const std::unordered_map<std::string,ItemInfo> & GetItemMap(void) { return item_map_;}


protected:
std::unordered_map<std::string,ItemInfo> item_map_;
};


#define DECLARE_PARSER_STRUCTURE(s) \
s(void)

#define DECLARE_PARSER_ENTRY(e) \
{\
typedef decltype(e) T ;\
ItemInfo info; \
info.type_info=&typeid(T);\
info.data=(char*)&e -(char *)this; \
info.cpy_func=[](void * data, const void * v){ *(T*)data=*(const T*)v;}; \
info.cpy_any=[](void * data, const any& n){ *(T*)data=any_cast<T>(n);};\
item_map_[# e]=info; \
}



Expand Down
46 changes: 46 additions & 0 deletions core/include/tengine_c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,52 @@ const char * get_tensor_name(tensor_t tensor);
*/
node_t get_graph_node(graph_t graph, const char * node_name);


/*!
* @brief get the param value (int) of a node
*
* @param node, the target node
* @param param_name, the name of the param to be retrieval
* @param param_val, pointer to the int val to be saved
*
* @return 0, retrieval value successfully;
* <0, failed; probably the name does not exist or the type mismatch
*/

int get_node_param_int(node_t node, const char * param_name, int * param_val);

/*!
* @brief get the param value (float) of a node
*
* @param node, the target node
* @param param_name, the name of the param to be retrieval
* @param param_val, pointer to the float val to be saved
*
* @return 0, retrieval value successfully;
* <0, failed; probably the name does not exist or the type mismatch
*/

int get_node_param_float(node_t node, const char * param_name, float * param_val);

/*!
* @brief get the param value of a node, the data type is indicated by type_info
* this interface only works in c++, as type_info refers std::type_info
*
* @param node, the target node
* @param param_name, the name of the param to be retrieval
* @param type_info, pointer to the std::type_info of wanted type
* @param param_val, pointer to the float val to be saved
*
* @return 0, retrieval value successfully;
* <0, failed; probably the name does not exist or the type mismatch
*/

int get_node_param_generic(node_t node, const char * param_name, const void * type_info, void * param_val);

int set_node_param_int(node_t node, const char * param_name, const int * param_val);
int set_node_param_float(node_t node, const char * param_name, const float * param_val);
int set_node_param_generic(node_t node, const char * param_name, const void * type_info, const void * param_val);

/*!
* @brief initialize resource for graph execution
*
Expand Down
4 changes: 4 additions & 0 deletions core/include/tengine_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@

namespace TEngine {

template <typename T>
bool ConvertSpecialAny(T& entry, const std::type_info & info, any& data);


struct TEngineConfig
{
static bool tengine_mt_mode; // multithread mode
Expand Down
Loading

0 comments on commit 571f350

Please sign in to comment.