Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / front / squared_difference.py
1 """
2  Copyright (c) 2017-2019 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
17 import networkx as nx
18
19 from mo.front.common.replacement import FrontReplacementOp
20 from mo.graph.graph import Node, Graph
21 from mo.ops.eltwise import Eltwise
22 from mo.ops.power import Power
23
24
25 class SquaredDifference(FrontReplacementOp):
26     """
27     Example class illustrating how to implement replacement of a single op in the front-end of the MO pipeline.
28     This class replaces a single op "SquaredDifference" by a sub-graph consisting of 3 lower-level ops.
29     """
30
31     op = "SquaredDifference"
32     enabled = True
33
34     def replace_op(self, graph: Graph, node: Node):
35         negate = Power(graph, dict(scale=-1, name=node.name + '/negate_'))
36         add = Eltwise(graph, dict(operation='sum', name=node.name + '/add_'))
37         squared = Power(graph, dict(power=2, name=node.name + '/squared_'))
38
39         out_node = squared.create_node([add.create_node([node.in_node(0), negate.create_node([node.in_node(1)])])])
40         # Replace edge from out port 0 of the matched node with a edge from node out_node.id with port 0.
41         # The "explicit" version of the return value is: [(out_node.id, 0)])
42         return [out_node.id]