Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / src / cpu / ref_convolution.hpp
1 /*******************************************************************************
2 * Copyright 2016-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 CPU_REF_CONVOLUTION_HPP
18 #define CPU_REF_CONVOLUTION_HPP
19
20 #include <assert.h>
21
22 #include "c_types_map.hpp"
23 #include "cpu_convolution_pd.hpp"
24 #include "cpu_engine.hpp"
25 #include "type_helpers.hpp"
26 #include "utils.hpp"
27
28 namespace mkldnn {
29 namespace impl {
30 namespace cpu {
31
32 template <impl::data_type_t src_type,
33          impl::data_type_t wei_type = src_type,
34          impl::data_type_t dst_type = src_type,
35          impl::data_type_t acc_type = dst_type>
36 struct ref_convolution_fwd_t: public cpu_primitive_t {
37     struct pd_t: public cpu_convolution_fwd_pd_t {
38         pd_t(engine_t *engine,
39                 const convolution_desc_t *adesc,
40                 const primitive_attr_t *attr,
41                 const typename pd_t::base_class *hint_fwd_pd)
42             : cpu_convolution_fwd_pd_t(engine, adesc, attr, hint_fwd_pd)
43         {}
44
45         DECLARE_COMMON_PD_T("ref:any", ref_convolution_fwd_t);
46
47         virtual status_t init() override {
48             using namespace prop_kind;
49             using namespace data_type;
50             assert(this->engine()->kind() == engine_kind::cpu);
51             bool ok = true
52                 && this->set_default_params() == status::success
53                 && utils::one_of(this->desc()->prop_kind, forward_training,
54                         forward_inference)
55                 && utils::one_of(this->desc()->alg_kind,
56                            alg_kind::convolution_auto,
57                            alg_kind::convolution_direct)
58                 && this->desc()->src_desc.data_type == src_type
59                 && this->desc()->weights_desc.data_type == wei_type
60                 && this->desc()->accum_data_type == acc_type
61                 && this->desc()->dst_desc.data_type == dst_type
62                 && IMPLICATION(this->with_bias(), true
63                         && IMPLICATION(src_type == u8,
64                             utils::one_of(this->desc()->bias_desc.data_type,
65                                 f32, s32, s8, u8))
66                         && IMPLICATION(src_type == f32,
67                             this->desc()->bias_desc.data_type == f32))
68                 && this->attr()->has_default_values();
69             return ok ? status::success : status::unimplemented;
70         }
71     };
72
73     ref_convolution_fwd_t(const pd_t *apd, const input_vector &inputs,
74             const output_vector &outputs)
75         : cpu_primitive_t(apd, inputs, outputs) {}
76
77     typedef typename prec_traits<src_type>::type src_data_t;
78     typedef typename prec_traits<wei_type>::type wei_data_t;
79     typedef typename prec_traits<dst_type>::type dst_data_t;
80     typedef typename prec_traits<acc_type>::type acc_data_t;
81
82     virtual void execute(event_t *e) const {
83         switch (pd()->desc()->prop_kind) {
84         case prop_kind::forward_training:
85         case prop_kind::forward_inference:
86             execute_forward();
87             break;
88         default:
89             assert(!"invalid prop_kind");
90         }
91         e->set_state(event_t::ready);
92     }
93
94 private:
95     void execute_forward() const;
96     const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
97 };
98
99 template <impl::data_type_t diff_src_type, impl::data_type_t wei_type,
100          impl::data_type_t diff_dst_type,
101          impl::data_type_t acc_type = diff_src_type>
102 struct ref_convolution_bwd_data_t: public cpu_primitive_t {
103     struct pd_t: public cpu_convolution_bwd_data_pd_t {
104         pd_t(engine_t *engine,
105                 const convolution_desc_t *adesc,
106                 const primitive_attr_t *attr,
107                 const convolution_fwd_pd_t *hint_fwd_pd)
108             : cpu_convolution_bwd_data_pd_t(engine, adesc, attr, hint_fwd_pd)
109         {}
110
111         DECLARE_COMMON_PD_T("ref:any", ref_convolution_bwd_data_t);
112
113         virtual status_t init() override {
114             using namespace prop_kind;
115             assert(this->engine()->kind() == engine_kind::cpu);
116             bool ok = true
117                 && this->set_default_params() == status::success
118                 && this->desc()->prop_kind == backward_data
119                 && utils::one_of(this->desc()->alg_kind,
120                            alg_kind::convolution_auto,
121                            alg_kind::convolution_direct)
122                 && this->desc()->diff_dst_desc.data_type == diff_dst_type
123                 && this->desc()->weights_desc.data_type == wei_type
124                 && this->desc()->accum_data_type == acc_type
125                 && this->desc()->diff_src_desc.data_type == diff_src_type
126                 && this->attr()->has_default_values();
127             return ok ? status::success : status::unimplemented;
128         }
129
130         virtual bool support_bias() const override { return true; }
131     };
132
133     ref_convolution_bwd_data_t(const pd_t *apd, const input_vector &inputs,
134             const output_vector &outputs)
135         : cpu_primitive_t(apd, inputs, outputs) {}
136
137     typedef typename prec_traits<diff_src_type>::type diff_src_data_t;
138     typedef typename prec_traits<wei_type>::type wei_data_t;
139     typedef typename prec_traits<diff_dst_type>::type diff_dst_data_t;
140     typedef typename prec_traits<acc_type>::type acc_data_t;
141
142     virtual void execute(event_t *e) const {
143         switch (pd()->desc()->prop_kind) {
144         case prop_kind::backward_data:
145             execute_backward_data();
146             break;
147         default:
148             assert(!"invalid prop_kind");
149         }
150         e->set_state(event_t::ready);
151     }
152
153 private:
154     void execute_backward_data() const;
155     const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
156 };
157
158 template <impl::data_type_t src_type, impl::data_type_t diff_wei_type,
159          impl::data_type_t diff_dst_type,
160          impl::data_type_t acc_type = diff_wei_type>
161 struct ref_convolution_bwd_weights_t: public cpu_primitive_t {
162     struct pd_t: public cpu_convolution_bwd_weights_pd_t {
163         pd_t(engine_t *engine,
164                 const convolution_desc_t *adesc,
165                 const primitive_attr_t *attr,
166                 const convolution_fwd_pd_t *hint_fwd_pd)
167             : cpu_convolution_bwd_weights_pd_t(engine, adesc, attr, hint_fwd_pd)
168         {}
169
170         DECLARE_COMMON_PD_T("ref:any", ref_convolution_bwd_weights_t);
171
172         virtual status_t init() override {
173             using namespace prop_kind;
174             assert(this->engine()->kind() == engine_kind::cpu);
175             bool ok = true
176                 && this->set_default_params() == status::success
177                 && this->desc()->prop_kind == backward_weights
178                 && utils::one_of(this->desc()->alg_kind,
179                            alg_kind::convolution_auto,
180                            alg_kind::convolution_direct)
181                 && this->desc()->src_desc.data_type == src_type
182                 && this->desc()->diff_weights_desc.data_type == diff_wei_type
183                 && this->desc()->diff_dst_desc.data_type == diff_dst_type
184                 && this->desc()->accum_data_type == acc_type
185                 && IMPLICATION(this->with_bias(),
186                         this->desc()->diff_bias_desc.data_type
187                         == diff_wei_type)
188                 && this->attr()->has_default_values();
189             return ok ? status::success : status::unimplemented;
190         }
191     };
192
193     ref_convolution_bwd_weights_t(const pd_t *apd, const input_vector &inputs,
194             const output_vector &outputs)
195         : cpu_primitive_t(apd, inputs, outputs) {}
196
197     typedef typename prec_traits<src_type>::type src_data_t;
198     typedef typename prec_traits<diff_wei_type>::type diff_wei_data_t;
199     typedef typename prec_traits<diff_dst_type>::type diff_dst_data_t;
200     typedef typename prec_traits<acc_type>::type acc_data_t;
201
202     virtual void execute(event_t *e) const {
203         switch (pd()->desc()->prop_kind) {
204         case prop_kind::backward_weights:
205             execute_backward_weights();
206             break;
207         default:
208             assert(!"invalid prop_kind");
209         }
210         e->set_state(event_t::ready);
211     }
212
213 private:
214     void execute_backward_weights() const;
215     const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
216 };
217
218 }
219 }
220 }
221
222 #endif
223
224 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s