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 *******************************************************************************/
20 #include "c_types_map.hpp"
21 #include "type_helpers.hpp"
22 #include "mkldnn_thread.hpp"
24 #include "ref_depthwise.hpp"
30 using namespace alg_kind;
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;
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;
40 ref_depthwise_scalar_fwd_t::ref_depthwise_scalar_fwd_t(const alg_kind_t alg_)
42 using namespace alg_kind;
44 assert(utils::one_of(alg, depthwise_scale_shift, depthwise_prelu));
47 float ref_depthwise_scalar_fwd_t::compute_scalar(float s, const float* weights, const float* bias) {
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");
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());
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));
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;
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)
80 ? data_d.off(n, c, d, h, w)
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];
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");
96 template struct ref_depthwise_fwd_t<data_type::f32>;
102 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s