Prototype Definition Derivative APIs.
The operator prototype definition APIs automatically generate derivative APIs used for IR model building. For details about the following APIs, see Ascend Graph Developer Guide.
REG_OP
Registers an operator type. Two constructors corresponding to the operator type are derived automatically.
For example, if the operator type Conv2D is registered by using the REG_OP(Conv2D) call. Two Conv2D constructors are generated: Conv2D() is the default constructor while Conv2D(const AscendString& name) takes an operator name.
class Conv2D : public Operator {
typedef Conv2D _THIS_TYPE;
public:
explicit Conv2D(const char *name);
explicit Conv2D();
}
INPUT
After an input is successfully registered, APIs for obtaining the input name and setting the input description are derived automatically.
For example, if the INPUT(x, TensorType{DT_FLOAT}) call is issued to register operator input x of type TensorType{DT_FLOAT}, the following APIs are automatically derived on successful registration:
static const string name_in_x(); // Returns the input name (x). _THIS_TYPE&set_input_x(Operator& v, const string& srcName);// Associates input x with output srcName of operator object v and returns the operator object itself. _THIS_TYPE &set_input_x_by_name(Operator& v, const char *srcName); // Associates input x with output srcName of operator object v and returns the operator object itself. _THIS_TYPE &set_input_x(Operator &v, uint32_t index); // Associates input x with output indexed index of operator object v and returns the operator object itself. _THIS_TYPE& set_input_x(Operator& v); // Associates input x with output 0 of operator object v and returns the operator object itself. TensorDesc get_input_desc_x(); // Returns the description of input x. graphStatus update_input_desc_x(const TensorDesc& tensorDesc);// Sets the description of input x, including Shape, DataType, and Format. graphStatus indicates the uint32_t type. If a non-zero value is returned, an error occurs.
OPTIONAL_INPUT
After an optional input is successfully registered, APIs for obtaining the input name and setting the input description are derived automatically.
For example, if the OPTIONAL_INPUT(b, TensorType{DT_FLOAT}) call is issued to register optional input b of type TensorType{DT_FLOAT}, the following APIs are automatically derived on successful registration:
static const string name_in_b(); // Returns the input name (b). _THIS_TYPE& set_input_b(Operator& v, const string& srcName);// Associates input b with output srcName of operator object v and returns the operator object itself. _THIS_TYPE& set_input_b_by_name(Operator& v, const char *srcName);// Associates input b with output srcName of operator object v and returns the operator object itself. _THIS_TYPE& set_input_b(Operator& v); // Associates input b with output 0 of operator object v and returns the operator object itself. TensorDesc get_input_desc_b(); // Returns the description of input b. graphStatus update_input_desc_b(const TensorDesc& tensorDesc);// Sets the description of input b, including Shape, DataType, and Format.
DYNAMIC_INPUT
After a dynamic input is successfully registered, APIs for creating dynamic input and setting the input description are derived automatically.
For example, if the DYNAMIC_INPUT(d, TensorType{DT_FLOAT}) call is issued to register dynamic input b of type TensorType{DT_FLOAT}, the following APIs are automatically derived on successful registration:
_THIS_TYPE& create_dynamic_input_d(unsigned int num); // Creates the dynamic input d, including num inputs. The d input is the last input of the operator. _THIS_TYPE &create_dynamic_input_byindex_d(unsigned int num, size_t index) // Creates the dynamic input d, including num inputs, and inserts y behind the position indexed index. This API is mutually exclusive with create_dynamic_input_d. TensorDesc get_dynamic_input_desc_d(unsigned int index); // Returns the description indexed index of dynamic input d, including Shape, DataType, and Format. graphStatus update_dynamic_input_desc_d(unsigned int index, const TensorDesc& tensorDesc); // Updates the description indexed index of dynamic input d. _THIS_TYPE& set_dynamic_input_d(unsigned int dstIndex, Operator &v); // Associates the input indexed dstIndex of dynamic input d with output 0 of operator object v, and returns the operator object itself. _THIS_TYPE& set_dynamic_input_d(unsigned int dstIndex, Operator &v, const string &srcName); // Associates the input indexed dstIndex of dynamic input d with output srcName of operator object v and returns the operator object itself. _THIS_TYPE& set_dynamic_input_d(unsigned int dstIndex, Operator &v, const char *srcName); // Associates the input indexed dstIndex of dynamic input d with output srcName of operator object v and returns the operator object itself.
OUTPUT
After an output is successfully registered, APIs for obtaining the output name and setting the output description are derived automatically.
For example, if the OUTPUT(y, TensorType{DT_FLOAT}) call is issued to register output y of type TensorType{DT_FLOAT}, the following APIs are automatically derived on successful registration:
static const string name_out_y();// Returns the output name (y). TensorDesc get_output_desc_y();// Returns the description of output y. graphStatus update_output_desc_y(const TensorDesc& tensorDesc); // Sets the description of output y, including Shape, DataType, and Format.
DYNAMIC_OUTPUT
After a dynamic output is successfully registered, APIs for creating dynamic output and setting the output description are derived automatically.
For example, if the DYNAMIC_OUTPUT(d, TensorType{DT_FLOAT}) call is issued to register dynamic output d of type TensorType{DT_FLOAT}, the following APIs are automatically derived on successful registration:
_THIS_TYPE& create_dynamic_output_d(unsigned int num); // Creates the dynamic output d, including num inputs. TensorDesc get_dynamic_output_desc_d(unsigned int index);//Returns the description indexed index of dynamic output d, including Shape, DataType, and Format. graphStatus update_dynamic_output_desc_d(unsigned int index, const TensorDesc& tensorDesc);// Updates the description indexed index of dynamic output d.
REQUIRED_ATTR
After an operator attribute is successfully registered, external APIs for obtaining the attribute name, obtaining the attribute value, and setting the attribute value are derived automatically.
For example, if the REQUIRED_ATTR(mode, Int) call is issued to register an int64_t attribute named mode, the following APIs are automatically derived on successful registration:
static const string name_attr_mode(); // Returns the attribute name (mode). static const void name_attr_mode(AscendString &attr_name);// Outputs the attribute name (mode). OpInt get_attr_mode() const; // Returns the value of attribute mode. OpInt indicates int64_t. _THIS_TYPE& set_attr_mode(const OpInt& v); // Sets the value of the mode attribute and returns the operator object itself.
ATTR
After an operator attribute is successfully registered, external APIs for obtaining the attribute name, obtaining the attribute value, and setting the attribute value are derived automatically.
The following takes the registration of an int64_t attribute and an int64_t list attribute as an example:
- For example, if the ATTR(mode, Int, 1) call is issued to register an int64_t attribute named mode with default value 1, the following APIs are automatically derived on successful registration:
static const string name_attr_mode(); // Returns the attribute name (mode). static const void name_attr_mode(AscendString &attr_name);// Outputs the attribute name (mode). OpInt get_attr_mode() const; // Returns the value of attribute mode. OpInt indicates int64_t. _THIS_TYPE& set_attr_mode(const OpInt& v); // Sets the value of the mode attribute and returns the operator object itself.
- For example, if the ATTR(pad, ListInt, {0, 0, 0, 0}) call is issued to register an int64_t attribute named pad with default value {0,0,0,0}, the following APIs are automatically derived on successful registration:
static const string name_attr_pad(); // Returns the attribute name (pad). static const void name_attr_pad(AscendString &attr_name);// Outputs the attribute name (pad). OpListInt get_attr_pad() const;; // Returns the value of attribute pad. OpListInt indicates vector<int64_t>. _THIS_TYPE& set_attr_pad(const OpListInt& v); // Sets the value of attribute pad and returns the operator object itself.
The following takes the registration of a string attribute as an example:
If the ATTR(data_format, String, "NHWC") call is issued to register a string attribute named data_format, the following APIs are automatically derived on successful registration:
static const string name_attr_data_format(); // Returns the attribute name (data_format). static const void name_attr_data_format(AscendString &attr_name);// Outputs the attribute name (data_format). OpString get_attr_data_format() const; // Returns the value of attribute data_format. OpString indicates string. graphStatus get_attr_data_format(AscendString &val);// Outputs the value of attribute data_format. _THIS_TYPE& set_attr_data_format(const string& v); // Sets the value of attribute data_format and returns the operator object itself. _THIS_TYPE& set_attr_data_format(const char* v); // Sets the value of attribute data_format and returns the operator object itself.
GRAPH
After an operator subgraph is successfully registered, the APIs for obtaining the subgraph name, obtaining the subgraph description, and setting the subgraph description are derived automatically.
For example, if the GRAPH(y) call is issued to register an operator subgraph, the following APIs are automatically derived on successful registration:
static const string name_graph_y();// Returns the name of the operator subgraph (y). SubgraphBuilder get_subgraph_builder_y() const;// Returns the SubgraphBuilder object of subgraph y. _THIS_TYPE &set_subgraph_builder_y(const SubgraphBuilder &v);// Sets the SubgraphBuilder object of subgraph y. Graph get_subgraph_y() const;// Obtains the Graph object of subgraph y.
DYNAMIC_GRAPH
After a dynamic operator subgraph is successfully registered, APIs for creating a dynamic subgraph and setting the dynamic subgraph description are derived automatically.
For example, if the DYNAMIC_GRAPH(branches) call is issued to register a dynamic operator subgraph, the following APIs are automatically derived on successful registration:
_THIS_TYPE& create_dynamic_subgraph_branches(unsigned int num); // Creates dynamic subgraphs branches, including num subgraphs. SubgraphBuilder get_dynamic_subgraph_builder_branches(unsigned int index) ;// Returns the SubgraphBuilder object of the dynamic subgraph indexed index. Graph get_dynamic_subgraph_branches(unsigned int index) ;// Returns the Graph object of the dynamic subgraph indexed index. _THIS_TYPE &set_dynamic_subgraph_builder_branches(unsigned int index,const SubgraphBuilder &v);// Sets the SubgraphBuilder object of the dynamic subgraph indexed index.