Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / front / kaldi / extractor.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
17 from mo.graph.graph import Node
18 from mo.front.common.partial_infer.elemental import copy_shape_infer, single_output_infer
19 from mo.utils.error import Error
20 from mo.utils.utils import refer_to_faq_msg
21
22
23 def node_pb_arg(pb_extractor):
24     return lambda node: pb_extractor(node.parameters)
25
26
27 kaldi_type_extractors = {}
28
29
30 def common_kaldi_fields(node: Node) -> dict:
31     layer_type = node.op
32     return {
33         'kind': 'op',
34         'name': node.id,
35         'op': layer_type,
36         # generic code relies on op; it should be overridden by specific op extractor
37         'infer': None,
38         'precision': 'FP32'
39     }
40
41
42 def kaldi_extractor(node: Node) -> (bool, dict):
43     result = common_kaldi_fields(node)
44     layer_type = result['op']
45     if layer_type not in kaldi_type_extractors:
46         raise Error('Found unsupported layer {}. '.format(node.id) +
47                     'Model Optimizer does not support this layer type: {}. '.format(layer_type) +
48                     'Please, implement extension. ' +
49                     refer_to_faq_msg(45))
50
51     result.update(kaldi_type_extractors[layer_type](node))
52     supported = True
53
54     return supported, result