2 Copyright (C) 2018-2020 Intel Corporation
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
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
25 class AttributedClampNormalizerTests(unittest.TestCase):
27 def test_2_inputs(self):
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)),
37 edges = [*connect('placeholder', '0:a_clamp'),
38 *connect('min', '1:a_clamp'),
39 *connect('max', '2:a_clamp'),
40 *connect('a_clamp', 'result'),
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')])
46 (flag, resp) = compare_graphs(graph, ref_graph, 'result')
47 self.assertTrue(flag, resp)
49 def test_all_dynamic_inputs(self):
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'}),
59 edges = [*connect('placeholder', '0:a_clamp'),
60 *connect('min', '1:a_clamp'),
61 *connect('max', '2:a_clamp'),
62 *connect('a_clamp', 'result'),
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')
73 (flag, resp) = compare_graphs(graph, ref_graph, 'result')
74 self.assertTrue(flag, resp)
76 def test_no_max_input(self):
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)),
84 edges = [*connect('placeholder', '0:a_clamp'),
85 *connect('min', '1:a_clamp'),
86 *connect('a_clamp', 'result'),
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')
95 (flag, resp) = compare_graphs(graph, ref_graph, 'result')
96 self.assertTrue(flag, resp)
98 def test_no_min_input(self):
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)),
106 edges = [*connect('placeholder', '0:a_clamp'),
107 *connect('max', '2:a_clamp'),
108 *connect('a_clamp', 'result'),
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')
117 (flag, resp) = compare_graphs(graph, ref_graph, 'result')
118 self.assertTrue(flag, resp)