Adapter Plug-in APIs
- Class OpRegistrationData
- Overview
- Constructor and Destructor
- REGISTER_CUSTOM_OP
- FrameworkType
- OriginOpType
- ParseParamsFn
- ParseParamsByOperatorFn
- FusionParseParamsFn
- ParseSubgraphPostFn
- ImplyType
- DelInputWithCond
- DelInputWithOriginalType
- GetImplyType
- GetOmOptype
- GetOriginOpTypeSet
- GetFrameworkType
- GetParseParamFn
- GetParseParamByOperatorFn
- GetFusionParseParamFn
- GetParseSubgraphPostFn
- AutoMappingFn
- AutoMappingFnDynamic
- AutoMappingSubgraphIndex
- Class OpReceiver
Class OpRegistrationData
Overview
After completing the implementation code of a custom operator, you need to develop an adaptation plug-in to map an operator developed based on a third-party framework to that supported by the Ascend AI processor. Call the REGISTER_CUSTOM_OP macro to implement operator conversion. As shown in the following, the calling starts with REGISTER_CUSTOM_OP. Then, APIs such as FrameworkType, OriginOpType, and ParseParamsFn are linked with a dot (.).
Example:
REGISTER_CUSTOM_OP("OpType") .FrameworkType(TENSORFLOW) .OriginOpType("OriginOpType") .ParseParamsFn(ParseParamFunc);
Constructor and Destructor
Function
OpRegistrationData constructor and destructor.
Syntax
OpRegistrationData(const std::string& om_optype);
~OpRegistrationData()
Parameter Description
Parameter |
Input/Output |
Description |
---|---|---|
om_optype |
Input |
Specifies the operator type supported by models of the Ascend AI Processor. |
Return Value
The OpRegistrationData constructor returns an object of the OpRegistrationData type.
Exception Handling
None
Restriction
None
REGISTER_CUSTOM_OP
Function
Registers an operator with a specified name.
Syntax
REGISTER_CUSTOM_OP(name)
Parameter Description
Parameter |
Input/Output |
Description |
---|---|---|
name |
Input |
Operator type name. |
FrameworkType
Description
Sets the framework type of the original model.
Prototype
OpRegistrationData &FrameworkType(const domi::FrameworkType& fmk_type)
Parameters
Parameter |
Input/Output |
Description |
---|---|---|
fmk_type |
Input |
Framework type
The enum values of FrameworkType are defined as follows: enum FrameworkType { CAFFE = 0, TENSORFLOW = 3, ANDROID_NN, FRAMEWORK_RESERVED, }; |
OriginOpType
Description
Sets the operator type or list of operator types of the original model.
Prototype
OpRegistrationData &OriginOpType(const std::initializer_list<std::string>& ori_optype_list);
OpRegistrationData &OriginOpType(const std::string& ori_optype)
Parameters
Parameter |
Input/Output |
Description |
---|---|---|
ori_optype_list |
Input |
List of operator types of the original model |
ori_optype |
Input |
Operator type of the original model |
ParseParamsFn
Description
Registers the function for parsing operator attributes.
Restrictions
ParseParamsFn will not be available in future releases. User ParseParamsByOperatorFn instead.
Prototype
OpRegistrationData &ParseParamsFn(const ParseParamFunc& parseParamFn)
Parameters
Parameter |
Input/Output |
Description |
---|---|---|
parseParamFn |
Input |
ParseParamFunc for parsing operator attributes. For details, see Callback ParseParamFunc. If the attributes of a TensorFlow operator are consistent with those of an operator of the Ascend AI Processor (both the number and sequence), you can use the AutoMappingFn function to implement automatic mapping. |
Callback ParseParamFunc
Customizes and implements a function of class FusionParseParamFunc to map attributes of the original model to an offline model adapted to Ascend AI Processor and writes the result to an Operator class.
Status ParseParamFunc(const Message* op_origin, ge::Operator& op_dest)
Parameter |
Input/Output |
Description |
---|---|---|
op_origin |
Input |
Data structure in protobuf format (from the Caffe prototxt file), which stores the operator information |
op_dest |
Output |
Operator data structure of the offline model adapted to Ascend AI Processor, which stores the operator information For details about class Operator, see Class Operator. |
ParseParamsByOperatorFn
Description
Registers the function for parsing the attributes of a custom Caffe operator.
Prototype
OpRegistrationData &ParseParamsByOperatorFn(const ParseParamByOpFunc &parse_param_by_op_fn)
Parameters
Parameter |
Input/Output |
Description |
---|---|---|
parse_param_by_op_fn |
Input |
Function for parsing the attributes of a custom Caffe operator. For details, see Callback Function ParseParamByOpFunc. |
Callback Function ParseParamByOpFunc
Customizes and implements a function of class ParseParamByOpFunc to map attributes of the original model to an offline model adapted to Ascend AI Processor and writes the result to an Operator class.
Status ParseParamByOpFunc(const ge::Operator & op_origin, ge::Operator& op_dest)
Parameter |
Input/Output |
Description |
---|---|---|
op_origin |
Input |
An object of class Operator defined by the framework, including the custom operator attributes of the parsed Caffe model. For details about class Operator, see Class Operator. |
op_dest |
Output |
Operator data structure of the offline model adapted to Ascend AI Processor, storing the operator information. For details about class Operator, see Class Operator. |
Restrictions
None
FusionParseParamsFn
Description
Registers a function for parsing the attributes of fused operators.
Prototype
OpRegistrationData &FusionParseParamsFn(const FusionParseParamFunc &fusionParseParamFn)
Parameters
Parameter |
Input/Output |
Description |
---|---|---|
fusionParseParamFn |
Input |
Function for parsing the attributes of fused operators. For details, see Callback FusionParseParamFunc. |
Callback FusionParseParamFunc
Customizes and implements a function of class FusionParseParamFunc to map attributes of the original model to the offline model adapted to Ascend AI Processor and fills the result in an Operator class.
Status FusionParseParamFunc(const vector<const google::protobuf::Message *> &v_op_origin, ge::Operator &op_dest)
Parameter |
Input/Output |
Description |
---|---|---|
v_op_origin |
Input |
protobuf data structures (from the prototxt file of the original model) within the scope, which stores the operator information. |
op_dest |
Output |
Data structure of fused operators, which stores the fused operator information. For details, see Class Operator. |
ParseSubgraphPostFn
Description
Registers the function for mapping between the input and output nodes in the subgraphs of an operator and the input and output nodes of the operator based on the operator type.
Prototype
OpRegistrationData &OpRegistrationData::ParseSubgraphPostFn(const ParseSubgraphFunc &subgraph_post_fn)
Parameters
Parameter |
Input/Output |
Description |
---|---|---|
subgraph_post_fn |
Input |
Function object for mapping between the input and output nodes in the operator subgraphs and the input and output nodes of the operator |
Restrictions
None
ImplyType
Function
Sets the operator execution mode.
Syntax
OpRegistrationData& ImplyType(const domi::ImplyType& imply_type)
Parameter Description
Parameter |
Input/Output |
Description |
---|---|---|
imply_type |
Input |
Operator execution mode. enum class ImplyType : unsigned int { BUILDIN = 0,// Built-in operator, which is normally executed by the OME TVM, // Executed after being compiled into a TVM binary file CUSTOM, // The computation logic is customized by the user and executed by the CPU. AI_CPU, // AI CPU custom operator type INVALID = 0xFFFFFFFF, }; |
DelInputWithCond
Function
Deletes an input edge of an operator based on the operator attribute.
Prototype
OpRegistrationData& DelInputWithCond(int inputIdx, const std::string& attrName, bool attrValue)
Parameters
Parameter |
Input/Output |
Description |
---|---|---|
inputIdx |
Input |
Index of the input edge to be deleted |
attrName |
Input |
Attribute name |
attrValue |
Input |
Attribute value |
Restrictions
None
DelInputWithOriginalType
Description
Deletes an input edge of an operator based on the operator type.
Prototype
OpRegistrationData &DelInputWithOriginalType(int input_idx, const std::string &ori_type)
Parameters
Parameter |
Input/Output |
Description |
---|---|---|
inputIdx |
Input |
Index of the input edge to be deleted |
ori_type |
Input |
Original operator type |
Restrictions
None
GetImplyType
Description
Obtains the operator execution mode.
Prototype
ImplyType GetImplyType () const
Parameters
None
Restrictions
None
GetOmOptype
Description
Obtains the operator types of a model.
Prototype
string GetOmOptype () const
Parameters
None
Restrictions
None
GetOriginOpTypeSet
Description
Obtains the operator types of the original model.
Prototype
set<std::string> GetOriginOpTypeSet () const
Parameters
None
Restrictions
None
GetFrameworkType
Description
Obtains the framework type of the original model.
Prototype
FrameworkType GetFrameworkType() const
Parameters
None
Restrictions
None
GetParseParamFn
Description
Obtains the function for parsing operator attributes.
Prototype
ParseParamFunc GetParseParamFn() const
Parameters
None
Restrictions
None
GetParseParamByOperatorFn
Description
Obtains the function for parsing the attributes of a custom Caffe operator.
Prototype
ParseParamByOpFunc GetParseParamByOperatorFn() const
Parameters
None
Restrictions
None
GetFusionParseParamFn
Description
Obtains the function for parsing the attributes of a fused operator.
Prototype
FusionParseParamFunc GetFusionParseParamFn() const
Parameters
None
Restrictions
None
GetParseSubgraphPostFn
Description
Obtains the function for mapping between the input and output nodes in the operator subgraphs and the input and output nodes of the operator based on the operator type.
Prototype
ParseParamFunc OpRegistrationData::GetParseParamFn()
Parameters
None
Restrictions
None
AutoMappingFn
Description
Implements automatic mapping.
Prototype
Status AutoMappingFn(const google::protobuf::Message* op_src, ge::Operator& op)
Parameters
Parameter |
Input/Output |
Description |
---|---|---|
op_src |
Input |
Original operator |
op |
Input |
Mapped operator |
AutoMappingFnDynamic
Function
Implements automatic mapping.
Syntax
Status AutoMappingFnDynamic(const google::protobuf::Message *op_src, ge::Operator &op, std::map<std::string, std::pair<std::string, std::string>> dynamic_name_attr_value, int in_pos = -1, int out_pos = -1)
Parameter Description
Parameter |
Input/Output |
Description |
---|---|---|
op_src |
Input |
Original operator before conversion. |
op |
Input |
Mapped operator. |
dynamic_name_attr_value |
Input |
Actual number of dynamic inputs or outputs. key specifies whether they are inputs or outputs. |
in_pos |
Input |
Port ID for the dynamic input. |
out_pos |
Input |
Port ID for the dynamic output. |
AutoMappingSubgraphIndex
Description
Sets the mapping between the input and output of the subgraph and the input and output of the parent node of the main model.
Prototype
Status AutoMappingSubgraphIndex(const ge::Graph &graph,
const std::function<int(int data_index)> &input,
const std::function<int(int netoutput_index)> &output)
Status AutoMappingSubgraphIndex(const ge::Graph &graph,
const std::function<Status(int data_index, int &parent_input_index)> &input,
const std::function<Status(int netoutput_index, int &parent_output_index)> &output)
Parameters
Parameter |
Input/Output |
Description |
---|---|---|
graph |
Input |
Subgraph object |
input |
Input |
Input mapping function |
output |
Input |
Output mapping function |
Restrictions
None
Class OpReceiver
Constructor and Destructor
Function
The OpReceiver constructor receives the registration information of a custom operator.
Syntax
OpReceiver(OpRegistrationData& reg_data);
~OpReceiver()
Parameter Description
Parameter |
Input/Output |
Description |
---|---|---|
reg_data |
Input |
Operator information to be registered. |
Return Value
Returns an object of class OpReceiver.
Exception Handling
None
Restriction
None
- Class OpRegistrationData
- Overview
- Constructor and Destructor
- REGISTER_CUSTOM_OP
- FrameworkType
- OriginOpType
- ParseParamsFn
- ParseParamsByOperatorFn
- FusionParseParamsFn
- ParseSubgraphPostFn
- ImplyType
- DelInputWithCond
- DelInputWithOriginalType
- GetImplyType
- GetOmOptype
- GetOriginOpTypeSet
- GetFrameworkType
- GetParseParamFn
- GetParseParamByOperatorFn
- GetFusionParseParamFn
- GetParseSubgraphPostFn
- AutoMappingFn
- AutoMappingFnDynamic
- AutoMappingSubgraphIndex
- Class OpReceiver