Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / front / kaldi / extractors / lstm_projected_streams_ext.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 from extensions.ops.lstm_cell import LSTMCell
17 from mo.front.caffe.extractors.utils import embed_input
18 from mo.front.extractor import FrontExtractorOp
19 from mo.front.kaldi.loader.utils import collect_until_token, collect_until_whitespace, get_uint32
20 from mo.front.kaldi.utils import read_binary_matrix, read_binary_vector
21
22
23 class LSTMProjectedStreamsFrontExtractor(FrontExtractorOp):
24     op = 'lstmprojectedstreams'
25     enabled = True
26
27     @staticmethod
28     def extract(node):
29         clip_value = 50
30         pb = node.parameters
31         res = collect_until_whitespace(pb)
32         if res == b'<CellClip>':
33             clip_value = get_uint32(pb.read(4))
34         collect_until_token(pb, b'FM')
35         gifo_x_weights, gifo_x_weights_shape = read_binary_matrix(pb, False)
36         gifo_r_weights, gifo_r_weights_shape = read_binary_matrix(pb)
37         gifo_biases = read_binary_vector(pb)
38         input_gate_weights = read_binary_vector(pb)
39         forget_gate_weights = read_binary_vector(pb)
40         output_gate_weights = read_binary_vector(pb)
41
42         projection_weights, projection_weights_shape = read_binary_matrix(pb)
43
44         mapping_rule = {'gifo_x_weights_shape': gifo_x_weights_shape,
45                         'gifo_r_weights_shape': gifo_r_weights_shape,
46                         'projection_weights_shape': projection_weights_shape,
47                         'clip_value': clip_value
48                         }
49
50         embed_input(mapping_rule, 1, 'gifo_x_weights', gifo_x_weights)
51         embed_input(mapping_rule, 2, 'gifo_r_weights', gifo_r_weights)
52         embed_input(mapping_rule, 3, 'gifo_biases', gifo_biases)
53         embed_input(mapping_rule, 4, 'input_gate_weights', input_gate_weights)
54         embed_input(mapping_rule, 5, 'forget_gate_weights', forget_gate_weights)
55         embed_input(mapping_rule, 6, 'output_gate_weights', output_gate_weights)
56         embed_input(mapping_rule, 7, 'projection_weights', projection_weights)
57
58         LSTMCell.update_node_stat(node, mapping_rule)
59         return __class__.enabled
60
61
62 class LSTMProjectedFrontExtractor(FrontExtractorOp):
63     op = 'lstmprojected'
64     enabled = True
65
66     @staticmethod
67     def extract(node):
68         return LSTMProjectedStreamsFrontExtractor.extract(node)