mxnet
Public Member Functions | Static Public Member Functions | Public Attributes | Friends | List of all members
nnvm::Op Class Reference

Operator structure. More...

#include <op.h>

Collaboration diagram for nnvm::Op:
Collaboration graph

Public Member Functions

Opdescribe (const std::string &descr)
 setter function during registration Set the description of operator More...
 
Opadd_argument (const std::string &name, const std::string &type, const std::string &description)
 Add argument information to the function. More...
 
Opadd_arguments (const std::vector< ParamFieldInfo > &args)
 Append list if arguments to the end. More...
 
Opset_num_inputs (uint32_t n)
 Set the num_inputs. More...
 
Opset_support_level (uint32_t level)
 Set the support level of op. More...
 
Opset_num_inputs (std::function< uint32_t(const NodeAttrs &attr)> fn)
 Set the get_num_outputs function. More...
 
Opset_num_outputs (uint32_t n)
 Set the num_outputs. More...
 
Opset_num_outputs (std::function< uint32_t(const NodeAttrs &attr)> fn)
 Set the get_num_outputs function. More...
 
Opset_attr_parser (std::function< void(NodeAttrs *attrs)> fn)
 Set the attr_parser function. More...
 
template<typename ValueType >
Opset_attr (const std::string &attr_name, const ValueType &value, int plevel=10)
 Register additional attributes to operator. More...
 
Opadd_alias (const std::string &alias)
 Add another alias to this operator. The same Op can be queried with Op::Get(alias) More...
 
Opinclude (const std::string &group_name)
 Include all the attributes from an registered op group. More...
 

Static Public Member Functions

static const OpGet (const std::string &op_name)
 Get an Op for a given operator name. Will raise an error if the op has not been registered. More...
 
template<typename ValueType >
static const OpMap< ValueType > & GetAttr (const std::string &attr_name)
 Get additional registered attribute about operators. If nothing has been registered, an empty OpMap will be returned. More...
 

Public Attributes

std::string name
 name of the operator More...
 
std::string description
 detailed description of the operator This can be used to generate docstring automatically for the operator. More...
 
std::vector< ParamFieldInfo > arguments
 
uint32_t num_inputs = 1
 number of inputs to the operator, -1 means it is variable length When get_num_inputs is presented, the number will be decided by get_num_inputs instead. More...
 
uint32_t num_outputs = 1
 number of outputs of the operator When get_num_outputs is presented. The number of outputs will be decided by get_num_outputs function More...
 
uint32_t support_level = 10
 support level of the operator, The lower the more priority it contains. This is in analogies to BLAS levels. More...
 
std::function< uint32_t(const NodeAttrs &attrs)> get_num_outputs = nullptr
 get number of outputs given information about the node. More...
 
std::function< uint32_t(const NodeAttrs &attrs)> get_num_inputs = nullptr
 get number of inputs given information about the node. More...
 
std::function< void(NodeAttrs *attrs)> attr_parser = nullptr
 Attribute parser to parse the NodeAttrs information. More...
 

Friends

template<typename ValueType >
class OpMap
 
class OpGroup
 
class dmlc::Registry< Op >
 

Detailed Description

Operator structure.

Besides the fields in the structure, arbitary additional information can be associated with each op. See function GetAttr for details.

// Example usage of Op
// registeration of oeprators
// NOTE that the attr function can register any
// additional attributes to the operator
.describe("add two inputs together")
.set_num_inputs(2)
.set_attr<OpKernel>("OpKernel<gpu>", AddKernel)
.include("ElementwiseOpAttr");
// can register attribute by group
// all the ops that include the group get the attribute.
NNVM_REGISTER_OP_GROUP(ElementwiseOpAttr)
.set_attr<FInferShape>("FInferShape", ElementwiseInferShape);
.describe("substract one tensor from another")
.set_num_inputs(2);
// Can call regster multiple times in different files
// to register different part of information
.set_attr<OpKernel>("OpKernel<gpu>", SubKernel);
.include("ElementwiseOpAttr");
// get operators from registry.
void my_function() {
const Op* add = Op::Get("add");
const Op* sub = Op::Get("sub");
// query basic information about each operator.
assert(op->name == "plus");
assert(op->num_inputs == 2);
// get additional registered information,
// Assume user registered a OpKernel type attribute as gpu_kernel on each operator.
const OpMap<OpKernel>& kernel = Op::GetAttr<OpKernel>("OpKernel<gpu>");
// we can get the kernel functions by using operator as key.
auto add_kernel = kernel[add];
auto sub_kernel = kernel[sub];
// subsequent code can make use of the queried kernel functions.
}

Member Function Documentation

Op& nnvm::Op::add_alias ( const std::string &  alias)

Add another alias to this operator. The same Op can be queried with Op::Get(alias)

Parameters
aliasThe alias of the operator.
Returns
reference to self.
Op & nnvm::Op::add_argument ( const std::string &  name,
const std::string &  type,
const std::string &  description 
)
inline

Add argument information to the function.

Parameters
nameName of the argument.
typeType of the argument.
descriptionDescription of the argument.
Returns
reference to self.
Op & nnvm::Op::add_arguments ( const std::vector< ParamFieldInfo > &  args)
inline

