def test_out_port_no_data(self): graph = build_graph_with_attrs(nodes_with_attrs=self.nodes_out, edges_with_attrs=self.edges_out) new_input_shape = np.array([1, 2, 3, 4]) graph_ref = build_graph_with_attrs(nodes_with_attrs=self.nodes_out, edges_with_attrs=self.edges_out[1:], new_nodes_with_attrs=[ ('input_node', { 'kind': 'op', 'op': 'Parameter', 'shape': new_input_shape }) ], new_edges_with_attrs=[ ('input_node', 'future_input', { 'in': 0, 'out': 0 }) ]) add_input_op(graph, 'op_node', 1, data=False, shape=new_input_shape, is_out_port=True) graph.remove_edge('op_node', 'future_input') (flag, resp) = compare_graphs(graph, graph_ref, last_node='another_node') self.assertTrue(flag, resp) (flag, resp) = compare_graphs(graph, graph_ref, last_node='future_input') self.assertTrue(flag, resp)
def test_simple_tile(self): graph = build_graph(nodes_attrs=self.nodes, edges=self.edges, update_attributes={ 'tile': { 'tile_array': [3, 1, 1] }, 'input_op_data': { 'shape': np.array([1, 4, 5]) } }) pattern = TileMultipleAxisReplacer() pattern.find_and_replace_pattern(graph) graph_ref = build_graph(nodes_attrs=self.nodes, edges=self.edges, update_attributes={ 'tile': { 'tile_array': [3, 1, 1] }, 'input_op_data': { 'shape': np.array([1, 4, 5]) } }) (flag, resp) = compare_graphs(graph, graph_ref, 'next_op', check_op_attrs=True) self.assertTrue(flag, resp)
def test_force_precision_parameter(self): precision = 'FP16' shape = np.array([2, 3, 4]) data = np.zeros(shape) graph = build_graph_with_attrs( nodes_with_attrs=self.nodes, edges_with_attrs=self.edges, update_nodes_attributes=[('data_node', {'shape': shape, 'value': data, 'force_precision': precision})] ) graph_ref = build_graph_with_attrs( nodes_with_attrs=self.nodes + self.new_nodes, edges_with_attrs=self.edges + self.new_edges, update_nodes_attributes=[('data_node', {'shape': shape, 'value': data}), ('const_data', {'shape': shape, 'value': data, 'force_precision': precision}), ('const', {'force_precision': precision})] ) tested_pattern = CreateConstNodesReplacement() tested_pattern.find_and_replace_pattern(graph) (flag, resp) = compare_graphs(graph, graph_ref, last_node='next_node') self.assertTrue(flag, resp) #check that force precision was added to data and Const nodes force_precision_const_node = graph.nodes['data_node_const']['force_precision'] force_precision_new_data = graph.nodes['data_node_copy_']['force_precision'] self.assertEqual(force_precision_const_node, precision) self.assertEqual(force_precision_new_data, precision)
def test_scale_input_1(self): graph = build_graph( nodes_attributes, [('placeholder_1', 'placeholder_1_data'), ('placeholder_1_data', 'op_output')], {'placeholder_1': { 'shape': np.array([1, 3, 224, 224]) }}, nodes_with_edges_only=True) graph_ref = build_graph(nodes_attributes, [('placeholder_1', 'mul_1_data'), ('mul_1_data', 'mul_1'), ('mul_1_w', 'mul_1'), ('mul_1', 'placeholder_1_data'), ('placeholder_1_data', 'op_output')], { 'mul_1_w': { 'shape': np.array([1, 1, 1]), 'value': np.array([1 / 255]) } }, nodes_with_edges_only=True) graph.graph['layout'] = 'NCHW' graph.graph['cmd_params'] = Namespace(scale=255) ScaleInput().find_and_replace_pattern(graph) (flag, resp) = compare_graphs(graph, graph_ref, 'placeholder_1_data') self.assertTrue(flag, resp)
def test_merge_infer_simple_case_one_executable(self): graph = build_graph_with_attrs(nodes_with_attrs=self.nodes, edges_with_attrs=self.edges) # We should propagate value of the first input since only this input is executable graph_ref = build_graph_with_attrs(nodes_with_attrs=self.nodes, edges_with_attrs=self.edges, update_nodes_attributes=[ ('merge_output', { 'shape': np.array([2, 2]), 'value': np.ones((2, 2)) }), ('merge', { 'is_not_fully_inferred': False }) ]) tested_class = Merge(graph=graph, attrs={}) node = Node(graph, 'merge') tested_class.merge_infer(node) (flag, resp) = compare_graphs(graph, graph_ref, 'merge_output', check_op_attrs=True) self.assertTrue(flag, resp)
def test_add_input_with_input_port_before_infer(self): shape = np.array([1, 2, 3, 4]) inputs = {'conv_1': [{'shape': shape, 'in': 0}]} nodes = { 'old_input': {'type': 'Identity', 'kind': 'op', 'op': 'Placeholder'}, 'conv_1': {'type': 'Convolution', 'kind': 'op', 'op': 'NotPlaceholder'}, 'relu_1': {'type': 'ReLU', 'kind': 'op', 'op': 'NotPlaceholder'}, 'output': {'type': 'SoftMax', 'kind': 'op', 'op': 'NotPlaceholder'} } edges = [ ('old_input', 'conv_1'), ('conv_1', 'relu_1'), ('relu_1', 'output') ] graph = build_graph(nodes, edges) add_input_ops(graph=graph, user_defined_inputs=inputs, before_infer=True) # Check that graph graph_ref = build_graph(nodes, edges, update_attributes={'old_input': {'shape': shape}}) (flag, resp) = compare_graphs(graph, graph_ref, last_node='output') self.assertTrue(flag, resp) # also checks that new old_input was changed new_input = list(graph.in_edges('conv_1'))[0][0] self.assertFalse(graph.node['old_input']['is_input']) self.assertTrue(graph.node[new_input]['is_input']) self.assertTrue((new_input, 'conv_1') in graph.edges()) self.assertTrue(('old_input', 'conv_1') not in graph.edges())
def test_div_test_2(self): # Test with two same inputs from one placeholder graph = build_graph(nodes_attributes, [('placeholder_1', 'Div'), ('placeholder_1', 'Div'), ('Div', 'last') ], {'placeholder_1': {'shape': np.array([1, 227, 227, 3])}, }, nodes_with_edges_only=True) graph_ref = build_graph(nodes_attributes, [('power_1', 'mul_1'), ('placeholder_1', 'mul_1'), ('placeholder_1', 'power_1'), ('mul_1', 'last'), ], {'placeholder_1': {'shape': np.array([1, 227, 227, 3])}, 'power_1': {'scale': np.array(1), 'power': np.array(-1), 'shift': np.array(0), 'type': 'Power'}, 'mul_1': {'type': 'Eltwise', 'op': 'Mul'}, }, nodes_with_edges_only=True) graph.stage = 'front' tested_class = Div() tested_class.find_and_replace_pattern(graph) (flag, resp) = compare_graphs(graph, graph_ref, 'last', check_op_attrs=True) self.assertTrue(flag, resp)
def test_move_scaleshift_to_preprocess_3(self): graph = build_graph(nodes_attributes, [('placeholder_1', 'placeholder_1_data'), ('placeholder_1_data', 'scaleshift_1'), ('scaleshift_1', 'scaleshift_1_data'), ('scaleshift_1_w', 'scaleshift_1'), ('scaleshift_1_data', 'op_output'), ('placeholder_1_data', 'op_output_1') ], {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])}, 'scaleshift_1_w': {'shape': np.array([3]), 'value': np.array((1, 2, 3))}, }) del graph['placeholder_1']['placeholder_1_data'][0]['in'] del graph['scaleshift_1']['scaleshift_1_data'][0]['in'] graph_ref = build_graph(nodes_attributes, [('placeholder_1', 'placeholder_1_data'), ('placeholder_1_data', 'scaleshift_1'), ('scaleshift_1', 'scaleshift_1_data'), ('scaleshift_1_w', 'scaleshift_1'), ('scaleshift_1_data', 'op_output'), ('placeholder_1_data', 'op_output_1') ], {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])}, 'scaleshift_1_w': {'shape': np.array([3]), 'value': np.array((1, 2, 3))}, }) move_scaleshift_to_preprocess(graph) self.assertTrue(graph.graph.get('mean_values', None) == None) (flag, resp) = compare_graphs(graph, graph_ref, 'scaleshift_1_data') self.assertTrue(flag, resp)
def test_image_scaler_test_2(self): graph = build_graph(nodes_attributes, [('placeholder_1', 'placeholder_1_data'), ('placeholder_1_data', 'im_scaler'), ('im_scaler', 'im_scaler_data'), ('im_scaler_data', 'last'), ], {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])}, 'im_scaler': {'scale': np.array(1.0), 'bias': np.reshape(np.array([1, 2, 3]), [3, 1, 1])}, }, nodes_with_edges_only=True) graph_ref = build_graph(nodes_attributes, [('placeholder_1', 'placeholder_1_data'), ('placeholder_1_data', 'add_1'), ('const_add_1_w', 'add_1_w'), ('add_1_w', 'add_1'), ('add_1', 'add_1_data'), ('add_1_data', 'last') ], {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])}, 'const_add_1_w': {'shape': np.array([3, 1, 1]), 'value': np.reshape(np.array([1, 2, 3]), [3, 1, 1])}, }, nodes_with_edges_only=True) graph.graph['layout'] = 'NCHW' graph.stage = 'middle' replacer = ImageScaler() replacer.find_and_replace_pattern(graph) (flag, resp) = compare_graphs(graph, graph_ref, 'last') self.assertTrue(flag, resp)
def test_two_nodes_with_bin(self): """Test case for data node with 2 consumers with bin edge attr. Nothing should happened.""" shape = np.array([2, 3, 4]) data = np.zeros(shape) graph = build_graph_with_attrs( nodes_with_attrs=self.nodes + [('next_node_2', { 'kind': 'op' })], edges_with_attrs=self.edges + [('data_node', 'next_node_2')], update_nodes_attributes=[('data_node', { 'shape': shape, 'value': data })], update_edge_attrs={ ('data_node', 'next_node', 0): { 'bin': 0 }, ('data_node', 'next_node_2', 0): { 'bin': 0 } }, ) tested_pattern = CreateConstNodesReplacement() tested_pattern.find_and_replace_pattern(graph) (flag, resp) = compare_graphs(graph, graph, last_node='next_node') self.assertTrue(flag, resp)
def test_two_nodes_one_bin(self): """Test case for two output nodes, one with 'bin' parameter, other without.""" shape = np.array([2, 3, 4]) data = np.zeros(shape) graph = build_graph_with_attrs( nodes_with_attrs=self.nodes + [('next_node_2', { 'kind': 'op' })], edges_with_attrs=self.edges + [('data_node', 'next_node_2')], update_nodes_attributes=[('data_node', { 'shape': shape, 'value': data })], update_edge_attrs={('data_node', 'next_node', 0): { 'bin': 0 }}, ) graph_ref = build_graph_with_attrs( nodes_with_attrs=self.nodes + self.new_nodes + [('next_node_2', { 'kind': 'op' })], edges_with_attrs=self.edges + self.new_edges + [('data_node', 'next_node_2')], update_nodes_attributes=[('data_node', { 'shape': shape, 'value': data }), ('const_data', { 'shape': shape, 'value': data })]) tested_pattern = CreateConstNodesReplacement() tested_pattern.find_and_replace_pattern(graph) (flag, resp) = compare_graphs(graph, graph_ref, last_node='next_node') self.assertTrue(flag, resp)
def test_mul_add_neg_5(self): graph = self._create_graph_with_mul_add(np.array([3]), np.array([3, 2, 1])) graph_ref = build_graph( nodes_attributes, [ ('placeholder_1', 'placeholder_1_data'), ('placeholder_1_data', 'scaleshift_1'), ('scaleshift_1_w', 'scaleshift_1'), ('scaleshift_1_b', 'scaleshift_1'), ('scaleshift_1', 'add_1_data'), ], { 'scaleshift_1_w': { 'shape': np.array([3]), 'value': np.array([3, 3, 3]) }, 'scaleshift_1_b': { 'shape': np.array([3]), 'value': np.array([3, 2, 1]) }, 'add_1_data': { 'is_output': True } }) convert_muladd_to_scaleshift_or_power(graph) graph_clean_up(graph) (flag, resp) = compare_graphs(graph, graph_ref, 'add_1_data', 'add_1_data', check_op_attrs=True) self.assertTrue(flag, resp)
def test_2(self): graph = build_graph( nodes_attributes, [('placeholder_1', 'placeholder_1_data'), ('placeholder_1_data', 'reshape_1'), ('reshape_1', 'reshape_1_data'), ('reshape_1_data', 'transpose_1'), ('transpose_1', 'transpose_1_data'), ('transpose_1_data', 'reshape_2'), ('reshape_2', 'reshape_2_data')], { 'placeholder_1_data': { 'shape': np.array([1, 112, 227, 227]) }, 'reshape_1_data': { 'shape': np.array([1, 4, 28, 227, 227]) }, 'transpose_1': { 'order': np.array([0, 2, 1, 3, 4]) }, 'transpose_1_data': { 'shape': np.array([1, 28, 4, 227, 227]) }, 'reshape_2_data': { 'shape': np.array([1, 112, 227, 227]) }, }) graph.graph['layout'] = 'NCHW' graph_ref = build_graph( nodes_attributes, [('placeholder_1', 'placeholder_1_data'), ('placeholder_1_data', 'reshape_1'), ('reshape_1', 'reshape_1_data'), ('reshape_1_data', 'transpose_1'), ('transpose_1', 'transpose_1_data'), ('transpose_1_data', 'reshape_2'), ('reshape_2', 'reshape_2_data')], { 'placeholder_1_data': { 'shape': np.array([1, 112, 227, 227]) }, 'reshape_1_data': { 'shape': np.array([1, 4, 28, 227 * 227]) }, 'transpose_1': { 'order': np.array([0, 2, 1, 3]) }, 'transpose_1_data': { 'shape': np.array([1, 28, 4, 227 * 227]) }, 'reshape_2_data': { 'shape': np.array([1, 112, 227, 227]) }, }) pattern = FeatureShuffleReshape() pattern.find_and_replace_pattern(graph) (flag, resp) = compare_graphs(graph, graph_ref, 'reshape_2_data', check_op_attrs=True) self.assertTrue(flag, resp)
def test_swish_test_1(self): # Test with two different inputs from two placeholders graph = build_graph(nodes_attributes, [('placeholder_1', 'swish'), ('swish', 'last')], nodes_with_edges_only=True) graph_ref = build_graph(nodes_attributes, [ ('placeholder_1', 'sigmoid', { 'out': 0 }), ('placeholder_1', 'mul', { 'in': 0, 'out': 0 }), ('sigmoid', 'mul', { 'in': 1 }), ('mul', 'last'), ], nodes_with_edges_only=True) graph.stage = 'front' Swish().find_and_replace_pattern(graph) (flag, resp) = compare_graphs(graph, graph_ref, 'last', check_op_attrs=True) self.assertTrue(flag, resp)
def test_scaleshift_to_mul_1(self): graph = build_graph(nodes_attributes, [('placeholder_1', 'placeholder_1_data'), ('placeholder_1_data', 'scaleshift_1'), ('const_scaleshift_1_w', 'scaleshift_1_w'), ('scaleshift_1_w', 'scaleshift_1'), ('scaleshift_1', 'scaleshift_1_data'), ('scaleshift_1_data', 'op_output') ], {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])}, 'scaleshift_1_w': {'shape': np.array([3]), 'value': np.array([1, 2, 3])}, 'scaleshift_1_data': {} }) graph_ref = build_graph(nodes_attributes, [('placeholder_1', 'placeholder_1_data'), ('placeholder_1_data', 'mul_1'), ('const_mul_1_w', 'mul_1_w'), ('mul_1_w', 'mul_1'), ('mul_1', 'scaleshift_1_data'), ('scaleshift_1_data', 'op_output') ], {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])}, 'const_mul_1_w': {'shape': np.array([3]), 'value': np.array([1, 2, 3])}, 'mul_1_w': {'shape': np.array([3]), 'value': np.array([1, 2, 3])}, 'mul_1': {'can_be_fused': True}, 'scaleshift_1_data': {} }) graph.graph['layout'] = 'NHWC' convert_scale_shift_to_mul_add(graph) graph.clean_up() (flag, resp) = compare_graphs(graph, graph_ref, 'placeholder_1') self.assertTrue(flag, resp)
def test_mul_add_to_power_1(self): graph = self._create_graph_with_mul_add(np.array([3]), np.array([2])) graph_ref = build_graph(nodes_attributes, [ ('placeholder_1', 'placeholder_1_data'), ('placeholder_1_data', 'power_1'), ('power_1', 'power_1_data'), ('power_1_data', 'op_output'), ], { 'power_1': { 'scale': 3, 'shift': 2, 'power': 1 }, 'power_1_data': {} }) convert_muladd_to_scaleshift_or_power(graph) graph_clean_up(graph) (flag, resp) = compare_graphs(graph, graph_ref, 'add_1_data', 'power_1_data', check_op_attrs=True) self.assertTrue(flag, resp)
def test_mean_to_avg_1(self): graph = self._create_graph_with_mean(axis=np.array([1, 2])) graph_ref = build_graph( nodes_attributes, [ ('placeholder_1', 'placeholder_1_data'), ('placeholder_1_data', 'pool_1'), ('pool_1', 'pool_1_data'), ('pool_1_data', 'op_output'), ], { 'pool_1': { 'pool_method': 'avg', 'rounding_type': 'ceil', 'exclude_pad': 'true', 'op': 'AvgPool', 'shape': np.array([1, 227, 227, 3]) }, 'pool_1_data': { 'shape': np.array([1, 227, 227, 3]) } }) MeanToAvgPool().find_and_replace_pattern(graph) graph_clean_up(graph) (flag, resp) = compare_graphs(graph, graph_ref, 'mean_1_data', 'pool_1_data', check_op_attrs=True) self.assertTrue(flag, resp)
def test_select_infer_condition_true(self): graph = build_graph_with_attrs(nodes_with_attrs=self.nodes, edges_with_attrs=self.edges, update_nodes_attributes=[('condition', { 'value': np.array([True]) })]) # We should propagate shapes and values graph_ref = build_graph_with_attrs(nodes_with_attrs=self.nodes, edges_with_attrs=self.edges, update_nodes_attributes=[ ('select_output', { 'shape': np.array([2, 2]), 'value': np.ones((2, 2)) }) ]) tested_class = Select(graph=graph, attrs={}) node = Node(graph, 'select') tested_class.infer(node) (flag, resp) = compare_graphs(graph, graph_ref, 'select_output', check_op_attrs=True) self.assertTrue(flag, resp)
def test_move_scaleshift_to_preprocess_1(self): graph = build_graph(nodes_attributes, [('placeholder_1', 'placeholder_1_data'), ('placeholder_1_data', 'scaleshift_1'), ('scaleshift_1', 'scaleshift_1_data'), ('scaleshift_1_w', 'scaleshift_1'), ('scaleshift_1_b', 'scaleshift_1'), ('scaleshift_1_data', 'op_output') ], {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])}, 'scaleshift_1_w': {'shape': np.array([3]), 'value': np.ones(3)}, 'scaleshift_1_b': {'shape': np.array([3]), 'value': np.array([-1, -2, -3])}, }) del graph['placeholder_1']['placeholder_1_data'][0]['in'] del graph['scaleshift_1']['scaleshift_1_data'][0]['in'] graph_ref = build_graph(nodes_attributes, [('placeholder_1', 'scaleshift_1_data'), ('scaleshift_1_data', 'op_output') ]) move_scaleshift_to_preprocess(graph) self.assertTrue(graph.graph['mean_values'] is not None) self.assertTrue(np.array_equal(graph.graph['mean_values']['placeholder_1'], np.array([1, 2, 3]))) (flag, resp) = compare_graphs(graph, graph_ref, 'scaleshift_1_data') self.assertTrue(flag, resp)
def test_scaleshift_to_nothing(self): graph = build_graph(nodes_attributes, [('placeholder_1', 'placeholder_1_data'), ('placeholder_1_data', 'scaleshift_1'), ('const_scaleshift_1_w', 'scaleshift_1_w'), ('const_scaleshift_1_b', 'scaleshift_1_b'), ('scaleshift_1_w', 'scaleshift_1'), ('scaleshift_1_b', 'scaleshift_1'), ('scaleshift_1', 'scaleshift_1_data'), ('scaleshift_1_data', 'op_output') ], {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])}, 'scaleshift_1_w': {'shape': np.array([3]), 'value': np.array([1, 1, 1])}, 'scaleshift_1_b': {'shape': np.array([3]), 'value': np.array([0, 0, 0])}, 'scaleshift_1_data': {'shape': np.array([1, 227, 227, 3])} }, nodes_with_edges_only=True) graph_ref = build_graph(nodes_attributes, [('placeholder_1', 'placeholder_1_data'), ('placeholder_1_data', 'op_output') ], {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])}} ,nodes_with_edges_only=True) graph.graph['layout'] = 'NHWC' convert_scale_shift_to_mul_add(graph) graph.clean_up() (flag, resp) = compare_graphs(graph, graph_ref, 'placeholder_1') self.assertTrue(flag, resp)
def test_neg_reciprocal_1(self): # Test if power = 0 graph = build_graph(nodes_attributes, [('placeholder_1', 'reciprocal_1'), ('reciprocal_1', 'last') ], {'placeholder_1': {'shape': np.array([1, 227, 227, 3])}, }, nodes_with_edges_only=True) graph_ref = build_graph(nodes_attributes, [('placeholder_1', 'pow'), ('const', 'pow', {'in': 1}), ('pow', 'last'), ], {'placeholder_1': {'shape': np.array([1, 227, 227, 3])}, 'const': {'value': np.array(0)}, }, nodes_with_edges_only=True) graph.stage = 'front' pattern = ReciprocalReplacer() pattern.find_and_replace_pattern(graph) (flag, resp) = compare_graphs(graph, graph_ref, 'last', check_op_attrs=True) self.assertTrue(not flag)
def test_consecutive_stride_slices_removal(self): graph = build_graph(nodes_attributes, [ ('placeholder', 'placeholder_data'), ('placeholder_data', 'strided_slice'), ('strided_slice_input_1_data', 'strided_slice'), ('strided_slice_input_2_data', 'strided_slice'), ('strided_slice_input_3_data', 'strided_slice'), ('strided_slice', 'strided_slice_data'), ('strided_slice_data', 'strided_slice_2'), ('strided_slice_input_1_data', 'strided_slice_2'), ('strided_slice_input_2_data', 'strided_slice_2'), ('strided_slice_input_3_data', 'strided_slice_2'), ('strided_slice_2', 'strided_slice_2_data'), ('strided_slice_2_data', 'output_op'), ], {}, nodes_with_edges_only=True) UselessStridedSliceEraser().find_and_replace_pattern(graph) shape_inference(graph) graph_ref = build_graph(nodes_attributes, [ ('placeholder', 'placeholder_data'), ('placeholder_data', 'output_op'), ], {'placeholder_data': { 'shape': int64_array([4, 1, 6]) }}) (flag, resp) = compare_graphs(graph, graph_ref, 'output_op', check_op_attrs=True) self.assertTrue(flag, resp)
def test_unsqueeze_infer(self): graph = build_graph( self.nodes_attributes, [('data_1', 'unsq'), ('unsq', 'data_2')], { 'data_1': { 'shape': np.array([1, 3, 64, 64]) }, 'unsq': { 'unsqueeze_dims': np.array([0, 4]) } }) graph_ref = build_graph( self.nodes_attributes, [('data_1', 'unsq'), ('unsq', 'data_2')], { 'data_1': { 'shape': np.array([1, 3, 64, 64]) }, 'unsq': { 'unsqueeze_dims': np.array([0, 4]) }, 'data_2': { 'shape': np.array([1, 1, 3, 64, 1, 64]) } }) unsqueeze_node = Node(graph, 'unsq') Unsqueeze.infer(unsqueeze_node) (flag, resp) = compare_graphs(graph, graph_ref, 'data_2') self.assertTrue(flag, resp)
def test_in_port_with_data(self): graph = build_graph_with_attrs(nodes_with_attrs=self.nodes, edges_with_attrs=self.edges) new_input_shape = np.array([1, 2, 3, 4]) graph_ref = build_graph_with_attrs(nodes_with_attrs=self.nodes, edges_with_attrs=self.edges[1:], new_nodes_with_attrs=[ ('input_node', { 'kind': 'op', 'op': 'Parameter', 'shape': new_input_shape }), ('input_data', { 'kind': 'data' }) ], new_edges_with_attrs=[ ('input_node', 'input_data', { 'in': 0, 'out': 0 }), ('input_data', 'op_node', { 'in': 1, 'out': 0 }) ]) add_input_op(graph, 'op_node', 1, data=True, shape=new_input_shape) graph.remove_edge('future_input', 'op_node') (flag, resp) = compare_graphs(graph, graph_ref, last_node='op_node') self.assertTrue(flag, resp)
def test_squared_difference_test_1(self): graph = build_graph(nodes_attributes, [ ('placeholder_1', 'squared_difference'), ('placeholder_2', 'squared_difference'), ('squared_difference', 'last'), ], nodes_with_edges_only=True) graph_ref = build_graph(nodes_attributes, [ ('placeholder_1', 'add_1'), ('placeholder_2', 'negate_1'), ('n_const', 'negate_1'), ('negate_1', 'add_1'), ('add_1', 'square_1', { 'in': 0 }), ('s_const', 'square_1', { 'in': 1 }), ('square_1', 'last'), ], nodes_with_edges_only=True) graph.stage = 'front' replacer = SquaredDifference() replacer.find_and_replace_pattern(graph) (flag, resp) = compare_graphs(graph, graph_ref, 'last', check_op_attrs=True) self.assertTrue(flag, resp)
def test_single_stride_slice_removal(self): graph = build_graph(nodes_attributes, [ ('placeholder', 'placeholder_data'), ('placeholder_data', 'strided_slice'), ('strided_slice_input_1_data', 'strided_slice'), ('strided_slice_input_2_data', 'strided_slice'), ('strided_slice_input_3_data', 'strided_slice'), ('strided_slice', 'strided_slice_data'), ('strided_slice_data', 'output_op'), ], {}, nodes_with_edges_only=True) pattern = UselessStridedSliceEraser() pattern.find_and_replace_pattern(graph) graph_ref = build_graph(nodes_attributes, [ ('placeholder', 'placeholder_data'), ('placeholder_data', 'output_op'), ], {'placeholder_data': { 'shape': np.array([4, 5, 6]) }}) (flag, resp) = compare_graphs(graph, graph_ref, 'output_op', check_op_attrs=True) self.assertTrue(flag, resp)
def test_mul_to_scaleshift_1(self): graph = AddToScaleShift._create_graph_with_mul(np.array([1, 2, 3], dtype=np.float32)) graph.stage = 'middle' graph_ref = build_graph(nodes_attributes, [('placeholder_1', 'placeholder_1_data'), ('placeholder_1_data', 'scaleshift_1'), ('const_scaleshift_1_w', 'scaleshift_1_w'), ('const_scaleshift_1_b', 'scaleshift_1_b'), ('scaleshift_1_w', 'scaleshift_1'), ('scaleshift_1_b', 'scaleshift_1'), ('scaleshift_1', 'scaleshift_1_data'), ('scaleshift_1_data', 'op_output') ], {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])}, 'scaleshift_1_data': {'shape': np.array([1, 227, 227, 3])}, 'const_scaleshift_1_w': {'shape': np.array([3]), 'value': np.array([1, 2, 3])}, 'scaleshift_1_w': {'shape': np.array([3]), 'value': np.array([1, 2, 3])}, 'const_scaleshift_1_b': {'shape': np.array([3]), 'value': np.array([0, 0, 0])}, 'scaleshift_1_b': {'shape': np.array([3]), 'value': np.array([0, 0, 0])}, }, nodes_with_edges_only=True) convert_add_or_mul_to_scaleshift(graph) graph.clean_up() (flag, resp) = compare_graphs(graph, graph_ref, 'op_output') self.assertTrue(flag, resp) scsh_node = Node(graph, 'op_output').in_port(0).get_source().node self.assertTrue(graph.get_edge_data(scsh_node.in_node(1).id, scsh_node.id)[0]['bin'] == 'weights') self.assertTrue(graph.get_edge_data(scsh_node.in_node(2).id, scsh_node.id)[0]['bin'] == 'biases')
def test_switch_infer_no_condition(self): nodes = [('tensor', { 'value': None, 'kind': 'data', 'executable': True, 'shape': np.array([1, 2, 1]) }), ('pred_id', { 'value': None, 'kind': 'data', 'executable': True }), ('switch', { 'type': 'Switch', 'kind': 'op', 'op': 'Switch' }), ('switch_data_0', { 'value': None, 'kind': 'data', 'executable': True }), ('switch_data_1', { 'value': None, 'kind': 'data', 'executable': True })] edges = [('tensor', 'switch', { 'in': 0 }), ('pred_id', 'switch', { 'in': 1 }), ('switch', 'switch_data_0', { 'out': 0 }), ('switch', 'switch_data_1', { 'out': 1 })] graph = build_graph_with_attrs(nodes_with_attrs=nodes, edges_with_attrs=edges) # We should propagate only shapes graph_ref = build_graph_with_attrs(nodes_with_attrs=nodes, edges_with_attrs=edges, update_nodes_attributes=[ ('switch_data_0', { 'shape': np.array([1, 2, 1]) }), ('switch_data_1', { 'shape': np.array([1, 2, 1]) }) ]) tested_class = Switch(graph=graph, attrs={}) node = Node(graph, 'switch') tested_class.infer(node) (flag, resp) = compare_graphs(graph, graph_ref, 'switch_data_0', check_op_attrs=True) self.assertTrue(flag, resp)
def test_mul_add_neg_4(self): graph = self._create_graph_with_mul_add(np.array([1, 2, 3]), np.array([3])) graph_ref = self._create_graph_with_mul_add(np.array([1, 2, 3]), np.array([3])) convert_muladd_to_scaleshift(graph) graph.clean_up() (flag, resp) = compare_graphs(graph, graph_ref, 'add_1_data', 'add_1_data', check_op_attrs=True) self.assertTrue(flag, resp)
def test_split_with_one_consumer(self): nodes = { 'placeholder': {'op': 'Parameter', 'kind': 'op', 'shape': int64_array([2, 10, 4])}, 'unpack': {'op': 'Unpack', 'kind': 'op', 'axis': 2}, 'act_0': {'op': 'Sigmoid', 'kind': 'op'}, 'act_1': {'op': 'Sigmoid', 'kind': 'op'}, 'act_2': {'op': 'Sigmoid', 'kind': 'op'}, 'act_3': {'op': 'Sigmoid', 'kind': 'op'}, 'concat': {'op': 'Concat', 'kind': 'op', 'axis': 2}, 'op_output': {'op': 'OpOutput', 'kind': 'op'}, 'squeeze_0': {'op': 'Squeeze', 'kind': 'op'}, 'squeeze_1': {'op': 'Squeeze', 'kind': 'op'}, 'squeeze_2': {'op': 'Squeeze', 'kind': 'op'}, 'squeeze_3': {'op': 'Squeeze', 'kind': 'op'}, 'squeeze_0_dim': {'op': 'Const', 'kind': 'op', 'value': int64_array(2)}, 'squeeze_1_dim': {'op': 'Const', 'kind': 'op', 'value': int64_array(2)}, 'squeeze_2_dim': {'op': 'Const', 'kind': 'op', 'value': int64_array(2)}, 'squeeze_3_dim': {'op': 'Const', 'kind': 'op', 'value': int64_array(2)}, } graph = build_graph(nodes, [ ('placeholder', 'unpack', {'out': 0, 'in': 0}), ('unpack', 'act_0', {'out': 0, 'in': 0}), ('unpack', 'act_1', {'out': 1, 'in': 0}), ('unpack', 'act_2', {'out': 2, 'in': 0}), ('unpack', 'act_3', {'out': 3, 'in': 0}), ('act_0', 'concat', {'out': 0, 'in': 0}), ('act_1', 'concat', {'out': 0, 'in': 1}), ('act_2', 'concat', {'out': 0, 'in': 2}), ('act_3', 'concat', {'out': 0, 'in': 3}), ('concat', 'op_output', {'out': 1, 'in': 0}), ], nodes_with_edges_only=True) graph.stage = 'front' graph_ref = build_graph(nodes, [ ('placeholder', 'unpack', {'out': 0, 'in': 0}), ('unpack', 'squeeze_0', {'out': 0, 'in': 0}), ('unpack', 'squeeze_1', {'out': 1, 'in': 0}), ('unpack', 'squeeze_2', {'out': 2, 'in': 0}), ('unpack', 'squeeze_3', {'out': 3, 'in': 0}), ('squeeze_0_dim', 'squeeze_0', {'out': 0, 'in': 1}), ('squeeze_1_dim', 'squeeze_1', {'out': 1, 'in': 1}), ('squeeze_2_dim', 'squeeze_2', {'out': 2, 'in': 1}), ('squeeze_3_dim', 'squeeze_3', {'out': 3, 'in': 1}), ('squeeze_0', 'act_0', {'out': 0, 'in': 0}), ('squeeze_1', 'act_1', {'out': 0, 'in': 0}), ('squeeze_2', 'act_2', {'out': 0, 'in': 0}), ('squeeze_3', 'act_3', {'out': 0, 'in': 0}), ('act_0', 'concat', {'out': 0, 'in': 0}), ('act_1', 'concat', {'out': 0, 'in': 1}), ('act_2', 'concat', {'out': 0, 'in': 2}), ('act_3', 'concat', {'out': 0, 'in': 3}), ('concat', 'op_output', {'out': 1, 'in': 0}), ], nodes_with_edges_only=True) Unpack().find_and_replace_pattern(graph=graph) (flag, resp) = compare_graphs(graph, graph_ref, 'op_output', check_op_attrs=True) self.assertTrue(flag, resp)