def build_cast_test_graphs(input_data, dst_type_str='FP16'): nodes = { **valued_const_with_data('input', float32_array(input_data)), **regular_op_with_empty_data( 'cast', { 'type': 'Convert', 'op': 'Cast', 'dst_type': np.float32, 'infer': Cast.infer }), **result('res'), } nodes_ref = deepcopy(nodes) nodes_ref.update({ **regular_op_with_empty_data( 'cast', { 'type': 'Convert', 'op': 'Cast', 'dst_type': data_type_str_to_np(dst_type_str), 'infer': Cast.infer }), }) edges = [ *connect('input', 'cast'), *connect('cast', 'res'), ] graph = build_graph(nodes, edges) graph_ref = build_graph(nodes_ref, edges) graph = partial_infer(graph) graph.graph['cmd_params'].data_type = dst_type_str convert_blobs(graph, dst_type_str) return graph, graph_ref
def test_one(self): nodes = { **regular_op_with_empty_data('input', {'type': 'Parameter'}), **regular_op_with_empty_data('some_op', {'type': 'SomeOp', 'name': 'some_op_name'}), **regular_op_with_empty_data('fake_output', {'type': None, 'kind': 'op', 'op': 'FakeOutput', 'name': 'my_output_name'}), **result('result'), } edges = [*connect('input', 'some_op'), *connect('some_op', 'fake_output'), *connect('fake_output', 'result'), ] graph = build_graph(nodes, edges) edges_ref = [*connect('input', 'some_op'), *connect('some_op', 'result'), ] graph_ref = build_graph(nodes, edges_ref, {'some_op': {'name': 'my_output_name'}}) FakeOutputResolver().find_and_replace_pattern(graph) (flag, resp) = compare_graphs(graph, graph_ref, 'result') self.assertTrue(flag, resp)
def test_2(self): graph = build_graph( nodes_attrs={ **shaped_parameter('input', int64_array([1, 3, 15, 15])), **regular_op_with_empty_data('layer_norm', {'op': 'LayerNorm', 'epsilon': 0.001, 'axis': 1, 'output_mean_var': False}), **shaped_const_with_data('gamma', None), **shaped_const_with_data('beta', None), **result('result') }, edges=[ *connect('input', '0:layer_norm'), *connect('gamma', '1:layer_norm'), *connect('beta', '2:layer_norm'), *connect('layer_norm', 'result') ] ) ref_graph = build_graph( nodes_attrs={ **shaped_parameter('input', int64_array([1, 3, 15, 15])), **shaped_const_with_data('mvn_const', None), **regular_op_with_empty_data('mvn', {'eps': 0.001, 'across_channels': 1, 'normalize_variance': 1, 'eps_mode': 'inside_sqrt', 'op': 'MVN', 'type': 'MVN'}), **shaped_const_with_data('gamma', None), **regular_op_with_empty_data('gamma_unsqueeze', {'op': 'Unsqueeze', 'type': 'Unsqueeze'}), **shaped_const_with_data('gamma_unsqueeze_const', None), **regular_op_with_empty_data('beta_unsqueeze', {'op': 'Unsqueeze', 'type': 'Unsqueeze'}), **shaped_const_with_data('beta_unsqueeze_const', None), **regular_op_with_empty_data('mul', {'op': 'Mul', 'type': 'Multiply'}), **shaped_const_with_data('beta', None), **regular_op_with_empty_data('add', {'op': 'Add', 'type': 'Add'}), **result('result') }, edges=[ *connect('input', '0:mvn'), *connect('mvn_const', '1:mvn'), *connect('mvn', '0:mul'), *connect('gamma', 'gamma_unsqueeze'), *connect('gamma_unsqueeze_const', '1:gamma_unsqueeze'), *connect('gamma_unsqueeze', '1:mul'), *connect('mul', '0:add'), *connect('beta', 'beta_unsqueeze'), *connect('beta_unsqueeze_const', '1:beta_unsqueeze'), *connect('beta_unsqueeze', '1:add'), *connect('add', 'result') ], update_attributes={ 'mvn_const': {'value': int64_array([1]), 'shape': int64_array([1])}, 'gamma_unsqueeze_const': {'value': int64_array([0, 2, 3]), 'shape': int64_array([3])}, 'beta_unsqueeze_const': {'value': int64_array([0, 2, 3]), 'shape': int64_array([3])} } ) LayerNormalization().find_and_replace_pattern(graph) flag, resp = compare_graphs(graph, ref_graph, 'result', 'result', check_op_attrs=True) self.assertTrue(flag, resp)
def test_run_with_solitary_shapeof_in_shape_value_subgraph(self): # in this case MarkNodesWithShapeValues must leave graph unchanged # so reference nodes are exactly the same inp_shape_1 = int64_array((1, 3, 100, 100)) inp_shape_2 = int64_array((1, 3, 100, 50)) # inp_2 and const will be concatenated to (1, 3, 200, 50) const_shape = int64_array((1, 3, 100, 50)) nodes = { **regular_op_with_shaped_data('input_1', inp_shape_1, {'op': 'Parameter', 'type': 'Parameter'}), **regular_op_with_shaped_data('input_2', inp_shape_2, {'op': 'Parameter', 'type': 'Parameter', 'returns_shape_value': False}), **shaped_const_with_data('const', const_shape), **regular_op_with_empty_data('concat', {'op': 'Concat', 'type': 'Concat', 'axis': 2, 'returns_shape_value': False}), **regular_op_with_empty_data('shapeof', {'op': 'ShapeOf', 'type': 'ShapeOf'}), **regular_op_with_empty_data('reshape', {'op': 'Reshape', 'type': 'Reshape'}), **result('res'), } edges = [ *connect('input_1', '0:reshape'), *connect('input_2', '0:concat'), *connect('const', '1:concat'), *connect('concat', 'shapeof'), *connect('shapeof', '1:reshape'), *connect('reshape', 'res'), ] graph = build_graph(nodes, edges) MarkNodesWithShapeValues().find_and_replace_pattern(graph) graph_ref = build_graph(nodes, edges) (flag, resp) = compare_graphs(graph, graph_ref, 'res', check_op_attrs=True) self.assertTrue(flag, "'returns_shape_value' should be False or unset for ShapeOf input nodes" + ': ' + str(resp))
def test_slice_infer(self, inp_value, inp_shape, starts, ends, axes, steps, expected_value, expected_shape): if inp_value is None: input_node = shaped_data('data_1', int64_array(inp_shape)) else: input_node = valued_data('data_1', int64_array(inp_value)) if inp_value is not None and inp_shape is not None: assert np.array_equal(np.array(inp_value).shape, inp_shape) def convert_args(val, name=''): if val is not None: return valued_const_with_data(name, int64_array(val)) else: return shaped_const_with_data(name, [0]) #fake shape starts = convert_args(starts, 'starts') ends = convert_args(ends, 'ends') axes = convert_args(axes, 'axes') steps = convert_args(steps, 'steps') if expected_shape is not None: expected_shape = shape_array(expected_shape) nodes = { **input_node, **regular_op_with_empty_data('slice', {'op': 'Slice'}), **starts, **ends, **axes, **steps, } graph = build_graph( nodes, [('data_1', 'slice'), *connect('starts', '1:slice'), *connect('ends', '2:slice'), *connect('axes', '3:slice'), *connect('steps', '4:slice'), *connect('slice', 'slice_d')]) graph.stage = 'middle' slice_node = Node(graph, 'slice') Slice.infer(slice_node) if expected_value is not None: self.assertTrue( strict_compare_tensors(slice_node.out_node().value, expected_value)) self.assertTrue( strict_compare_tensors(slice_node.out_node().shape, expected_shape))
def build_graph_to_test_type_alignment(edges, input_1_type=np.float32, input_2_type=np.float32, const_type=np.float32): input_shape = int64_array([1, 3, 255, 255]) const_value = np.array([1], dtype=const_type) nodes = { **shaped_parameter('input_1', input_shape, {'data_type': input_1_type}), **shaped_parameter('input_2', input_shape, {'data_type': input_2_type}), **regular_op_with_empty_data('add', {'op': 'Add', 'type': 'Add', 'type_infer': Elementwise.type_infer}), **valued_const_with_data('const', const_value, kwargs={'data_type': const_type}), **result('result'), } graph = build_graph(nodes, edges, nodes_with_edges_only=True) graph.stage = 'back' return graph
def test_broadcast(self, data, target_shape, axes_mapping=None, mode='numpy', ref_out=None, test_raising=False): if ref_out is not None: input = valued_const_with_data('data', int64_array(data)) else: input = shaped_data('data', int64_array(data)) nodes = { **input, **valued_const_with_data('target_shape', int64_array(target_shape)), **regular_op_with_empty_data('broadcast', { 'op': 'Broadcast', 'mode': mode }), } edges = [('data', 'broadcast'), ('target_shape', 'broadcast'), ('broadcast', 'broadcast_d')] if axes_mapping is not None: nodes.update(**valued_const_with_data('axes_mapping', int64_array(axes_mapping))) edges.append(('axes_mapping', 'broadcast')) graph = build_graph(nodes, edges) broadcast_node = Node(graph, 'broadcast') if test_raising: self.assertRaises(AssertionError, Broadcast.infer, broadcast_node) return Broadcast.infer(broadcast_node) if ref_out is not None: self.assertTrue( np.array_equal(broadcast_node.out_node().value, np.array(ref_out))) else: self.assertTrue( np.array_equal(broadcast_node.out_node().shape, np.array(target_shape)))
def test_v10_group_convolution_resolver_depthwise_conv2d(self): nodes = { **regular_op_with_shaped_data('input', [1, 1, 224, 224], { 'type': 'Parameter' }), **valued_const_with_data('weights', np.ones([1, 8, 7, 7])), **valued_const_with_data('dim', int64_array([1, 8, 1, 7, 7])), **regular_op_with_empty_data('reshape', {'type': 'Reshape'}), **regular_op_with_shaped_data( 'convolution', None, { 'type': 'Convolution', 'group': 1, 'output': 8, 'op': 'DepthwiseConv2dNative' }), **result(), } graph = build_graph(nodes, [ *connect('input', '0:convolution'), *connect('weights', '1:convolution'), *connect('convolution', 'output'), ], nodes_with_edges_only=True) V10ConvolutionWithGroupsResolver().find_and_replace_pattern(graph) nodes['convolution']['type'] = 'GroupConvolution' del nodes['convolution']['group'] graph_ref = build_graph(nodes, [ *connect('input', '0:convolution'), *connect('weights', '0:reshape'), *connect('dim', '1:reshape'), *connect('reshape', '1:convolution'), *connect('convolution', 'output'), ], nodes_with_edges_only=True) (flag, resp) = compare_graphs(graph, graph_ref, last_node='output', check_op_attrs=True) self.assertTrue(flag, resp)
def test_slice_infer_negative(self, inp_value, starts, ends, axes, steps, expected, inp_shape=None): if inp_value is None: input_node = shaped_data('data_1', int64_array(inp_shape)) else: input_node = valued_data('data_1', int64_array(inp_value)) def convert_args(val, name=''): if val is not None: return valued_const_with_data(name, int64_array(val)) else: return shaped_const_with_data(name, [0]) #fake shape starts = convert_args(starts, 'starts') ends = convert_args(ends, 'ends') axes = convert_args(axes, 'axes') steps = convert_args(steps, 'steps') nodes = { **input_node, **regular_op_with_empty_data('slice', {'op': 'Slice'}), **starts, **ends, **axes, **steps } graph = build_graph( nodes, [('data_1', 'slice'), *connect('starts', '1:slice'), *connect('ends', '2:slice'), *connect('axes', '3:slice'), *connect('steps', '4:slice'), *connect('slice', 'slice_d')]) graph.stage = 'middle' slice_node = Node(graph, 'slice') self.assertRaises(Error, Slice.infer, slice_node)
def test_broadcast_dynamic(self, data, target_shape_shape, mode='numpy', ref_out_shape=None, test_raising=False): nodes = { **shaped_data('data', int64_array(data)), **shaped_data('target_shape', int64_array(target_shape_shape)), **regular_op_with_empty_data('broadcast', {'op': 'Broadcast', 'mode': mode}), } edges = [('data', 'broadcast'), ('target_shape', 'broadcast'), ('broadcast', 'broadcast_d')] graph = build_graph(nodes, edges) broadcast_node = Node(graph, 'broadcast') if test_raising: self.assertRaises(AssertionError, Broadcast.infer, broadcast_node) return Broadcast.infer(broadcast_node) self.assertTrue(np.array_equal(broadcast_node.out_node().shape, ref_out_shape))
def check_shape_infer(self, data_shape, indices_shape, axis, ref): nodes = { **shaped_parameter('data', data_shape), **shaped_parameter('indices', indices_shape), **regular_op_with_empty_data('gather_elements', {'op': 'GatherElements', 'axis': axis}), **result() } graph = build_graph(nodes_attrs=nodes, edges=[ *connect('data', '0:gather_elements'), *connect('indices', '1:gather_elements'), *connect('gather_elements', 'output') ], nodes_with_edges_only=True) graph.stage = 'middle' gather_el_node = Node(graph, 'gather_elements') GatherElements.infer(gather_el_node) res_output_shape = gather_el_node.out_node().shape self.assertTrue(strict_compare_tensors(res_output_shape, ref))
def build_and_test_reverse_inference(data_shape, indices_shape, axis, batch_dims, out_shape, ref_shape): in_port_with_defined_shape = 0 if data_shape is not None else 1 defined_shape = shape_array( data_shape if data_shape is not None else indices_shape) nodes = { **shaped_parameter('data', data_shape, { 'reverse_infer': Parameter.reverse_infer }), **shaped_parameter('indices', indices_shape, { 'reverse_infer': Parameter.reverse_infer }), **valued_const_with_data('axis', int64_array(axis)), **regular_op_with_empty_data( 'gather', { 'op': 'Gather', 'batch_dims': batch_dims, 'infer': Gather.infer, 'reverse_infer': Gather.reverse_infer }), **result('res'), } edges = [ *connect('data', '0:gather'), *connect('indices', '1:gather'), *connect('axis', '2:gather'), *connect('gather', 'res') ] graph = build_graph(nodes, edges) graph.stage = 'middle' Node(graph, 'gather').out_port(0).data.set_shape(shape_array(out_shape)) Node(graph, 'gather').in_port( in_port_with_defined_shape).data.set_shape(defined_shape) partial_infer(graph) actual_shape = Node(graph, 'gather').in_port( int(not in_port_with_defined_shape)).data.get_shape() assert strict_compare_tensors(actual_shape, shape_array(ref_shape))
def test_slice_infer(self, inp_value, starts, ends, axes, steps, expected, inp_shape=None): if inp_value is None: input_node = shaped_data('data_1', int64_array(inp_shape)) else: input_node = valued_data('data_1', int64_array(inp_value)) nodes = { **input_node, **regular_op_with_empty_data('slice', {'op': 'Slice'}), **valued_const_with_data('starts', int64_array(starts)), **valued_const_with_data('ends', int64_array(ends)), **valued_const_with_data('axes', int64_array(axes)), **valued_const_with_data('steps', int64_array(steps)), } graph = build_graph( nodes, [('data_1', 'slice'), *connect('starts', '1:slice'), *connect('ends', '2:slice'), *connect('axes', '3:slice'), *connect('steps', '4:slice'), *connect('slice', 'slice_d')]) graph.stage = 'middle' slice_node = Node(graph, 'slice') Slice.infer(slice_node) if inp_value is not None: self.assertTrue( np.array_equal(slice_node.out_node().value, expected)) else: self.assertTrue( np.array_equal(slice_node.out_node().shape, expected))
def test_v7_group_convolution_resolver(self): nodes = { **regular_op_with_shaped_data('input', [1, 3, 224, 224], { 'type': 'Parameter' }), **valued_const_with_data('weights', np.ones([3, 8, 7, 7])), **valued_const_with_data('dim', int64_array([24, -1, 0, 0])), **regular_op_with_empty_data('reshape', {'type': 'Reshape'}), **regular_op_with_shaped_data('convolution', None, { 'type': 'Convolution', 'group': 3, 'output': 24 }), **result(), } graph = build_graph(nodes, [ *connect('input', '0:convolution'), *connect('weights', '1:convolution'), *connect('convolution', 'output'), ], nodes_with_edges_only=True) V7ConvolutionWithGroupsResolver().find_and_replace_pattern(graph) graph_ref = build_graph(nodes, [ *connect('input', '0:convolution'), *connect('weights', '0:reshape'), *connect('dim', '1:reshape'), *connect('reshape', '1:convolution'), *connect('convolution', 'output'), ], nodes_with_edges_only=True) (flag, resp) = compare_graphs(graph, graph_ref, last_node='output', check_op_attrs=True) self.assertTrue(flag, resp)
def test_gatherelements_value_infer(self, data, indices, axis, ref_res): nodes = { **valued_const_with_data('data', int64_array(data)), **valued_const_with_data('indices', int64_array(indices)), **regular_op_with_empty_data('gather_elements', {'op': 'GatherElements', 'axis': axis}), **result() } graph = build_graph(nodes_attrs=nodes, edges=[ *connect('data', '0:gather_elements'), *connect('indices', '1:gather_elements'), *connect('gather_elements', 'output') ], nodes_with_edges_only=True) graph.stage = 'middle' gather_el_node = Node(graph, 'gather_elements') GatherElements.infer(gather_el_node) res_output_shape = gather_el_node.out_node().shape self.assertTrue(np.array_equal(int64_array(ref_res).shape, res_output_shape)) res_output_value = gather_el_node.out_node().value if res_output_value is not None: self.assertTrue(np.array_equal(int64_array(ref_res), res_output_value))
def test_negative(self): graph = build_graph( nodes_attrs={ **shaped_parameter('input', int64_array([1, 3, 15, 15])), **regular_op_with_empty_data('layer_norm', {'op': 'LayerNorm', 'epsilon': 0.001, 'axis': -1, 'output_mean_var': True}), **shaped_const_with_data('gamma', None), **shaped_const_with_data('beta', None), **result('result'), **result('result_1'), **result('result_2') }, edges=[ *connect('input', '0:layer_norm'), *connect('gamma', '1:layer_norm'), *connect('beta', '2:layer_norm'), *connect('layer_norm:0', 'result'), *connect('layer_norm:1', 'result_1'), *connect('layer_norm:2', 'result_2') ] ) with self.assertRaises(Error): LayerNormalization().find_and_replace_pattern(graph)
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)
import unittest from extensions.middle.StridedSliceReplacer import ReplaceStridedSliceWithSqueezeUnsqueeze from mo.utils.ir_engine.compare_graphs import compare_graphs from unit_tests.utils.graph import regular_op_with_shaped_data, regular_op_with_empty_data, shaped_const_with_data, \ result, connect, build_graph nodes = { **regular_op_with_shaped_data('input', [1, 3, 5, 5], { 'type': 'Parameter', 'op': 'Parameter' }), **regular_op_with_empty_data( 'strided_slice', { 'type': 'StridedSlice', 'op': 'StridedSlice', 'begin_mask': [0, 0, 0, 0], 'end_mask': [0, 0, 0, 0] }), **shaped_const_with_data('begin', [4]), **shaped_const_with_data('end', [4]), **result('result'), **regular_op_with_empty_data('squeeze', { 'type': 'Squeeze', 'op': 'Squeeze' }), **shaped_const_with_data('squeeze_axes', None), **regular_op_with_empty_data('unsqueeze', { 'type': 'Unsqueeze', 'op': 'Unsqueeze' }),
import unittest import numpy as np from generator import generator, generate from openvino.tools.mo.ops.Cast import Cast from openvino.tools.mo.middle.passes.convert_data_type import packed_U4, packed_I4 from openvino.tools.mo.middle.passes.infer import partial_infer from openvino.tools.mo.utils.ir_engine.compare_graphs import compare_graphs from unit_tests.utils.graph import valued_const_with_data, regular_op_with_empty_data, result, build_graph, connect nodes = lambda value, dst_type: { **valued_const_with_data('value', np.array(value)), **regular_op_with_empty_data('convert', { 'dst_type': dst_type, 'infer': Cast.infer }), **result(), } @generator class CastTest(unittest.TestCase): """ Example of checking: 7 == 0111, padded to 0111 0000, results in 112 7 == 0111, 8 == 1000 packed to 0111 1000, results in 120 -8 == 1000, padded to 1000 0000, results in 128 """ @generate(*[
**regular_op_with_empty_data( "Loop", { 'op': "Loop", 'type': 'Loop', 'sub_graphs': ['body'], "body": None, 'input_port_map': [{ 'external_port_id': 1, 'internal_layer_id': 2, 'axis': None }, { 'external_port_id': 2, 'internal_layer_id': 0, 'axis': None }, { 'external_port_id': 3, 'internal_layer_id': 1, 'axis': None }], 'output_port_map': [{ 'external_port_id': 0, 'internal_layer_id': 4, 'axis': None }, { 'external_port_id': -1, 'internal_layer_id': 5, 'axis': None, 'purpose': "execution_condition" }], 'back_edges': [{ 'from_layer': 8, 'to_layer': 7 }, { 'from_layer': 10, 'to_layer': 9 }], 'infer': Loop.infer }),
# Copyright (C) 2018-2021 Intel Corporation # SPDX-License-Identifier: Apache-2.0 import unittest from openvino.tools.mo.front.tf.floor_div_decomposition import FloorDivDecomposition from openvino.tools.mo.utils.ir_engine.compare_graphs import compare_graphs from unit_tests.utils.graph import build_graph, result, connect, \ connect_data, regular_op_with_empty_data nodes = { **regular_op_with_empty_data('placeholder_1', { 'type': 'Parameter', 'op': 'Parameter' }), **regular_op_with_empty_data('placeholder_2', { 'type': 'Parameter', 'op': 'Parameter' }), **regular_op_with_empty_data('floor_div', { 'op': 'FloorDiv', 'type': None, 'name': 'my_floor_div' }), **regular_op_with_empty_data('div', { 'type': 'Divide', 'op': 'Div' }), **regular_op_with_empty_data('floor', { 'type': 'Floor', 'op': 'Floor'
# Copyright (C) 2018-2022 Intel Corporation # SPDX-License-Identifier: Apache-2.0 import unittest import numpy as np from openvino.tools.mo.middle.ArgOpsToTopK import ArgOpsToTopK from openvino.tools.mo.front.common.partial_infer.utils import int64_array from openvino.tools.mo.utils.ir_engine.compare_graphs import compare_graphs from unit_tests.utils.graph import regular_op_with_empty_data, result, build_graph, connect, \ valued_const_with_data, regular_op, empty_data, connect_front nodes_attributes = { **regular_op_with_empty_data('input', { 'op': 'Parameter', 'type': 'Parameter' }), **regular_op_with_empty_data( 'argmax', { 'op': 'ArgMax', 'type': None, 'out_max_val': 0, 'top_k': 1, 'axis': 0, 'output_type': np.int32, 'remove_values_output': True }), **regular_op_with_empty_data( 'argmin', { 'op': 'ArgMin', 'type': None,
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 numpy as np from generator import generator, generate from openvino.tools.mo.middle.PreserveRuntimeInfo import PreserveRuntimeInfo from openvino.tools.mo.ops.transpose import Transpose from openvino.tools.mo.front.common.partial_infer.elemental import copy_shape_infer from openvino.tools.mo.graph.graph import Node from openvino.tools.mo.ops.op import PermuteAttrs from openvino.tools.mo.utils.ir_engine.compare_graphs import compare_graphs from openvino.tools.mo.utils.runtime_info import RTInfo from unit_tests.utils.graph import build_graph, connect, valued_const_with_data, regular_op_with_empty_data, \ regular_op_with_shaped_data nodes = { **regular_op_with_empty_data('placeholder2', {'type': 'Parameter'}), **regular_op_with_empty_data('transpose_parameter', { 'type': 'Transpose', 'op': 'Transpose', 'infer': Transpose.infer }), **regular_op_with_empty_data('transpose_result', { 'type': 'Transpose', 'op': 'Transpose', 'infer': Transpose.infer }), } edges = [ *connect('placeholder1', '0:add'), *connect('placeholder2', '1:add'), *connect('add', 'result')
# Copyright (C) 2018-2021 Intel Corporation # SPDX-License-Identifier: Apache-2.0 import unittest from extensions.front.onnx.MvnOnnxToMvn import MvnOnnxToMvn from mo.front.common.partial_infer.utils import int64_array from mo.utils.ir_engine.compare_graphs import compare_graphs from unit_tests.utils.graph import build_graph, regular_op_with_empty_data, result, const, connect_front nodes = { **regular_op_with_empty_data('input', {'type': 'Parameter'}), **regular_op_with_empty_data('mvn_onnx', {'op': 'MVNOnnx', 'axes': int64_array([2, 3]), 'eps': 1e-9, 'eps_mode': 'outside_sqrt', 'normalize_variance': 1}), **result(), # nodes after replacement **const('axes', int64_array([2, 3])), **regular_op_with_empty_data('mvn', {'op': 'MVN', 'type': None}), } class MvnOnnxToMvnTest(unittest.TestCase): def test_mvn_normalize(self): graph = build_graph(nodes, [('input', 'mvn_onnx'), ('mvn_onnx', 'output')], nodes_with_edges_only=True) graph.stage = 'front'
def get_graphs(input_shape, reshape_0_pattern, order, reshape_1_pattern, block_size): nodes = { **regular_op_with_shaped_data( 'input', input_shape, { 'type': 'Parameter', 'shape': int64_array(input_shape), 'infer': Parameter.infer }), **valued_const_with_data('reshape_0_pattern', int64_array(reshape_0_pattern)), **regular_op_with_empty_data('reshape_0', { 'type': 'Reshape', 'infer': Reshape.infer }), **valued_const_with_data('order', int64_array(order)), **regular_op_with_empty_data('transpose', { 'type': 'Transpose', 'infer': Transpose.infer }), **valued_const_with_data('reshape_1_pattern', int64_array(reshape_1_pattern)), **regular_op_with_empty_data('reshape_1', { 'type': 'Reshape', 'infer': Reshape.infer, 'name': 'final_reshape' }), **result(), } edges = [ *connect('input', '0:reshape_0'), *connect('reshape_0_pattern', '1:reshape_0'), *connect('reshape_0', '0:transpose'), *connect('order', '1:transpose'), *connect('transpose', '0:reshape_1'), *connect('reshape_1_pattern', '1:reshape_1'), *connect('reshape_1', 'output'), ] graph = build_graph(nodes, edges, nodes_with_edges_only=True, cli=Namespace()) for node in graph.get_op_nodes(): node['op'] = node['type'] graph.clean_up() ref_nodes = { **regular_op_with_shaped_data( 'input', input_shape, { 'type': 'Parameter', 'shape': int64_array(input_shape), 'infer': Parameter.infer }), **regular_op_with_empty_data( 'depth_to_space', { 'type': 'DepthToSpace', 'infer': DepthToSpaceOp.infer, 'name': 'final_reshape', 'block_size': block_size }), **result() } ref_edges = [ *connect('input', 'depth_to_space'), *connect('depth_to_space', 'output') ] graph_ref = build_graph(ref_nodes, ref_edges, nodes_with_edges_only=True) for node in graph_ref.get_op_nodes(): node['op'] = node['type'] graph_ref.clean_up() graph.graph['layout'] = 'NCHW' graph_ref.graph['layout'] = 'NCHW' return graph, graph_ref
def get_graphs(input_shape, reshape_0_pattern, order, reshape_1_pattern, group): nodes = { **regular_op_with_shaped_data( 'input', input_shape, { 'type': 'Parameter', 'shape': int64_array(input_shape), 'infer': Parameter.infer }), **valued_const_with_data('reshape_0_pattern', int64_array(reshape_0_pattern)), **regular_op_with_empty_data('reshape_0', { 'type': 'Reshape', 'infer': Reshape.infer }), **valued_const_with_data('order', int64_array(order)), **regular_op_with_empty_data('transpose', { 'type': 'Transpose', 'infer': Transpose.infer }), **valued_const_with_data('reshape_1_pattern', int64_array(reshape_1_pattern)), **regular_op_with_empty_data('reshape_1', { 'type': 'Reshape', 'infer': Reshape.infer, 'name': 'final_reshape' }), **result(), } edges = [ *connect('input', '0:reshape_0'), *connect('reshape_0_pattern', '1:reshape_0'), *connect('reshape_0', '0:transpose'), *connect('order', '1:transpose'), *connect('transpose', '0:reshape_1'), *connect('reshape_1_pattern', '1:reshape_1'), *connect('reshape_1', 'output'), ] graph = build_graph(nodes, edges, nodes_with_edges_only=True) for node in graph.get_op_nodes(): node['op'] = node['type'] graph.clean_up() ref_nodes = { **regular_op_with_shaped_data( 'input', input_shape, { 'type': 'Parameter', 'shape': int64_array(input_shape), 'infer': Parameter.infer }), **regular_op_with_empty_data( 'shuffle_channel', { 'type': 'ShuffleChannels', 'infer': ShuffleChannels.infer, 'name': 'final_reshape', 'group': group }), **result() } ref_edges = [ *connect('input', 'shuffle_channel'), *connect('shuffle_channel', 'output') ] graph_ref = build_graph(ref_nodes, ref_edges, nodes_with_edges_only=True) for node in graph_ref.get_op_nodes(): node['op'] = node['type'] graph_ref.clean_up() return graph, graph_ref
def test_fake_results(self): then_graph_nodes = { **valued_const_with_data('fake_const', int64_array(0)), **regular_op_with_empty_data( 'shapeof', { 'kind': 'op', 'type': 'ShapeOf', 'op': 'ShapeOf', 'infer': Shape.infer, 'output_type': np.int64 }), **regular_op_with_empty_data( 'res_1', { 'kind': 'op', 'type': 'Result', 'op': 'Result', 'infer': lambda x: 0, 'output_id': 0 }) } then_graph_edges = [ *connect('fake_const', 'shapeof'), *connect('shapeof', 'res_1'), ] 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( 'res_1', { 'kind': 'op', 'type': 'Result', 'op': 'Result', 'infer': lambda x: 0, 'output_id': 0 }) } else_graph_edges = [*connect('param_1', 'res_1')] 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', shape_array([dynamic_dimension_value])), **valued_const_with_data( 'input_1', int64_array([1, 2, 3, 3, 2, 3]).reshape((2, 3))), **regular_op_with_empty_data( 'if', { 'kind': 'op', 'op': 'If', 'then_graph': then_graph, 'else_graph': else_graph, 'infer': If.infer }), **result('res_1') } external_graph_edges = [ *connect('cond', '0:if'), *connect('input_1', '1:if'), *connect('if', 'res_1') ] graph = build_graph(external_graph_nodes, external_graph_edges) graph.stage = 'middle' partial_infer(graph) npt.assert_array_equal( Node(graph, 'if').out_port(0).data.get_shape(), int64_array([2, 3]))
# Copyright (C) 2018-2022 Intel Corporation # SPDX-License-Identifier: Apache-2.0 import unittest import numpy as np from openvino.tools.mo.front.tf.TFSliceToSlice import TFSliceToSliceReplacer from openvino.tools.mo.utils.ir_engine.compare_graphs import compare_graphs from unit_tests.utils.graph import build_graph, regular_op_with_empty_data, result, const, connect_front nodes = { **regular_op_with_empty_data('input', {'type': 'Parameter'}), **regular_op_with_empty_data('tfslice', { 'op': 'TFSlice', 'type': None }), **const('begin', np.array(0)), **const('size', np.array([-1])), **regular_op_with_empty_data('john_doe', { 'op': 'Sum', 'type': None }), **result(), # nodes after replacement **const('minus_one', np.array(-1)), **regular_op_with_empty_data('shapeof', { 'op': 'ShapeOf', 'type': 'ShapeOf' }),
# Copyright (C) 2018-2022 Intel Corporation # SPDX-License-Identifier: Apache-2.0 import unittest import numpy as np from generator import generator, generate from openvino.tools.mo.front.rank_decomposer import RankDecomposer from openvino.tools.mo.front.common.partial_infer.utils import int64_array from openvino.tools.mo.utils.ir_engine.compare_graphs import compare_graphs from unit_tests.utils.graph import build_graph, regular_op_with_empty_data, result, connect, \ valued_const_with_data nodes = lambda output_type: { **regular_op_with_empty_data('input', {'type': 'Parameter'}), **regular_op_with_empty_data('rank', {'op': 'Rank', 'type': None, 'output_type': output_type, 'name': 'my_rank'}), **result(), **regular_op_with_empty_data('shape', {'type': 'ShapeOf', 'output_type': output_type}), **regular_op_with_empty_data('rank_1D', {'type': 'ShapeOf', 'output_type': output_type}), **valued_const_with_data('zero', int64_array(0)), **regular_op_with_empty_data('rank_0D', {'type': 'Squeeze'}), } @generator class RankDecomposerTest(unittest.TestCase): @generate(np.int32, np.int64) def test_rank_decomposer(self, output_type):