Append list if arguments to the end.

Parameters
argsAdditional list of arguments.
Returns
reference to self.
Op & nnvm::Op::describe ( const std::string &  descr)
inline

setter function during registration Set the description of operator

Parameters
descrthe description string.
Returns
reference to self.
static const Op* nnvm::Op::Get ( const std::string &  op_name)
static

Get an Op for a given operator name. Will raise an error if the op has not been registered.

Parameters
op_nameName of the operator.
Returns
Pointer to a Op, valid throughout program lifetime.
template<typename ValueType >
const OpMap< ValueType > & nnvm::Op::GetAttr ( const std::string &  attr_name)
inlinestatic

Get additional registered attribute about operators. If nothing has been registered, an empty OpMap will be returned.

Parameters
attr_nameThe name of the attribute.
Returns
An OpMap of specified attr_name.
Template Parameters
ValueTypeThe type of the attribute.
Op& nnvm::Op::include ( const std::string &  group_name)

Include all the attributes from an registered op group.

Parameters
group_nameThe name of the group.
Returns
reference to self.
See also
NNVM_REGISTER_OP_GROUP
template<typename ValueType >
Op & nnvm::Op::set_attr ( const std::string &  attr_name,
const ValueType &  value,
int  plevel = 10 
)
inline

Register additional attributes to operator.

Parameters
attr_nameThe name of the attribute.
valueThe value to be set.
plevelThe priority level of this set, an higher priority level attribute will replace lower priority level attribute. Must be bigger than 0.

Cannot set with same plevel twice in the code.

Template Parameters
ValueTypeThe type of the value to be set.
Op & nnvm::Op::set_attr_parser ( std::function< void(NodeAttrs *attrs)>  fn)
inline

Set the attr_parser function.

Parameters
fnThe number of outputs to be set.
Returns
reference to self.
Op & nnvm::Op::set_num_inputs ( uint32_t  n)
inline

Set the num_inputs.

Parameters
nThe number of inputs to be set.
Returns
reference to self.
Op & nnvm::Op::set_num_inputs ( std::function< uint32_t(const NodeAttrs &attr)>  fn)
inline

Set the get_num_outputs function.

Parameters
fnThe function to be set.
Returns
reference to self.
Op & nnvm::Op::set_num_outputs ( uint32_t  n)
inline

Set the num_outputs.

Parameters
nThe number of outputs to be set.
Returns
reference to self.
Op & nnvm::Op::set_num_outputs ( std::function< uint32_t(const NodeAttrs &attr)>  fn)
inline

Set the get_num_outputs function.

Parameters
fnThe function to be set.
Returns
reference to self.
Op & nnvm::Op::set_support_level ( uint32_t  level)
inline

Set the support level of op.

Parameters
levelThe support level.
Returns
reference to self.

Friends And Related Function Documentation

friend class dmlc::Registry< Op >
friend
friend class OpGroup
friend
template<typename ValueType >
friend class OpMap
friend

Member Data Documentation

std::vector<ParamFieldInfo> nnvm::Op::arguments
std::function<void(NodeAttrs* attrs)> nnvm::Op::attr_parser = nullptr

Attribute parser to parse the NodeAttrs information.

This can help to get quick access to a parsed attribute object

// Example usage of attr_parser.
// Suppose we want to register operator sum.
// The parameters about sum operator
struct SumParam {
int axis;
};
// The parser function
void SumAttrParser(NodeAttrs* attrs) {
// This will be invoked during node construction.
SumParam param;
// parse axis string to integer
param.axis = atoi(attrs->dict["axis"].c_str());
// set the parsed parameter
attrs->parsed = std::move(param);
}
// The other function that can utilize the parsed result.
TShape SumInferShape(const NodeAttrs& attrs,
const std::vector<TShape>& ishapes) {
// we can use the parsed version of param
// without repeatively parsing the parameter
const SumParam& param = nnvm::get<SumParam>(attrs.parsed);
}
std::string nnvm::Op::description

detailed description of the operator This can be used to generate docstring automatically for the operator.

std::function<uint32_t(const NodeAttrs& attrs)> nnvm::Op::get_num_inputs = nullptr

get number of inputs given information about the node.

Parameters
attrsThe attribute of the node
Returns
number of inputs
std::function<uint32_t(const NodeAttrs& attrs)> nnvm::Op::get_num_outputs = nullptr

get number of outputs given information about the node.

Parameters
attrsThe attribute of the node
Returns
number of outputs.
std::string nnvm::Op::name

name of the operator

uint32_t nnvm::Op::num_inputs = 1

number of inputs to the operator, -1 means it is variable length When get_num_inputs is presented, the number will be decided by get_num_inputs instead.

See also
get_num_inputs
uint32_t nnvm::Op::num_outputs = 1

number of outputs of the operator When get_num_outputs is presented. The number of outputs will be decided by get_num_outputs function

See also
get_num_outputs
uint32_t nnvm::Op::support_level = 10

support level of the operator, The lower the more priority it contains. This is in analogies to BLAS levels.


The documentation for this class was generated from the following file: