Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / src / common / rnn_pd.hpp
1 /*******************************************************************************
2 * Copyright 2018 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 #ifndef RNN_PD_HPP
18 #define RNN_PD_HPP
19
20 #include "mkldnn.h"
21
22 #include "c_types_map.hpp"
23 #include "memory_pd.hpp"
24 #include "primitive_desc.hpp"
25
26 namespace mkldnn {
27 namespace impl {
28
29 // struct rnn_fwd_pd_t;
30
31 struct rnn_pd_t : public primitive_desc_t {
32     static constexpr auto base_pkind = primitive_kind::rnn;
33
34     rnn_pd_t(mkldnn::impl::engine_t *engine, const rnn_desc_t *adesc,
35             const primitive_attr_t *attr, const rnn_pd_t *hint_pd)
36         : primitive_desc_t(engine, attr, primitive_kind::rnn)
37         , desc_(*adesc)
38         , hint_pd_(hint_pd) {}
39     virtual ~rnn_pd_t() {}
40
41     const rnn_desc_t *desc() const { return &desc_; }
42     virtual const op_desc_t *op_desc() const override {
43         return reinterpret_cast<const op_desc_t *>(this->desc());
44     }
45     virtual void init_info() override { init_info_rnn(this, this->info_); }
46
47     virtual status_t query(query_t what, int idx, void *result) const override {
48         switch (what) {
49         case query::rnn_d: *(const rnn_desc_t **)result = desc(); break;
50         default: return primitive_desc_t::query(what, idx, result);
51         }
52         return status::success;
53     }
54
55     inline bool is_training() const {
56         return utils::one_of(desc_.prop_kind, prop_kind::forward_training,
57                 prop_kind::backward);
58     }
59
60     inline bool is_fwd() const {
61         return utils::one_of(desc_.prop_kind, prop_kind::forward_training,
62                 prop_kind::forward_inference);
63     }
64
65     int T() const { return desc_.src_layer_desc.dims[0]; }
66     int MB() const { return desc_.src_layer_desc.dims[1]; }
67
68     int L() const { return desc_.weights_layer_desc.dims[0]; }
69     int D() const { return desc_.weights_layer_desc.dims[1]; }
70
71     int SIC() const { return desc_.weights_iter_desc.dims[2]; }
72
73     int SLC() const { return desc_.weights_layer_desc.dims[2]; }
74     int G() const { return desc_.weights_layer_desc.dims[3]; }
75     int DIC() const { return desc_.weights_layer_desc.dims[4]; }
76
77     int DLC() const { return desc_.dst_layer_desc.dims[2]; }
78
79     bool with_bias() const {
80         return !memory_desc_wrapper(desc_.bias_desc).is_zero();
81     }
82
83     bool with_src_iter() const {
84         return !(memory_desc_wrapper(desc_.src_iter_desc).is_zero());
85     }
86
87     bool with_dst_iter() const {
88         return !memory_desc_wrapper(desc_.dst_iter_desc).is_zero();
89     }
90
91     mkldnn::impl::alg_kind_t cell_kind() const {
92         return desc_.cell_desc.cell_kind;
93     }
94     mkldnn::impl::alg_kind_t activation_kind() const {
95         return desc_.cell_desc.activation_kind;
96     }
97
98     bool is_lbr() const {
99         return cell_kind() == mkldnn_gru_linear_before_reset;
100     }
101
102     mkldnn_rnn_direction_t direction() const { return desc_.direction; }
103
104 protected:
105     rnn_desc_t desc_;
106     const rnn_pd_t *hint_pd_;
107 };
108
109 struct rnn_fwd_pd_t : public rnn_pd_t {
110     typedef rnn_fwd_pd_t base_class;
111     typedef rnn_fwd_pd_t hint_class;
112
113     using rnn_pd_t::rnn_pd_t;
114     virtual ~rnn_fwd_pd_t() {}
115
116     virtual const memory_pd_t *input_pd(int index = 0) const override {
117         if (index == 0) return src_pd(0);
118         if (with_src_iter() && index == 1) return src_pd(1);
119         index = index - 1 - with_src_iter();
120
121         if (index < 3) return weights_pd(index);
122
123         return nullptr;
124     }
125
126     virtual const memory_pd_t *output_pd(int index = 0) const override {
127         if (index == 0) return dst_pd(0);
128         if (with_dst_iter() && index == 1) return dst_pd(1);
129         index = index - 1 - with_dst_iter();
130
131         if (is_training() && index == 0) return workspace_pd();
132
133         return nullptr;
134     }
135
136     virtual int n_inputs() const override {
137         return 3 + with_bias() + with_src_iter();
138     }
139
140     virtual int n_outputs() const override {
141         return 1 + with_dst_iter() + is_training();
142     }
143
144     int ws_idx() const { return 1 + with_dst_iter(); }
145 };
146
147 struct rnn_bwd_pd_t : public rnn_pd_t {
148     typedef rnn_bwd_pd_t base_class;
149     typedef rnn_fwd_pd_t hint_class;
150
151     using rnn_pd_t::rnn_pd_t;
152     virtual ~rnn_bwd_pd_t() {}
153
154     virtual const memory_pd_t *input_pd(int index = 0) const override {
155         if (index == 0) return src_pd(0);
156         if (with_src_iter() && index == 1) return src_pd(1);
157         index = index - 1 - with_src_iter();
158
159         if (index < 2) return weights_pd(index);
160         if (with_bias() && index == 2) return weights_pd(2);
161         index = index - 2 - with_bias();
162
163         if (index == 0) return dst_pd(0);
164         if (with_dst_iter() && index == 1) return dst_pd(1);
165         index = index - 1 - with_dst_iter();
166
167         if (index == 0) return diff_dst_pd(0);
168         if (with_dst_iter() && index == 1) return diff_dst_pd(1);
169         index = index - 1 - with_dst_iter();
170
171         if (index == 0) return workspace_pd();
172
173         return nullptr;
174     }
175
176     virtual const memory_pd_t *output_pd(int index = 0) const override {
177         if (index == 0) return diff_src_pd(0);
178         if (with_src_iter() && index == 1) return diff_src_pd(1);
179         index = index - 1 - with_src_iter();
180
181         if (index < 3) return diff_weights_pd(index);
182
183         return nullptr;
184     }
185
186     virtual int n_inputs() const override {
187         return 6 + with_src_iter() + with_bias() + 2 * with_dst_iter();
188     }
189     virtual int n_outputs() const override {
190         return 3 + with_src_iter() + with_bias();
191     }
192
193     int ws_idx() const {
194         return 5 + with_src_iter() + with_bias() + 2 * with_dst_iter();
195     }
196 };
197 }
198 }
199
200 #endif