class HSigmoidWithReluMulTest(unittest.TestCase): nodes = { **regular_op('input', {'type': 'Parameter'}), **regular_op('add', {'op': 'Add'}), **regular_op('relu', {'op': 'ReLU'}), **regular_op('min', {'op': 'Minimum'}), **regular_op('mul', { 'op': 'Mul', 'name': 'final_mul' }), **const('add_const', float_array([3.0])), **const('min_const', float_array([6.0])), **const('mul_const', float_array([1.0 / 6.0])), **result('result'), } edges = [('input', 'add', { 'in': 0, 'out': 0 }), ('add_const', 'add', { 'in': 1, 'out': 0 }), ('add', 'relu', { 'in': 0, 'out': 0 }), ('relu', 'min', { 'in': 0, 'out': 0 }), ('min_const', 'min', { 'in': 1, 'out': 0 }), ('min', 'mul', { 'in': 0, 'out': 0 }), ('mul_const', 'mul', { 'in': 1, 'out': 0 }), ('mul', 'result', { 'in': 0, 'out': 0 })] def test_hsigmoid_with_relu_mul(self): graph = build_graph_with_edge_attrs(self.nodes, self.edges, {}) graph_ref = build_graph(ref_nodes, ref_edges) graph.stage = 'front' HSigmoidWithReluMul().find_and_replace_pattern(graph) (flag, resp) = compare_graphs(graph, graph_ref, 'result') self.assertTrue(flag, resp) self.assertTrue( len(graph.get_op_nodes(name='final_mul')) == 1 and graph.get_op_nodes(name='final_mul')[0].op == 'HSigmoid') self.assertTrue( graph.get_op_nodes( name='final_mul')[0].out_nodes()[0].node == 'result') def test_hsigmoid_with_relu_mul_wrong_constant(self): graph = build_graph_with_edge_attrs( self.nodes, self.edges, {'add_const': { 'value': float_array([0.00001]) }}) graph_ref = graph.copy() graph.stage = 'front' HSigmoidWithReluMul().find_and_replace_pattern(graph) (flag, resp) = compare_graphs(graph, graph_ref, 'result') self.assertTrue(flag, resp) def test_hsigmoid_with_relu_mul_different_tensors(self): graph = build_graph_with_edge_attrs( { **regular_op('input', {'type': 'Parameter'}), **regular_op('input_2', {'type': 'Parameter'}), **regular_op('add', {'op': 'Add'}), **regular_op('max', {'op': 'Maximum'}), **regular_op('min', {'op': 'Minimum'}), **regular_op('mul', {'op': 'Mul'}), **regular_op('mul_2', { 'op': 'Mul', 'name': 'final_mul' }), **const('const_0', float_array([0.0])), **const('const_3', float_array([3.0])), **const('const_6', float_array([6.0])), **const('const_1_6', float_array([1.0 / 6.0])), **result('result'), }, [('input_2', 'mul', { 'in': 1, 'out': 0 }), ('input', 'add', { 'in': 0, 'out': 0 }), ('const_3', 'add', { 'in': 1, 'out': 0 }), ('add', 'max', { 'in': 0, 'out': 0 }), ('const_0', 'max', { 'in': 1, 'out': 0 }), ('max', 'min', { 'in': 0, 'out': 0 }), ('const_6', 'min', { 'in': 1, 'out': 0 }), ('min', 'mul', { 'in': 0, 'out': 0 }), ('mul', 'mul_2', { 'in': 0, 'out': 0 }), ('const_1_6', 'mul_2', { 'in': 1, 'out': 0 }), ('mul_2', 'result', { 'in': 0, 'out': 0 })]) graph_ref = graph.copy() graph.stage = 'front' HSigmoidWithReluMul().find_and_replace_pattern(graph) (flag, resp) = compare_graphs(graph, graph_ref, 'result') self.assertTrue(flag, resp)
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(nodes_attrs={ **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'}), **regular_op_with_empty_data('shapeof', {'type': 'ShapeOf'}), **regular_op_with_empty_data('select', {'type': 'Select'}), **regular_op_with_empty_data('gather', {'type': 'Gather'}), 'gather_const': {'type': 'Gather', 'kind': 'op', 'op': 'Gather'}, 'equal': {'type': 'Equal', 'kind': 'op', 'op': 'Equal'}, # start **valued_const_with_data('start', np.array(0)), # limit **valued_const_with_data('minus_one_0', np.array(-1)), **valued_const_with_data('zero_0', np.array(0)), **valued_const_with_data('minus_one_1', np.array(-1)), **valued_const_with_data('zero_1', np.array(0)), # delta **valued_const_with_data('delta', np.array(1)), **regular_op_with_shaped_data('range', [1, 384], {'type': 'Range'}), # keep dims **valued_const_with_data('axes', np.array([0])), **regular_op_with_shaped_data('keep_shape', [1, 384], {'type': 'Unsqueeze'}), **valued_const_with_data('one', np.array(1)), **result(), }, edges=[ *connect('value', 'shapeof'), *connect('gather', '0:equal'), ('gather', 'select', {'in': 2, 'out': 0}), ('gather_const', 'select', {'in': 1}), ('equal', 'select', {'in': 0}), *connect('minus_one_0', '1:gather'), *connect('zero_0', '2:gather'), *connect('shapeof', '0:gather_const'), *connect('minus_one_1', '1:gather_const'), *connect('zero_1', '2:gather_const'), *connect('start', '0:range'), *connect('select', '1:range'), *connect('delta', '2:range'), *connect('range', '0:keep_shape'), *connect('axes', '1:keep_shape'), *connect('keep_shape', '0:bc'), *connect('one', '1:equal'), *connect('shape', '1:bc'), ('shape_d', 'gather', {'out': 0, 'in': 0}), *connect('bc', 'output'), ], update_attributes={ 'range_d': {'value': np.arange(0, 384).reshape((1, 384))}, 'keep_shape_d': {'value': np.arange(0, 384).reshape((1, 384))}, }) (flag, resp) = compare_graphs(graph, graph_ref, 'output', check_op_attrs=True) self.assertTrue(flag, resp)
'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 }), **result("OUT_1") } sub_graph_1_nodes = { **shaped_parameter("IN_2", int64_array([1, 4, 64, 54]), { 'internal_layer_id': 0 }), **valued_const_with_data("M_2", int64_array([10])), **valued_const_with_data("cond_2", int64_array([1])), **regular_op_with_empty_data( "Loop_2", { 'op': "Loop", 'type': 'Loop', 'sub_graphs': ['body'],
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]))
def test_tdnnreplacer(self, weights, biases, time_offsets): def generate_offsets(): offset_edges = [] offset_nodes = {} for i, t in enumerate(time_offsets): offset_nodes.update(**regular_op('memoryoffset_' + str(i), {'type': None})) if t != 0: offset_edges.append( ('placeholder', 'memoryoffset_' + str(i), { 'out': 0, 'in': 0 })) offset_edges.append(('memoryoffset_' + str(i), 'concat', { 'out': 0, 'in': i })) else: offset_edges.append(('placeholder', 'concat', { 'out': 0, 'in': i })) return offset_nodes, offset_edges offset_nodes, ref_offset_edges = generate_offsets() nodes = { **offset_nodes, **regular_op('placeholder', {'type': 'Parameter'}), **regular_op( 'tdnncomponent', { 'op': 'tdnncomponent', 'weights': np.array(weights), 'biases': np.array(biases), 'time_offsets': np.array(time_offsets) }), **const('weights', np.array(weights)), **const('biases', np.array(biases)), **regular_op('concat', { 'type': 'Concat', 'axis': 1 }), **regular_op('memoryoffset_0', {'type': None}), **regular_op('memoryoffset_1', {'type': None}), **regular_op('memoryoffset_2', {'type': None}), **regular_op('fully_connected', {'type': 'FullyConnected'}), **result('result'), } graph = build_graph(nodes, [ *connect_front('placeholder', 'tdnncomponent'), *connect_front('tdnncomponent', 'result') ], nodes_with_edges_only=True) graph.stage = 'front' ref_graph = build_graph(nodes, [ *ref_offset_edges, *connect_front('concat', '0:fully_connected'), *connect_front('weights', '1:fully_connected'), *connect_front('biases', '2:fully_connected'), *connect_front('fully_connected', 'result') ], nodes_with_edges_only=True) TdnnComponentReplacer().find_and_replace_pattern(graph) (flag, resp) = compare_graphs(graph, ref_graph, 'result', check_op_attrs=True) self.assertTrue(flag, resp)
class GeLUMergerErfTest(unittest.TestCase): nodes = { **regular_op('input', { 'op': 'Parameter', 'type': 'Parameter' }), **regular_op('mul', {'op': 'Mul'}), **regular_op('mul0', { 'op': 'Mul', 'name': 'final_mul' }), **regular_op('div', {'op': 'Div'}), **regular_op('erf', {'op': 'Erf'}), **regular_op('add', {'op': 'Add'}), **const('mul_param', float_array([0.5])), **const('div_param', float_array([sqrt(2.)])), **const('add_param', int64_array([1])), **result('result'), } def test_gelu_p1(self): edges = [('input', 'mul'), ('mul', 'mul0'), ('input', 'div'), ('div', 'erf'), ('erf', 'add'), ('add', 'mul0'), ('mul_param', 'mul'), ('div_param', 'div'), ('add_param', 'add'), ('mul0', 'result')] graph = build_graph(self.nodes, edges) graph_ref = build_graph(ref_nodes, ref_edges) graph.stage = 'front' GeLUMergerErf().find_and_replace_pattern(graph) graph.clean_up() (flag, resp) = compare_graphs(graph, graph_ref, 'result') self.assertTrue(flag, resp) self.assertTrue( graph.get_op_nodes(op='Gelu')[0].approximation_mode == 'erf') self.assertTrue( len(graph.get_op_nodes(name='final_mul')) == 1 and graph.get_op_nodes(name='final_mul')[0].op == 'Gelu') def test_gelu_p2(self): edges = [('input', 'mul'), ('div', 'erf'), ('erf', 'add'), ('add', 'mul'), ('mul', 'mul0'), ('mul_param', 'mul0'), ('div_param', 'div'), ('add_param', 'add'), ('mul0', 'result')] graph = build_graph(self.nodes, edges) graph_ref = build_graph(ref_nodes, ref_edges) graph.stage = 'front' GeLUMergerErf().find_and_replace_pattern(graph) graph.clean_up() (flag, resp) = compare_graphs(graph, graph_ref, 'result') self.assertTrue(flag, resp) self.assertTrue( graph.get_op_nodes(op='Gelu')[0].approximation_mode == 'erf') self.assertTrue( len(graph.get_op_nodes(name='final_mul')) == 1 and graph.get_op_nodes(name='final_mul')[0].op == 'Gelu') def test_gelu_p3(self): edges = [('input', 'mul'), ('div', 'erf'), ('erf', 'add'), ('add', 'mul'), ('mul', 'mul0'), ('mul_param', 'mul'), ('div_param', 'div'), ('add_param', 'add'), ('mul0', 'result')] graph = build_graph(self.nodes, edges) graph_ref = build_graph(ref_nodes, ref_edges) graph.stage = 'front' GeLUMergerErf().find_and_replace_pattern(graph) graph.clean_up() (flag, resp) = compare_graphs(graph, graph_ref, 'result') self.assertTrue(flag, resp) self.assertTrue( graph.get_op_nodes(op='Gelu')[0].approximation_mode == 'erf') self.assertTrue( len(graph.get_op_nodes(name='final_mul')) == 1 and graph.get_op_nodes(name='final_mul')[0].op == 'Gelu')
# Copyright (C) 2018-2022 Intel Corporation # SPDX-License-Identifier: Apache-2.0 import unittest import numpy as np from openvino.tools.mo.front.caffe.MVNCaffeToMVN import MVNCaffeToMVN 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('mvn_caffe', {'op': 'MVNCaffe'}), **result(), # nodes after replacement **const('start_1', np.array(1)), **const('start_2', np.array(2)), **const('step', np.array(1)), **regular_op_with_empty_data('rank', { 'op': 'Rank', 'type': None }), **regular_op_with_empty_data('range', { 'op': 'Range', 'type': None }), **regular_op_with_empty_data('mvn', { 'op': 'MVN', 'type': None
from mo.graph.graph import Node from mo.utils.ir_engine.compare_graphs import compare_graphs from unit_tests.utils.graph import build_graph, regular_op, result nodes = { **regular_op('Op1', { 'type': 'Op1', 'kind': 'op', 'op': 'Op1' }), **regular_op('Op2', { 'type': 'Op2', 'kind': 'op', 'op': 'Op2' }), **result('result1'), **result('result2'), 'Op1_data': { 'kind': 'data', 'fw_tensor_debug_info': [('Op1', 0, 'Op1_tensor')] }, 'Op2_data': { 'kind': 'data', 'fw_tensor_debug_info': [('Op1', 0, 'Op2_tensor')] }, } class ResultRenameTest(unittest.TestCase): def test_case1(self): graph = build_graph(nodes, [('Op1', 'Op1_data'),
'type': 'Gather', 'op': 'Gather' }), **regular_op_with_empty_data('inverse_broadcast', { 'type': 'Broadcast', 'op': 'Broadcast' }), **regular_op_with_shaped_data( 'inverse_reverse', [1, 2, 34, 56], { 'type': 'ReverseSequence', 'op': 'ReverseSequence', 'seq_axis': 2, 'batch_axis': 0 }), **regular_op_with_empty_data('some_op', {'op': 'SomeOp'}), **result() } ref_nodes = { **regular_op_with_shaped_data('parameter', [1, 3, 227, 227], { 'type': 'Parameter', 'op': 'Parameter', 'shape': [1, 3, 227, 227] }), **regular_op_with_empty_data('init_hidden', { 'type': 'Init', 'op': 'Init' }), **regular_op_with_empty_data( 'ti', { 'type':
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'), *connect('identityN:0', 'output0'), ('identityN', 'identityN_1_d', {'out': 1}), ('identityN_1_d', 'output1', {'out': 1}), ], nodes_with_edges_only=True) IdentityN_to_Identity().find_and_replace_pattern(graph)
def test_accuracy(self, data, in_low, in_high, out_low, out_high, levels): nodes = nodes_dict(np.float32, None, levels, data, in_low, in_high, out_low, out_high) graph = build_graph(nodes, [ *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', 'output'), ], nodes_with_edges_only=True) graph_ref = graph.copy() CompressQuantizeWeights().find_and_replace_pattern(graph) for node in graph.get_op_nodes() + graph_ref.get_op_nodes(): node['stop_value_propagation'] = False node['need_shape_inference'] = node.soft_get('need_shape_inference', True) graph.clean_up() graph_ref.clean_up() const_result_graph = build_graph({**shaped_const_with_data('weights', np.array(data).shape), **result()}, [*connect('weights', 'output')], nodes_with_edges_only=True) (flag, resp) = compare_graphs(graph, const_result_graph, 'output', check_op_attrs=True) self.assertTrue(flag, resp) (flag, resp) = compare_graphs(graph_ref, const_result_graph, 'output', check_op_attrs=True) self.assertTrue(flag, resp) # as this two graphs calculated the same data through different constant folding functions, they resulted in # constants of different data type since FakeQuantize always have f32 output dtype, but eltwises use numpy # for folding which doesn't have such restriction const_node = graph.get_op_nodes(type='Const') self.assertEqual(len(const_node), 1) if const_node[0].data_type == np.float64: const_node[0].data_type = np.float32 (flag, resp) = compare_graphs(graph, graph_ref, 'output', check_op_attrs=True) self.assertTrue(flag, resp)
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)), **valued_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
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))
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]))
}), **regular_op_with_shaped_data('identity_10', [1, 3, 60, 160], { 'identity': True, 'op': 'Identity' }), **regular_op_with_shaped_data('identity_11', [1, 3, 60, 160], { 'identity': True, 'op': 'Identity' }), **regular_op_with_shaped_data('concat', [1, 7, 60, 160], { 'type': 'Concat', 'axis': 1, 'op': 'Concat' }), **valued_const_with_data('N', np.array([1])), **result('output'), **result('output_1'), } @generator class TestInterpolateConcat(unittest.TestCase): def test_interpolate_concat_reshape_graph_comparison(self): graph = build_graph(nodes, [ *connect('placeholder', '0:interpolate'), *connect('out_shape', '1:interpolate'), *connect('interpolate', '0:concat'), *connect('placeholder_1', '1:concat'), *connect('concat', 'output'), ], nodes_with_edges_only=True)
from mo.graph.graph import Node, Graph from mo.utils.ir_engine.compare_graphs import compare_graphs from mo.utils.runtime_info import OldAPIMapOrder, RTInfo from unit_tests.utils.graph import build_graph, result, connect, regular_op_with_shaped_data, valued_const_with_data nodes = { **regular_op_with_shaped_data('placeholder1', [1, 3, 10, 10], {'type': 'Parameter', 'rt_info': RTInfo()}), **regular_op_with_shaped_data('placeholder2', [1, 1, 1, 1], {'type': 'Parameter'}), **regular_op_with_shaped_data('mul', [1, 3, 10, 10], {'type': 'Multiply'}), **regular_op_with_shaped_data('reverse_channels', [1, 3, 10, 10], {'type': 'ReverseChannels', 'axis': 1}), **regular_op_with_shaped_data('pad', [1, 3, 10, 10], {'type': 'Pad'}), **result('result'), } nodes2 = { **regular_op_with_shaped_data('placeholder', [1, 3, 10, 10], {'type': 'Parameter'}), **valued_const_with_data('mul_const', float32_array([-127.5, -127.5, -127.5])), **regular_op_with_shaped_data('mul', [1, 3, 10, 10], {'type': 'Multiply'}), **valued_const_with_data('pad_const_1', int64_array([0, 0, 0, 0])), **valued_const_with_data('pad_const_2', int64_array([0, 0, 1, 1])), **regular_op_with_shaped_data('pad', [1, 3, 10, 10], {'type': 'Pad'}), **regular_op_with_shaped_data('reverse_channels', [1, 3, 10, 10], {'type': 'ReverseChannels', 'axis': 1}), **result('result'), **result('result2'), }
import unittest from math import sqrt from openvino.tools.mo.front.GeLUMerger_Erf import GeLUMergerErf from openvino.tools.mo.front.common.partial_infer.utils import float_array, int64_array from openvino.tools.mo.utils.ir_engine.compare_graphs import compare_graphs from unit_tests.utils.graph import const, regular_op, result, build_graph ref_nodes = { **regular_op('input', {'type': 'Parameter'}), **regular_op('gelu', { 'type': 'Gelu', 'approximation_mode': 'erf', 'name': 'final_mul' }), **result('result') } ref_edges = [('input', 'gelu'), ('gelu', 'result')] class GeLUMergerErfTest(unittest.TestCase): nodes = { **regular_op('input', { 'op': 'Parameter', 'type': 'Parameter' }), **regular_op('mul', {'op': 'Mul'}), **regular_op('mul0', { 'op': 'Mul', 'name': 'final_mul' }),
}), **regular_op_with_shaped_data('add_mean', [1, 3, 227, 227], { 'type': 'Add', 'op': 'Add' }), **valued_const_with_data( 'scale', np.array([1. / 1., 1. / 2., 1. / 3.]).reshape((1, 3, 1, 1))), **valued_const_with_data('mean', np.array([-1., -2., -3.]).reshape((1, 3, 1, 1))), **regular_op_with_shaped_data('shape_of', [4], { 'type': 'ShapeOf', 'op': 'ShapeOf' }), **regular_op_with_shaped_data('op', [1, 3, 227, 227], {}), **result('result'), **result('result_2'), } class AddMeanScaleValuesTest(unittest.TestCase): def check_graph_attrs(self, graph: Graph, graph_ref: Graph, parameter_node_names: list): for node in graph.get_op_nodes(): if node.soft_get('name') in parameter_node_names: self.assertTrue(node.soft_get('type') == 'Parameter') out_node = node.out_node(0) out_node_ref = Node(graph_ref, node.id).out_node(0) self.assertTrue(out_node['fw_tensor_debug_info'] == out_node_ref['fw_tensor_debug_info']) else:
'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'), } @generator class ReduceLpTest(unittest.TestCase): @generate(*[ ([3, 2, 2], [0], True, 1), ([3, 2, 2], [0], True, 2), ([3, 2, 2], [1], True, 2), ([3, 2, 2], [2], True, 2), ([3, 2, 2], [0], False, 1), ([3, 2, 2], [0], False, 2), ([3, 2, 2], [1], False, 2), ([3, 2, 2], [2], False, 2), ])
def test_set_value_and_shape_with_force_shape_attribute_in_data(self): import numpy as np graph = build_graph({**valued_const_with_data('const', np.array([1, 2, 3])), **result()}, [*connect('const', 'output')]) node = Node(graph, 'const') Node(graph, 'const_d')['force_shape'] = np.array([2, 5, 7], dtype=np.int64) node.out_port(0).data.set_value(np.zeros(30)) self.assertTrue(np.array_equal(node.out_port(0).data.get_shape(), np.array([2, 5, 7], dtype=np.int64)), "node.out_port(0).data.get_shape()={} != [2, 5, 7]".format( node.out_port(0).data.get_shape()))
def test_1(self): graph = build_graph(nodes_attrs={ **shaped_parameter('input', int64_array([1, 3, 15, 15])), **regular_op_with_empty_data('div_sqrt_dim', { 'op': '_contrib_div_sqrt_dim' }), **result('result') }, edges=[ *connect('input', 'div_sqrt_dim'), *connect('div_sqrt_dim', 'result') ]) ref_graph = build_graph( nodes_attrs={ **shaped_parameter('input', int64_array([1, 3, 15, 15])), **regular_op_with_empty_data('div_sqrt_shape_of', { 'op': 'ShapeOf', 'type': 'ShapeOf' }), **shaped_const_with_data('gather_axis', None), **shaped_const_with_data('gather_indices', None), **regular_op_with_empty_data('gather', { 'op': 'Gather', 'type': 'Gather' }), **regular_op_with_empty_data('power', { 'op': 'AttributedPower', 'power': 0.5, 'type': 'Power' }), **regular_op_with_empty_data('cast', { 'op': 'Cast', 'type': 'Convert', 'dst_type': np.float32 }), **regular_op_with_empty_data('z_convert_like', { 'op': 'ConvertLike', 'type': 'ConvertLike' }), **regular_op_with_empty_data('div', { 'op': 'Div', 'type': 'Divide' }), **result('result') }, edges=[ *connect('input', '0:div'), *connect_data('input', 'div_sqrt_shape_of'), *connect('div_sqrt_shape_of', '0:gather'), *connect('gather_axis', '1:gather'), *connect('gather_indices', '2:gather'), *connect('gather', 'cast'), *connect('cast', 'power'), *connect('power', '0:z_convert_like'), *connect_front('input_d', '1:z_convert_like'), *connect('z_convert_like', '1:div'), *connect('div', 'result') ], ) DivSqrtDim().find_and_replace_pattern(graph) flag, resp = compare_graphs(graph, ref_graph, 'result', 'result', check_op_attrs=True) self.assertTrue(flag, resp)
def test_per_sample_weights(self): nodes = { **const('weights_inp', np.random.randn(100, 2)), **regular_op('indices_inp', {'type': 'Parameter'}), **regular_op('offsets_inp', {'type': 'Parameter'}), **regular_op('per_sample_weights', {'type': 'Parameter'}), **regular_op('aten', {'type': None, 'kind': 'op', 'op': 'ATen', 'operator': 'embedding_bag', 'mode': 0, 'name': 'my_aten'}), **regular_op('emb_bag', {'type': 'EmbeddingBagOffsetsSum', 'kind': 'op', 'op': 'EmbeddingBagOffsetsSum'}), **regular_op('WeightsRank', {'type': None, 'kind': 'op', 'op': 'Rank'}), **regular_op('WeightsRank/axis', {'type': 'Add', 'kind': 'op', 'op': 'Add'}), **regular_op('gather1', {'type': 'Gather', 'kind': 'op', 'op': 'Gather'}), **regular_op('gather2', {'type': 'Gather', 'kind': 'op', 'op': 'Gather'}), **regular_op('WeightsShape', {'type': 'ShapeOf', 'kind': 'op', 'op': 'ShapeOf'}), **regular_op('Broadcast', {'type': 'Broadcast', 'kind': 'op', 'op': 'Broadcast'}), **regular_op('Unsqueeze', {'type': 'Unsqueeze', 'kind': 'op', 'op': 'Unsqueeze'}), **const('WeightsShape/Axis', int64_array(0)), **const('zero1', int64_array(0)), **const('zero2', int64_array(0)), **const('Unsqueeze/value', int64_array(0)), **const('Broadcast/value', int64_array(0)), **const('neg', int64_array(-1)), **regular_op('Concat', {'type': 'Concat', 'kind': 'op', 'op': 'Concat'}), **result('result'), } edges = [('weights_inp', 'aten'), ('indices_inp', 'aten'), ('offsets_inp', 'aten'), ('per_sample_weights', 'aten'), ('aten', 'result'), ] graph = build_graph(nodes, edges, nodes_with_edges_only=True) graph.graph['layout'] = 'NCHW' graph.stage = 'front' edges_ref = [('weights_inp', 'Concat', {'in': 0, 'out': 0}), ('weights_inp', 'WeightsShape', {'in': 0, 'out': 0}), ('weights_inp', 'WeightsRank', {'in': 0, 'out': 0}), ('WeightsRank', 'WeightsRank/axis'), ('neg', 'WeightsRank/axis'), ('WeightsShape', 'gather1', {'in': 0, 'out': 0}), ('WeightsRank/axis', 'gather1'), ('WeightsShape/Axis', 'gather1'), ('WeightsShape', 'gather2', {'in': 0, 'out': 0}), ('zero1', 'gather2'), ('zero2', 'gather2'), ('Broadcast/value', 'Broadcast'), ('gather1', 'Broadcast'), ('Broadcast', 'Unsqueeze'), ('Unsqueeze/value', 'Unsqueeze'), ('Unsqueeze', 'Concat'), ('Concat', 'emb_bag'), ('indices_inp', 'emb_bag'), ('offsets_inp', 'emb_bag'), ('gather2', 'emb_bag'), ('per_sample_weights', 'emb_bag'), ('emb_bag', 'result'), ] graph_ref = build_graph(nodes, edges_ref, nodes_with_edges_only=True) AtenToEmbeddingBag().find_and_replace_pattern(graph) (flag, resp) = compare_graphs(graph, graph_ref, 'result') self.assertTrue(flag, resp)