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.
18 from mo.graph.graph import Graph
19 from mo.ops.op import Op
22 class AttributedClamp(Op):
23 op = 'AttributedClamp'
25 def __init__(self, graph: Graph, attrs: dict):
26 super().__init__(graph, {
35 def supported_attrs(self):
43 name = node.soft_get('name', node.id)
44 connected_in_ports = [port for port in node.in_ports().values() if not port.disconnected()]
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)
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']))
56 node.out_port(0).data.set_shape(node.in_port(0).data.get_shape())
62 def __init__(self, graph: Graph, attrs: dict):
63 super().__init__(graph, {
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()
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
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))
86 node.out_port(0).data.set_shape(node.in_port(0).data.get_shape())