2 Copyright (c) 2018-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.
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
27 class ReplaceSpliceNodePattern(FrontReplacementOp):
29 This pass decomposes Splice layer to the sequence Slice Concat and Memory layers
31 Let's suppose we have next graph:
33 Input (N, H) -> Slice -> Next_Layer (N, k*H)
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
38 So this pass will convert this graph to the next one:
44 Memory [N, k*H] -> Slice [N, (k-1)*H] Memory [N, k*H]
50 def replace_op(self, graph: Graph, node: Node):
51 input_node = node.in_nodes()[0]
52 memory_pair_id = unique_id('id')
54 input_memory = Memory(graph, {'name': 'prev_splice_memory',
58 'shape': np.array(([input_node.shape[1] * len(node.context)]),
59 dtype=np.int64)}).create_node()
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])
72 concat_node = Concat(graph, {'name': 'Splice_Concat',
74 'axis': 1}).create_node([crop, input_node])
76 # Concat -> Memory(out)
77 Memory(graph, {'name': 'out_splice_memory',
81 'shape': np.array([input_node.shape[1] * len(node.context)],
82 dtype=np.int64)}).create_node([concat_node])
83 return [concat_node.id]