Exemple #1
0
 def insert_result(model_graph, node, child_node, name):
     if node.graph != model_graph:
         model_graph.graph[
             'additional_outputs'] = child_node.fullname.split('|')
         res_op = AddOutputRecursive().find_and_replace_pattern(model_graph)
         ie_result_name = res_op[0].name
     else:
         ie_result_name = f'{name}_{node.name}'
         res_op = Result(node.graph, {
             'name': f'Result_{ie_result_name}'
         }).create_node()
         child_node.out_port(0).connect(res_op.in_port(0))
     return (False, ie_result_name)
Exemple #2
0
    def test_1_if_graph_change_add_output(self):
        graph = self.create_graph()
        graph.graph['additional_outputs'] = [
            'Loop', 'If_2', ['OUT_2', 'OUT_2_else']
        ]

        AddOutputRecursive().find_and_replace_pattern(graph)

        loop_node = Node(graph, 'Loop')
        if_node = Node(loop_node.body, 'If_2')
        left_node = Node(if_node.then_graph, 'OUT_2')
        right_node = Node(if_node.else_graph, 'OUT_2_else')
        self.assertEqual(len(left_node.out_port(0).get_destinations()), 2)
        self.assertEqual(
            left_node.out_port(0).get_destinations()[1].node.op, 'Result')

        self.assertEqual(len(right_node.out_port(0).get_destinations()), 2)
        self.assertEqual(
            right_node.out_port(0).get_destinations()[1].node.op, 'Result')

        self.assertTrue(len(if_node.out_ports()), 2)
        self.assertTrue(
            if_node.out_port(1).get_destination().node.op, 'Result')

        self.assertTrue(len(loop_node.out_ports()), 2)
        self.assertTrue(
            loop_node.out_port(1).get_destination().node.op, 'Result')
Exemple #3
0
    def test_linear_graph_change(self):
        graph = self.create_graph()
        path = ['Loop', 'in_1_int']
        ref_path = []
        loop_node = Node(graph, 'Loop')
        ref_path.append({'node': loop_node, 'graph': graph})
        ref_path.append({
            'node': Node(loop_node.body, 'in_1_int'),
            'graph': loop_node.body
        })

        tracks = AddOutputRecursive().split_path_to_simple_tracks(graph, path)

        self.assertTrue(np.all(tracks[0] == ref_path))
Exemple #4
0
    def test_1_if_graph_change(self):
        graph = self.create_graph()
        path = ['Loop', 'If_2', ['OUT_2', 'OUT_2_else']]
        ref_path = [[]]
        loop_node = Node(graph, 'Loop')
        ref_path[0].append({'node': loop_node, 'graph': graph})
        if_node = Node(loop_node.body, 'If_2')
        ref_path[0].append({'node': if_node, 'graph': loop_node.body})
        ref_path.append([])
        ref_path[1] = ref_path[0][:]
        ref_path[0].append({
            'node': Node(if_node.then_graph, 'OUT_2'),
            'graph': if_node.then_graph
        })
        ref_path[1].append({
            'node': Node(if_node.else_graph, 'OUT_2_else'),
            'graph': if_node.else_graph
        })

        tracks = AddOutputRecursive().split_path_to_simple_tracks(graph, path)

        self.assertTrue(np.all(tracks[0] == ref_path[0]))
        self.assertTrue(np.all(tracks[1] == ref_path[1]))
