mxnet
Public Member Functions | Static Public Member Functions | List of all members
mxnet::OperatorProperty Class Referenceabstract

OperatorProperty is a object that stores all information about Operator. It also contains method to generate context(device) specific operators. More...

#include <operator.h>

Collaboration diagram for mxnet::OperatorProperty:
Collaboration graph

Public Member Functions

virtual ~OperatorProperty ()
 virtual destructor More...
 
virtual void Init (const std::vector< std::pair< std::string, std::string > > &kwargs)=0
 Initialize the Operator by setting the parameters This function need to be called before all other functions. More...
 
virtual std::map< std::string, std::string > GetParams () const =0
 Get a map representation of internal parameters. This can be used by Init to recover the state of OperatorProperty. More...
 
virtual std::vector< std::string > ListArguments () const
 Get input arguments of the Operator. More...
 
virtual std::vector< std::string > ListOutputs () const
 Get name of output values of Operator. More...
 
virtual std::vector< std::string > ListAuxiliaryStates () const
 Get name of auxiliary states of Operator. More...
 
virtual int NumOutputs () const
 
virtual int NumVisibleOutputs () const
 get number of visible return values during Symbol creation. If NumVisibleOutputs() = k, and NumOutputs() = n. The first k returns will be presented in the resulting symbol. More...
 
virtual bool InferShape (mxnet::ShapeVector *in_shape, mxnet::ShapeVector *out_shape, mxnet::ShapeVector *aux_shape) const =0
 infer the shapes of outputs and unknown input arguments More...
 
virtual bool InferType (std::vector< int > *in_type, std::vector< int > *out_type, std::vector< int > *aux_type) const
 infer the data types of outputs and unknown input arguments More...
 
virtual OperatorPropertyCopy () const =0
 Copy this OperatorProperty. More...
 
virtual OperatorCreateOperator (Context ctx) const =0
 Create a Operator on specific context. More...
 
virtual OperatorCreateOperatorEx (Context ctx, mxnet::ShapeVector *in_shape, std::vector< int > *in_type) const
 Create a Operator on specific context and input shape/type. More...
 
virtual std::string TypeString () const =0
 return the type string of the Operator subclasses override this function. More...
 
virtual std::vector< ResourceRequestForwardResource (const mxnet::ShapeVector &in_shape) const
 Declare additional resource required in forward pass. These additional resources will be presented in OpContext.requested in the same order of the returned Resource. More...
 
virtual std::vector< ResourceRequestBackwardResource (const mxnet::ShapeVector &in_shape) const
 Declare additional resource required in backward pass. These additional resources will be presented in OpContext.requested in the same order of the returned Resource. More...
 
virtual std::vector< int > DeclareBackwardDependency (const std::vector< int > &out_grad, const std::vector< int > &in_data, const std::vector< int > &out_data) const
 Declare the input requirement of Backward pass. More...
 
virtual std::vector< std::pair< int, void * > > ForwardInplaceOption (const std::vector< int > &in_data, const std::vector< void * > &out_data) const
 Get possible forward inplace options. This function enables optimization to reuse memory of inputs in output. Only override when necessary, by default in-place is disabled. More...
 
virtual std::vector< std::pair< int, void * > > BackwardInplaceOption (const std::vector< int > &out_grad, const std::vector< int > &in_data, const std::vector< int > &out_data, const std::vector< void * > &in_grad) const
 Get possible backward inplace options. This function enables optimization to reuse memory of inputs in output. Only override when necessary, by default in-place is disabled. More...
 
template<typename T >
std::vector< T > BackwardInputs (const std::vector< T > &out_grad, const std::vector< T > &in_data, const std::vector< T > &out_data) const
 Get Backward Input Dependency for generic types of data. Normally T can be pointer of Symbol::DataEntry, or NDArray. This function will select the result list of T according to DeclareBackwardDependency. More...
 
virtual ExecType exec_type () const
 

Static Public Member Functions

static OperatorPropertyCreate (const char *type_name)
 create OperatorProperty More...
 

Detailed Description

OperatorProperty is a object that stores all information about Operator. It also contains method to generate context(device) specific operators.

It also contains various functions that can be optimally overriden to provide optimization chance for computation engine.

Constructor & Destructor Documentation

virtual mxnet::OperatorProperty::~OperatorProperty ( )
inlinevirtual

virtual destructor

Member Function Documentation

