def test_not_useless_pad_constant_input(self): nodes = { **regular_op_with_shaped_data('placeholder', [1, 10, 20, 3], { 'type': 'Parameter' }), **regular_op_with_shaped_data('pad', [1, 10, 20, 3], { 'type': 'Pad', 'op': 'Pad' }), **valued_const_with_data('pads_begin', int64_array([0, 0, 0, 0])), **valued_const_with_data('pads_end', int64_array([0, 1, 0, 0])), **valued_const_with_data('fill_value', np.array(1)), **result('result'), } edges = [ *connect('placeholder', '0:pad'), *connect('pads_begin', '1:pad'), *connect('pads_end', '2:pad'), *connect('fill_value', '3:pad'), *connect('pad', 'result'), ] graph = build_graph(nodes, edges) RemoveUselessPad().find_and_replace_pattern(graph) ref_graph = build_graph(nodes, edges) (flag, resp) = compare_graphs(graph, ref_graph, 'result') self.assertTrue(flag, resp)
def test_2_inputs(self): nodes = { **regular_op_with_shaped_data('placeholder', [1, 3, 20, 20], { 'type': 'Parameter' }), **regular_op_with_shaped_data('a_clamp', [1, 3, 20, 20], { 'type': None, 'op': 'Clamp' }), **regular_op_with_shaped_data('clamp', [1, 3, 20, 20], { 'type': 'Clamp', 'op': 'AttributedClamp', 'min': -3.5, 'max': 3.5 }), **valued_const_with_data('min', np.array(-3.5)), **valued_const_with_data('max', np.array(3.5)), **result('result'), } edges = [ *connect('placeholder', '0:a_clamp'), *connect('min', '1:a_clamp'), *connect('max', '2:a_clamp'), *connect('a_clamp', 'result'), ] graph = build_graph(nodes, edges) ClampNormalizer().find_and_replace_pattern(graph) ref_graph = build_graph( nodes, [*connect('placeholder', '0:clamp'), *connect('clamp', 'result')]) (flag, resp) = compare_graphs(graph, ref_graph, 'result') self.assertTrue(flag, resp)
def test_not_useless_pad_non_constant_input(self): nodes = { **regular_op_with_shaped_data('placeholder', [10, 20, 3], {'type': 'Parameter'}), **regular_op_with_shaped_data('shape_of_1', [3], {'type': 'ShapeOf'}), **regular_op_with_shaped_data('sub', [3], {'type': 'Subtract', 'op': 'Sub'}), **valued_const_with_data('desired_output_size', int64_array([10, 20, 3])), **regular_op_with_shaped_data('pad', [10, 20, 3], {'type': 'Pad', 'op': 'Pad'}), **valued_const_with_data('fill_value', np.array(1)), **result('result'), } edges = [*connect('placeholder', '0:pad'), *connect('placeholder', 'shape_of_1'), *connect('shape_of_1', '0:sub'), *connect('desired_output_size', '1:sub'), *connect('sub', '1:pad'), *connect_data('sub', '2:pad'), *connect('fill_value', '3:pad'), *connect('pad', 'result'), ] graph = build_graph(nodes, edges) RemoveUselessPad().find_and_replace_pattern(graph) ref_graph = build_graph(nodes, edges) (flag, resp) = compare_graphs(graph, ref_graph, 'result') self.assertTrue(flag, resp)
def test_backward_bfs_multi_consumer_data_nodes(self): # Placeholder-> Mul -> Result # Const -/ \- Result2 graph = build_graph( { **regular_op_with_shaped_data('parameter', [1], { 'op': 'Parameter' }), **valued_const_with_data('const', int64_array([5])), **regular_op_with_shaped_data('mul', [1], {'op': 'Mul'}), **result('result'), **result('result2'), }, [ *connect('parameter', '0:mul'), *connect('const', '1:mul'), *connect('mul:0', 'result'), *connect_data('mul', 'result2'), ]) res = common_bfs(Node(graph, 'result'), ['Mul'], ['Parameter'], is_backward=True, attr_to_check='op', follow_multi_consumer_data_nodes=True) self.assertTrue( len(res) == 1, 'The multi-consumer data node "mul_d" was not followed') res = common_bfs(Node(graph, 'result'), ['Mul'], ['Parameter'], is_backward=True, attr_to_check='op') self.assertTrue( len(res) == 0, 'The multi-consumer data node "mul_d" was followed')
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_with_shaped_data( 'nms', None, { 'op': 'NonMaxSuppression', 'type': 'NonMaxSuppression', 'name': 'nms' }), **result('output'), } self.graph = build_graph(nodes, [ *connect('boxes', '0:nms'), *connect('scores', '1:nms'), *connect('max_output_per_class', '2:nms'), *connect('nms', 'output'), ], nodes_with_edges_only=True)
def test_v7_group_convolution_resolver_weight_are_in_the_right_layout( self): nodes = { **regular_op_with_shaped_data('input', None, { 'type': 'Parameter' }), **valued_const_with_data('weights', np.ones([24, 1, 7, 7])), **regular_op_with_shaped_data('convolution', None, { 'type': 'Convolution', 'group': 3, 'output': 24 }), **result(), } edges = [ *connect('input', '0:convolution'), *connect('weights', '1:convolution'), *connect('convolution', 'output'), ] graph = build_graph(nodes, edges) V7ConvolutionWithGroupsResolver().find_and_replace_pattern(graph) graph_ref = build_graph(nodes, edges) (flag, resp) = compare_graphs(graph, graph_ref, last_node='output', check_op_attrs=True) self.assertTrue(flag, resp)
def test_div_with_integer(self): # Test where transformation should not be applied because the divisor is integer graph = build_graph( { **regular_op_with_shaped_data('parameter', [1, 227, 227, 3], { 'type': 'Parameter', 'data_type': np.int32 }), **valued_const_with_data('const', np.array([-1.], dtype=np.int32)), **regular_op_with_shaped_data('div', None, { 'op': 'Div', 'type': 'Divide', 'name': 'my_div' }), **result() }, [ *connect('parameter:0', '0:div'), *connect_data('const:0', '1:div'), *connect('div', 'output'), ]) graph_ref = graph.copy() Div().find_and_replace_pattern(graph) (flag, resp) = compare_graphs(graph, graph_ref, 'output', check_op_attrs=True) self.assertTrue(flag, resp)
def test_reshape_on_the_A_input(self, in1_shape, in2_shape, reshape_pattern, transpose_a, transpose_b, updated_pattern): nodes = { **regular_op_with_shaped_data( 'in_1', in1_shape, dict(type='Parameter', op='Parameter')), **regular_op_with_shaped_data( 'in_2', in2_shape, dict(type='Parameter', op='Parameter')), **valued_const_with_data('dim', int64_array(reshape_pattern)), **op_with_empty_data( 'reshape', dict(type='Reshape', op='Reshape', infer=Reshape.infer, need_shape_inference=True)), **op_with_empty_data( 'matmul', dict(type='MatMul', op='MatMul', infer=MatMul.infer, need_shape_inference=True, transpose_a=transpose_a, transpose_b=transpose_b, dim_attrs={})), **result(), } edges = [ *connect('in_1:0', '0:reshape'), *connect('dim:0', '1:reshape'), *connect('reshape:0', '0:matmul'), *connect('in_2:0', '1:matmul'), *connect('matmul:0', 'output'), ] graph = build_graph(nodes_attrs=nodes, edges=edges, cli=Namespace(static_shape=True)) graph.clean_up() SmartReshape_HC_Reshape_MatMul().find_and_replace_pattern(graph) graph.clean_up() graph_ref = build_graph(nodes_attrs=nodes, edges=edges, update_attributes={ 'dim': { 'value': int64_array(updated_pattern) }, 'dim_d': { 'value': int64_array(updated_pattern) } }) graph_ref.clean_up() (flag, resp) = compare_graphs(graph, graph_ref, 'output', check_op_attrs=True) self.assertTrue(flag, resp)
def nodes_dict(original, transformed=None, levels=255, data=None, il=[-127], ih=[127], ol=[-127], oh=[127]): shape = [1, 2, 3, 4] if data is None else np.array(data).shape data = np.ones(shape, dtype=original) if data is None else np.array( data, dtype=original) int_data = data.astype(dtype=np.int8) transformed = transformed if transformed is not None else original return { **valued_const_with_data('weights', data), **valued_const_with_data('int_weights', int_data), **regular_op_with_shaped_data( 'cast', shape, { 'type': 'Convert', 'op': 'Cast', 'infer': Cast.infer, 'dst_type': transformed }), **valued_const_with_data('il', np.array(il)), **valued_const_with_data('ih', np.array(ih)), **valued_const_with_data('ol', np.array(ol)), **valued_const_with_data('oh', np.array(oh)), **regular_op_with_shaped_data( 'FQ', shape, { 'type': 'FakeQuantize', 'infer': FakeQuantize.infer, 'stop_value_propagation': True, 'levels': levels, 'op': 'FakeQuantize' }), **valued_const_with_data('zp', np.array([0])), **valued_const_with_data('scale', np.array([1])), **regular_op_with_shaped_data( 'sub', shape, { 'type': 'Subtract', 'op': 'Sub', 'infer': lambda node: eltwise_infer(node, Sub.operation) }), **regular_op_with_shaped_data( 'mul', shape, { 'type': 'Multiply', 'op': 'Mul', 'infer': lambda node: eltwise_infer(node, Mul.operation) }), **result() }
def test_broadcast_with_range_positive_test(self): graph = build_graph({ **regular_op_with_shaped_data('shape', [2], {'type': 'Parameter'}), **valued_const_with_data('value', np.arange(0, 384).reshape((1, 384))), **regular_op_with_empty_data('bc', {'type': 'Broadcast'}), **result(), }, [ *connect('value', '0:bc'), *connect('shape', '1:bc'), *connect('bc', 'output'), ], nodes_with_edges_only=True) ExpandRangeConstant().find_and_replace_pattern(graph) graph_ref = build_graph({ **regular_op_with_shaped_data('shape', [2], {'type': 'Parameter'}), # start **valued_const_with_data('start', np.array(0)), # limit **valued_const_with_data('minus_one', np.array(-1)), **valued_const_with_data('zero', np.array(0)), **regular_op_with_empty_data('range_dim', {'type': 'Gather'}), # delta **valued_const_with_data('delta', np.array(1)), **regular_op_with_empty_data('range', {'type': 'Range'}), # keep dims **valued_const_with_data('axes', np.array([0])), **regular_op_with_empty_data('keep_shape', {'type': 'Unsqueeze'}), **regular_op_with_empty_data('bc', {'type': 'Broadcast'}), **result(), }, [ *connect('start', '0:range'), *connect('shape', '0:range_dim'), *connect('minus_one', '1:range_dim'), *connect('zero', '2:range_dim'), *connect('range_dim', '1:range'), *connect('delta', '2:range'), *connect('range', '0:keep_shape'), *connect('axes', '1:keep_shape'), *connect('keep_shape', '0:bc'), *connect_data('shape', '1:bc'), *connect('bc', 'output'), ], nodes_with_edges_only=True) (flag, resp) = compare_graphs(graph, graph_ref, 'output', check_op_attrs=True) self.assertTrue(flag, resp)
def test_deletion_trailing_unconnected_ports(self): nodes = { **shaped_const_with_data('input_0', [5, 3]), **regular_op_with_shaped_data('concat', [5, 3], { 'type': 'Concat', 'axis': 1 }), **result(), } edges_before = [ *connect('input_0', '0:concat'), *connect('concat', 'output'), ] edges_after = [ *connect('input_0', '0:concat'), *connect('concat', 'output'), ] graph = build_graph(nodes, edges_before, nodes_with_edges_only=True) Node(graph, 'concat').add_input_port(1) ConcatOdInputEraserAndPortsReconnect().find_and_replace_pattern(graph) graph_ref = build_graph(nodes, edges_after, nodes_with_edges_only=True) (flag, resp) = compare_graphs(graph, graph_ref, 'output', check_op_attrs=True) self.assertTrue(flag, resp) self.assertTrue(1 not in Node(graph, 'concat').in_ports())
def test_deletion(self): nodes = { **shaped_const_with_data('input_0', [1]), **shaped_const_with_data('input_1', [1]), **shaped_const_with_data('input_2', [0]), **shaped_const_with_data('input_3', [1]), **regular_op_with_shaped_data('concat', [3], {'type': 'Concat'}), **result(), } edges_before = [ *connect('input_0', '0:concat'), *connect('input_1', '1:concat'), *connect('input_2', '2:concat'), *connect('input_3', '3:concat'), *connect('concat', 'output'), ] edges_after = [ *connect('input_0', '0:concat'), *connect('input_1', '1:concat'), *connect('input_3', '3:concat'), *connect('concat', 'output'), ] graph = build_graph(nodes, edges_before, nodes_with_edges_only=True) ConcatOdInputEraser().find_and_replace_pattern(graph) graph_ref = build_graph(nodes, edges_after, nodes_with_edges_only=True) (flag, resp) = compare_graphs(graph, graph_ref, 'output', check_op_attrs=True) self.assertTrue(flag, resp)
def generate_nodes(data, axis=-1, depth=4, on_value=1., off_value=0.): return { 'indices': {'Op': 'Parameter', 'value': data, 'shape': int64_array(data.shape)}, 'indices_d': {'kind': 'data', 'value': data, 'shape': int64_array(data.shape)}, **valued_const_with_data('depth', int64_array(depth)), **valued_const_with_data('on_value', float_array(on_value)), **valued_const_with_data('off_value', float_array(off_value)), **regular_op_with_shaped_data('one_hot', None, {'type': 'OneHot', 'axis': axis, 'Op': 'OneHot'}) }
def test_leaky_relu_mul_multiple_consumers(self): # multiple consumers of Mul operation graph = build_graph_with_edge_attrs(nodes, edges, {}) additional_result = Result(graph, {'name': 'result_2'}).create_node() Node(graph, 'mul').out_port(0).connect(additional_result.in_port(0)) ref_nodes = { **regular_op_with_shaped_data('input', shape, { 'type': 'Parameter', 'op': 'Parameter' }), **regular_op_with_shaped_data('mul', shape, { 'type': 'Multiply', 'name': 'mul' }), **regular_op_with_shaped_data('max', shape, { 'type': 'Maximum', 'name': 'final_max' }), **valued_const_with_data('const', float_array([0.5])), **regular_op_with_shaped_data('leaky_relu', shape, { 'type': 'LeakyReLU', 'name': 'max_final', 'negative_slope': None }), **result('result'), **result('result_2') } ref_edges = [ *connect('input:0', '0:mul'), *connect('const', '1:mul'), *connect('max:0', 'result'), *connect('mul:0', 'result_2'), *connect_data('input', 'leaky_relu'), *connect('leaky_relu', 'result') ] graph_ref = build_graph_with_edge_attrs(ref_nodes, ref_edges) LeakyReLUFusion().find_and_replace_pattern(graph) graph.clean_up() (flag, resp) = compare_graphs(graph, graph_ref, 'result') self.assertTrue(flag, resp) (flag, resp) = compare_graphs(graph, graph_ref, 'result_2') self.assertTrue(flag, resp)
def setUp(self): nodes = { **regular_op_with_shaped_data('data', [20, 100, 4], {'type': 'Parameter', 'value': None, '_out_port_data_type': {0: np.float32}}), **valued_const_with_data('k', int64_array(10)), **regular_op_with_shaped_data('topk', None, {'op': 'TopK', 'type': 'TopK', 'name': 'topk', 'axis': 1}), 'topk_d2': {'kind': 'data', 'shape': None, 'value': None}, **result('output_1'), **result('output_2'), } self.graph = build_graph(nodes, [ *connect('data', '0:topk'), *connect('k', '1:topk'), ('topk', 'topk_d', {'out': 0}), ('topk', 'topk_d2', {'out': 1}), ('topk_d', 'output_1'), ('topk_d2', 'output_2'), ], nodes_with_edges_only=True)
def test_v10_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])), **const_with_data('dim', int64_array([3, 8, 1, 7, 7])), **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) 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_all_dynamic_inputs(self): nodes = { **regular_op_with_shaped_data('placeholder', [1, 3, 20, 20], {'type': 'Parameter'}), **regular_op_with_shaped_data('min', [1, 3, 20, 20], {'type': 'Parameter'}), **regular_op_with_shaped_data('max', [1, 3, 20, 20], {'type': 'Parameter'}), **regular_op_with_shaped_data('a_clamp', [1, 3, 20, 20], {'type': None, 'op': 'Clamp'}), **regular_op_with_shaped_data('maximum', [1, 3, 20, 20], {'type': 'Maximum', 'op': 'Maximum'}), **regular_op_with_shaped_data('minimum', [1, 3, 20, 20], {'type': 'Minimum', 'op': 'Minimum'}), **result('result'), } edges = [*connect('placeholder', '0:a_clamp'), *connect('min', '1:a_clamp'), *connect('max', '2:a_clamp'), *connect('a_clamp', 'result'), ] graph = build_graph(nodes, edges) ClampNormalizer().find_and_replace_pattern(graph) ref_graph = build_graph(nodes, [*connect('placeholder', '0:maximum'), *connect('min', '1:maximum'), *connect('maximum', '0:minimum'), *connect('max', '1:minimum'), *connect('minimum', 'result') ]) (flag, resp) = compare_graphs(graph, ref_graph, 'result') self.assertTrue(flag, resp)
def test_assertion_error(self): nodes = { **shaped_const_with_data('input_0', [0]), **shaped_const_with_data('input_1', [0]), **shaped_const_with_data('input_2', [0]), **shaped_const_with_data('input_3', [0]), **regular_op_with_shaped_data('concat', [0], {'type': 'Concat'}), **result(), } edges = [ *connect('input_0', '0:concat'), *connect('input_1', '1:concat'), *connect('input_2', '2:concat'), *connect('input_3', '3:concat'), *connect('concat', 'output'), ] graph = build_graph(nodes, edges, nodes_with_edges_only=True) self.assertRaises(AssertionError, ConcatOdInputEraser().find_and_replace_pattern, graph)
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. """ import unittest import numpy as np from extensions.ops.cumsum import CumSum from mo.front.common.partial_infer.utils import int64_array from mo.graph.graph import Node from mo.utils.unittest.graph import build_graph, valued_const_with_data, regular_op_with_shaped_data, result, connect nodes_attributes = { **regular_op_with_shaped_data('data', [1, 3, 224, 224], {'type': 'Parameter', 'value': None, '_out_port_data_type': {0: np.float32}}), **valued_const_with_data('axis', int64_array(0)), **regular_op_with_shaped_data('cumsum', None, {'op': 'CumSum', 'type': 'CumSum', 'name': 'cumsum'}), **regular_op_with_shaped_data('identity', None, {'op': 'Identity', 'name': 'identity'}), **result('output'), } class TestCumSum(unittest.TestCase): def test_cumsum_axis(self): graph = build_graph(nodes_attributes, [*connect('data', '0:cumsum'), *connect('axis', '1:cumsum'), *connect('cumsum', '0:identity'), ('identity', 'identity_d', {'out': 0}), ('identity_d', 'output'),
# Copyright (C) 2018-2021 Intel Corporation # SPDX-License-Identifier: Apache-2.0 import unittest import numpy as np from extensions.front.div import Div from mo.utils.ir_engine.compare_graphs import compare_graphs from mo.utils.unittest.graph import build_graph, result, regular_op_with_shaped_data, valued_const_with_data, connect, \ connect_data nodes = { **regular_op_with_shaped_data('placeholder_1', [1, 227, 227, 3], { 'type': 'Parameter' }), **regular_op_with_shaped_data('placeholder_2', [1, 227, 227, 3], { 'type': 'Parameter' }), **regular_op_with_shaped_data('div', None, { 'op': 'Div', 'type': 'Divide', 'name': 'my_div' }), **regular_op_with_shaped_data('reciprocal', [1, 227, 227, 3], { 'type': 'Power' }), **valued_const_with_data('minus_one', np.array(-1.)), **regular_op_with_shaped_data('mul', None, {'type': 'Multiply'}), **result(), }
from argparse import Namespace import numpy as np from extensions.middle.AddMeanScaleValues import AddMeanScaleValues from extensions.middle.ScaleInput import ScaleInput from mo.graph.graph import Graph, Node from mo.utils.cli_parser import get_mean_scale_dictionary, parse_tuple_pairs from mo.utils.ir_engine.compare_graphs import compare_graphs from mo.utils.unittest.graph import build_graph, regular_op_with_shaped_data, result, connect, connect_data, \ valued_const_with_data nodes = { **regular_op_with_shaped_data('parameter', [1, 3, 227, 227], { 'type': 'Parameter', 'op': 'Parameter', 'shape': [1, 3, 227, 227] }), **regular_op_with_shaped_data('parameter_2', [1, 3, 227, 227], { 'type': 'Parameter', 'op': 'Parameter', 'shape': [1, 3, 227, 227] }), **regular_op_with_shaped_data('mul_scale', [1, 3, 227, 227], { 'type': 'Multiply', 'op': 'Mul' }), **regular_op_with_shaped_data('add_mean', [1, 3, 227, 227], { 'type': 'Add', 'op': 'Add' }),
See the License for the specific language governing permissions and limitations under the License. """ 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 mo.utils.unittest.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',
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
See the License for the specific language governing permissions and limitations under the License. """ import unittest import numpy as np from extensions.front.sub import Sub from mo.utils.ir_engine.compare_graphs import compare_graphs from mo.utils.unittest.graph import build_graph, result, regular_op_with_shaped_data, valued_const_with_data, connect, \ connect_data nodes = { **regular_op_with_shaped_data('placeholder_1', [1, 227, 227, 3], { 'type': 'Parameter' }), **regular_op_with_shaped_data('placeholder_2', [1, 227, 227, 3], { 'type': 'Parameter' }), **regular_op_with_shaped_data('sub', None, { 'op': 'Sub', 'type': 'Subtract', 'name': 'my_sub' }), **regular_op_with_shaped_data('negate', [1, 227, 227, 3], { 'type': 'Multiply' }), **valued_const_with_data('minus_one', np.array(-1.)), **regular_op_with_shaped_data('add', None, {'type': 'Add'}), **result(),
def graph_template(weights_initial_shape, new_reshape_shape, limits_initial_shape, limits_new_shape=None): limits_new_shape = limits_initial_shape if limits_new_shape is None else limits_new_shape core_connections = [ *connect('input:0', '0:convolution'), *connect('convolution:0', '0:output'), ] core_nodes = lambda weights_shape, limit_shape, reshape_shape: { **regular_op_with_shaped_data('input', None, { 'type': 'Parameter', 'op': 'Parameter' }), **valued_const_with_data('weights', np.ones(weights_shape)), **const_with_data('dim', int64_array(reshape_shape)), **regular_op_with_shaped_data('reshape', reshape_shape, { 'type': 'Reshape', 'infer': Reshape.infer, 'op': 'Reshape' }), **valued_const_with_data('il', np.ones(limit_shape)), **valued_const_with_data('ih', np.ones(limit_shape)), **valued_const_with_data('ol', np.ones(limit_shape)), **valued_const_with_data('oh', np.ones(limit_shape)), **regular_op_with_shaped_data( 'FQ', weights_shape, { 'type': 'FakeQuantize', 'infer': FakeQuantize.infer, 'stop_value_propagation': True, 'levels': 2, 'op': 'FakeQuantize' }), **regular_op_with_shaped_data('convolution', None, { 'type': 'Convolution', 'op': 'Convolution' }), **result(), } nodes_before = core_nodes(weights_initial_shape, limits_initial_shape, new_reshape_shape) edges_before = [ *connect('weights:0', '0:FQ'), *connect('il:0', '1:FQ'), *connect('ih:0', '2:FQ'), *connect('ol:0', '3:FQ'), *connect('oh:0', '4:FQ'), *connect('FQ:0', '0:reshape'), *connect('dim:0', '1:reshape'), *connect('reshape:0', '1:convolution'), *core_connections, ] graph = build_graph(nodes_attrs=nodes_before, edges=edges_before, nodes_with_edges_only=True) nodes_after = core_nodes(new_reshape_shape, limits_new_shape, []) edges_after = [ *connect('weights:0', '0:FQ'), *connect('il:0', '1:FQ'), *connect('ih:0', '2:FQ'), *connect('ol:0', '3:FQ'), *connect('oh:0', '4:FQ'), *connect('FQ:0', '1:convolution'), *core_connections, ] graph_ref = build_graph(nodes_attrs=nodes_after, edges=edges_after, nodes_with_edges_only=True) return graph, graph_ref
# Copyright (C) 2018-2021 Intel Corporation # SPDX-License-Identifier: Apache-2.0 import unittest from argparse import Namespace import numpy as np from extensions.back.InterpolateReshape import InterpolateReshapeWA, InterpolateConcat from mo.utils.ir_engine.compare_graphs import compare_graphs from mo.utils.unittest.graph import build_graph, result, regular_op_with_shaped_data, valued_const_with_data, connect, \ connect_data nodes = { **regular_op_with_shaped_data('placeholder', [1, 3, 30, 40], {'type': 'Parameter', 'op': 'Parameter'}), **valued_const_with_data('out_shape', np.array([60, 160])), **regular_op_with_shaped_data('interpolate', [1, 3, 60, 160], {'type': 'Interpolate', 'axes': [2, 3], 'op': 'Interpolate', 'version': 'opset1'}), **regular_op_with_shaped_data('shape', [4], {'type': 'ShapeOf', 'op': 'ShapeOf'}), **valued_const_with_data('indices', np.array([2, 3])), **valued_const_with_data('axis', np.array(0)), **regular_op_with_shaped_data('gather', [2], {'type': 'Gather', 'op': 'Gather'}), **valued_const_with_data('multiplier', np.array([2, 4])), **regular_op_with_shaped_data('mul', [2], {'type': 'Multiply', 'op': 'Mul'}), **regular_op_with_shaped_data('placeholder_1', [1, 3, 60, 160], {'type': 'Parameter', 'op': 'Parameter'}), **regular_op_with_shaped_data('concat', [1, 7, 60, 160], {'type': 'Concat', 'axis': 1, 'op': 'Concat'}),
# SPDX-License-Identifier: Apache-2.0 import unittest from extensions.middle.LeakyReluPattern import LeakyReLUFusion from mo.front.common.partial_infer.utils import float_array, int64_array from mo.graph.graph import Node from mo.ops.result import Result from mo.utils.ir_engine.compare_graphs import compare_graphs from mo.utils.unittest.graph import build_graph, result, build_graph_with_edge_attrs, connect, \ regular_op_with_shaped_data, valued_const_with_data, connect_data shape = int64_array([1, 3, 5, 2]) nodes = { **regular_op_with_shaped_data('input', shape, { 'type': 'Parameter', 'op': 'Parameter' }), **regular_op_with_shaped_data('mul', shape, { 'type': 'Multiply', 'name': 'mul' }), **regular_op_with_shaped_data('max', shape, { 'type': 'Maximum', 'name': 'final_max' }), **valued_const_with_data('const', float_array([0.5])), **result('result') } edges = [ *connect('input:0', '0:mul'),
import unittest import numpy as np from generator import generate, generator from extensions.ops.ReduceOps import reduce_infer from mo.front.common.partial_infer.utils import int64_array from mo.graph.graph import Node from mo.utils.unittest.graph import build_graph, regular_op_with_shaped_data, result, connect, valued_const_with_data nodes_attributes = { **regular_op_with_shaped_data('data', [1, 3, 224, 224], { 'type': 'Parameter', 'value': None, '_out_port_data_type': { 0: np.float32 } }), **valued_const_with_data('axis', int64_array(0)), **regular_op_with_shaped_data('reduce_lp', None, { 'op': 'ReduceLp', 'type': None, 'name': 'my_reduce_lp' }), **regular_op_with_shaped_data('identity', None, { 'op': 'Identity', 'name': 'identity' }), **result('output'), }
limitations under the License. """ import unittest import numpy as np from extensions.middle.SliceConverter import ConvertSlice from mo.front.common.partial_infer.utils import int64_array from mo.utils.ir_engine.compare_graphs import compare_graphs from mo.utils.unittest.graph import build_graph, regular_op_with_shaped_data, valued_const_with_data, \ regular_op_with_empty_data, result, connect, connect_data nodes_attributes = { **regular_op_with_shaped_data('input', [2, 3, 300, 300], { 'type': 'Parameter', 'op': 'Parameter' }), **regular_op_with_empty_data('starts', { 'op': 'Const', 'type': 'Const' }), **regular_op_with_empty_data('ends', { 'op': 'Const', 'type': 'Const' }), **regular_op_with_empty_data('axes', { 'op': 'Const', 'type': 'Const' }), **regular_op_with_empty_data('steps', { 'op': 'Const',