diff --git a/tests/depends/ops_stub/ops_stub.h b/tests/depends/ops_stub/ops_stub.h index 0299df1..2a261a8 100644 --- a/tests/depends/ops_stub/ops_stub.h +++ b/tests/depends/ops_stub/ops_stub.h @@ -108,6 +108,183 @@ REG_OP(PartitionedCall) .ATTR(config_proto, String, "") .ATTR(executor_type, String, "") .OP_END_FACTORY_REG(PartitionedCall) +REG_OP(TensorArray) + .INPUT(size, TensorType({DT_INT32})) + .OUTPUT(handle, TensorType({DT_RESOURCE})) + .OUTPUT(flow, TensorType({DT_FLOAT})) + .REQUIRED_ATTR(dtype, Type) + .ATTR(element_shape, ListInt, ge::UNKNOWN_RANK) + .ATTR(dynamic_size, Bool, false) + .ATTR(clear_after_read, Bool, true) + .ATTR(identical_element_shapes, Bool, false) + .ATTR(tensor_array_name, String, "") + .OP_END_FACTORY_REG(TensorArray) + +REG_OP(TensorArrayWrite) + .INPUT(handle, TensorType({DT_RESOURCE})) + .INPUT(index, TensorType({DT_INT32})) + .INPUT(value, TensorType({DT_FLOAT, DT_FLOAT16, DT_DOUBLE, DT_INT8, + DT_INT16, DT_UINT16, DT_UINT8, DT_INT32, DT_INT64, DT_BOOL, + DT_STRING, DT_COMPLEX64, DT_COMPLEX128})) + .INPUT(flow_in, TensorType({DT_FLOAT})) + .OUTPUT(flow_out, TensorType({DT_FLOAT})) + .OP_END_FACTORY_REG(TensorArrayWrite) + +REG_OP(AvgPool3DGrad) + .INPUT(orig_input_shape, TensorType({DT_INT32})) + .INPUT(grads, TensorType({DT_FLOAT16, DT_FLOAT32, DT_DOUBLE})) + .OUTPUT(output, TensorType({DT_FLOAT16, DT_FLOAT32, DT_DOUBLE})) + .REQUIRED_ATTR(ksize, ListInt) + .REQUIRED_ATTR(strides, ListInt) + .REQUIRED_ATTR(pads, ListInt) + .ATTR(ceil_mode, Bool, false) + .ATTR(count_include_pad, Bool, true) + .ATTR(divisor_override, Int, 0) + .ATTR(data_format, String, "NDHWC") + .OP_END_FACTORY_REG(AvgPool3DGrad) + +REG_OP(Merge) + .DYNAMIC_INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, + DT_INT8, DT_INT16, DT_INT32, DT_INT64, DT_UINT8, DT_UINT16, DT_UINT32, + DT_UINT64, DT_BOOL})) + .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, + DT_INT8, DT_INT16, DT_INT32, DT_INT64, DT_UINT8, DT_UINT16, DT_UINT32, + DT_UINT64, DT_BOOL})) + .OUTPUT(value_index, TensorType({DT_INT32})) + .OP_END_FACTORY_REG(Merge) + +REG_OP(NoOp) + .OP_END_FACTORY_REG(NoOp) + +REG_OP(VarIsInitializedOp) + .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16, DT_INT8, DT_INT16, DT_UINT16, DT_UINT8, + DT_INT32, DT_INT64, DT_UINT32, DT_UINT64, DT_BOOL, DT_DOUBLE})) + .OUTPUT(y, TensorType({DT_BOOL})) + .OP_END_FACTORY_REG(VarIsInitializedOp) + +REG_OP(AssignVariableOp) + .INPUT(resource, TensorType({DT_RESOURCE})) + .INPUT(value, TensorType({DT_FLOAT, DT_FLOAT16, DT_INT8, DT_INT16, \ + DT_UINT16, DT_UINT8, DT_INT32, DT_INT64, DT_BOOL, DT_DOUBLE})) + .REQUIRED_ATTR(dtype, Type) + .OP_END_FACTORY_REG(AssignVariableOp) + +REG_OP(ReadVariableOp) + .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16, DT_INT8, DT_INT16, DT_UINT16, DT_UINT8, + DT_INT32, DT_INT64, DT_UINT32, DT_UINT64, DT_BOOL, DT_DOUBLE})) + .OUTPUT(y, TensorType({DT_FLOAT, DT_FLOAT16, DT_INT8, DT_INT16, DT_UINT16, DT_UINT8, + DT_INT32, DT_INT64, DT_UINT32, DT_UINT64, DT_BOOL, DT_DOUBLE})) + .ATTR(dtype, Int, DT_INT32) + .OP_END_FACTORY_REG(ReadVariableOp) + +REG_OP(Reshape) + .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16, DT_INT8, DT_INT16, DT_UINT16, DT_UINT8, DT_INT32, + DT_INT64, DT_UINT32, DT_UINT64, DT_BOOL, DT_DOUBLE})) + .INPUT(shape, TensorType({DT_INT32, DT_INT64})) + .OUTPUT(y, TensorType({DT_FLOAT, DT_FLOAT16, DT_INT8, DT_INT16, DT_UINT16, DT_UINT8, DT_INT32, + DT_INT64, DT_UINT32, DT_UINT64, DT_BOOL, DT_DOUBLE})) + .ATTR(axis, Int, 0) + .ATTR(num_axes, Int, -1) + .OP_END_FACTORY_REG(Reshape) + +REG_OP(VarHandleOp) + .ATTR(container, String, "") + .ATTR(shared_name, String, "") + .REQUIRED_ATTR(dtype, Type) + .ATTR(shape, ListInt, ge::UNKNOWN_SHAPE) + .OUTPUT(y, TensorType({DT_RESOURCE})) + .OP_END_FACTORY_REG(VarHandleOp) + +REG_OP(Squeeze) + .INPUT(x, TensorType::ALL()) + .OUTPUT(y, TensorType::ALL()) + .ATTR(axis, ListInt, {}) + .OP_END_FACTORY_REG(Squeeze) + +REG_OP(Fill) + .INPUT(dims, TensorType::IndexNumberType()) + .INPUT(value, TensorType({DT_FLOAT, DT_DOUBLE, DT_INT32, DT_UINT8, DT_INT16, + DT_INT8, DT_COMPLEX64, DT_INT64, DT_BOOL, DT_QINT8, + DT_QUINT8, DT_QINT32, DT_QINT16, DT_QUINT16, DT_UINT16, + DT_COMPLEX128, DT_FLOAT16, DT_UINT32, DT_UINT64})) + .OUTPUT(y, TensorType({DT_FLOAT, DT_DOUBLE, DT_INT32, DT_UINT8, DT_INT16, + DT_INT8, DT_COMPLEX64, DT_INT64, DT_BOOL, DT_QINT8, + DT_QUINT8, DT_QINT32, DT_QINT16, DT_QUINT16, DT_UINT16, + DT_COMPLEX128, DT_FLOAT16, DT_UINT32, DT_UINT64})) + .OP_END_FACTORY_REG(Fill) + +REG_OP(ShapeN) + .DYNAMIC_INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16, DT_INT8, DT_INT16, DT_UINT16, DT_UINT8, + DT_INT32, DT_INT64, DT_UINT32, DT_UINT64, DT_BOOL, DT_DOUBLE})) + .DYNAMIC_OUTPUT(y, TensorType({DT_INT32, DT_INT64})) + .ATTR(dtype, Int, DT_INT32) + .OP_END_FACTORY_REG(ShapeN) + +REG_OP(Switch) + .INPUT(data, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, + DT_INT8, DT_INT16, DT_INT32, DT_INT64, DT_UINT8, DT_UINT16, DT_UINT32, + DT_UINT64, DT_BOOL})) + .INPUT(pred, TensorType({DT_BOOL})) + .OUTPUT(output_false, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, + DT_INT8, DT_INT16, DT_INT32, DT_INT64, DT_UINT8, DT_UINT16, DT_UINT32, + DT_UINT64, DT_BOOL})) + .OUTPUT(output_true, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, + DT_INT8, DT_INT16, DT_INT32, DT_INT64, DT_UINT8, DT_UINT16, DT_UINT32, + DT_UINT64, DT_BOOL})) + .OP_END_FACTORY_REG(Switch) + +REG_OP(RefSwitch) + .INPUT(data, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, + DT_INT8, DT_INT16, DT_INT32, DT_INT64, DT_UINT8, DT_UINT16, DT_UINT32, + DT_UINT64, DT_BOOL})) + .INPUT(pred, TensorType({DT_BOOL})) + .OUTPUT(output_false, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, + DT_INT8, DT_INT16, DT_INT32, DT_INT64, DT_UINT8, DT_UINT16, DT_UINT32, + DT_UINT64, DT_BOOL})) + .OUTPUT(output_true, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, + DT_INT8, DT_INT16, DT_INT32, DT_INT64, DT_UINT8, DT_UINT16, DT_UINT32, + DT_UINT64, DT_BOOL})) + .OP_END_FACTORY_REG(RefSwitch) + +REG_OP(Enter) + .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, + DT_INT8, DT_INT16, DT_INT32, DT_INT64, DT_UINT8, DT_UINT16, DT_UINT32, + DT_UINT64, DT_BOOL})) + .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, + DT_INT8, DT_INT16, DT_INT32, DT_INT64, DT_UINT8, DT_UINT16, DT_UINT32, + DT_UINT64, DT_BOOL})) + .REQUIRED_ATTR(frame_name, String) + .REQUIRED_ATTR(is_constant, Bool) + .OP_END_FACTORY_REG(Enter) + +REG_OP(VariableV2) + .INPUT(x, TensorType({DT_FLOAT, DT_FLOAT16, DT_INT8, DT_INT16, DT_UINT16, \ + DT_UINT8, DT_INT32, DT_INT64, DT_UINT32, DT_UINT64, DT_BOOL, DT_DOUBLE})) + .OUTPUT(y, TensorType({DT_FLOAT, DT_FLOAT16, DT_INT8, DT_INT16, DT_UINT16, \ + DT_UINT8, DT_INT32, DT_INT64, DT_UINT32, DT_UINT64, DT_BOOL, DT_DOUBLE})) + .ATTR(index, Int, 0) + .ATTR(value, Tensor, Tensor()) + .ATTR(container, String, "") + .ATTR(shared_name, String, "") + .OP_END_FACTORY_REG(VariableV2) + +REG_OP(Constant) + .OUTPUT(y, TensorType({DT_FLOAT, DT_FLOAT16, DT_INT8, DT_INT16, DT_UINT16, + DT_UINT8, DT_INT32, DT_INT64, DT_UINT32, DT_UINT64, DT_BOOL, DT_DOUBLE})) + .ATTR(value, Tensor, Tensor()) + .OP_END_FACTORY_REG(Constant) + +REG_OP(Mul) + .INPUT(x1, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, DT_UINT8, DT_INT8, + DI_UINT16, DT_INT16, DT_INT32, DT_INT64, + DT_COMPLEX64, DT_COMPLEX128})) + .INPUT(x2, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, DT_UINT8, DT_INT8, + DI_UINT16, DT_INT16, DT_INT32, DT_INT64, + DT_COMPLEX64, DT_COMPLEX128})) + .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, DT_UINT8, DT_INT8, + DI_UINT16, DT_INT16, DT_INT32, DT_INT64, + DT_COMPLEX64, DT_COMPLEX128})) + .OP_END_FACTORY_REG(Mul) // for plugin static Status ParseParamsStub(const google::protobuf::Message* op_src, ge::Operator& op_dest) { @@ -209,5 +386,36 @@ REGISTER_CUSTOM_OP("PartitionedCall") .OriginOpType({"ai.onnx::9::Clip"}) .ParseParamsFn(ParseParamsClipV9Stub) .ParseOpToGraphFn(ParseOpToGraphClipV9Stub); +REGISTER_CUSTOM_OP("TensorArray") + .FrameworkType(domi::TENSORFLOW) + .OriginOpType("TensorArrayV3") + .ParseParamsFn(ParseParamsStub); + +REGISTER_CUSTOM_OP("TensorArrayWrite") + .FrameworkType(domi::TENSORFLOW) + .OriginOpType("TensorArrayWriteV3") + .ParseParamsFn(ParseParamsStub); + +REGISTER_CUSTOM_OP("DynamicRNN") + .FrameworkType(domi::TENSORFLOW) + .OriginOpType("BlockLSTM") + .ParseParamsFn(ParseParamsStub); + +REGISTER_CUSTOM_OP("Merge") + .FrameworkType(domi::TENSORFLOW) + .OriginOpType("HistogramSummary") + .ParseParamsFn(ParseParamsStub); + +REGISTER_CUSTOM_OP("NoOp") + .FrameworkType(domi::TENSORFLOW) + .OriginOpType("NoOp") + .ParseParamsFn(ParseParamsStub); + +REGISTER_CUSTOM_OP("Fill") + .FrameworkType(domi::TENSORFLOW) + .OriginOpType("Fill") + .ParseParamsFn(ParseParamsStub); } // namespace ge + + #endif // MAIN_OPS_STUB_H diff --git a/tests/st/testcase/origin_models/avgpool3dgrad.pb.txt b/tests/st/testcase/origin_models/avgpool3dgrad.pb.txt new file mode 100644 index 0000000..31f8a32 Binary files /dev/null and b/tests/st/testcase/origin_models/avgpool3dgrad.pb.txt differ diff --git a/tests/st/testcase/origin_models/conv2d.pb b/tests/st/testcase/origin_models/conv2d.pb new file mode 100644 index 0000000..79843d5 Binary files /dev/null and b/tests/st/testcase/origin_models/conv2d.pb differ diff --git a/tests/st/testcase/origin_models/conv2d_depthwise_pb_gen.py b/tests/st/testcase/origin_models/conv2d_depthwise_pb_gen.py new file mode 100644 index 0000000..7ac9c2d --- /dev/null +++ b/tests/st/testcase/origin_models/conv2d_depthwise_pb_gen.py @@ -0,0 +1,26 @@ +import tensorflow as tf +import os + +pb_file_path = os.getcwd() + +with tf.compat.v1.Session(graph=tf.Graph()) as sess: + # NHWC + fmap_shape = [17, 101, 101, 17] + filter_size = [5, 5, 17, 1] + dy_shape = [17, 49, 49, 17] + strideh, stridew = [2, 2] + padding = 'VALID' + tensor_x1 = tf.compat.v1.placeholder(dtype="float16", shape=fmap_shape) + tensor_x2 = tf.compat.v1.placeholder(dtype="float16", shape=fmap_shape) + tensor_x = tf.add(tensor_x1, tensor_x2) + tensor_dy1 = tf.compat.v1.placeholder(dtype="float16", shape=dy_shape) + tensor_dy2 = tf.compat.v1.placeholder(dtype="float16", shape=dy_shape) + tensor_dy = tf.add(tensor_dy1, tensor_dy2) + + op = tf.nn.depthwise_conv2d_backprop_filter(tensor_x, filter_size, tensor_dy, + strides=[1, strideh, stridew, 1], + padding=padding, + data_format='NHWC', + dilations=[1,1,1,1]) + + tf.io.write_graph(sess.graph, logdir="./", name="test_depth_wise_conv2d.pb", as_text=False) diff --git a/tests/st/testcase/origin_models/merge.pb b/tests/st/testcase/origin_models/merge.pb new file mode 100644 index 0000000..0d2c053 Binary files /dev/null and b/tests/st/testcase/origin_models/merge.pb differ diff --git a/tests/st/testcase/origin_models/model.pb b/tests/st/testcase/origin_models/model.pb new file mode 100644 index 0000000..9bd83df Binary files /dev/null and b/tests/st/testcase/origin_models/model.pb differ diff --git a/tests/st/testcase/origin_models/tensor_array.pb b/tests/st/testcase/origin_models/tensor_array.pb new file mode 100644 index 0000000..9493e0b Binary files /dev/null and b/tests/st/testcase/origin_models/tensor_array.pb differ diff --git a/tests/st/testcase/origin_models/tensor_array_pb_gen.py b/tests/st/testcase/origin_models/tensor_array_pb_gen.py new file mode 100644 index 0000000..c035978 --- /dev/null +++ b/tests/st/testcase/origin_models/tensor_array_pb_gen.py @@ -0,0 +1,14 @@ +import tensorflow as tf +import os +from tensorflow.python.framework import graph_util +from tensorflow.python.ops import gen_data_flow_ops +import numpy as np + +with tf.compat.v1.Session(graph=tf.Graph()) as sess: + size = tf.compat.v1.placeholder(dtype="int32", shape=()) + value = tf.compat.v1.placeholder(dtype="float32", shape=(2,2)) + index = tf.compat.v1.placeholder(dtype="int32", shape=()) + flow = tf.compat.v1.placeholder(dtype="float32", shape=()) + handleTensor = gen_data_flow_ops.tensor_array_v3(size= size, dtype = np.float32) + output = gen_data_flow_ops.tensor_array_write_v3(handle = handleTensor[0], index=index, value=value, flow_in=flow) + tf.io.write_graph(sess.graph, logdir="./", name="tensor_array.pb", as_text=False) diff --git a/tests/st/testcase/origin_models/test.json b/tests/st/testcase/origin_models/test.json new file mode 100644 index 0000000..d1dbcc4 --- /dev/null +++ b/tests/st/testcase/origin_models/test.json @@ -0,0 +1,71 @@ +{ + "node": [ + { + "attr": [ + { + "key": "dtype", + "value": { + "type": "DT_HALF" + } + }, + { + "key": "shape", + "value": { + "shape": { + "dim": [ + { + "size": 1 + } + ] + } + } + } + ], + "name": "Placeholder", + "op": "Placeholder" + }, + { + "attr": [ + { + "key": "dtype", + "value": { + "type": "DT_HALF" + } + }, + { + "key": "shape", + "value": { + "shape": { + "dim": [ + { + "size": 1 + } + ] + } + } + } + ], + "name": "Placeholder_1", + "op": "Placeholder" + }, + { + "attr": [ + { + "key": "T", + "value": { + "type": "DT_HALF" + } + } + ], + "input": [ + "Placeholder", + "Placeholder_1" + ], + "name": "add_test_1", + "op": "Add" + } + ], + "versions": { + "producer": 134 + } +} \ No newline at end of file diff --git a/tests/st/testcase/origin_models/test_VarIsInitializedOp.pb b/tests/st/testcase/origin_models/test_VarIsInitializedOp.pb new file mode 100644 index 0000000..ac9f932 Binary files /dev/null and b/tests/st/testcase/origin_models/test_VarIsInitializedOp.pb differ diff --git a/tests/st/testcase/origin_models/test_VarIsInitializedOp_pb_gen.py b/tests/st/testcase/origin_models/test_VarIsInitializedOp_pb_gen.py new file mode 100644 index 0000000..79d86b6 --- /dev/null +++ b/tests/st/testcase/origin_models/test_VarIsInitializedOp_pb_gen.py @@ -0,0 +1,14 @@ +import tensorflow as tf +import numpy as np + +def generate_VarIsInitializedOp_pb(): + with tf.compat.v1.Session(graph=tf.Graph()) as sess: + x = tf.compat.v1.placeholder(dtype="int32", shape=()) + y = tf.Variable(tf.compat.v1.random_normal(shape=[4,3],mean=0,stddev=1), dtype="float32", name='y') + init = tf.compat.v1.global_variables_initializer() + sess.run(init) + op = tf.compat.v1.raw_ops.VarIsInitializedOp(resource=y, name="VarIsInitializedOp") + tf.io.write_graph(sess.graph, logdir="./", name="test_VarIsInitializedOp.pb", as_text=False) + +if __name__=='__main__': + generate_VarIsInitializedOp_pb() \ No newline at end of file diff --git a/tests/st/testcase/origin_models/test_VariableV2.pb b/tests/st/testcase/origin_models/test_VariableV2.pb new file mode 100644 index 0000000..1494ce5 Binary files /dev/null and b/tests/st/testcase/origin_models/test_VariableV2.pb differ diff --git a/tests/st/testcase/origin_models/test_avgpool3dgrad_pb_gen.py b/tests/st/testcase/origin_models/test_avgpool3dgrad_pb_gen.py new file mode 100644 index 0000000..a22f5ba --- /dev/null +++ b/tests/st/testcase/origin_models/test_avgpool3dgrad_pb_gen.py @@ -0,0 +1,41 @@ +import tensorflow as tf +import os +import numpy as np +from tensorflow.python.framework import graph_util + +pb_file_path = os.getcwd() + +def generate_case_1(): + with tf.compat.v1.Session(graph=tf.Graph()) as sess: + grads_1 = tf.compat.v1.placeholder(dtype="float16", shape=(1,2,2,2,1)) + grads_2 = tf.compat.v1.placeholder(dtype="float16", shape=(1,2,2,2,1)) + grads = tf.add(grads_1, grads_2) + orig_input_shape = tf.constant(np.array([1,3,3,3,1]).astype("int32")) + op = tf.raw_ops.AvgPool3DGrad(orig_input_shape=orig_input_shape, + grad=grads, + ksize=[1,2,2,2,1], + strides=[1,1,1,1,1], + padding="VALID", + data_format='NDHWC', + name='AvgPool3DGrad') + + tf.io.write_graph(sess.graph, logdir="./", name="avgpool3dgrad_case_1.pb", as_text=False) + +def generate_case_2(): + with tf.compat.v1.Session(graph=tf.Graph()) as sess: + grads_1 = tf.compat.v1.placeholder(dtype="float16", shape=(1,1,2,2,2)) + grads_2 = tf.compat.v1.placeholder(dtype="float16", shape=(1,1,2,2,2)) + grads = tf.add(grads_1, grads_2) + orig_input_shape = tf.constant(np.array([1,1,3,3,3]).astype("int32"), ) + op = tf.raw_ops.AvgPool3DGrad(orig_input_shape=orig_input_shape, + grad=grads, + ksize=[1,1,2,2,2], + strides=[1,1,1,1,1], + padding="VALID", + data_format='NCDHW', + name='AvgPool3DGrad') + + tf.io.write_graph(sess.graph, logdir="./", name="avgpool3dgrad.pb.txt", as_text=False) + +if __name__=='__main__': + generate_case_2() \ No newline at end of file diff --git a/tests/st/testcase/origin_models/test_blocklstm_pb.gen.py b/tests/st/testcase/origin_models/test_blocklstm_pb.gen.py new file mode 100644 index 0000000..b9d2908 --- /dev/null +++ b/tests/st/testcase/origin_models/test_blocklstm_pb.gen.py @@ -0,0 +1,34 @@ +import tensorflow as tf +import os + +pb_file_path = os.getcwd() + +def generate_case_0(): + with tf.compat.v1.Session(graph=tf.Graph()) as sess: + input_dtype = tf.float32 + input_shape0 = [1, ] + input_shape1 = [202, 1, 768] + input_shape2 = [1, 1] + input_shape3 = [1, 1] + input_shape4 = [769, 4] + input_shape5 = [1, ] + input_shape6 = [1, ] + input_shape7 = [1, ] + input_shape8 = [4, ] + + d0 = tf.compat.v1.placeholder(dtype=tf.int64, shape=input_shape0) + d1 = tf.compat.v1.placeholder(dtype=input_dtype, shape=input_shape1) + d2 = tf.compat.v1.placeholder(dtype=input_dtype, shape=input_shape2) + d3 = tf.compat.v1.placeholder(dtype=input_dtype, shape=input_shape3) + d4 = tf.compat.v1.placeholder(dtype=input_dtype, shape=input_shape4) + d5 = tf.compat.v1.placeholder(dtype=input_dtype, shape=input_shape5) + d6 = tf.compat.v1.placeholder(dtype=input_dtype, shape=input_shape6) + d7 = tf.compat.v1.placeholder(dtype=input_dtype, shape=input_shape7) + d8 = tf.compat.v1.placeholder(dtype=input_dtype, shape=input_shape8) + + i1, cs1, f1, o1, ci1, co1, h1 = tf.raw_ops.BlockLSTM(seq_len_max=d0, x=d1, cs_prev=d2, h_prev=d3, w=d4, wci=d5, wcf=d6, wco=d7, b=d8, + forget_bias=1, cell_clip=3, use_peephole=False, name="blockLSTM") + tf.io.write_graph(sess.graph, logdir="./", name="blocklstm_case.pb", as_text=False) + +if __name__=='__main__': + generate_case_0() diff --git a/tests/st/testcase/origin_models/test_constant.pb b/tests/st/testcase/origin_models/test_constant.pb new file mode 100644 index 0000000..31f8a32 Binary files /dev/null and b/tests/st/testcase/origin_models/test_constant.pb differ diff --git a/tests/st/testcase/origin_models/test_constant_pb_gen.py b/tests/st/testcase/origin_models/test_constant_pb_gen.py new file mode 100644 index 0000000..45921a4 --- /dev/null +++ b/tests/st/testcase/origin_models/test_constant_pb_gen.py @@ -0,0 +1,21 @@ +import tensorflow as tf +import numpy as np +from tensorflow.python.framework import graph_util + +def generate_constant_pb(): + with tf.compat.v1.Session(graph=tf.Graph()) as sess: + grads_1 = tf.compat.v1.placeholder(dtype="float16", shape=(1,1,2,2,2)) + grads_2 = tf.compat.v1.placeholder(dtype="float16", shape=(1,1,2,2,2)) + grads = tf.add(grads_1, grads_2) + orig_input_shape = tf.constant(np.array([1,1,3,3,3]).astype("int32"), ) + op = tf.raw_ops.AvgPool3DGrad(orig_input_shape=orig_input_shape, + grad=grads, + ksize=[1,1,2,2,2], + strides=[1,1,1,1,1], + padding="VALID", + data_format='NCDHW', + name='AvgPool3DGrad') + tf.io.write_graph(sess.graph, logdir="./", name="test_constant.pb", as_text=False) + +if __name__ == "__main__": + generate_constant_pb() diff --git a/tests/st/testcase/origin_models/test_conv2d_pb_gen.py b/tests/st/testcase/origin_models/test_conv2d_pb_gen.py new file mode 100644 index 0000000..defd5f3 --- /dev/null +++ b/tests/st/testcase/origin_models/test_conv2d_pb_gen.py @@ -0,0 +1,26 @@ +import tensorflow as tf +import os +from tensorflow.python.framework import graph_util + +pb_file_path = os.getcwd() + +def generate_conv2d_pb(): + with tf.compat.v1.Session(graph=tf.Graph()) as sess: + input_x = tf.compat.v1.placeholder(dtype="float32", shape=(1,56,56,64)) + input_filter = tf.compat.v1.placeholder(dtype="float32", shape=(3,3,64,64)) + op = tf.nn.conv2d(input_x, input_filter, strides=[1,1,1,1], padding=[[0,0],[1,1],[1,1],[0,0]], + data_format="NHWC", dilations=[1,1,1,1], name='conv2d_res') + tf.io.write_graph(sess.graph, logdir="./", name="conv2d.pb", as_text=False) + +def generate_add_pb(): + with tf.compat.v1.Session(graph=tf.Graph()) as sess: + x = tf.compat.v1.placeholder(tf.int32, name='x') + y = tf.compat.v1.placeholder(tf.int32, name='y') + b = tf.Variable(1, name='b') + xy = tf.multiply(x, y) + op = tf.add(xy, b, name='op_to_store') + tf.io.write_graph(sess.graph, logdir="./", name="model.pb", as_text=False) + +if __name__=='__main__': + generate_conv2d_pb() + generate_add_pb() \ No newline at end of file diff --git a/tests/st/testcase/origin_models/test_depth_wise_conv2d.pb b/tests/st/testcase/origin_models/test_depth_wise_conv2d.pb new file mode 100644 index 0000000..1b050bb Binary files /dev/null and b/tests/st/testcase/origin_models/test_depth_wise_conv2d.pb differ diff --git a/tests/st/testcase/origin_models/test_enter.pb b/tests/st/testcase/origin_models/test_enter.pb new file mode 100644 index 0000000..a933678 Binary files /dev/null and b/tests/st/testcase/origin_models/test_enter.pb differ diff --git a/tests/st/testcase/origin_models/test_enter_pb_gen.py b/tests/st/testcase/origin_models/test_enter_pb_gen.py new file mode 100644 index 0000000..291dfd8 --- /dev/null +++ b/tests/st/testcase/origin_models/test_enter_pb_gen.py @@ -0,0 +1,13 @@ +import tensorflow as tf +from tensorflow.python.ops import control_flow_ops + +def generate_enter_pb(): + with tf.compat.v1.Session(graph=tf.Graph()) as sess: + x = tf.compat.v1.placeholder(dtype="int32", shape=()) + y = tf.compat.v1.placeholder(dtype="int32", shape=()) + output1 = control_flow_ops.enter(x, frame_name = "output1") + output2 = control_flow_ops.enter(y, frame_name = "output2") + tf.io.write_graph(sess.graph, logdir="./", name="test_enter.pb", as_text=False) + +if __name__=='__main__': + generate_enter_pb() \ No newline at end of file diff --git a/tests/st/testcase/origin_models/test_fill.pb b/tests/st/testcase/origin_models/test_fill.pb new file mode 100644 index 0000000..f351f14 Binary files /dev/null and b/tests/st/testcase/origin_models/test_fill.pb differ diff --git a/tests/st/testcase/origin_models/test_fill_pb_gen.py b/tests/st/testcase/origin_models/test_fill_pb_gen.py new file mode 100644 index 0000000..d7ed9f1 --- /dev/null +++ b/tests/st/testcase/origin_models/test_fill_pb_gen.py @@ -0,0 +1,12 @@ +import tensorflow as tf +import numpy as np + +def generate_fill_pb(): + with tf.compat.v1.Session(graph=tf.Graph()) as sess: + x = tf.compat.v1.placeholder(dtype="int32", shape=(2,2)) + y = tf.fill([1,2], value = 5) + z = tf.add(x,y) + tf.io.write_graph(sess.graph, logdir="./", name="test_fill.pb", as_text=False) + +if __name__ == "__main__": + generate_fill_pb() \ No newline at end of file diff --git a/tests/st/testcase/origin_models/test_identity.pb b/tests/st/testcase/origin_models/test_identity.pb new file mode 100644 index 0000000..8d8184e Binary files /dev/null and b/tests/st/testcase/origin_models/test_identity.pb differ diff --git a/tests/st/testcase/origin_models/test_identity_pb_gen.py b/tests/st/testcase/origin_models/test_identity_pb_gen.py new file mode 100644 index 0000000..1ff3781 --- /dev/null +++ b/tests/st/testcase/origin_models/test_identity_pb_gen.py @@ -0,0 +1,13 @@ +import tensorflow as tf + +def generate_identity_pb(): + with tf.compat.v1.Session(graph=tf.Graph()) as sess: + x = tf.compat.v1.placeholder(dtype="int32", shape=()) + x_plus_1 = tf.add(x, 1, name='x_plus') + with tf.control_dependencies([x_plus_1]): + y = x + z = tf.identity(x,name='identity') + tf.io.write_graph(sess.graph, logdir="./", name="test_identity.pb", as_text=False) + +if __name__=='__main__': + generate_identity_pb() \ No newline at end of file diff --git a/tests/st/testcase/origin_models/test_merge_pb_gen.py b/tests/st/testcase/origin_models/test_merge_pb_gen.py new file mode 100644 index 0000000..709f81b --- /dev/null +++ b/tests/st/testcase/origin_models/test_merge_pb_gen.py @@ -0,0 +1,18 @@ +import tensorflow as tf +from tensorflow.python.framework import graph_util +import numpy as np + +def generate_merge_pb(): + with tf.compat.v1.Session(graph=tf.Graph()) as sess: + dist = tf.compat.v1.placeholder(tf.float32, [100]) + tf.compat.v1.summary.histogram(name="Merge", values=dist) + writer = tf.compat.v1.summary.FileWriter("./tf_summary_merge_pb") + op = tf.compat.v1.summary.merge_all() + for step in range(10): + mean_moving_normal = np.random.normal(loc=step, scale=1, size=[100]) + summ = sess.run(op, feed_dict = {dist : mean_moving_normal}) + writer.add_summary(summ, global_step=step) + tf.io.write_graph(sess.graph, logdir="./", name="merge.pb", as_text=False) + +if __name__=='__main__': + generate_merge_pb() diff --git a/tests/st/testcase/origin_models/test_no_op.pb b/tests/st/testcase/origin_models/test_no_op.pb new file mode 100644 index 0000000..a56f4ac Binary files /dev/null and b/tests/st/testcase/origin_models/test_no_op.pb differ diff --git a/tests/st/testcase/origin_models/test_no_op_pb_gen.py b/tests/st/testcase/origin_models/test_no_op_pb_gen.py new file mode 100644 index 0000000..1e4ba29 --- /dev/null +++ b/tests/st/testcase/origin_models/test_no_op_pb_gen.py @@ -0,0 +1,14 @@ +import tensorflow as tf +import numpy as np + +def generate_no_op_pb(): + with tf.compat.v1.Session(graph=tf.Graph()) as sess: + x = tf.compat.v1.placeholder(dtype="int32", shape=()) + y = tf.compat.v1.placeholder(dtype="int32", shape=()) + add_op = tf.add(x, y) + y2 = tf.no_op(name="train") + z = tf.group([add_op, y2]) + tf.io.write_graph(sess.graph, logdir="./", name="test_no_op.pb", as_text=False) + +if __name__=='__main__': + generate_no_op_pb() \ No newline at end of file diff --git a/tests/st/testcase/origin_models/test_reshape.pb b/tests/st/testcase/origin_models/test_reshape.pb new file mode 100644 index 0000000..1b772ba Binary files /dev/null and b/tests/st/testcase/origin_models/test_reshape.pb differ diff --git a/tests/st/testcase/origin_models/test_reshape_pb_gen.py b/tests/st/testcase/origin_models/test_reshape_pb_gen.py new file mode 100644 index 0000000..f65ea67 --- /dev/null +++ b/tests/st/testcase/origin_models/test_reshape_pb_gen.py @@ -0,0 +1,10 @@ +import tensorflow as tf + +def generate_reshape_pb(): + with tf.compat.v1.Session(graph=tf.Graph()) as sess: + x = tf.compat.v1.placeholder(dtype="int32", shape=(2,2)) + y = tf.compat.v1.reshape(x, [2,2]) + tf.io.write_graph(sess.graph, logdir="./", name="test_reshape.pb", as_text=False) + +if __name__ == "__main__": + generate_reshape_pb() \ No newline at end of file diff --git a/tests/st/testcase/origin_models/test_sequeeze.pb b/tests/st/testcase/origin_models/test_sequeeze.pb new file mode 100644 index 0000000..3c301e6 Binary files /dev/null and b/tests/st/testcase/origin_models/test_sequeeze.pb differ diff --git a/tests/st/testcase/origin_models/test_sequeeze_pb_gen.py b/tests/st/testcase/origin_models/test_sequeeze_pb_gen.py new file mode 100644 index 0000000..b9a4078 --- /dev/null +++ b/tests/st/testcase/origin_models/test_sequeeze_pb_gen.py @@ -0,0 +1,13 @@ +import tensorflow as tf +import numpy as np + +def generate_sequeeze_pb(): + with tf.compat.v1.Session(graph=tf.Graph()) as sess: + x = tf.compat.v1.placeholder(dtype="int32", shape=(2,2)) + y = tf.constant([[1,2],[2,3]]) + z = tf.add(x,y) + op = tf.squeeze(z,name = "squeeze") + tf.io.write_graph(sess.graph, logdir="./", name="test_sequeeze.pb", as_text=False) + +if __name__ == "__main__": + generate_sequeeze_pb() \ No newline at end of file diff --git a/tests/st/testcase/origin_models/test_shape_n.pb b/tests/st/testcase/origin_models/test_shape_n.pb new file mode 100644 index 0000000..39ce087 Binary files /dev/null and b/tests/st/testcase/origin_models/test_shape_n.pb differ diff --git a/tests/st/testcase/origin_models/test_shape_n_pb_gen.py b/tests/st/testcase/origin_models/test_shape_n_pb_gen.py new file mode 100644 index 0000000..2f29597 --- /dev/null +++ b/tests/st/testcase/origin_models/test_shape_n_pb_gen.py @@ -0,0 +1,11 @@ +import tensorflow as tf +import numpy as np + +def generate_shape_n_pb(): + with tf.compat.v1.Session(graph=tf.Graph()) as sess: + x = tf.compat.v1.placeholder(dtype="int32", shape=(2,2)) + y = tf.shape_n([1,2], name= "shape_n") + tf.io.write_graph(sess.graph, logdir="./", name="test_shape_n.pb", as_text=False) + +if __name__ == "__main__": + generate_shape_n_pb() \ No newline at end of file diff --git a/tests/st/testcase/origin_models/test_switch.pb b/tests/st/testcase/origin_models/test_switch.pb new file mode 100644 index 0000000..7379e78 Binary files /dev/null and b/tests/st/testcase/origin_models/test_switch.pb differ diff --git a/tests/st/testcase/origin_models/test_switch_pb_gen.py b/tests/st/testcase/origin_models/test_switch_pb_gen.py new file mode 100644 index 0000000..5b2f87c --- /dev/null +++ b/tests/st/testcase/origin_models/test_switch_pb_gen.py @@ -0,0 +1,13 @@ +import tensorflow as tf +from tensorflow.python.ops import control_flow_ops + +def generate_switch_pb(): + with tf.compat.v1.Session(graph=tf.Graph()) as sess: + x = tf.compat.v1.placeholder(dtype="int32", shape=()) + y = tf.compat.v1.placeholder(dtype="int32", shape=()) + output1 = control_flow_ops.switch(x,False) + output2 = control_flow_ops.switch(y,True) + tf.io.write_graph(sess.graph, logdir="./", name="test_switch.pb", as_text=False) + +if __name__=='__main__': + generate_switch_pb() \ No newline at end of file diff --git a/tests/st/testcase/origin_models/test_variableV2_pb_gen.py b/tests/st/testcase/origin_models/test_variableV2_pb_gen.py new file mode 100644 index 0000000..fa1416c --- /dev/null +++ b/tests/st/testcase/origin_models/test_variableV2_pb_gen.py @@ -0,0 +1,13 @@ +import tensorflow as tf + +def generate_VariableV2_pb(): + with tf.compat.v1.Session(graph=tf.Graph()) as sess: + x = tf.compat.v1.placeholder(dtype="int32", shape=(2,3)) + op = tf.raw_ops.VariableV2(shape=[2,3], dtype="int32", name="VariableV2") + init = tf.compat.v1.global_variables_initializer() + op_add = tf.add(x,op) + sess.run(init) + tf.io.write_graph(sess.graph, logdir="./", name="test_VariableV2.pb", as_text=False) + +if __name__=='__main__': + generate_VariableV2_pb() \ No newline at end of file diff --git a/tests/st/testcase/test_tensorflow_parser.cc b/tests/st/testcase/test_tensorflow_parser.cc index 454ac91..3366c47 100644 --- a/tests/st/testcase/test_tensorflow_parser.cc +++ b/tests/st/testcase/test_tensorflow_parser.cc @@ -15,14 +15,38 @@ */ #include + +#define protected public +#define private public #include "parser/common/op_parser_factory.h" #include "parser/tensorflow/tensorflow_parser.h" #include "graph/operator_reg.h" #include "register/op_registry.h" +#include "external/register/register.h" #include "parser/common/register_tbe.h" -#include "external/parser/tensorflow_parser.h" #include "st/parser_st_utils.h" #include "tests/depends/ops_stub/ops_stub.h" +#include "parser/common/acl_graph_parser_util.h" +#include "metadef/third_party/graphengine/inc/external/ge/ge_api_types.h" +#include "omg/parser/parser_factory.h" +#include "common/pre_checker.h" +#include "common/util.h" +#include "external/parser/tensorflow_parser.h" +#include "parser/tensorflow/tensorflow_constant_parser.h" +#include "common/types.h" +#include "parser/common/op_def/variable_op.h" +#include "parser/tensorflow/tensorflow_ref_switch_parser.h" +#undef protected +#undef private + +using namespace std; +using namespace domi::tensorflow; +using namespace domi; +using namespace testing; +using namespace std; +using namespace google::protobuf; + +static const string GRAPH_DEFAULT_NAME = "default"; namespace ge { class STestTensorflowParser : public testing::Test { @@ -55,6 +79,378 @@ void STestTensorflowParser::RegisterCustomOp() { domi::OpRegistry::Instance()->registrationDatas.clear(); } +namespace { + NodeDef *initNodeDef() + { + NodeDef * nodeDef = new NodeDef(); + nodeDef->set_op("Const"); + ::google::protobuf::Map< ::std::string, ::tensorflow::AttrValue >* node_attr_map = nodeDef->mutable_attr(); + + //设置 T属性 + domi::tensorflow::AttrValue t_attr_value; + t_attr_value.set_type(domi::tensorflow::DT_INT32); + (*node_attr_map)[TENSORFLOW_ATTR_T] = t_attr_value; + + domi::tensorflow::AttrValue dtype_attr_value; + dtype_attr_value.set_type(domi::tensorflow::DT_INT32); + (*node_attr_map)[TENSORFLOW_ATTR_DTYPE] = dtype_attr_value; + + // out_put + domi::tensorflow::AttrValue outputs_attr_value; + ::tensorflow::AttrValue_ListValue* list = outputs_attr_value.mutable_list(); + list->add_s("MatMul"); + (*node_attr_map)[TENSORFLOW_ATTR_OUTPUT_OP] = outputs_attr_value; + + // 设置 tensor 属性 + domi::tensorflow::AttrValue value_attr_value; + ::tensorflow::TensorProto* tensor = value_attr_value.mutable_tensor(); + ::tensorflow::TensorShapeProto* tensor_shape = tensor->mutable_tensor_shape(); + tensor_shape->clear_dim(); + tensor_shape->add_dim()->set_size(4); + tensor_shape->add_dim()->set_size(6); + tensor->set_dtype(domi::tensorflow::DT_INT32); + + float *addr = new float[24]; + for (int32_t i = 0; i < 24; i++) + { + *(addr + i) = 1.0 + i; + } + tensor->set_tensor_content((void *)addr, 24 * sizeof(float)); + + (*node_attr_map)[TENSORFLOW_ATTR_VALUE] = value_attr_value; + delete[] addr; + return nodeDef; + } + + NodeDef *MallocNodeDef(const string &name, const string &type) { + NodeDef* node_def = new (std::nothrow) NodeDef(); + if (node_def != nullptr) { + node_def->set_name(name); + node_def->set_op(type); + } + return node_def; + } + + void GenOriginNodeDef(ge::TensorFlowModelParser *tensorflow_parser, vector &node_name_list) { + NodeDef* pre_node_a = MallocNodeDef("pre_node_a", "Const"); + EXPECT_NE(pre_node_a, nullptr); + { + ::google::protobuf::Map< ::std::string, ::tensorflow::AttrValue >* node_attr_map = pre_node_a->mutable_attr(); + tensorflow::AttrValue attr_dtype; + attr_dtype.set_type(tensorflow::DT_FLOAT); + (*node_attr_map)["dtype"] = attr_dtype; + tensorflow::AttrValue attr_value; + tensorflow::TensorProto* tensor = attr_value.mutable_tensor(); + tensor->add_bool_val(true); + tensor->set_dtype(tensorflow::DT_BOOL); + (*node_attr_map)["value"] = attr_value; + } + tensorflow_parser->nodedef_map_["pre_node_a"] = pre_node_a; + node_name_list.push_back("pre_node_a"); + + NodeDef* pre_node_ctrl_in = MallocNodeDef("pre_node_ctrl_in", "Const"); + EXPECT_NE(pre_node_ctrl_in, nullptr); + { + ::google::protobuf::Map< ::std::string, ::tensorflow::AttrValue >* node_attr_map = pre_node_ctrl_in->mutable_attr(); + tensorflow::AttrValue attr_dtype; + attr_dtype.set_type(tensorflow::DT_FLOAT); + (*node_attr_map)["dtype"] = attr_dtype; + tensorflow::AttrValue attr_value; + tensorflow::TensorProto* tensor = attr_value.mutable_tensor(); + tensor->add_bool_val(true); + tensor->set_dtype(tensorflow::DT_BOOL); + (*node_attr_map)["value"] = attr_value; + } + tensorflow_parser->nodedef_map_["pre_node_ctrl_in"] = pre_node_ctrl_in; + node_name_list.push_back("pre_node_ctrl_in"); + + NodeDef* post_node_b = MallocNodeDef("post_node_b", "Identity"); + EXPECT_NE(post_node_b, nullptr); + tensorflow_parser->nodedef_map_["post_node_b"] = post_node_b; + node_name_list.push_back("post_node_b"); + + NodeDef* post_node_c = MallocNodeDef("post_node_c", "Identity"); + EXPECT_NE(post_node_c, nullptr); + tensorflow_parser->nodedef_map_["post_node_c"] = post_node_c; + node_name_list.push_back("post_node_c"); + + NodeDef* post_node_d = MallocNodeDef("post_node_d", "Identity"); + EXPECT_NE(post_node_d, nullptr); + tensorflow_parser->nodedef_map_["post_node_d"] = post_node_d; + node_name_list.push_back("post_node_d"); + } + + void FreeNodeDefMap(ge::TensorFlowModelParser *tensorflow_parser, set &malloc_node_name_list) { + for (auto &item : tensorflow_parser->nodedef_map_) { + if (item.second != nullptr && malloc_node_name_list.count(item.first) > 0) { + delete (item.second); + item.second = nullptr; + } + } + } + void GenFusionScopesResult(shared_ptr &scope_graph, FusionScopesResult *fusion_rlt, + const string &fusion_op_name) { + if (fusion_rlt == nullptr) { + return; + } + fusion_rlt->InsertInputs("scope_node_1", {0}); // scope input 0 + fusion_rlt->InsertOutputs("scope_node_m", {0}); // scope output 0 + fusion_rlt->InsertOutputs("scope_node_n", {1}); // scope output 1 + + fusion_rlt->SetType(ge::kScopeToMultiNodes); + fusion_rlt->SetName(fusion_op_name); + fusion_rlt->SetDescription("Description for fusion node"); + + // Add inner nodes in sequence. + auto node1 = fusion_rlt->AddInnerNode("inner_node_1", "Unique"); // add inner node1 + CHECK_INNER_NODE_CONDITION(node1 != nullptr, fusion_rlt); + auto ret = node1 + ->InsertInput(ge::kInputFromFusionScope, 0) // Input from 0th of boundary (a) + .InsertOutput(ge::kOutputToFusionScope, 0) // Output to 0th of boundary (b) + .InsertOutput("inner_node_2", 0) // Output to input 0th of internal node 2 + .BuildInnerNode(); // Construct an internal Operator + CHECK_INNER_NODE_CONDITION(ret == ge::GRAPH_SUCCESS, fusion_rlt); + string str_val = "This is a string."; + node1->MutableOperator()->SetAttr("key1", 2); // Set integer attribute + node1->MutableOperator()->SetAttr("key2", str_val); // Set the string attribute + node1->MutableOperator()->SetAttr("key3", true); // Set boolean attribute + + auto node2 = fusion_rlt->AddInnerNode("inner_node_2", "Identity"); // add inner node2 + CHECK_INNER_NODE_CONDITION(node2 != nullptr, fusion_rlt); + ret = node2 + ->InsertInput("inner_node_1", 1) // The input comes from the 1st output of internal node 1 + .InsertOutput("inner_node_3", 0) // Output to input 0th of internal node 3 + .BuildInnerNode(); + CHECK_INNER_NODE_CONDITION(ret == ge::GRAPH_SUCCESS, fusion_rlt); + node2->SetInputFormat("x", "NHWC"); + node2->SetOutputFormat("y", "NHWC"); + + auto node3 = fusion_rlt->AddInnerNode("inner_node_3", "Identity"); // add inner node3 + CHECK_INNER_NODE_CONDITION(node3 != nullptr, fusion_rlt); + ret = node3 + ->InsertInput("inner_node_2", 0) // The input comes from the 0th output of internal node 2 + .InsertOutput(ge::kOutputToFusionScope, 1) // Output to 1st of boundary (c) + .BuildInnerNode(); + CHECK_INNER_NODE_CONDITION(ret == ge::GRAPH_SUCCESS, fusion_rlt); + + scope_graph->impl_->AddFusionScopesResult(fusion_rlt); + } + + void GenOriginContext(ge::TensorFlowModelParser *tensorflow_parser, const string &fusion_op_name) { + // op_node_context for fusion op + ge::OpNodeContext op_node_context; + op_node_context.input_map["pre_node_a"].push_back({0, 0}); + op_node_context.input_map["pre_node_ctrl_in"].push_back({-1, -1}); // ctrl edges + op_node_context.output_map["post_node_b"].push_back({0, 0}); + op_node_context.output_map["post_node_c"].push_back({1, 0}); + op_node_context.output_map["post_node_d"].push_back({-1, -1}); // ctrl edges + tensorflow_parser->op_node_context_map_[fusion_op_name] = op_node_context; + tensorflow_parser->SaveEdgesControlInfo(fusion_op_name, -1); + + // op_node_context for pre_node_a + ge::OpNodeContext op_node_context_a; + op_node_context_a.output_map[fusion_op_name].push_back({0, 0}); + tensorflow_parser->op_node_context_map_["pre_node_a"] = op_node_context_a; + + // op_node_context for pre_node_ctrl_in + ge::OpNodeContext op_node_context_ctrl_in; + op_node_context_ctrl_in.output_map[fusion_op_name].push_back({-1, -1}); // ctrl edges + tensorflow_parser->op_node_context_map_["pre_node_ctrl_in"] = op_node_context_ctrl_in; + + // op_node_context for post_node_b + ge::OpNodeContext op_node_context_b; + op_node_context_b.input_map[fusion_op_name].push_back({0, 0}); + tensorflow_parser->op_node_context_map_["post_node_b"] = op_node_context_b; + + // op_node_context for post_node_c + ge::OpNodeContext op_node_context_c; + op_node_context_c.input_map[fusion_op_name].push_back({1, 0}); + op_node_context_c.output_map["post_node_d"].push_back({0, 0}); + tensorflow_parser->op_node_context_map_["post_node_c"] = op_node_context_c; + + // op_node_context for post_node_d + ge::OpNodeContext op_node_context_d; + op_node_context_d.input_map["post_node_c"].push_back({0, 0}); + op_node_context_d.input_map[fusion_op_name].push_back({-1, -1}); // ctrl edges + tensorflow_parser->op_node_context_map_["post_node_d"] = op_node_context_d; + tensorflow_parser->SaveEdgesControlInfo("post_node_d", -1); + + string fusion_op_type = ge::kScopeToMultiNodes; + string description = "fusion op description"; + tensorflow_parser->fusion_op_type_map_[fusion_op_name].push_back(fusion_op_type); + tensorflow_parser->fusion_op_type_map_[fusion_op_name].push_back(description); + } + void register_tbe_op() + { + std::vector registrationDatas = OpRegistry::Instance()->registrationDatas; + for(OpRegistrationData reg_data : registrationDatas) + { + OpRegistrationTbe::Instance()->Finalize(reg_data); + OpRegistry::Instance()->Register(reg_data); + } + OpRegistry::Instance()->registrationDatas.clear(); + } +} + +namespace { +REG_OP(Data) + .INPUT(x, TensorType::ALL()) + .OUTPUT(y, TensorType::ALL()) + .ATTR(index, Int, 0) + .OP_END_FACTORY_REG(Data) + +REG_OP(Add) + .INPUT(x1, TensorType({DT_FLOAT, DT_INT32, DT_INT64, DT_FLOAT16, DT_INT16, + DT_INT8, DT_UINT8, DT_DOUBLE, DT_COMPLEX128, + DT_COMPLEX64, DT_STRING})) + .INPUT(x2, TensorType({DT_FLOAT, DT_INT32, DT_INT64, DT_FLOAT16, DT_INT16, + DT_INT8, DT_UINT8, DT_DOUBLE, DT_COMPLEX128, + DT_COMPLEX64, DT_STRING})) + .OUTPUT(y, TensorType({DT_FLOAT, DT_INT32, DT_INT64, DT_FLOAT16, DT_INT16, + DT_INT8, DT_UINT8, DT_DOUBLE, DT_COMPLEX128, + DT_COMPLEX64, DT_STRING})) + .OP_END_FACTORY_REG(Add) +} + +struct MemBuffer +{ + void* data; + uint32_t size; +}; + +static MemBuffer* MemBufferFromFile(const char *path) +{ + char path_temp[PATH_MAX + 1] = {0x00}; + if(strlen(path) > PATH_MAX || nullptr == realpath(path, path_temp)) { + return nullptr; + } + FILE *fp = fopen(path_temp, "r+"); + if (fp == nullptr) { + return nullptr; + } + + // get model file length + if (0 != fseek(fp, 0, SEEK_END)) { + fclose(fp); + return nullptr; + } + long file_length = ftell(fp); + if (fseek(fp, 0, SEEK_SET)) { + fclose(fp); + return nullptr; + } + if (file_length <= 0) { + fclose(fp); + return nullptr; + } + + // alloc model buffer + void *data = malloc((unsigned int)file_length); + if (!data) { + fclose(fp); + return nullptr; + } + + // read file into memory + uint32_t read_size = (uint32_t)fread(data, 1, (unsigned int)file_length, fp); + + // check if read success + if ((long)read_size != file_length) { + free(data); + data = nullptr; + fclose(fp); + return nullptr; + } + + // close model file + fclose(fp); + + // create an MemBuffer + MemBuffer* membuf = new MemBuffer(); + if (!membuf) { + free(data); + data = nullptr; + return nullptr; + } + membuf->data = malloc((unsigned int)read_size); + + // set size && data + membuf->size = (uint32_t)read_size; + memcpy((char*)membuf->data, (char*)data, read_size); + free(data); + return membuf; +} + + +/// placeholder0 placeholder1 +/// | /\ /\ | +/// | / \/ \ | +/// | / /\ \ | +/// | | / \ | | +/// | add0 mul0 | +/// | / /c | \ | +/// mul1 --- / | add1 +/// \ | | +/// \ ---- add2 | +/// | | +/// retval0 retval1 + +void CreateGraphDef(domi::tensorflow::GraphDef &graph_def) { + // 1. add node + auto placeholder0 = graph_def.add_node(); + auto placeholder1 = graph_def.add_node(); + auto add0 = graph_def.add_node(); + auto add1 = graph_def.add_node(); + auto mul0 = graph_def.add_node(); + auto mul1 = graph_def.add_node(); + auto add2 = graph_def.add_node(); + auto retval0 = graph_def.add_node(); + auto retval1 = graph_def.add_node(); + + // 2. set info + placeholder0->set_name("placeholder0"); + placeholder0->set_op("PlaceHolder"); + placeholder1->set_name("placeholder1"); + placeholder1->set_op("PlaceHolder"); + + add0->set_name("add0"); + add0->set_op("Add"); + add1->set_name("add1"); + add1->set_op("Add"); + add2->set_name("add2"); + add2->set_op("Add"); + + mul0->set_name("mul0"); + mul0->set_op("Mul"); + mul1->set_name("mul1"); + mul1->set_op("Mul"); + + retval0->set_name("retval0"); + retval0->set_op("_RetVal"); + retval1->set_name("retval1"); + retval1->set_op("_RetVal"); + + // 3. add edges + add0->add_input("placeholder0"); + add0->add_input("placeholder1"); + + mul0->add_input("placeholder0"); + mul0->add_input("placeholder1"); + + mul1->add_input("placeholder0"); + mul1->add_input("add0"); + mul1->add_input("^mul0"); + + add1->add_input("mul0"); + add1->add_input("placeholder1"); + + add2->add_input("mul1"); + add2->add_input("mul0"); + + retval0->add_input("add2:0"); + retval1->add_input("add1:0"); +} + TEST_F(STestTensorflowParser, tensorflow_parser_success) { RegisterCustomOp(); @@ -75,4 +471,662 @@ TEST_F(STestTensorflowParser, tensorflow_parser_success) { ASSERT_EQ(net_out_name.size(), 1); EXPECT_EQ(net_out_name.at(0), "add_test_1:0"); } + +TEST_F(STestTensorflowParser, tensorflow_model_Failed) { + ge::Graph graph; + std::string caseDir = __FILE__; + std::size_t idx = caseDir.find_last_of("/"); + caseDir = caseDir.substr(0, idx); + + std::string modelFile = caseDir + "/origin_models/model.pb"; + auto status = ge::aclgrphParseTensorFlow(modelFile.c_str(), graph); + EXPECT_EQ(status, ge::SUCCESS); + + modelFile = caseDir + "/origin_models/test_depth_wise_conv2d.pb"; + status = ge::aclgrphParseTensorFlow(modelFile.c_str(), graph); + EXPECT_EQ(status, ge::GRAPH_FAILED); +} + +TEST_F(STestTensorflowParser, tensorflow_model_not_exist) { + ge::Graph graph; + std::string caseDir = __FILE__; + std::size_t idx = caseDir.find_last_of("/"); + caseDir = caseDir.substr(0, idx); + + // model file is not exist + std::string modelFile = caseDir + "/origin_models/conv2d_explicit1_pad.pb"; + auto status = ge::aclgrphParseTensorFlow(modelFile.c_str(), graph); + EXPECT_EQ(status, ge::GRAPH_FAILED); +} + +TEST_F(STestTensorflowParser, parser_tensorflow_model) { + std::string caseDir = __FILE__; + std::size_t idx = caseDir.find_last_of("/"); + caseDir = caseDir.substr(0, idx); + std::string modelFile = caseDir + "/origin_models/tf_add.pb"; + const char *model_file = modelFile.c_str(); + std::string op_name = "ge_ascend_irgraph"; + ge::Graph graph(op_name); + + std::map parser_options = { + {ge::AscendString(ge::ir_option::INPUT_FORMAT), ge::AscendString("NHWC")}, + }; + auto ret_graph = ge::aclgrphParseTensorFlow(model_file, parser_options, graph); + EXPECT_EQ(ret_graph, ge::FAILED); + + // parser tensorflow model out_node_size is equal to index + string graph_name; + AclGrphParseUtil acl_graph_parse_util; + std::map out_nodes_with_node_and_index = { + {AscendString(ge::ir_option::OUT_NODES), AscendString("Placeholder:0;Placeholder_1:1")}}; + ParerSTestsUtils::ClearParserInnerCtx(); + auto ret = acl_graph_parse_util.ParseParamsBeforeGraph(out_nodes_with_node_and_index, graph_name); + ret_graph = ge::aclgrphParseTensorFlow(model_file, graph); + EXPECT_EQ(ret_graph, domi::FAILED); + + // parser tensorflow model success + modelFile = caseDir + "/origin_models/model.pb"; + model_file = modelFile.c_str(); + out_nodes_with_node_and_index = {{AscendString(ge::ir_option::OUT_NODES), AscendString("x:0;y:0")}}; + ParerSTestsUtils::ClearParserInnerCtx(); + ret = acl_graph_parse_util.ParseParamsBeforeGraph(out_nodes_with_node_and_index, graph_name); + ret_graph = ge::aclgrphParseTensorFlow(model_file, graph); + EXPECT_EQ(ret_graph, domi::SUCCESS); +} + +TEST_F(STestTensorflowParser, tensorflow_parser_to_json) +{ + TensorFlowModelParser modelParser; + std::string caseDir = __FILE__; + std::size_t idx = caseDir.find_last_of("/"); + caseDir = caseDir.substr(0, idx); + std::string modelFile = caseDir + "/origin_models/tf_add.pb"; + std::string jsonFile = caseDir + "/origin_models/test.json"; + const char *model_file = modelFile.c_str(); + const char *json_file = jsonFile.c_str(); + Status ret = modelParser.ToJson(model_file, json_file); + EXPECT_EQ(ret, SUCCESS); +} + +TEST_F(STestTensorflowParser, tensorflow_parserfrommemory_failed) +{ + TensorFlowModelParser modelParser; + std::string caseDir = __FILE__; + std::size_t idx = caseDir.find_last_of("/"); + caseDir = caseDir.substr(0, idx); + std::string modelFile = caseDir + "/origin_models/tf_add.pb"; + const char *data = modelFile.c_str(); + uint32_t size = 1; + ge::Graph graph; + std::map parser_params; + Status ret = ge::aclgrphParseTensorFlow(modelFile.c_str(), parser_params, graph); + ASSERT_EQ(ret, SUCCESS); + + modelFile = caseDir + "/origin_models/tf_add.pb"; + parser_params = {{AscendString(ge::ir_option::OUT_NODES), AscendString("Placeholder:0;Placeholder_1:0")}}; + ret = ge::aclgrphParseTensorFlow(modelFile.c_str(), parser_params, graph); + ge::ComputeGraphPtr compute_graph = ge::GraphUtils::GetComputeGraph(graph); + ret = modelParser.ParseFromMemory(data, size, compute_graph); + EXPECT_EQ(ret, INTERNAL_ERROR); +} + +TEST_F(STestTensorflowParser, modelparser_parsefrommemory_success) +{ + std::string caseDir = __FILE__; + std::size_t idx = caseDir.find_last_of("/"); + caseDir = caseDir.substr(0, idx); + std::string modelFile = caseDir + "/origin_models/tf_add.pb"; + const char* tmp_tf_pb_model = modelFile.c_str(); + ge::Graph graph; + std::map parser_params; + Status ret = ge::aclgrphParseTensorFlow(modelFile.c_str(), parser_params, graph); + ASSERT_EQ(ret, SUCCESS); + + ge::ComputeGraphPtr compute_graph = ge::GraphUtils::GetComputeGraph(graph); + TensorFlowModelParser modelParser; + MemBuffer* memBuffer = MemBufferFromFile(tmp_tf_pb_model); + PreChecker::Instance().HasError() == false; + ret = modelParser.ParseFromMemory((char*)memBuffer->data, memBuffer->size, compute_graph); + free(memBuffer->data); + delete memBuffer; +} + +TEST_F(STestTensorflowParser, weightsparser_parsefrommemory_success) +{ + std::string caseDir = __FILE__; + std::size_t idx = caseDir.find_last_of("/"); + caseDir = caseDir.substr(0, idx); + std::string modelFile = caseDir + "/origin_models/tf_add.pb"; + const char* tmp_tf_pb_model = modelFile.c_str(); + ge::Graph graph; + std::map parser_params; + Status ret = ge::aclgrphParseTensorFlow(modelFile.c_str(), parser_params, graph); + ASSERT_EQ(ret, SUCCESS); + + ge::ComputeGraphPtr compute_graph = ge::GraphUtils::GetComputeGraph(graph); + auto weights_parser = domi::WeightsParserFactory::Instance()->CreateWeightsParser(domi::TENSORFLOW); + MemBuffer* memBuffer = MemBufferFromFile(tmp_tf_pb_model); + ret = weights_parser->ParseFromMemory((char*)memBuffer->data, memBuffer->size, compute_graph); + free(memBuffer->data); + delete memBuffer; + EXPECT_EQ(SUCCESS, ret); +} + +std::string getGraphCallbackV2(string subgraph_name) +{ + std::string caseDir = __FILE__; + std::size_t idx = caseDir.find_last_of("/"); + caseDir = caseDir.substr(0, idx); + subgraph_name = caseDir + "/origin_models/tf_add.pb"; + return subgraph_name; +} + +TEST_F(STestTensorflowParser, parser_ParseProtoWithSubgraphV2) +{ + std::string caseDir = __FILE__; + std::size_t idx = caseDir.find_last_of("/"); + caseDir = caseDir.substr(0, idx); + const std::string root_proto = caseDir + "/origin_models/tf_add.pb"; + ge::Graph graph; + std::map parser_params; + Status ret = ge::aclgrphParseTensorFlow(root_proto.c_str(), parser_params, graph); + ASSERT_EQ(ret, SUCCESS); + + ge::ComputeGraphPtr root_graph = ge::GraphUtils::GetComputeGraph(graph); + domi::GetGraphCallbackV2 callback(&getGraphCallbackV2); + TensorFlowModelParser parser; + ret = parser.ParseProtoWithSubgraph(root_proto, callback, root_graph); +} + +TEST_F(STestTensorflowParser, parser_ConvertToGeDataType) +{ + // convert to ge type success + const uint32_t type1 = domi::tensorflow::DataType::DT_FLOAT; + TensorFlowModelParser parser; + ge::DataType dataType = parser.ConvertToGeDataType(type1); + ASSERT_EQ(dataType, ge::DataType::DT_FLOAT); + + const uint32_t type2 = 80; // invalid type + dataType = parser.ConvertToGeDataType(type2); + ASSERT_EQ(dataType, ge::DataType::DT_UNDEFINED); +} + +TEST_F(STestTensorflowParser, tensorflow_ParserProto_failed) +{ + std::string caseDir = __FILE__; + std::size_t idx = caseDir.find_last_of("/"); + caseDir = caseDir.substr(0, idx); + const std::string root_proto = caseDir + "/origin_models/avgpool3dgrad.pb.txt"; + domi::tensorflow::GraphDef graphDef; + ge::Graph graph; + std::map parser_params; + Status ret = ge::aclgrphParseTensorFlow(root_proto.c_str(), parser_params, graph); + ASSERT_EQ(ret, SUCCESS); + + ge::ComputeGraphPtr root_graph = ge::GraphUtils::GetComputeGraph(graph); + TensorFlowModelParser tensorflow_parser; + ret = tensorflow_parser.ParseProto(reinterpret_cast(&graphDef), root_graph); + EXPECT_EQ(PARAM_INVALID, ret); + + // proto解析失败 + bool protoRet = parser::ReadProtoFromText(root_proto.c_str(), &graphDef); + ASSERT_EQ(protoRet, false); + ret = tensorflow_parser.ParseProto(reinterpret_cast(&graphDef), root_graph); + ASSERT_EQ(ret, PARAM_INVALID); +} + +TEST_F(STestTensorflowParser, tensorflow_parserAllGraph_failed) +{ + std::string caseDir = __FILE__; + std::size_t idx = caseDir.find_last_of("/"); + caseDir = caseDir.substr(0, idx); + const std::string root_proto = caseDir + "/origin_models/conv2d.pb"; + domi::tensorflow::GraphDef graphDef; + CreateGraphDef(graphDef); + + auto no_op = graphDef.add_node(); + no_op->set_name("no_op"); + no_op->set_op("NoOp"); + no_op->add_input("placeholder0"); + no_op->add_input("placeholder1"); + + ge::Graph graph; + std::map parser_params; + Status ret = ge::aclgrphParseTensorFlow(root_proto.c_str(), parser_params, graph); + ASSERT_EQ(ret, SUCCESS); + + ge::ComputeGraphPtr root_graph = ge::GraphUtils::GetComputeGraph(graph); + TensorFlowModelParser tensorflow_parser; + ret = tensorflow_parser.ParseAllGraph(reinterpret_cast(&graphDef), root_graph); + EXPECT_EQ(INTERNAL_ERROR, ret); +} + +TEST_F(STestTensorflowParser, test_parse_acl_output_nodes) +{ + AclGrphParseUtil acl_graph_parse_util; + string graph_name; + // case 1: Normal with 'node and index' + ParerSTestsUtils::ClearParserInnerCtx(); + GetParserContext().type = domi::ONNX; + std::map out_nodes_with_node_and_index = { + {AscendString(ge::ir_option::OUT_NODES), AscendString("Out1:0;Out2:1")}}; + ParerSTestsUtils::ClearParserInnerCtx(); + auto ret = acl_graph_parse_util.ParseParamsBeforeGraph(out_nodes_with_node_and_index, graph_name); + ASSERT_EQ(ret, SUCCESS); + EXPECT_EQ(ge::GetParserContext().user_out_nodes.size(), 2); + EXPECT_EQ(ge::GetParserContext().out_nodes_map.size(), 2); + EXPECT_EQ(ge::GetParserContext().user_out_tensors.size(), 0); + + // case 2: Normal with 'tensor name' + ParerSTestsUtils::ClearParserInnerCtx(); + GetParserContext().type = domi::ONNX; + std::map out_nodes_with_tensor_name = { + {AscendString(ge::ir_option::OUT_NODES), AscendString("Out_tensor_1;Out_tensor_2")}}; + ret = acl_graph_parse_util.ParseParamsBeforeGraph(out_nodes_with_tensor_name, graph_name); + ASSERT_EQ(ret, SUCCESS); + EXPECT_EQ(ge::GetParserContext().user_out_nodes.size(), 0); + EXPECT_EQ(ge::GetParserContext().out_nodes_map.size(), 0); + EXPECT_EQ(ge::GetParserContext().user_out_tensors.size(), 2); + + // case 3: Failed with 'node and index' before 'tensor name' + ParerSTestsUtils::ClearParserInnerCtx(); + GetParserContext().type = domi::ONNX; + std::map out_nodes_mode_mixex_pre = { + {AscendString(ge::ir_option::OUT_NODES), AscendString("Out1:0;Out2:1;Out_tensor_1;Out_tensor_2")}}; + ret = acl_graph_parse_util.ParseParamsBeforeGraph(out_nodes_mode_mixex_pre, graph_name); + ASSERT_EQ(ret, PARAM_INVALID); + EXPECT_EQ(ge::GetParserContext().user_out_nodes.size(), 2); + EXPECT_EQ(ge::GetParserContext().out_nodes_map.size(), 2); + EXPECT_EQ(ge::GetParserContext().user_out_tensors.size(), 0); + + // case 4: Failed with 'node and index' inserted in 'tensor name' + ParerSTestsUtils::ClearParserInnerCtx(); + GetParserContext().type = domi::ONNX; + std::map out_nodes_mode_mixex_mid = { + {AscendString(ge::ir_option::OUT_NODES), AscendString("Out_tensor_1;Out1:0;Out2:1;Out_tensor_2")}}; + ret = acl_graph_parse_util.ParseParamsBeforeGraph(out_nodes_mode_mixex_mid, graph_name); + ASSERT_EQ(ret, PARAM_INVALID); + EXPECT_EQ(ge::GetParserContext().user_out_nodes.size(), 0); + EXPECT_EQ(ge::GetParserContext().out_nodes_map.size(), 0); + EXPECT_EQ(ge::GetParserContext().user_out_tensors.size(), 1); + + // case 5: Failed with 'node and index' after 'tensor name' + ParerSTestsUtils::ClearParserInnerCtx(); + GetParserContext().type = domi::ONNX; + std::map out_nodes_mode_mixex_post = { + {AscendString(ge::ir_option::OUT_NODES), AscendString("Out_tensor_1;Out_tensor_2;Out1:0;Out2:1")}}; + ret = acl_graph_parse_util.ParseParamsBeforeGraph(out_nodes_mode_mixex_post, graph_name); + ASSERT_EQ(ret, PARAM_INVALID); + EXPECT_EQ(ge::GetParserContext().user_out_nodes.size(), 0); + EXPECT_EQ(ge::GetParserContext().out_nodes_map.size(), 0); + EXPECT_EQ(ge::GetParserContext().user_out_tensors.size(), 2); +} + +TEST_F(STestTensorflowParser, parse_AutoMappingByOp) { + static const string KEY_STRING = "key_string"; + static const string KEY_INT = "key_int"; + static const string KEY_FLOAT = "key_float"; + static const string KEY_BOOL = "key_bool"; + static const string KEY_TYPE = "key_type"; + static const string VALUE_STRING = "string"; + static const int64_t VALUE_INT = 1; + static const float VALUE_FLOAT = 1.0; + static const bool VALUE_BOOL = true; + static const domi::tensorflow::DataType VALUE_TYPE = domi::tensorflow::DataType::DT_FLOAT; + + std::cout << "test data_type value_type: " << (int64_t)VALUE_TYPE << std::endl; + static const string VALUE_NAME = "test_name"; + ge::OpDescPtr op_desc = std::make_shared(); + NodeDef node_def; + domi::tensorflow::AttrValue value; + ge::Operator op = ge::OpDescUtils::CreateOperatorFromOpDesc(op_desc); + + node_def.set_name(VALUE_NAME); + value.set_s(VALUE_STRING); + TensorFlowUtil::AddNodeAttr(KEY_STRING, value, &node_def); + value.set_i(VALUE_INT); + TensorFlowUtil::AddNodeAttr(KEY_INT, value, &node_def); + value.set_f(VALUE_FLOAT); + TensorFlowUtil::AddNodeAttr(KEY_FLOAT, value, &node_def); + value.set_b(VALUE_BOOL); + TensorFlowUtil::AddNodeAttr(KEY_BOOL, value, &node_def); + value.set_type(VALUE_TYPE); + TensorFlowUtil::AddNodeAttr(KEY_TYPE, value, &node_def); + + domi::Status status = domi::AutoMappingFn(reinterpret_cast(&node_def), op); + EXPECT_EQ(domi::SUCCESS, status); + EXPECT_EQ(VALUE_NAME, op_desc->GetName()); + + string value_string = ""; + ge::AttrUtils::GetStr(op_desc, KEY_STRING, value_string); + EXPECT_EQ(VALUE_STRING, value_string); + + int64_t value_int = 0; + ge::AttrUtils::GetInt(op_desc, KEY_INT, value_int); + EXPECT_EQ(VALUE_INT, value_int); + + float value_float = 0.0; + ge::AttrUtils::GetFloat(op_desc, KEY_FLOAT, value_float); + EXPECT_EQ(VALUE_FLOAT, value_float); + + bool value_bool = false; + ge::AttrUtils::GetBool(op_desc, KEY_BOOL, value_bool); + EXPECT_EQ(VALUE_BOOL, value_bool); + + ge::DataType data_type = ge::DT_UNDEFINED; + ge::AttrUtils::GetDataType(op_desc, KEY_TYPE, data_type); + EXPECT_EQ(ge::DT_FLOAT, data_type); + + // test AutoMappingByOpFn + ge::OpDescPtr op_desc_dest = std::make_shared(); + ge::Operator op_dest = ge::OpDescUtils::CreateOperatorFromOpDesc(op_desc_dest); + + status = domi::AutoMappingByOpFn(op, op_dest); + EXPECT_EQ(domi::SUCCESS, status); + EXPECT_EQ(VALUE_NAME, op_dest.GetName()); + + value_string = ""; + ge::AttrUtils::GetStr(op_desc_dest, KEY_STRING, value_string); + EXPECT_EQ(VALUE_STRING, value_string); + + value_int = 0; + ge::AttrUtils::GetInt(op_desc_dest, KEY_INT, value_int); + EXPECT_EQ(VALUE_INT, value_int); + + value_float = 0.0; + ge::AttrUtils::GetFloat(op_desc_dest, KEY_FLOAT, value_float); + EXPECT_EQ(VALUE_FLOAT, value_float); + + value_bool = false; + ge::AttrUtils::GetBool(op_desc_dest, KEY_BOOL, value_bool); + EXPECT_EQ(VALUE_BOOL, value_bool); + + data_type = ge::DT_UNDEFINED; + ge::AttrUtils::GetDataType(op_desc_dest, KEY_TYPE, data_type); + EXPECT_EQ(ge::DT_FLOAT, data_type); +} + +TEST_F(STestTensorflowParser, parse_ParseNodeDef) +{ + NodeDef * node_def = new NodeDef(); + node_def->set_name("test_name"); + node_def->set_op("PlaceholderWithDefault"); + + bool isDatasetInit = true; + TensorFlowModelParser model_parser; + Status ret = model_parser.AdaptOpType(node_def, isDatasetInit); + EXPECT_EQ(domi::SUCCESS, ret); + + node_def->set_op("Add"); + ret = model_parser.AdaptOpType(node_def, isDatasetInit); + EXPECT_EQ(domi::SUCCESS, ret); + delete node_def; +} + +TEST_F(STestTensorflowParser, parse_AddFmkNode) +{ + TensorFlowModelParser modelParser; + std::string caseDir = __FILE__; + std::size_t idx = caseDir.find_last_of("/"); + caseDir = caseDir.substr(0, idx); + std::string modelFile = caseDir + "/origin_models/tf_add.pb"; + ge::Graph graph; + string graph_name; + AclGrphParseUtil acl_graph_parse_util; + std::map parser_options = {{AscendString(ge::ir_option::OUT_NODES), AscendString("Placeholder:0;Placeholder_1:0")}}; + ParerSTestsUtils::ClearParserInnerCtx(); + Status ret = acl_graph_parse_util.ParseParamsBeforeGraph(parser_options, graph_name); + ret = aclgrphParseTensorFlow(modelFile.c_str(), parser_options, graph); + ASSERT_EQ(ret, SUCCESS); + + ge::ComputeGraphPtr compute_graph = std::make_shared(GRAPH_DEFAULT_NAME); + tensorflow::GraphDef *graphDef = new (std::nothrow) tensorflow::GraphDef(); + ScopePassManager pass_manager; + std::shared_ptr scope_graph = pass_manager.BuildScopeGraph(graphDef); + + std::string fusion_op_name = "fusion_op_name"; + FusionScopesResult *fusion_rlt = new (std::nothrow) FusionScopesResult(); + EXPECT_NE(fusion_rlt, nullptr); + fusion_rlt->Init(); + GenFusionScopesResult(scope_graph, fusion_rlt, fusion_op_name); + GenOriginContext(&modelParser, fusion_op_name); + + // origin inner node def + NodeDef* node_def = MallocNodeDef("scope_node_1", "Add"); + EXPECT_NE(node_def, nullptr); + modelParser.fusion_op_nodedef_map_[fusion_op_name].push_back(node_def); + + bool train_flag_backup = ge::GetParserContext().train_flag; + ge::GetParserContext().train_flag = true; + + REGISTER_CUSTOM_OP("Identity") + .FrameworkType(domi::TENSORFLOW) + .OriginOpType("Identity") + .ParseParamsFn(ParseParams) + .ImplyType(ImplyType::TVM); + REGISTER_CUSTOM_OP("Constant") + .FrameworkType(domi::TENSORFLOW) + .OriginOpType("Const") + .ParseParamsFn(ParseParams) + .ImplyType(ImplyType::TVM); + + register_tbe_op(); + + std::vector node_name_list; + GenOriginNodeDef(&modelParser, node_name_list); + std::set malloc_node_name_list(node_name_list.begin(), node_name_list.end()); + node_name_list.push_back(fusion_op_name); + + ret = modelParser.AddFmkNode(compute_graph, scope_graph, node_name_list, false); + EXPECT_EQ(ret, PARAM_INVALID); + EXPECT_EQ(modelParser.scope_inner_node_map_.size(), 0); + EXPECT_EQ(modelParser.nodedef_map_.size(), 5); + + ret = modelParser.AddEdges(compute_graph); + EXPECT_EQ(ret, SUCCESS); + + // release resource + delete graphDef; + delete node_def; + modelParser.DeleteFuisonNodeDef(); + FreeNodeDefMap(&modelParser, malloc_node_name_list); + ge::GetParserContext().train_flag = train_flag_backup; +} + +TEST_F(STestTensorflowParser, parse_AddScopeInnerNode) +{ + TensorFlowModelParser modelParser; + std::string caseDir = __FILE__; + std::size_t idx = caseDir.find_last_of("/"); + caseDir = caseDir.substr(0, idx); + std::string modelFile = caseDir + "/origin_models/tf_add.pb"; + std::string op_name = "ge_ascend_irgraph"; + ge::Graph graph(op_name); + ge::ComputeGraphPtr compute_graph = ge::GraphUtils::GetComputeGraph(graph); + std::map parser_params = { + {AscendString(ge::ir_option::OUT_NODES), AscendString("Placeholder:0;Placeholder_1:0")}}; + Status ret = ge::aclgrphParseTensorFlow(modelFile.c_str(), parser_params, graph); + EXPECT_EQ(ret, SUCCESS); + + std::mutex graph_mutex; + tensorflow::NodeDef *node_def = new NodeDef(); + node_def->set_name("FastrcnnPredictions"); + node_def->set_op("FastrcnnPredictions"); + // can't find in scope_inner_node_map + ret = modelParser.AddScopeInnerNode(&modelParser, compute_graph, &graph_mutex, node_def); + EXPECT_EQ(ret, PARAM_INVALID); + delete node_def; +} + +TEST_F(STestTensorflowParser, dyncmic_rnn_scope_pass_plugin_test) { + ge::Graph graph; + + std::cout << __FILE__ << std::endl; + std::string caseDir = __FILE__; + std::size_t idx = caseDir.find_last_of("/"); + caseDir = caseDir.substr(0, idx); + std::string modelFile = caseDir + "/origin_models/tensor_array.pb"; + std::map params; + string key ="enable_scope_fusion_passes"; + string value ="ScopeDynamicRNNPass"; + params.insert(std::make_pair(ge::AscendString(key.c_str()), ge::AscendString(value.c_str()))); + auto status = aclgrphParseTensorFlow(modelFile.c_str(), params, graph); + EXPECT_EQ(status, SUCCESS); +} + +TEST_F(STestTensorflowParser, avgpool3dgrad_plugin_test_format_NDHWC) { + ge::Graph graph; + std::cout << __FILE__ << std::endl; + std::string caseDir = __FILE__; + std::size_t idx = caseDir.find_last_of("/"); + caseDir = caseDir.substr(0, idx); + std::string modelFile = caseDir + "/origin_models/avgpool3dgrad_case_1.pb"; + auto status = aclgrphParseTensorFlow(modelFile.c_str(), graph); + EXPECT_EQ(status, SUCCESS); +} + +TEST_F(STestTensorflowParser, tensorflow_merge_test) { + ge::Graph graph; + std::cout << __FILE__ << std::endl; + std::string caseDir = __FILE__; + std::size_t idx = caseDir.find_last_of("/"); + caseDir = caseDir.substr(0, idx); + std::string modelFile = caseDir + "/origin_models/merge.pb"; + auto status = aclgrphParseTensorFlow(modelFile.c_str(), graph); +} + +TEST_F(STestTensorflowParser, tensorflow_no_op_test) { + ge::Graph graph; + std::cout << __FILE__ << std::endl; + std::string caseDir = __FILE__; + std::size_t idx = caseDir.find_last_of("/"); + caseDir = caseDir.substr(0, idx); + std::string modelFile = caseDir + "/origin_models/test_no_op.pb"; + auto status = aclgrphParseTensorFlow(modelFile.c_str(), graph); + EXPECT_EQ(status, SUCCESS); +} + +TEST_F(STestTensorflowParser, tensorflow_identity_test) { + ge::Graph graph; + std::cout << __FILE__ << std::endl; + std::string caseDir = __FILE__; + std::size_t idx = caseDir.find_last_of("/"); + caseDir = caseDir.substr(0, idx); + std::string modelFile = caseDir + "/origin_models/test_identity.pb"; + auto status = aclgrphParseTensorFlow(modelFile.c_str(), graph); + EXPECT_EQ(status, SUCCESS); +} + +TEST_F(STestTensorflowParser, tensorflow_constant_test) { + ge::Graph graph; + std::cout << __FILE__ << std::endl; + std::string caseDir = __FILE__; + std::size_t idx = caseDir.find_last_of("/"); + caseDir = caseDir.substr(0, idx); + std::string modelFile = caseDir + "/origin_models/test_constant.pb"; + auto status = aclgrphParseTensorFlow(modelFile.c_str(), graph); + EXPECT_EQ(status, SUCCESS); + + TensorFlowConstantParser constantParser; + ge::OpDescPtr op_dest = make_shared("constant", ge::parser::CONSTANT); + NodeDef* node_def = initNodeDef(); + node_def->set_name("Constant"); + auto params = constantParser.ParseParams(node_def, op_dest); + EXPECT_EQ(params, SUCCESS); + + auto value = constantParser.ParseValue(node_def, op_dest); + EXPECT_EQ(value, SUCCESS); + + ConstantOperator op; + auto type = constantParser.ParseDType(node_def, &op); + EXPECT_EQ(type, SUCCESS); +} + +TEST_F(STestTensorflowParser, tensorflow_reshpae_test) { + ge::Graph graph; + std::cout << __FILE__ << std::endl; + std::string caseDir = __FILE__; + std::size_t idx = caseDir.find_last_of("/"); + caseDir = caseDir.substr(0, idx); + std::string modelFile = caseDir + "/origin_models/test_reshape.pb"; + auto status = aclgrphParseTensorFlow(modelFile.c_str(), graph); + EXPECT_EQ(status, SUCCESS); +} + +TEST_F(STestTensorflowParser, tensorflow_squeeze_test) { + ge::Graph graph; + std::cout << __FILE__ << std::endl; + std::string caseDir = __FILE__; + std::size_t idx = caseDir.find_last_of("/"); + caseDir = caseDir.substr(0, idx); + std::string modelFile = caseDir + "/origin_models/test_sequeeze.pb"; + auto status = aclgrphParseTensorFlow(modelFile.c_str(), graph); + EXPECT_EQ(status, SUCCESS); +} + +TEST_F(STestTensorflowParser, tensorflow_fill_test) { + ge::Graph graph; + std::cout << __FILE__ << std::endl; + std::string caseDir = __FILE__; + std::size_t idx = caseDir.find_last_of("/"); + caseDir = caseDir.substr(0, idx); + std::string modelFile = caseDir + "/origin_models/test_fill.pb"; + auto status = aclgrphParseTensorFlow(modelFile.c_str(), graph); + EXPECT_EQ(status, SUCCESS); +} + +TEST_F(STestTensorflowParser, tensorflow_shape_n_test) { + ge::Graph graph; + std::cout << __FILE__ << std::endl; + std::string caseDir = __FILE__; + std::size_t idx = caseDir.find_last_of("/"); + caseDir = caseDir.substr(0, idx); + std::string modelFile = caseDir + "/origin_models/test_shape_n.pb"; + auto status = aclgrphParseTensorFlow(modelFile.c_str(), graph); + EXPECT_EQ(status, SUCCESS); +} + +TEST_F(STestTensorflowParser, tensorflow_switch_test) { + ge::Graph graph; + std::cout << __FILE__ << std::endl; + std::string caseDir = __FILE__; + std::size_t idx = caseDir.find_last_of("/"); + caseDir = caseDir.substr(0, idx); + std::string modelFile = caseDir + "/origin_models/test_switch.pb"; + auto status = aclgrphParseTensorFlow(modelFile.c_str(), graph); + EXPECT_EQ(status, SUCCESS); + + TensorFlowRefSwitchParser refSwitchParser; + ge::OpDescPtr op_dest = make_shared("constant", ge::parser::CONSTANT); + NodeDef* node_def = initNodeDef(); + node_def->set_name("RefSwitch"); + auto params = refSwitchParser.ParseParams(node_def, op_dest); + EXPECT_EQ(params, SUCCESS); + + RefSwitchOperator op; + auto parseRet = refSwitchParser.ParseT(node_def, &op); + EXPECT_EQ(parseRet, SUCCESS); +} + +TEST_F(STestTensorflowParser, tensorflow_enter_test) { + ge::Graph graph; + std::cout << __FILE__ << std::endl; + std::string caseDir = __FILE__; + std::size_t idx = caseDir.find_last_of("/"); + caseDir = caseDir.substr(0, idx); + std::string modelFile = caseDir + "/origin_models/test_enter.pb"; + auto status = aclgrphParseTensorFlow(modelFile.c_str(), graph); + EXPECT_EQ(status, SUCCESS); +} + +TEST_F(STestTensorflowParser, tensorflow_VariableV2_test) { + ge::Graph graph; + std::string caseDir = __FILE__; + std::size_t idx = caseDir.find_last_of("/"); + caseDir = caseDir.substr(0, idx); + std::string modelFile = caseDir + "/origin_models/test_VariableV2.pb"; + auto status = aclgrphParseTensorFlow(modelFile.c_str(), graph); + EXPECT_EQ(status, SUCCESS); +} + } // namespace ge