Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / src / cpu / ref_depthwise.cpp
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 #include <assert.h>
18 #include <math.h>
19
20 #include "c_types_map.hpp"
21 #include "type_helpers.hpp"
22 #include "mkldnn_thread.hpp"
23
24 #include "ref_depthwise.hpp"
25
26 namespace mkldnn {
27 namespace impl {
28 namespace cpu {
29
30 using namespace alg_kind;
31
32 template <typename T> inline T scale_shift_fwd(T s_val, T w_val, T b_val) {
33     return s_val*w_val + b_val;
34 }
35
36 template <typename T> inline T prelu_fwd(T s_val, T w_val) {
37     return s_val >= 0 ? s_val : s_val*w_val;
38 }
39
40 ref_depthwise_scalar_fwd_t::ref_depthwise_scalar_fwd_t(const alg_kind_t alg_)
41         : alg(alg_) {
42     using namespace alg_kind;
43
44     assert(utils::one_of(alg, depthwise_scale_shift, depthwise_prelu));
45 }
46
47 float ref_depthwise_scalar_fwd_t::compute_scalar(float s, const float* weights, const float* bias) {
48     switch (alg) {
49         case depthwise_scale_shift: return scale_shift_fwd(s, *weights, *bias);
50         case depthwise_prelu: return prelu_fwd(s, *weights);
51         default: assert(!"unknown depthwise alg_kind");
52     }
53
54     return 0.0f;
55 }
56
57 template <impl::data_type_t data_type>
58 void ref_depthwise_fwd_t<data_type>::execute_forward() const {
59     auto src = reinterpret_cast<const data_t *>(this->input_memory(0));
60     auto weights = reinterpret_cast<const data_t *>(this->input_memory(1));
61     auto bias = reinterpret_cast<const data_t *>(this->input_memory(2));
62     auto dst = reinterpret_cast<data_t *>(this->memory());
63
64     const memory_desc_wrapper data_d(pd()->src_pd());
65     const memory_desc_wrapper weights_d(pd()->weights_pd(0));
66     const memory_desc_wrapper bias_d(pd()->weights_pd(1));
67
68     const int MB = pd()->MB();
69     const int C = pd()->C();
70     const int D = pd()->D();
71     const int H = pd()->H();
72     const int W = pd()->W();
73     const auto alg_kind = pd()->desc()->alg_kind;
74
75     parallel_nd(MB, C, D, H, W,
76         [&](int n, int c, int d, int h, int w) {
77         size_t data_off = data_d.ndims() == 4
78                         ? data_d.off(n, c, h, w)
79                         : data_d.ndims() == 5
80                             ? data_d.off(n, c, d, h, w)
81                             : data_d.off(n, c);
82
83         data_t s_val = src[data_off];
84         data_t w_val = weights[weights_d.off(c)];
85         data_t b_val = bias ? bias[bias_d.off(c)] : (data_t)0;
86         data_t &d_val = dst[data_off];
87
88         switch (alg_kind) {
89             case depthwise_scale_shift: d_val = scale_shift_fwd(s_val, w_val, b_val); break;
90             case depthwise_prelu: d_val = prelu_fwd(s_val, w_val); break;
91             default: assert(!"unknown depthwise alg_kind");
92         }
93     });
94 }
95
96 template struct ref_depthwise_fwd_t<data_type::f32>;
97
98 }
99 }
100 }
101
102 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s