Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / front / eltwise_n_test.py
1 """
2  Copyright (c) 2018-2019 Intel Corporation
3
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 """
16
17 import unittest
18
19 from extensions.front.eltwise_n import EltwiseNReplacement
20 from mo.utils.unittest.graph import build_graph
21 from mo.graph.graph import Node
22
23
24 class TestAddNFrontReplacement(unittest.TestCase):
25     def test_replase_eltwise_n(self):
26         graph = build_graph(
27             {'node_1': {'type': 'Identity', 'value': None, 'kind': 'op', 'op': 'Placeholder'},
28              'node_2': {'type': 'Identity', 'value': None, 'kind': 'op'},
29              'node_3': {'type': 'Identity', 'value': None, 'kind': 'op'},
30              'add_n': {'value': None, 'operation': 'sum', 'type': None, 'kind': 'op', 'op': 'EltwiseN'},
31              'node_4': {'type': 'Identity', 'value': None, 'kind': 'op'},
32              },
33             [('node_1', 'node_2'),
34              ('node_2', 'add_n'),
35              ('node_3', 'add_n'),
36              ('add_n', 'node_4'), ],
37         )
38
39         add_n_node = Node(graph, 'add_n')
40         rep_op = EltwiseNReplacement()
41         rep_op.replace_op(graph, add_n_node)
42         eltwise_nodes = [node for node, attrs in list(graph.nodes(data=True)) if attrs['type'] == 'Eltwise']
43         self.assertEqual(len(eltwise_nodes), 1)
44
45     def test_replase_eltwise_n_2(self):
46         graph = build_graph(
47             {'node_1': {'type': 'Identity', 'value': None, 'kind': 'op', 'op': 'Placeholder'},
48              'node_2': {'type': 'Identity', 'value': None, 'kind': 'op'},
49              'node_3': {'type': 'Identity', 'value': None, 'kind': 'op'},
50              'node_4': {'type': 'Identity', 'value': None, 'kind': 'op'},
51              'add_n': {'value': None, 'operation': 'sum', 'type': None, 'kind': 'op', 'op': 'EltwiseN'},
52              'node_5': {'type': 'Identity', 'value': None, 'kind': 'op'},
53              },
54             [('node_1', 'node_2'),
55              ('node_2', 'add_n'),
56              ('node_3', 'add_n'),
57              ('node_4', 'add_n'),
58              ('add_n', 'node_5'), ],
59         )
60
61         add_n_node = Node(graph, 'add_n')
62         rep_op = EltwiseNReplacement()
63         rep_op.replace_op(graph, add_n_node)
64         eltwise_nodes = [node for node, attrs in list(graph.nodes(data=True)) if attrs['type'] == 'Eltwise']
65         self.assertEqual(len(eltwise_nodes), 2)
66
67     def test_replase_eltwise_n_3(self):
68         graph = build_graph(
69             {'node_1': {'type': 'Identity', 'value': None, 'kind': 'op', 'op': 'Placeholder'},
70              'node_2': {'type': 'Identity', 'value': None, 'kind': 'op'},
71              'node_3': {'type': 'Identity', 'value': None, 'kind': 'op'},
72              'node_4': {'type': 'Identity', 'value': None, 'kind': 'op'},
73              'node_5': {'type': 'Identity', 'value': None, 'kind': 'op'},
74              'add_n': {'value': None, 'operation': 'sum', 'type': None, 'kind': 'op', 'op': 'EltwiseN'},
75              'node_6': {'type': 'Identity', 'value': None, 'kind': 'op'},
76              },
77             [('node_1', 'node_2'),
78              ('node_2', 'add_n'),
79              ('node_3', 'add_n'),
80              ('node_4', 'add_n'),
81              ('node_5', 'add_n'),
82              ('add_n', 'node_6'), ],
83         )
84
85         add_n_node = Node(graph, 'add_n')
86         rep_op = EltwiseNReplacement()
87         rep_op.replace_op(graph, add_n_node)
88         eltwise_nodes = [node for node, attrs in list(graph.nodes(data=True)) if attrs['type'] == 'Eltwise']
89         self.assertEqual(len(eltwise_nodes), 3)