Exemple #5
0
    def insert_statistic(self, model, stats_layout, stat_aliases=None):
        output_to_node_names = {}
        if stat_aliases is None or model is None:
            return model, list(stats_layout.keys()), output_to_node_names
        nodes_names = []
        copy_stat_aliases = deepcopy(stat_aliases)
        for algo_name, node_stats in copy_stat_aliases.items():
            for node_name, stats in node_stats.items():
                node_name_in_graph = node_name[0] if isinstance(
                    node_name, tuple) else node_name
                node_name_in_graph = node_name_in_graph.replace(
                    '/pre_fq_input', '')
                node = get_node_by_name(model, node_name_in_graph)
                node_in_main_graph = get_node_by_name(
                    model,
                    node_name_in_graph.split('|')[0])
                model_graph = node_in_main_graph.graph
                for stat, _ in list(stats.items()):
                    if not isinstance(stat, Statistic) or not stat.kwargs.get(
                            'inplace_statistics', False):
                        if node_name_in_graph not in nodes_names:
                            nodes_names.append(node_name_in_graph)
                        continue
                    type_stat = stat.kwargs['type']
                    add_output_node, op_name = getattr(
                        self, f'insert_{type_stat}')(model_graph, node,
                                                     type_stat, node.name,
                                                     **stat.kwargs)
                    if add_output_node:
                        if node_name_in_graph not in nodes_names:
                            nodes_names.append(op_name)
                        class_statistic = TensorStatistic if isinstance(
                            stat, TensorStatistic) else TensorStatisticAxis
                        fn = get_stats_function(ACTIVATIONS, type_stat,
                                                stat.kwargs.get('granularity'),
                                                'compute_statistic')
                        new_stat = class_statistic(
                            fn,
                            channel=stat.kwargs.get('channel', {}),
                            inplace_statistics=False,
                            granularity=stat.kwargs.get('granularity'),
                            type=type_stat,
                            layer_stat_name=op_name)
                    else:
                        new_stat = deepcopy(stat)
                        new_stat.kwargs['layer_stat_name'] = op_name

                    del stats_layout[node_name][stat]
                    stats_layout[node_name][new_stat] = new_stat

                    stat_name = stat_aliases[algo_name][node_name][stat]
                    del stat_aliases[algo_name][node_name][stat]
                    stat_aliases[algo_name][node_name][new_stat] = stat_name

                # add output if node in subgraph
                if model_graph != node.graph:
                    if node_name_in_graph in nodes_names:
                        nodes_names.remove(node_name_in_graph)

                    # Don't need adding extra output to the same node, but for another algo
                    if node_name_in_graph in output_to_node_names.values():
                        result_name = next(
                            (result
                             for result, node in output_to_node_names.items()
                             if node == node_name_in_graph))
                    else:
                        model_graph.graph[
                            'additional_outputs'] = node_name_in_graph.split(
                                '|')
                        results = AddOutputRecursive(
                        ).find_and_replace_pattern(model_graph)
                        assert len(results) == 1
                        result_name = results[0].name
                    if node_name in stats_layout:
                        stats_layout[result_name] = stats_layout.pop(node_name)
                    stat_aliases[algo_name][result_name] = stat_aliases[
                        algo_name].pop(node_name)
                    output_to_node_names[result_name] = node_name_in_graph

        return model, nodes_names, output_to_node_names
Exemple #6
0
    def test_add_output_1(self):
        sub_graph_2 = build_graph(nodes_attrs=if_sub_graph_2_then_nodes,
                                  edges=[
                                      *connect('in_2_int', 'OUT_2'),
                                      *connect('ones', 'OUT_2'),
                                      *connect('OUT_2', 'OUT_2_out')
                                  ],
                                  nodes_with_edges_only=True)

        sub_graph_2_else = build_graph(
            nodes_attrs=if_sub_graph_2_else_nodes,
            edges=[
                *connect('in_2_int_else', 'OUT_2_else'),
                *connect('ones_else', 'OUT_2_else'),
                *connect('OUT_2_else', 'OUT_2_out_else')
            ],
            nodes_with_edges_only=True)

        sub_graph_1 = build_graph(nodes_attrs=if_sub_graph_1_then_nodes,
                                  edges=[
                                      *connect('cond_2', '0:If_2'),
                                      *connect('IN_2', '1:If_2'),
                                      *connect('If_2:0', 'If_2_out'),
                                      *connect('in_1_int', 'in_1_int_out')
                                  ],
                                  nodes_with_edges_only=True)
        if_node_1 = Node(sub_graph_1, 'If_2')
        if_node_1.then_graph = sub_graph_2
        if_node_1.else_graph = sub_graph_2_else

        sub_graph_1_else = build_graph(
            nodes_attrs=if_sub_graph_1_else_nodes,
            edges=[*connect('in_1_int', 'in_1_int_out')],
            nodes_with_edges_only=True)

        main_graph = build_graph(nodes_attrs=if_main_graph_nodes,
                                 edges=[
                                     *connect('cond', '0:If'),
                                     *connect('IN_1', '1:If'),
                                     *connect('IN_2', "2:If"),
                                     *connect('If:0', 'OUT_1')
                                 ],
                                 nodes_with_edges_only=True)
        if_node = Node(main_graph, 'If')
        if_node.then_graph = sub_graph_1
        if_node.else_graph = sub_graph_1_else
        if_node_out_ports_len = len(if_node.out_ports())
        if_2_node_out_ports_len = len(if_node_1.out_ports())

        main_graph.graph['additional_outputs'] = ['If', ['If_2', 'in_1_int']]

        AddOutputRecursive().find_and_replace_pattern(main_graph)
        if_node = Node(main_graph, 'If')
        self.assertEqual(len(if_node.out_ports()), if_node_out_ports_len + 1)
        self.assertEqual(
            if_node.out_port(1).get_destination().node.op, 'Result')
        self.assertTrue(
            np.all(
                if_node.out_port(1).data.get_shape() == int64_array(
                    [1, 4, 64, 54])))
        last_node = Node(sub_graph_1, 'If_2')
        self.assertEqual(len(last_node.out_ports()), if_2_node_out_ports_len)
        self.assertEqual(
            last_node.out_port(0).get_destinations()[1].node.op, 'Result')
        self.assertTrue(
            np.all(
                last_node.out_port(0).data.get_shape() == int64_array(
                    [1, 4, 64, 54])))