virtual std::vector<std::pair<int, void*> > mxnet::OperatorProperty::BackwardInplaceOption ( const std::vector< int > &  out_grad,
const std::vector< int > &  in_data,
const std::vector< int > &  out_data,
const std::vector< void * > &  in_grad 
) const
inlinevirtual

Get possible backward inplace options. This function enables optimization to reuse memory of inputs in output. Only override when necessary, by default in-place is disabled.

The reason for void* type in the in_grad is to distinguish the order of mappings between the two, compiler will report error when in_data and out_data's order in the pair get reversed.

// The following code says in_grad[0] can share data with in_data[0]
vector<pair<int,int> > BackwardInplaceOption(
const std::vector<int> &out_grad,
const std::vector<int> &in_data,
const std::vector<int> &out_data,
const std::vector<int> &in_grad) const {
return {in_data[0], in_grad[0]}};
}
Parameters
in_dataThe input data in forward pass.
out_dataThe output data in forward pass.
in_gradGradient of inputs in backward pass.
out_gradGradient of outputs in backward pass.
Returns
list of pair of that maps input->output, indicating possible in place operations.
template<typename T >
std::vector<T> mxnet::OperatorProperty::BackwardInputs ( const std::vector< T > &  out_grad,
const std::vector< T > &  in_data,
const std::vector< T > &  out_data 
) const
inline

Get Backward Input Dependency for generic types of data. Normally T can be pointer of Symbol::DataEntry, or NDArray. This function will select the result list of T according to DeclareBackwardDependency.

Parameters
in_datathe input data in forward pass.
out_datathe output data in forward pass.
out_gradgradient of outputs in backward pass.
Template Parameters
Tthe generic type parameter.
Returns
vector of inputs the Backward Operation depends on.
See also
DeclareBackwardDependency
virtual std::vector<ResourceRequest> mxnet::OperatorProperty::BackwardResource ( const mxnet::ShapeVector in_shape) const
inlinevirtual

Declare additional resource required in backward pass. These additional resources will be presented in OpContext.requested in the same order of the returned Resource.

Parameters
in_shapeThe input shape to the operator, corresponds to shapes of in_data.
Returns
Additional resource request
virtual OperatorProperty* mxnet::OperatorProperty::Copy ( ) const
pure virtual

Copy this OperatorProperty.

Returns
a pointer to the copied OperatorProperty
static OperatorProperty* mxnet::OperatorProperty::Create ( const char *  type_name)
static

create OperatorProperty

Parameters
type_namethe type string of the OperatorProperty
Returns
a new constructed OperatorProperty
virtual Operator* mxnet::OperatorProperty::CreateOperator ( Context  ctx) const
pure virtual

Create a Operator on specific context.

virtual Operator* mxnet::OperatorProperty::CreateOperatorEx ( Context  ctx,
mxnet::ShapeVector in_shape,
std::vector< int > *  in_type 
) const
inlinevirtual

Create a Operator on specific context and input shape/type.

Parameters
ctxcontext of this operator
in_shapeshape of the input ndarrays
in_typedtype of the input ndarrays
Returns
the created operator
virtual std::vector<int> mxnet::OperatorProperty::DeclareBackwardDependency ( const std::vector< int > &  out_grad,
const std::vector< int > &  in_data,
const std::vector< int > &  out_data 
) const
inlinevirtual

Declare the input requirement of Backward pass.

Only the returned list of variables will be used in Backward. This function is used for memory optimization. It is advised to override and only return what is actually needed. If this function is not overriden, all the variables will be valid in Backward.

// The following code declares Backward need out_grad[0], in_data[0],in_data[1]
vector<int> BackwardInputs(const vector<int> &out_grad,
const vector<int> &in_data,
const vector<int> &out_data) const {
return {out_grad[0], in_data[0], in_data[1]};
}
Parameters
out_gradgradient of outputs in backward pass.
in_datathe input data in forward pass.
out_datathe output data in forward pass.
Returns
an integer vector indicating the input requirments
See also
BackwardInputs
virtual ExecType mxnet::OperatorProperty::exec_type ( ) const
inlinevirtual
Returns
execution type of the operator
virtual std::vector<std::pair<int, void*> > mxnet::OperatorProperty::ForwardInplaceOption ( const std::vector< int > &  in_data,
const std::vector< void * > &  out_data 
) const
inlinevirtual

Get possible forward inplace options. This function enables optimization to reuse memory of inputs in output. Only override when necessary, by default in-place is disabled.

The reason for void* type in the out_data is to distinguish the order of mappings between the two, compiler will report error when in_data and out_data's order in the pair get reversed.

