Implementation
Define the operators in the graph based on the operator prototypes, including creating operator instances, and setting operator input, output, and attributes.
Related Interfaces
After an operator prototype is successfully registered, the following interfaces are automatically generated for IR graph construction.
- Explicit OpType(const string& name): constructs operator type constructor.
- set_input_input name: sets an operator input.
- get_input_desc_input name: obtains an operator input description.
- update_input_desc_input name: sets an operator input description.
- update_output_desc_output name: sets an operator output description.
- set_attr_attribute name: sets an operator attribute value.
- get_attr_attribute name: obtains an operator attribute value.
- create_dynamic_input_dynamic input name: sets a dynamic operator input. By default, the newly created dynamic input is indexed last.
- create_dynamic_input_byindex_dynamic input name: sets a dynamic operator input, inserting the input following an input whose index is specified. This interface is mutually exclusive with create_dynamic_input_dynamic input name, that is, they cannot be both called.
- set_dynamic_input_dynamic input name: sets a dynamic operator input.
- create_dynamic_output_dynamic output name: sets a dynamic operator output.
Including Header Files
#include "all_ops.h" #include "types.h" #include "tensor.h"
- For built-in operators, the all_ops.h header file needs to be included to define the operator type. The header file is stored in /opp/op_proto/built-in/inc/all_ops.h in the OPP installation path.
- For the custom operators, the header file of the custom operator prototype definition needs to be included.
Creating an Operator Instance
In the operator development phase, after the operator type is registered using the REG_OP macro, an operator type constructor is automatically generated.
In the IR graph construction phase, pass the operator name to the operator type constructor. For example, pass softmax to SoftmaxV2(const string& name).
auto softmax = op::SoftmaxV2("softmax")
The name of each operator in a graph must be unique.
Setting the Operator Input
The operator prototype defines the input names, input types, and data types supported by the operator. Based on the input types, operator inputs can be classified into optional inputs, required inputs, and dynamic inputs.
Different interfaces are used for setting different input types.
- For optional and required inputs, use set_input_input name.The following is an example.
auto softmax = op::SoftmaxV2("Softmax") // Create a SoftmaxV2 operator instance. .set_input_x(bias_add_3); // Set bias_add_3 as the input of SoftmaxV2.
- For dynamic inputs, use create_dynamic_input_input name and set_dynamic_input_input name. For details, see Dynamic Multi-Input Operator Definition.
Setting Operator Attribute
The operator prototype defines the operator attribute names, attribute types, data types supported by the attributes, as well as the default attribute values and value ranges. Based on the attribute types, operator attributes can be classified into required attributes (REQUIRED_ATTR, which must be specified during operator definition) and optional attributes (ATTR, the default values are used if not specified).
Required and optional attributes can be set using the set_attr_attribute name interface. The following is an example.
auto maxpool1 = op::MaxPool("MaxPool1") .set_input_x(tanh1) .set_attr_ksize({1, 1, 2, 1}) // Set the ksize attribute. .set_attr_strides({1, 1, 2, 1}) // Set the strides attribute. .set_attr_padding("SAME"); // Set the padding attribute.
Specifying Operator Connections
The connections between operators are implemented by specifying the inputs and outputs of the operators.
If an operator has only one output, pass only the operator name to the set_input_input name call of its downstream operator. In the following example, the bias_add_3 has only one output. In this case, only the operator name bias_add_3 needs to be passed to SoftmaxV2.
auto bias_add_3 = op::BiasAdd("bias_add_3") .set_input_x(matmul_2) .set_input_bias(bias_add_const_3) .set_attr_data_format("NCHW"); auto softmax = op::SoftmaxV2("Softmax") .set_input_x(bias_add_3);
If an operator has multiple outputs, both the operator name and the output name need to be passed to the set_input_input name call of its downstream operator. For details, see Dynamic Multi-Output Operator Definition.