Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / ops / GRU.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 extensions.ops.RNN import rnn_infer
17 from mo.graph.graph import Node, Graph
18 from mo.ops.op import Op
19 import numpy as np
20
21
22 class GRU(Op):
23     op = 'GRU'
24
25     def __init__(self, graph: Graph, attrs: dict):
26         mandatory_props = {
27             'type': 'RNNSequence',  # should be never emitted to IR; for debugging purposes
28             'op': __class__.op,
29             'blobs_wrb': False,
30             'has_num_directions': False,
31             'direction': 'forward',
32             'infer': __class__.infer,
33             'multiplier': 3,
34             'multilayers': False,
35             'gate_order': np.array([0, 1, 2]),  # TODO: change it later
36             'normalized': False,
37
38             'activation_alpha': None,
39             'activation_beta': None,
40             'activations': None,
41             'clip': None,
42             'linear_before_reset': None,
43             'in_ports_count': 6,
44             'out_ports_count': 2,
45         }
46         super().__init__(graph, mandatory_props, attrs)
47
48     @staticmethod
49     def supported_attrs():
50         return [
51             'hidden_size',  # number of the elements in hidden cell size
52             'direction',  # one of 'forward', 'reverse', or 'bidirectional'
53             'axis',
54
55             'activation_alpha',
56             'activation_beta',
57             'activations',
58             'clip',
59             'linear_before_reset',
60         ]
61
62     def backend_attrs(self):
63         return [
64             'hidden_size',  # number of the elements in hidden cell size
65             'direction',  # one of 'forward', 'reverse', or 'bidirectional'
66             'axis',
67
68             'activation_alpha',
69             'activation_beta',
70             ('activations', lambda node: ','.join(node.activations) if node.activations is not None else None),
71             'clip',
72             'linear_before_reset',
73         ]
74
75     @staticmethod
76     def infer(node: Node):
77         assert len(node.in_nodes()) >= 3  # X, W and R
78         assert len(node.in_nodes()) <= 5
79         assert len(node.out_nodes()) <= 2
80
81         rnn_infer(node, [1])