Exemple #7
0
    def test_add_output_several_iterations_negative_stride(self):
        sub_graph_2 = build_graph(nodes_attrs=ti_sub_graph_2_nodes,
                                  edges=[
                                      *connect('cond_2_int', 'cond_2_int_out'),
                                      *connect('in_2_int', 'OUT_2'),
                                      *connect('ones', 'OUT_2'),
                                      *connect('OUT_2', 'OUT_2_out'),
                                      *connect('in_2_int', 'in_2_int_out')
                                  ],
                                  nodes_with_edges_only=True)

        sub_graph_1 = build_graph(nodes_attrs=ti_sub_graph_1_nodes,
                                  edges=[
                                      *connect('cond_2', '1:Loop_2'),
                                      *connect('IN_2', '0:Loop_2'),
                                      *connect('Loop_2:0', 'Loop_2_out'),
                                      *connect('in_1_int', 'in_1_int_out'),
                                      *connect('cond_1_int', 'cond_1_int_out')
                                  ],
                                  nodes_with_edges_only=True)
        loop_node_1 = Node(sub_graph_1, 'Loop_2')
        loop_node_1.body = sub_graph_2
        loop_node_1.in_edge(0)['external_port_id'] = 0
        loop_node_1.in_edge(1)['external_port_id'] = 1
        loop_node_1.out_edge(0)['external_port_id'] = 2

        main_graph = build_graph(nodes_attrs=ti_main_graph_nodes,
                                 edges=[
                                     *connect('M', '0:Loop'),
                                     *connect('cond', '1:Loop'),
                                     *connect('IN_2', '2:Loop'),
                                     *connect('IN_1', "3:Loop"),
                                     *connect('Loop:0', 'OUT_1')
                                 ],
                                 nodes_with_edges_only=True)
        loop_node = Node(main_graph, 'Loop')
        loop_node.body = sub_graph_1
        loop_node.in_edge(0)['external_port_id'] = 0
        loop_node.in_edge(1)['external_port_id'] = 1
        loop_node.in_edge(2)['external_port_id'] = 2
        loop_node.in_edge(3)['external_port_id'] = 3
        loop_node.out_edge(0)['external_port_id'] = 4
        loop_node_output_port_map_len = len(loop_node.output_port_map)
        loop_node_out_ports_len = len(loop_node.out_ports())
        loop_2_node_out_ports_len = len(loop_node_1.out_ports())
        max_layer_id = 5
        loop_node.input_port_map[2]['axis'] = 1
        loop_node.input_port_map[2]['start'] = -1
        loop_node.input_port_map[2]['end'] = 0
        loop_node.input_port_map[2]['stride'] = -2
        loop_node.output_port_map[0]['axis'] = 1
        loop_node.output_port_map[0]['start'] = 0
        loop_node.output_port_map[0]['end'] = -1
        loop_node.output_port_map[0]['stride'] = 2

        main_graph.graph['additional_outputs'] = ['Loop', 'Loop_2']

        AddOutputRecursive().find_and_replace_pattern(main_graph)
        loop_node = Node(main_graph, 'Loop')
        self.assertEqual(len(loop_node.output_port_map),
                         loop_node_output_port_map_len + 1)
        self.assertEqual(len(loop_node.out_ports()),
                         loop_node_out_ports_len + 1)
        self.assertEqual(
            loop_node.out_port(1).get_destination().node.op, 'Result')
        self.assertTrue(
            np.all(
                loop_node.out_port(1).data.get_shape() == shape_array(
                    [2, 1, 4, 64, 54])))
        self.assertTrue(
            np.all(
                loop_node.out_port(0).data.get_shape() == shape_array(
                    [1, 2, 64, 54])))
        last_node = Node(sub_graph_1, 'Loop_2')
        self.assertEqual(len(last_node.out_ports()), loop_2_node_out_ports_len)
        unsq_node = last_node.out_port(0).get_destinations()[1].node
        self.assertEqual(unsq_node.op, 'Unsqueeze')
        self.assertEqual(
            unsq_node.out_port(0).get_destination().node.op, 'Result')
        self.assertEqual(
            unsq_node.out_port(0).get_destination().node.internal_layer_id,
            max_layer_id + 3)
        self.assertTrue(
            np.all(
                unsq_node.out_port(0).data.get_shape() == int64_array(
                    [1, 1, 4, 64, 54])))
