def setUp(self): nodes = { **regular_op_with_shaped_data('boxes', [10, 100, 4], {'type': 'Parameter'}), **regular_op_with_shaped_data('scores', [10, 5, 100], {'type': 'Parameter'}), **valued_const_with_data('max_output_per_class', int64_array(7)), **regular_op('nms', {'op': 'NonMaxSuppression', 'type': 'NonMaxSuppression', 'name': 'nms'}), **empty_data('nms_data_0'), **empty_data('nms_data_1'), **empty_data('nms_data_2'), **result('output_0'), **result('output_1'), **result('output_2'), } self.graph = build_graph(nodes, [ *connect('boxes', '0:nms'), *connect('scores', '1:nms'), *connect('max_output_per_class', '2:nms'), *connect('nms:0', 'nms_data_0', front_phase=True), # Use this WA for correct creating operation *connect('nms_data_0', 'output_0', front_phase=True), # with multiple outputs ], nodes_with_edges_only=True) self.graph_nms_5_2_outs = build_graph(nodes, [ *connect('boxes', '0:nms'), *connect('scores', '1:nms'), *connect('max_output_per_class', '2:nms'), *connect('nms:0', 'nms_data_0', front_phase=True), # Use this WA for correct creating operation *connect('nms_data_0', 'output_0', front_phase=True), # with multiple outputs *connect('nms:1', 'nms_data_1', front_phase=True), *connect('nms_data_1', 'output_1', front_phase=True), ], nodes_with_edges_only=True) self.graph_nms_5_3_outs = build_graph(nodes, [ *connect('boxes', '0:nms'), *connect('scores', '1:nms'), *connect('max_output_per_class', '2:nms'), *connect('nms:0', 'nms_data_0', front_phase=True), # Use this WA for correct creating operation *connect('nms_data_0', 'output_0', front_phase=True), # with multiple outputs *connect('nms:1', 'nms_data_1', front_phase=True), *connect('nms_data_1', 'output_1', front_phase=True), *connect('nms:2', 'nms_data_2', front_phase=True), *connect('nms_data_2', 'output_2', front_phase=True), ], nodes_with_edges_only=True)
def build_and_test_shape_inference(self, input_indices_sparse_shape, input_actual_shape, new_shape, ref_out_shape, input_indices=None, ref_out_indices=None): # sparse tensor is stored in COO format nodes = { **shaped_parameter('input_indices', shape_array(input_indices_sparse_shape), { 'value': input_indices }), **valued_const_with_data('input_shape', shape_array(input_actual_shape)), **valued_const_with_data('new_shape', shape_array(new_shape)), **regular_op_with_empty_data( 'sparse_reshape_node', { 'op': 'SparseReshape', 'special_zero': True, 'infer': SparseReshape.infer }), **empty_data('sparse_reshape_node_d:out_port_1'), **result('output_indices'), **result('output_shape'), } edges = [ *connect('input_indices', '0:sparse_reshape_node'), *connect('input_shape', '1:sparse_reshape_node'), *connect('new_shape', '2:sparse_reshape_node'), *connect('sparse_reshape_node:0', 'output_indices'), ('sparse_reshape_node', 'sparse_reshape_node_d:out_port_1', { 'out': 1 }), ('sparse_reshape_node_d:out_port_1', 'output_shape', { 'in': 0 }), ] graph = build_graph( nodes, edges, update_attributes={'input_indices_d': { 'value': input_indices }}) graph.stage = 'middle' partial_infer(graph) node = Node(graph, 'sparse_reshape_node') output_indices = node.out_port(0).data.get_value() actual_output_shape = node.out_port(1).data.get_value() self.assertTrue( strict_compare_tensors(actual_output_shape, ref_out_shape)) self.assertTrue(strict_compare_tensors(output_indices, ref_out_indices))
def test_multi(self): nodes = { **regular_op_with_empty_data('input', {'type': 'Parameter'}), **regular_op_with_empty_data('some_op', {'type': 'SomeOp', 'name': 'some_op_name'}), **empty_data('some_op_d2'), **regular_op_with_empty_data('fake_output1', {'type': None, 'kind': 'op', 'op': 'FakeOutput', 'name': 'my_output_name1'}), **regular_op_with_empty_data('fake_output2', {'type': None, 'kind': 'op', 'op': 'FakeOutput', 'name': 'my_output_name2'}), **valued_const_with_data('const1', int64_array(0)), **valued_const_with_data('const2', int64_array(0)), **regular_op_with_empty_data('add1', {'type': None, 'kind': 'op', 'op': 'Add', 'name': 'my_output_name1'}), **regular_op_with_empty_data('add2', {'type': None, 'kind': 'op', 'op': 'Add', 'name': 'my_output_name2'}), **result('result1'), **result('result2'), } edges = [*connect('input', 'some_op'), *connect('some_op', 'fake_output1'), ('some_op', 'some_op_d2'), ('some_op_d2', 'fake_output2'), *connect('fake_output1', 'result1'), *connect('fake_output2', 'result2'), ] graph = build_graph(nodes, edges) edges_ref = [*connect('input', 'some_op'), *connect('some_op', '0:add1'), *connect('const1', '1:add1'), ('some_op', 'some_op_d2'), ('some_op_d2', 'add2', {'in': 0}), *connect('const2', '1:add2'), *connect('add1', 'result1'), *connect('add2', 'result2'), ] graph_ref = build_graph(nodes, edges_ref) FakeOutputResolver().find_and_replace_pattern(graph) (flag, resp) = compare_graphs(graph, graph_ref, 'result1') self.assertTrue(flag, resp)
'type': None, 'top_k': 1, 'axis': 0, 'output_type': np.int32, 'remove_values_output': True }), **result('result'), **valued_const_with_data('axis_const', int64_array([1])), **regular_op( 'topk', { 'op': 'TopK', 'type': 'TopK', 'sort': 'index', 'index_element_type': np.int32 }), **empty_data('topk_out_0_data'), **empty_data('topk_out_1_data'), **regular_op_with_empty_data('topk_scalar', { 'op': 'Const', 'type': 'Const', 'value': int64_array([1]), 'shape': [] }), **regular_op_with_empty_data('concat', { 'op': 'Concat', 'type': 'Concat', 'axis': 1 }) }
def test_simple_shape_inf(self, cond, output_port_0_shape, output_port_1_shape): then_graph_nodes = { **regular_op_with_empty_data( 'param_1', { 'type': 'Parameter', 'kind': 'op', 'input_id': 1, 'shape': None, 'infer': Parameter.infer }), **regular_op_with_empty_data( 'param_2', { 'type': 'Parameter', 'kind': 'op', 'input_id': 2, 'shape': None, 'infer': Parameter.infer }), **regular_op_with_empty_data( 'add', { 'type': 'Add', 'kind': 'op', 'op': 'Add', 'infer': lambda node: eltwise_infer(node, Add.operation) }), **regular_op_with_empty_data( 'mul', { 'type': 'Mul', 'kind': 'op', 'op': 'Mul', 'infer': lambda node: eltwise_infer(node, Mul.operation) }), **regular_op_with_empty_data( 'res1', { 'kind': 'op', 'type': 'Result', 'op': 'Result', 'infer': lambda x: 0, 'output_id': 0 }), **regular_op_with_empty_data( 'res2', { 'kind': 'op', 'type': 'Result', 'op': 'Result', 'infer': lambda x: 0, 'output_id': 1 }) } then_graph_edges = [ *connect('param_1', '0:add'), *connect('param_2', '1:add'), *connect('param_1', '1:mul'), *connect('param_2', '0:mul'), *connect('add', 'res1'), *connect('mul', 'res2'), ] else_graph_nodes = { **regular_op_with_empty_data( 'param_1', { 'type': 'Parameter', 'kind': 'op', 'input_id': 1, 'shape': None, 'infer': Parameter.infer }), **regular_op_with_empty_data( 'param_2', { 'type': 'Parameter', 'kind': 'op', 'input_id': 3, 'shape': None, 'infer': Parameter.infer }), **regular_op_with_empty_data('identity', { 'kind': 'op', 'op': 'Identity', 'infer': Identity.infer }), **regular_op_with_empty_data('identity_1', { 'kind': 'op', 'op': 'Identity', 'infer': Identity.infer }), **regular_op_with_empty_data( 'res1', { 'kind': 'op', 'type': 'Result', 'op': 'Result', 'infer': lambda x: 0, 'output_id': 0 }), **regular_op_with_empty_data( 'res2', { 'kind': 'op', 'type': 'Result', 'op': 'Result', 'infer': lambda x: 0, 'output_id': 1 }) } else_graph_edges = [ *connect('param_1', 'identity'), *connect('param_2', 'identity_1'), *connect('identity_1', 'res2'), *connect('identity', 'res1'), ] then_graph = build_graph_with_edge_attrs(then_graph_nodes, then_graph_edges) else_graph = build_graph_with_edge_attrs(else_graph_nodes, else_graph_edges) external_graph_nodes = { **valued_const_with_data('cond', cond), **valued_const_with_data('input_2', int64_array([3, 2, 1])), **valued_const_with_data('input_1', int64_array([1, 2, 3])), **valued_const_with_data('input_3', int64_array([8, 4])), **regular_op( 'if', { 'kind': 'op', 'op': 'If', 'then_graph': then_graph, 'else_graph': else_graph, 'infer': If.infer }), **empty_data('if_d_1'), **empty_data('if_d_2'), **result('res_1'), **result('res_2') } external_graph_edges = [ *connect('cond', '0:if'), *connect('input_1', '1:if'), *connect('input_2', '2:if'), *connect('input_3', '3:if'), ('if', 'if_d_1', { 'out': 0 }), ('if', 'if_d_2', { 'out': 1 }), ('if_d_1', 'res_1'), ('if_d_2', 'res_2') ] graph = build_graph(external_graph_nodes, external_graph_edges) graph.stage = 'middle' partial_infer(graph) if_node = Node(graph, 'if') self.assertTrue( strict_compare_tensors( if_node.out_port(0).data.get_shape(), output_port_0_shape)) # shape of the "then" branch is [3] and shape of the "else" branch is [2], so the output shape is "[dynamic]" self.assertTrue( strict_compare_tensors( if_node.out_port(1).data.get_shape(), output_port_1_shape))
import unittest import numpy as np from extensions.front.tf.identityN_to_identity import IdentityN_to_Identity from mo.utils.ir_engine.compare_graphs import compare_graphs from unit_tests.utils.graph import result, regular_op_with_shaped_data, \ regular_op_with_empty_data, build_graph, connect, empty_data nodes = { **regular_op_with_shaped_data('placeholder_0', [1, 227, 227, 3], {'type': 'Parameter'}), **regular_op_with_shaped_data('placeholder_1', [1, 227, 227, 3], {'type': 'Parameter'}), **regular_op_with_empty_data('identityN', {'op': 'IdentityN', 'type': None, 'data_types': [np.int32, np.float], 'name': 'my_identity'}), **empty_data('identityN_1_d'), **regular_op_with_empty_data('identity0', {'op': 'Identity', 'type': None, 'data_type': np.int32, 'name': 'my_identity/0_port'}), **regular_op_with_empty_data('identity1', {'op': 'Identity', 'type': None, 'data_type': np.float, 'name': 'my_identity/1_port'}), **result('output0'), **result('output1'), } class TestIdentityN(unittest.TestCase): def test_identityN(self): graph = build_graph(nodes, [ *connect('placeholder_0', '0:identityN'), *connect('placeholder_1', '1:identityN'),
def test_simple_shape_inf(self): then_graph_nodes = {**regular_op_with_empty_data('param_1', {'type': 'Parameter', 'kind': 'op', 'input_id': 1, 'shape': None, 'infer': Parameter.infer}), **regular_op_with_empty_data('param_2', {'type': 'Parameter', 'kind': 'op', 'input_id': 2, 'shape': None, 'infer': Parameter.infer}), **regular_op_with_empty_data('add', {'type': 'Add', 'kind': 'op', 'op': 'Add', 'infer': lambda node: eltwise_infer(node, Add.operation)}), **regular_op_with_empty_data('mul', {'type': 'Mul', 'kind': 'op', 'op': 'Mul', 'infer': lambda node: eltwise_infer(node, Mul.operation)}), **regular_op_with_empty_data('res1', {'kind': 'op', 'type': 'Result', 'op': 'Result', 'infer': lambda x: 0, 'output_id': 0}), **regular_op_with_empty_data('res2', {'kind': 'op', 'type': 'Result', 'op': 'Result', 'infer': lambda x: 0, 'output_id': 1})} then_graph_edges = [*connect('param_1', '0:add'), *connect('param_2', '1:add'), *connect('param_1', '1:mul'), *connect('param_2', '0:mul'), *connect('add', 'res1'), *connect('mul', 'res2'), ] else_graph_nodes = {**regular_op_with_empty_data('param_1', {'type': 'Parameter', 'kind': 'op', 'input_id': 1, 'shape': None, 'infer': Parameter.infer}), **regular_op_with_empty_data('param_2', {'type': 'Parameter', 'kind': 'op', 'input_id': 3, 'shape': None, 'infer': Parameter.infer}), **regular_op_with_empty_data('identity', {'kind': 'op', 'op': 'Identity', 'infer': Identity.infer}), **regular_op_with_empty_data('identity_1', {'kind': 'op', 'op': 'Identity', 'infer': Identity.infer}), **regular_op_with_empty_data('res1', {'kind': 'op', 'type': 'Result', 'op': 'Result', 'infer': lambda x: 0, 'output_id': 0}), **regular_op_with_empty_data('res2', {'kind': 'op', 'type': 'Result', 'op': 'Result', 'infer': lambda x: 0, 'output_id': 1})} else_graph_edges = [*connect('param_1', 'identity'), *connect('param_2', 'identity_1'), *connect('identity_1', 'res2'), *connect('identity', 'res1'), ] then_graph = build_graph_with_edge_attrs(then_graph_nodes, then_graph_edges) else_graph = build_graph_with_edge_attrs(else_graph_nodes, else_graph_edges) external_graph_nodes = { **valued_const_with_data('cond', np.array([True], dtype=np.bool)), **valued_const_with_data('input_2', int64_array([3, 2, 1])), **valued_const_with_data('input_1', int64_array([1, 2, 3])), **valued_const_with_data('input_3', int64_array([8, 4])), **regular_op('if', {'kind': 'op', 'op': 'If', 'then_graph': then_graph, 'else_graph': else_graph, 'infer': If.infer}), **empty_data('if_d_1'), **empty_data('if_d_2'), **result('res_1'), **result('res_2')} external_graph_edges = [*connect('cond', '0:if'), *connect('input_1', '1:if'), *connect('input_2', '2:if'), *connect('input_3', '3:if'), ('if', 'if_d_1', {'out': 0}), ('if', 'if_d_2', {'out': 1}), ('if_d_1', 'res_1'), ('if_d_2', 'res_2')] graph = build_graph(external_graph_nodes, external_graph_edges) graph.stage = 'middle' partial_infer(graph) res_1 = Node(graph, 'res_1') res_2 = Node(graph, 'res_2') npt.assert_array_equal(res_1.in_port(0).data.get_shape(), int64_array([3])) npt.assert_array_equal(res_2.in_port(0).data.get_shape(), int64_array([3]))