1 /*******************************************************************************
2 * Copyright 2018 Intel Corporation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *******************************************************************************/
25 #include "dnn_types.hpp"
26 #include "mkldnn_common.hpp"
27 #include "mkldnn_debug.hpp"
28 #include "mkldnn_memory.hpp"
32 enum alg_t { VANILLA_RNN, VANILLA_LSTM, VANILLA_GRU, LBR_GRU };
33 alg_t str2alg(const char *str);
34 const char *alg2str(alg_t alg);
35 mkldnn_alg_kind_t alg2kind(alg_t alg);
37 enum activation_t { RELU, LOGISTIC, TANH };
38 activation_t str2activation(const char *str);
39 const char *activation2str(activation_t alg);
40 mkldnn_alg_kind_t activation2kind(activation_t alg);
42 mkldnn_rnn_direction_t str2direction(const char *str);
43 const char *direction2str(mkldnn_rnn_direction_t direction);
48 template <typename Telem>
49 struct array_offset_calculator {
50 template <typename... Targs>
51 array_offset_calculator(Telem *base, Targs... Fargs)
52 : _size(sizeof...(Fargs)) {
53 const int init_list[] = { Fargs... };
54 _dims = new int[_size];
55 for (int i = 0; i < _size; ++i)
56 _dims[i] = init_list[i];
60 ~array_offset_calculator() { delete[] _dims; }
61 template <typename... Targs>
62 inline Telem &operator()(Targs... Fargs) {
63 return *(_base_ptr + _offset(1, Fargs...));
67 template <typename... Targs>
68 inline int _offset(int const dimension, int element) {
72 template <typename... Targs>
73 inline int _offset(int const dimension, int theta, int element) {
74 return element + (_dims[dimension] * theta);
77 template <typename... Targs>
79 int const dimension, int theta, int element, Targs... Fargs) {
80 int t_prime = element + (_dims[dimension] * theta);
81 return _offset(dimension + 1, t_prime, Fargs...);
99 int str2desc(rnn_desc_t *desc, const char *str);
101 enum rnn_data_kind_t {
111 dst_diff_weights_input,
112 dst_diff_weights_states,
116 data_kind_total // should be last to provide the total number of data kinds
119 inline const char *rnn_data_kind2str(rnn_data_kind_t kind) {
121 case input: return "INPUT";
122 case states: return "STATES";
123 case weights_input: return "WEIGHTS_INPUT";
124 case weights_states: return "WEIGHTS_STATES";
125 case bias: return "BIAS";
126 case dst_last_layer: return "DST_LAST_LAYER";
127 case dst_last_iteration: return "DST_LAST_ITERATION";
129 assert(!"incorrect rnn data kind");
130 return "incorrect rnn data kind";
134 /** configuration structure, that controls initial data filling + error check
136 * dt defines precision
138 * for each lst data kind the values are filled as follows:
139 * if (rand() > f_sparsity) then:
142 * v <-- f_min + rand() * f_step % (f_max - f_min)
144 * on final check the resulting values should be in [min .. max] range, the
145 * relative difference should not exceed eps
148 typedef struct dt_conf_t {
149 mkldnn_data_type_t dt;
150 int min, max; /* representative */
151 int f_min, f_max; /* fill range */
152 int f_base; /* fill base, use 0 */
153 int f_step; /* fill step, use 1 */
154 double f_sparsity; /* amount of non-zeros, default 0.25 */
155 double eps; /* acceptable error */
156 } _dt_conf_t[data_kind_total];
158 extern const _dt_conf_t conf_f32;
160 struct rnn_prb_t : public rnn_desc_t {
161 rnn_prb_t(const rnn_desc_t desc, const dt_conf_t *cfg,
162 mkldnn_prop_kind_t prop, alg_t alg,
163 mkldnn_rnn_direction_t direction, activation_t activation)
164 : rnn_desc_t(desc), cfg(cfg), prop(prop), alg(alg),
165 direction(direction), activation(activation){
168 int n_directions() const {
169 return (direction == mkldnn_bidirectional_concat
170 || direction == mkldnn_bidirectional_sum) ?
174 int n_weights() const { return 1; }
175 int n_states() const { return alg == VANILLA_LSTM ? 2 : 1; }
176 int n_gates() const {
177 return alg == VANILLA_LSTM ?
179 (alg == VANILLA_GRU || alg == LBR_GRU ? 3 : 1);
182 const dt_conf_t *cfg;
183 mkldnn_prop_kind_t prop;
185 mkldnn_rnn_direction_t direction;
186 activation_t activation;
189 rnn_prb_t(const rnn_prb_t &) = delete;
190 rnn_prb_t &operator=(const rnn_prb_t &) = delete;
193 const size_t max_prb_len = 392;
194 void prb2str(const rnn_prb_t *p, const res_t *res, char *buffer);
196 void compute_ref_fwd(const rnn_prb_t *p, dnn_mem_t &input_m,
197 dnn_mem_t &states_m, dnn_mem_t &weights_input_m,
198 dnn_mem_t &weights_states_m, dnn_mem_t &bias_m,
199 dnn_mem_t &dst_last_layer_m, dnn_mem_t &dst_last_iteration_m,
200 mkldnn_rnn_direction_t direction);
202 void compute_ref_bwd(const rnn_prb_t *p, dnn_mem_t &input_m,
203 dnn_mem_t &states_m, dnn_mem_t &diff_last_layer_m,
204 dnn_mem_t &diff_last_iteration_m, dnn_mem_t &weights_input_m,
205 dnn_mem_t &weights_states_m, dnn_mem_t &bias_m,
206 dnn_mem_t &dst_last_layer_m, dnn_mem_t &dst_last_iteration_m,
207 dnn_mem_t &dst_diff_input_m, dnn_mem_t &dst_diff_states_m,
208 dnn_mem_t &dst_diff_weights_input_m,
209 dnn_mem_t &dst_diff_weights_states_m, dnn_mem_t &dst_diff_bias_m,
210 mkldnn_rnn_direction_t direction);
213 inline size_t ntc_off_f(const rnn_prb_t *p, int n, int t, int c) {
214 return ((size_t)n * p->n_iter + t) * p->slc + c;
217 inline void inv_ntc_off_f(
218 const rnn_prb_t *p, size_t off, int &n, int &t, int &c) {
229 inline size_t ldsnc_off_f(
230 const rnn_prb_t *p, int l, int d, int s, int n, int c) {
231 return ((((size_t)l * p->n_directions() + d) * p->n_states() + s) * p->mb
237 inline void inv_ldsnc_off_f(const rnn_prb_t *p, size_t off, int &l, int &d,
238 int &s, int &n, int &c) {
243 s = off % p->n_states();
244 off /= p->n_states();
245 d = off % p->n_directions();
246 off /= p->n_directions();
247 l = off % p->n_layer;
253 inline size_t ldigo_off_f(
254 const rnn_prb_t *p, int l, int d, int w, int ic, int oc) {
255 return ((((size_t)l * p->n_directions() + d) * p->n_weights() + w)
262 inline void inv_ldigo_off_f(const rnn_prb_t *p, size_t off, int &l, int &d,
263 int &w, int &ic, int &oc) {
266 ic = off % (4 * p->slc);
268 w = off % p->n_weights();
269 off /= p->n_weights();
270 d = off % p->n_directions();
271 off /= p->n_directions();
272 l = off % p->n_layer;
278 inline size_t ldwOcIc_off_f(
279 const rnn_prb_t *p, int l, int d, int w, int oc, int ic) {
280 return ((((size_t)l * p->n_directions() + d) * p->n_weights() + w)
287 inline void inv_ldwOcIc_off_f(const rnn_prb_t *p, size_t off, int &l, int &d,
288 int &w, int &oc, int &ic) {
291 oc = off % (4 * p->sic);
293 w = off % p->n_weights();
294 off /= p->n_weights();
295 d = off % p->n_directions();
296 off /= p->n_directions();
297 l = off % p->n_layer;
303 inline size_t ldgo_off_f(const rnn_prb_t *p, int l, int d, int b, int c) {
304 return (((size_t)l * p->n_directions() + d) * p->n_gates() + b) * p->sic
308 inline void inv_ldgo_off_f(
309 const rnn_prb_t *p, size_t off, int &l, int &d, int &b, int &c) {
312 b = off % p->n_gates();
314 d = off % p->n_directions();
315 off /= p->n_directions();
316 l = off % p->n_layer;
321 // dst_last_layer: mkldnn_tnc
322 inline size_t tnc_off_f(const rnn_prb_t *p, int s, int t, int n, int c) {
323 return (((size_t)s * p->n_iter + t) * p->mb + n) * p->sic + c;
326 inline void inv_tnc_off_f(
327 const rnn_prb_t *p, size_t off, int &s, int &t, int &n, int &c) {
334 s = off % p->n_states();
335 off /= p->n_states();
339 void perf_report(const rnn_prb_t *p, const res_t *r, const char *pstr);
341 int doit(const rnn_prb_t *p, res_t *res);
342 void check(rnn_desc_t *p);
343 int bench(int argc, char **argv, bool main_bench = true);