Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / front / kaldi / replace_splice_node_pattern.py
1 """
2  Copyright (c) 2018-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 import numpy as np
17
18 from extensions.front.kaldi.replace_lstm_node_pattern import unique_id
19 from mo.front.common.partial_infer.utils import int64_array
20 from mo.front.common.replacement import FrontReplacementOp
21 from mo.graph.graph import Node, Graph
22 from mo.ops.concat import Concat
23 from mo.ops.crop import Crop
24 from mo.ops.memory import Memory
25
26
27 class ReplaceSpliceNodePattern(FrontReplacementOp):
28     """
29        This pass decomposes Splice layer to the sequence Slice Concat and Memory layers
30        For example:
31            Let's suppose we have next graph:
32
33            Input (N, H) -> Slice -> Next_Layer (N, k*H)
34
35            Where (N, k*H) is is real input of subsequent topology.
36            Splice is used for accumulation next (k-1)/2 and previous (k-1)/2 input data
37
38            So this pass will convert this graph to the next one:
39
40                                     Input [N, H]                  __
41                                                 \               /
42                                                  Concat [N, k*H]
43                                                 /               \
44            Memory [N, k*H] -> Slice [N, (k-1)*H]                 Memory [N, k*H]
45
46    """
47     op = "Splice"
48     enabled = True
49
50     def replace_op(self, graph: Graph, node: Node):
51         input_node = node.in_nodes()[0]
52         memory_pair_id = unique_id('id')
53         # Memory(in)
54         input_memory = Memory(graph, {'name': 'prev_splice_memory',
55                                       'id': memory_pair_id,
56                                       'index': 1,
57                                       'size': 2,
58                                       'shape': np.array(([input_node.shape[1] * len(node.context)]),
59                                                         dtype=np.int64)}).create_node()
60         # Memory(in)  \
61         #             Crop
62         # Input(temp) /
63         crop = Crop(graph, {'name': 'Splice_Crop',
64                             'axis': np.array([1], dtype=np.int64),
65                             'offset': np.array([input_node.shape[1]], dtype=np.int64),
66                             'dim': np.array([input_node.shape[1] * (len(node.context) - 1)],
67                                             dtype=np.int64)}).create_node([input_memory])
68
69         # Crop   \
70         #         Concat
71         # Input  /
72         concat_node = Concat(graph, {'name': 'Splice_Concat',
73                                      'in_ports_count': 2,
74                                      'axis': 1}).create_node([crop, input_node])
75
76         # Concat -> Memory(out)
77         Memory(graph, {'name': 'out_splice_memory',
78                        'id': memory_pair_id,
79                        'index': 0,
80                        'size': 2,
81                        'shape': np.array([input_node.shape[1] * len(node.context)],
82                                          dtype=np.int64)}).create_node([concat_node])
83         return [concat_node.id]