2 Copyright (c) 2019 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.
19 from mo.front.extractor import add_attrs_props
20 from mo.front.extractor import update_ie_fields
21 from mo.graph.graph import Node, Graph
22 from mo.middle.replacement import MiddleReplacementPattern
25 class L2NormToNorm(MiddleReplacementPattern):
30 from extensions.middle.pass_separator import PreMiddleStart
31 return [PreMiddleStart]
34 from extensions.middle.pass_separator import MiddleStart
40 ('input', dict(kind='data')),
41 ('l2_normalize', dict(kind='op', op='Mul')),
42 ('l2_normalize_data', dict(kind='data')),
43 ('maximum', dict(kind='op', op='Maximum')),
44 ('maximum_data', dict(kind='data')),
45 ('maximum_y_data', dict(kind='data')),
46 ('rsqrt', dict(kind='op', op='Rsqrt')),
47 ('rsqrt_data', dict(kind='data')),
48 ('square', dict(kind='op', op='Square')),
49 ('square_data', dict(kind='data')),
50 ('sum', dict(kind='op', op='Reduce', reduce_type='sum')),
51 ('sum_data', dict(kind='data')),
55 ('square', 'square_data'),
56 ('square_data', 'sum'),
58 ('maximum_y_data', 'maximum'),
59 ('sum_data', 'maximum'),
60 ('maximum', 'maximum_data'),
61 ('maximum_data', 'rsqrt'),
62 ('rsqrt', 'rsqrt_data'),
63 ('rsqrt_data', 'l2_normalize'),
64 ('input', 'l2_normalize'),
65 ('l2_normalize', 'l2_normalize_data'),
69 def replace_pattern(self, graph: Graph, match: dict):
70 input_data_name = match['input'].node
71 output_data_name = match['l2_normalize_data'].node
73 if not match['maximum_y_data'].has_valid('value'):
75 if match['maximum_y_data'].value.shape != ():
77 y = match['maximum_y_data'].value
79 normalize_id = graph.unique_id()
80 graph.add_node(normalize_id,
82 dict(kind='op', precision="FP32", type='Normalize', name=str(graph.unique_id('normalize')),
83 op='Normalize', shape=None, eps=str(y), across_spatial=str(0), channel_shared=str(0),
84 data_type=None, infer=None, in_ports_count=2, out_ports_count=1)))
85 normalize_data_id = graph.unique_id()
87 graph.add_node(normalize_data_id, **add_attrs_props(graph.node[output_data_name]))
88 update_ie_fields(graph.node[normalize_id])
89 weights_id = graph.unique_id('weights_')
90 graph.add_node(weights_id, **add_attrs_props(
91 dict(kind='data', precision="FP32", name=weights_id, value=None, shape=None, data_type=None, infer=None)))
92 wnode = Node(graph, weights_id)
93 wnode['value'] = np.ones(shape=match['input'].shape[-1],
94 dtype=match['input'].data_type) # TODO feature dim instead of -1
95 wnode['shape'] = np.array(wnode['value'].shape)
96 output_edges = list(graph.out_edges(output_data_name, data=True))
97 graph.remove_edges_from([
98 (input_data_name, match['l2_normalize'].id),
99 (input_data_name, match['square'].id)
101 graph.remove_edges_from(list(graph.out_edges(output_data_name)))
102 graph.remove_node(output_data_name)
103 graph.add_edge(input_data_name, normalize_id, **{'in': 0})
104 graph.add_edge(weights_id, normalize_id, **{'in': 1, 'bin': 'weights'})
105 graph.add_edge(normalize_id, normalize_data_id, **{'out': 0})
106 for data, owner, attr in output_edges:
107 graph.add_edge(normalize_data_id, owner, **attr)