Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / ops / GRUCell.py
1 """
2  Copyright (c) 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 mo.front.common.partial_infer.utils import mark_input_bins
17 from mo.graph.graph import Node, Graph
18 from mo.ops.op import Op
19 from mo.utils.error import Error
20
21
22 class GRUCell(Op):
23     """ A single GRU cell (without a loop).
24
25         2 inputs:
26             - [0, required] input data (2D),
27             - [1, required] initial hidden state (2D),
28
29         2 blobs:
30             - [2, required] cell FC weights
31             - [3, required] cell FC biases
32
33         1 outputs:
34             - [required] output data / resulting hidden state (2D)
35     """
36     op = 'GRUCell'
37
38     def __init__(self, graph: Graph, attrs: dict):
39         mandatory_props = {
40             'type': __class__.op,
41             'op': __class__.op,
42             'infer': __class__.infer,
43             'in_ports_count': 4,
44             'out_ports_count': 1,
45         }
46         super().__init__(graph, mandatory_props, attrs)
47
48     def supported_attrs(self):
49         return [
50             'hidden_size',  # number of the elements in hidden cell size
51             'activations',
52             'activation_alpha',
53             'activation_beta',
54             'clip',
55             'linear_before_reset',
56         ]
57
58     def backend_attrs(self):
59         return [
60             'hidden_size',  # number of the elements in hidden cell size
61             ('activations', lambda node: ','.join(node.activations) if node.activations is not None else None),
62             'activation_alpha',
63             'activation_beta',
64             'clip',
65             'linear_before_reset',
66         ]
67
68     @staticmethod
69     def infer(node: Node):
70         assert len(node.out_nodes()) in [1, 2]
71
72         hidden_shape = node.in_node(1).shape.copy()
73
74         mark_input_bins(node, start_port=2)
75         node.out_node(0).shape = hidden_shape
76
77         hidden_size = hidden_shape[1]
78         if node.has_valid('hidden_size'):
79             if node.hidden_size != hidden_size:
80                 raise Error("Input shape {} for hidden size doesn't match pre-defined hidden_size in node {}".format(
81                     node.in_node(1).shape, node.soft_get('name')))
82         else:
83             node['hidden_size'] = hidden_size