Exemple #8
0
    def test_add_output_1(self):
        sub_graph_2 = build_graph(nodes_attrs=sub_graph_2_nodes,
                                  edges=[
                                      *connect('cond_2_int', 'cond_2_int_out'),
                                      *connect('in_2_int', 'OUT_2'),
                                      *connect('ones', 'OUT_2'),
                                      *connect('OUT_2', 'OUT_2_out'),
                                      *connect('in_2_int', 'in_2_int_out')
                                  ],
                                  nodes_with_edges_only=True)

        sub_graph_1 = build_graph(nodes_attrs=sub_graph_1_nodes,
                                  edges=[
                                      *connect('M_2', '0:Loop_2'),
                                      *connect('cond_2', '1:Loop_2'),
                                      *connect('IN_2', '2:Loop_2'),
                                      *connect('Loop_2:0', 'Loop_2_out'),
                                      *connect('in_1_int', 'in_1_int_out'),
                                      *connect('cond_1_int', 'cond_1_int_out')
                                  ],
                                  nodes_with_edges_only=True)
        loop_node_1 = Node(sub_graph_1, 'Loop_2')
        loop_node_1.body = sub_graph_2

        main_graph = build_graph(nodes_attrs=main_graph_nodes,
                                 edges=[
                                     *connect('M', '0:Loop'),
                                     *connect('cond', '1:Loop'),
                                     *connect('IN_2', '2:Loop'),
                                     *connect('IN_1', "3:Loop"),
                                     *connect('Loop:0', 'OUT_1')
                                 ],
                                 nodes_with_edges_only=True)
        loop_node = Node(main_graph, 'Loop')
        loop_node.body = sub_graph_1
        main_graph.graph['additional_outputs'] = ['Loop', 'Loop_2']
        loop_node_output_port_map_len = len(loop_node.output_port_map)
        loop_node_out_ports_len = len(loop_node.out_ports())
        loop_2_out_ports_len = len(loop_node_1.out_ports())
        max_layer_id = 5

        AddOutputRecursive().find_and_replace_pattern(main_graph)

        loop_node = Node(main_graph, 'Loop')
        self.assertEqual(len(loop_node.output_port_map),
                         loop_node_output_port_map_len + 1)
        self.assertEqual(len(loop_node.out_ports()),
                         loop_node_out_ports_len + 1)
        self.assertEqual(
            loop_node.out_port(1).get_destination().node.op, 'Result')
        self.assertTrue(
            np.all(
                loop_node.out_port(1).data.get_shape() == int64_array(
                    [5, 10, 4, 64, 54])))
        last_node = Node(sub_graph_1, 'Loop_2')
        self.assertEqual(len(last_node.out_ports()), loop_2_out_ports_len)
        unsq_node = last_node.out_port(0).get_destinations()[1].node
        self.assertEqual(unsq_node.op, 'Unsqueeze')
        self.assertEqual(
            unsq_node.out_port(0).get_destination().node.op, 'Result')
        self.assertEqual(
            unsq_node.out_port(0).get_destination().node.internal_layer_id,
            max_layer_id + 3)
        self.assertTrue(
            np.all(
                unsq_node.out_port(0).data.get_shape() == int64_array(
                    [1, 10, 4, 64, 54])))