// The following code says out_data[0] can share data with in_data[0]
vector<pair<int, void*> > ForwardInplaceOption(const vector<int> &in_data,
const vector<void*> &out_data) const {
return {{in_data[0], out_data[0]}};
}
Parameters
in_dataThe input data in forward pass.
out_dataThe output data in forward pass.
Returns
list of pair of that maps input->output, indicating possible in place operations.
virtual std::vector<ResourceRequest> mxnet::OperatorProperty::ForwardResource ( const mxnet::ShapeVector in_shape) const
inlinevirtual

Declare additional resource required in forward pass. These additional resources will be presented in OpContext.requested in the same order of the returned Resource.

Parameters
in_shapeThe input shape to the operator, corresponds to shapes of in_data.
Returns
Additional resource request
virtual std::map<std::string, std::string> mxnet::OperatorProperty::GetParams ( ) const
pure virtual

Get a map representation of internal parameters. This can be used by Init to recover the state of OperatorProperty.

virtual bool mxnet::OperatorProperty::InferShape ( mxnet::ShapeVector in_shape,
mxnet::ShapeVector out_shape,
mxnet::ShapeVector aux_shape 
) const
pure virtual

infer the shapes of outputs and unknown input arguments

Parameters
in_shapethe shape of input arguments of the operator this should be of same length as the vector returned by DescribeArgs in_shape allows unknown elements, which are checked by shape.ndim() == 0. For unknown shapes, InferShape will try to fill in the correct Shape in in_shape For known shapes, InferShape will check shape consistency

common practice: set the shape of data input, and usually weight's shape can be inferred

Parameters
out_shapethe shape of outputs of the operator InferShape will modify the vector to fill output mxnet::TShape
aux_shapethe shape of auxiliary states of the operator InferShape will modify the vector to fill output mxnet::TShape
Returns
true if the shape inference is successful, false if there is not enough information.
Exceptions
dmlc::Errorif the known arg_shapes are inconsistent.
virtual bool mxnet::OperatorProperty::InferType ( std::vector< int > *  in_type,
std::vector< int > *  out_type,
std::vector< int > *  aux_type 
) const
inlinevirtual

infer the data types of outputs and unknown input arguments

Parameters
in_typethe type of input arguments of the operator this should be of same length as the vector returned by DescribeArgs in_type allows unknown elements, which are checked by type.ndim() == 0. For unknown types, Infertype will try to fill in the correct type in in_type For known types, Infertype will check type consistency

common practice: set the type of data input, and usually weight's type can be inferred

Parameters
out_typethe type of outputs of the operator Infertype will modify the vector to fill output Ttype
aux_typethe type of auxiliary states of the operator Infertype will modify the vector to fill output Ttype
Returns
true if the type inference is successful, false if there is not enough information.
Exceptions
dmlc::Errorif the known arg_types are inconsistent.
virtual void mxnet::OperatorProperty::Init ( const std::vector< std::pair< std::string, std::string > > &  kwargs)
pure virtual

Initialize the Operator by setting the parameters This function need to be called before all other functions.

Parameters
kwargsthe keyword arguments parameters
virtual std::vector<std::string> mxnet::OperatorProperty::ListArguments ( ) const
inlinevirtual

Get input arguments of the Operator.

Returns
vector of arguments.
virtual std::vector<std::string> mxnet::OperatorProperty::ListAuxiliaryStates ( ) const
inlinevirtual

Get name of auxiliary states of Operator.

Returns
name of return values.
virtual std::vector<std::string> mxnet::OperatorProperty::ListOutputs ( ) const
inlinevirtual

Get name of output values of Operator.

Returns
name of output values.
virtual int mxnet::OperatorProperty::NumOutputs ( ) const
inlinevirtual
Returns
number of real return values of the Operator
virtual int mxnet::OperatorProperty::NumVisibleOutputs ( ) const
inlinevirtual

get number of visible return values during Symbol creation. If NumVisibleOutputs() = k, and NumOutputs() = n. The first k returns will be presented in the resulting symbol.

The rest of the returns can be used for auxiliary states for Backward. For example, Dropout will return [data, mask], with NumVisibleOutputs() == 1. So when user call sym = Dropout(input), only data is presented in sym. But all the returns will be presented in out_data parameter of Backward if requested.

Returns
number of default return values
virtual std::string mxnet::OperatorProperty::TypeString ( ) const
pure virtual

return the type string of the Operator subclasses override this function.

Returns
The type string.

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