[MO] Fix ONNX Clamp-11 shape infer with no min/max inputs (#2603)
[platform/upstream/dldt.git] / model-optimizer / extensions / back / ClampNormalizer_test.py
1 """
2  Copyright (C) 2018-2020 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 import unittest
17
18 import numpy as np
19
20 from extensions.back.ClampNormalizer import ClampNormalizer
21 from mo.utils.ir_engine.compare_graphs import compare_graphs
22 from mo.utils.unittest.graph import build_graph, regular_op_with_shaped_data, valued_const_with_data, result, connect
23
24
25 class AttributedClampNormalizerTests(unittest.TestCase):
26
27     def test_2_inputs(self):
28         nodes = {
29             **regular_op_with_shaped_data('placeholder', [1, 3, 20, 20], {'type': 'Parameter'}),
30             **regular_op_with_shaped_data('a_clamp', [1, 3, 20, 20], {'type': None, 'op': 'Clamp'}),
31             **regular_op_with_shaped_data('clamp', [1, 3, 20, 20],
32                                           {'type': 'Clamp', 'op': 'AttributedClamp', 'min': -3.5, 'max': 3.5}),
33             **valued_const_with_data('min', np.array(-3.5)),
34             **valued_const_with_data('max', np.array(3.5)),
35             **result('result'),
36         }
37         edges = [*connect('placeholder', '0:a_clamp'),
38                  *connect('min', '1:a_clamp'),
39                  *connect('max', '2:a_clamp'),
40                  *connect('a_clamp', 'result'),
41                  ]
42         graph = build_graph(nodes, edges)
43         ClampNormalizer().find_and_replace_pattern(graph)
44         ref_graph = build_graph(nodes, [*connect('placeholder', '0:clamp'), *connect('clamp', 'result')])
45
46         (flag, resp) = compare_graphs(graph, ref_graph, 'result')
47         self.assertTrue(flag, resp)
48
49     def test_all_dynamic_inputs(self):
50         nodes = {
51             **regular_op_with_shaped_data('placeholder', [1, 3, 20, 20], {'type': 'Parameter'}),
52             **regular_op_with_shaped_data('min', [1, 3, 20, 20], {'type': 'Parameter'}),
53             **regular_op_with_shaped_data('max', [1, 3, 20, 20], {'type': 'Parameter'}),
54             **regular_op_with_shaped_data('a_clamp', [1, 3, 20, 20], {'type': None, 'op': 'Clamp'}),
55             **regular_op_with_shaped_data('maximum', [1, 3, 20, 20], {'type': 'Maximum', 'op': 'Maximum'}),
56             **regular_op_with_shaped_data('minimum', [1, 3, 20, 20], {'type': 'Minimum', 'op': 'Minimum'}),
57             **result('result'),
58         }
59         edges = [*connect('placeholder', '0:a_clamp'),
60                  *connect('min', '1:a_clamp'),
61                  *connect('max', '2:a_clamp'),
62                  *connect('a_clamp', 'result'),
63                  ]
64         graph = build_graph(nodes, edges)
65         ClampNormalizer().find_and_replace_pattern(graph)
66         ref_graph = build_graph(nodes, [*connect('placeholder', '0:maximum'),
67                                         *connect('min', '1:maximum'),
68                                         *connect('maximum', '0:minimum'),
69                                         *connect('max', '1:minimum'),
70                                         *connect('minimum', 'result')
71                                         ])
72
73         (flag, resp) = compare_graphs(graph, ref_graph, 'result')
74         self.assertTrue(flag, resp)
75
76     def test_no_max_input(self):
77         nodes = {
78             **regular_op_with_shaped_data('placeholder', [1, 3, 20, 20], {'type': 'Parameter'}),
79             **regular_op_with_shaped_data('a_clamp', [1, 3, 20, 20], {'type': None, 'op': 'Clamp'}),
80             **regular_op_with_shaped_data('maximum', [1, 3, 20, 20], {'type': 'Maximum', 'op': 'Maximum'}),
81             **valued_const_with_data('min', np.array(-3.5)),
82             **result('result'),
83         }
84         edges = [*connect('placeholder', '0:a_clamp'),
85                  *connect('min', '1:a_clamp'),
86                  *connect('a_clamp', 'result'),
87                  ]
88         graph = build_graph(nodes, edges)
89         ClampNormalizer().find_and_replace_pattern(graph)
90         ref_graph = build_graph(nodes, [*connect('placeholder', '0:maximum'),
91                                         *connect('min', '1:maximum'),
92                                         *connect('maximum', 'result')
93                                         ])
94
95         (flag, resp) = compare_graphs(graph, ref_graph, 'result')
96         self.assertTrue(flag, resp)
97
98     def test_no_min_input(self):
99         nodes = {
100             **regular_op_with_shaped_data('placeholder', [1, 3, 20, 20], {'type': 'Parameter'}),
101             **regular_op_with_shaped_data('a_clamp', [1, 3, 20, 20], {'type': None, 'op': 'Clamp'}),
102             **regular_op_with_shaped_data('minimum', [1, 3, 20, 20], {'type': 'Minimum', 'op': 'Minimum'}),
103             **valued_const_with_data('max', np.array(3.5)),
104             **result('result'),
105         }
106         edges = [*connect('placeholder', '0:a_clamp'),
107                  *connect('max', '2:a_clamp'),
108                  *connect('a_clamp', 'result'),
109                  ]
110         graph = build_graph(nodes, edges)
111         ClampNormalizer().find_and_replace_pattern(graph)
112         ref_graph = build_graph(nodes, [*connect('placeholder', '0:minimum'),
113                                         *connect('max', '1:minimum'),
114                                         *connect('minimum', 'result')
115                                         ])
116
117         (flag, resp) = compare_graphs(graph, ref_graph, 'result')
118         self.assertTrue(flag, resp)