[MO] Fix ONNX Clamp-11 shape infer with no min/max inputs (#2603)
[platform/upstream/dldt.git] / model-optimizer / mo / ops / clamp.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 numpy as np
17
18 from mo.graph.graph import Graph
19 from mo.ops.op import Op
20
21
22 class AttributedClamp(Op):
23     op = 'AttributedClamp'
24
25     def __init__(self, graph: Graph, attrs: dict):
26         super().__init__(graph, {
27             'type': 'Clamp',
28             'op': self.op,
29             'version': 'opset1',
30             'infer': self.infer,
31             'in_ports_count': 1,
32             'out_ports_count': 1,
33         }, attrs)
34
35     def supported_attrs(self):
36         return [
37             'max',
38             'min'
39         ]
40
41     @staticmethod
42     def infer(node):
43         name = node.soft_get('name', node.id)
44         connected_in_ports = [port for port in node.in_ports().values() if not port.disconnected()]
45
46         assert len(connected_in_ports) == 1 and connected_in_ports[0].idx == 0, \
47             'AttributedClamp should have only one input, but it has {}'.format(len(connected_in_ports))
48         assert node.has_valid('max') and node.has_valid('min'), \
49             'Mandatory attributes `max` and `min` were not set for AttributedClamp node: `{}`'.format(name)
50         assert node.max >= node.min, \
51             'AttributedClamp max=={} is less than min=={} for node `{}`'.format(node.max, node.min, name)
52
53         if node.in_port(0).data.get_value() is not None:
54             node.out_port(0).data.set_value(np.clip(node.in_port(0).data.get_value(), node['min'], node['max']))
55         else:
56             node.out_port(0).data.set_shape(node.in_port(0).data.get_shape())
57
58
59 class Clamp(Op):
60     op = 'Clamp'
61
62     def __init__(self, graph: Graph, attrs: dict):
63         super().__init__(graph, {
64             'type': None,
65             'op': self.op,
66             'infer': self.infer,
67             'in_ports_count': 3,
68             'out_ports_count': 1,
69         }, attrs)
70
71     @staticmethod
72     def infer(node):
73         name = node.soft_get('name', node.id)
74         min_input_connected = node.has_port('in', 1) and not node.in_port(1).disconnected()
75         max_input_connected = node.has_port('in', 2) and not node.in_port(2).disconnected()
76
77         input_value = node.in_port(0).data.get_value()
78         min_value = node.in_port(1).data.get_value() if min_input_connected else np.finfo(np.float32).min
79         max_value = node.in_port(2).data.get_value() if max_input_connected else np.finfo(np.float32).max
80
81         if input_value is not None and min_value is not None and max_value is not None:
82             assert np.all(max_value >= min_value), \
83                 'Clamp max_value=={} is less than min_value=={} for node `{}`'.format(max_value, min_value, name)
84             node.out_port(0).data.set_value(np.clip(node.in_port(0).data.get_value(), min_value, max_value))
85         else:
86             node.out_port(0).data.set_shape(node.in_port(0).data.get_shape())