Defining a Dynamic Multi-Input Operator (AddN)
Some operators allow a varied number of inputs.
The following takes the AddN operator as an example to describe how to define an operator with multiple dynamic inputs.
1 2 3 4 5 | REG_OP(AddN) .DYNAMIC_INPUT(x, TensorType({NumberType(), DT_VARIANT})) .OUTPUT(y, TensorType({NumberType(), DT_VARIANT})) .REQUIRED_ATTR(N, Int) .OP_END_FACTORY_REG(AddN) |
The AddN operator allows multiple dynamic inputs. Call create_dynamic_input_inputName to create dynamic inputs, and call set_dynamic_input_inputName to set the dynamic inputs.
1 2 3 4 5 6 | auto data = op::Data().set_attr_index(0); auto addn = op::AddN("addn") .create_dynamic_input_x(2) // Create dynamic input x, including two inputs specified as the last inputs of the operator. .set_dynamic_input_x(0,data) // Set the first input. The value 0 indicates the index of the input (indexed from 0 by default). data indicates the input value. .set_dynamic_input_x(1,data) // Set the second input. The value 1 indicates the index of the input (indexed from 0 by default). data indicates the input value. .set_attr_N(2); // Set the attribute value of N to 2, indicating that the operator takes two inputs. |
You can also call create_dynamic_input_byindex_inputName to create a dynamic input with the input index specified. It is exclusive with create_dynamic_input_inputName, which uses the created dynamic input as the last input of the operator by default.
1 2 3 4 5 6 | auto concatv2 = op::ConcatV2("concatv2") .create_dynamic_input_byindex_x(2,0) // Create dynamic input x, including two inputs specified as the inputs of index 0 and index 1. The number 0 indicates the start position of the dynamic input index. .set_dynamic_input_x(0,data1) // Set the first input. The value 0 indicates the index of the input (indexed from 0 by default). data1 indicates the input value. .set_dynamic_input_x(1,data2) // Set the second input. The value 1 indicates the index of the input (indexed from 0 by default). data2 indicates the input value. .set_input_concat_dim(data3) // Set the third input. data3 indicates the input value. .set_attr_N(2); // Set the attribute value of N to 2, indicating that the operator takes two inputs. |
Parent topic: Operator Expression