Defining a Data Type Casting Operator (Cast)
To construct a graph from operator prototypes, it is necessary to guarantee that the connected operators have the same data types (dtype). Sometimes, you might need to insert Cast operators to implement data type consistency.
As shown in the following figure, the input of AddN operator is of type float32, but the output of the greater operator is of type bool. Therefore, a cast operator is inserted to cast the data type of the greater's output from bool to float32.

Cast operator prototype definition is as follows.
1 2 3 4 5 6 7 8 9 10 |
REG_OP(Cast) .INPUT(x, TensorType({DT_BOOL, DT_FLOAT16, DT_FLOAT, DT_INT8, DT_INT32, DT_UINT32, DT_UINT8, DT_INT64, DT_UINT64, DT_INT16, DT_UINT16, DT_DOUBLE, DT_COMPLEX64, DT_COMPLEX128, DT_QINT8, DT_QUINT8, DT_QINT16, DT_QUINT16, DT_QINT32, DT_BF16, DT_UINT1})) .OUTPUT(y, TensorType({DT_BOOL, DT_FLOAT16, DT_FLOAT, DT_INT8, DT_INT32, DT_UINT32, DT_UINT8, DT_INT64, DT_UINT64, DT_INT16, DT_UINT16, DT_DOUBLE, DT_COMPLEX64, DT_COMPLEX128, DT_QINT8, DT_QUINT8, DT_QINT16, DT_QUINT16, DT_QINT32, DT_BF16, DT_COMPLEX32})) .REQUIRED_ATTR(dst_type, Int) .OP_END_FACTORY_REG(Cast) |
As shown in the Cast operator prototype definition, the required attribute dst_type indicates the destination data type. If dst_type is set to 0, the destination data type is float32. For details about the mapping between the values and data types, see DataType.
1 2 3 4 5 6 7 |
auto greater = op::Greater("greater").set_input_x1(const1).set_input_x2(const2); auto cast = op::Cast("cast").set_input_x(greater) .set_attr_dst_type(0); auto addn = op::AddN("addn").create_dynamic_input_x(3) .set_dynamic_input_x(0,cast) .set_dynamic_input_x(1,data).set_dynamic_input_x(2,data) .set_attr_N(3); |