updated readme file due to moving CMake scripts to the root folder
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / src / cpu / gemm_x8s8s32x_convolution.hpp
1 /*******************************************************************************
2 * Copyright 2017-2019 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 GEMM_X8S8S32X_CONVOLUTION_HPP
18 #define GEMM_X8S8S32X_CONVOLUTION_HPP
19
20 #include "c_types_map.hpp"
21 #include "memory_tracking.hpp"
22
23 #include "cpu_convolution_pd.hpp"
24 #include "cpu_engine.hpp"
25 #include "jit_primitive_conf.hpp"
26 #include "jit_generator.hpp"
27 #include "gemm_convolution_utils.hpp"
28
29 #include "gemm/gemm.hpp"
30
31 namespace mkldnn {
32 namespace impl {
33 namespace cpu {
34
35 template <data_type_t src_type, data_type_t dst_type>
36 struct _gemm_x8s8s32x_convolution_fwd_t: public cpu_primitive_t {
37     struct pd_t: public cpu_convolution_fwd_pd_t {
38         pd_t(engine_t *engine, const convolution_desc_t *adesc,
39                 const primitive_attr_t *attr,
40                 const typename pd_t::base_class *hint_fwd_pd)
41             : cpu_convolution_fwd_pd_t(engine, adesc, attr, hint_fwd_pd)
42             , jcp_() {}
43
44         DECLARE_COMMON_PD_T(IGEMM_S8U8S32_IMPL_STR,
45                 _gemm_x8s8s32x_convolution_fwd_t<src_type, dst_type>);
46
47         virtual status_t init() override {
48             using namespace data_type;
49             using namespace memory_format;
50
51             assert(this->engine()->kind() == engine_kind::cpu);
52
53             bool ok = true
54                 && this->set_default_params() == status::success
55                 && utils::one_of(this->desc()->prop_kind,
56                         prop_kind::forward_training,
57                         prop_kind::forward_inference)
58                 && utils::one_of(this->desc()->alg_kind,
59                         alg_kind::convolution_auto,
60                         alg_kind::convolution_direct)
61                 && !this->has_zero_dim_memory()
62                 && this->desc()->src_desc.data_type == src_type
63                 && this->desc()->dst_desc.data_type == dst_type
64                 && this->desc()->weights_desc.data_type == s8
65                 && IMPLICATION(this->with_bias(), utils::one_of(
66                             this->desc()->bias_desc.data_type, f32, s32, s8,
67                             u8))
68                 && this->desc()->accum_data_type == data_type::s32
69                 && utils::one_of(this->src_pd_.desc()->format, nhwc, ndhwc)
70                 && this->src_pd_.desc()->format == this->dst_pd_.desc()->format
71                 && IMPLICATION(this->src_pd_.desc()->format == nhwc,
72                         this->weights_pd_.desc()->format == (this->with_groups()
73                                 ? ((src_type == data_type::s8) ? hwigo_s8s8 : hwigo)
74                                 : ((src_type == data_type::s8) ? hwio_s8s8 : hwio)))
75                 && IMPLICATION(this->src_pd_.desc()->format == ndhwc,
76                         this->weights_pd_.desc()->format == (this->with_groups()
77                                 ? ((src_type == data_type::s8) ? dhwigo_s8s8 : dhwigo)
78                                 : ((src_type == data_type::s8) ? dhwio_s8s8 : dhwio)))
79                 && this->is_gemm_conv_format();
80             if (!ok) return status::unimplemented;
81
82             auto scratchpad = scratchpad_registry().registrar();
83             return jit_gemm_convolution_utils::init_conf(jcp_, scratchpad,
84                     *this->desc(), this->src_pd(), this->weights_pd(0),
85                     this->dst_pd(), mkldnn_get_max_threads());
86         }
87
88         jit_gemm_conv_conf_t jcp_;
89
90     protected:
91         memory_format_t src_format() const {
92             using namespace memory_format;
93             const size_t ndims_sp = this->desc()->src_desc.ndims - 4;
94             return (utils::pick(ndims_sp, nhwc, ndhwc));
95         }
96
97         memory_format_t wei_format() const {
98             using namespace memory_format;
99             const size_t ndims_sp = this->desc()->src_desc.ndims - 4;
100             return this->with_groups()
101                 ? (src_type == data_type::s8) ? utils::pick(ndims_sp, hwigo_s8s8, dhwigo_s8s8)
102                                               : utils::pick(ndims_sp, hwigo, dhwigo)
103                 : (src_type == data_type::s8) ? utils::pick(ndims_sp, hwio_s8s8, dhwio_s8s8)
104                                               : utils::pick(ndims_sp, hwio, dhwio);
105         }
106
107         virtual status_t set_default_params() override {
108             using namespace memory_format;
109             if (this->src_pd_.desc()->format == any)
110                 CHECK(this->src_pd_.set_format(src_format()));
111             if (this->dst_pd_.desc()->format == any)
112                 CHECK(this->dst_pd_.set_format(src_format()));
113             if (this->weights_pd_.desc()->format == any)
114                 CHECK(this->weights_pd_.set_format(wei_format()));
115             if (this->bias_pd_.desc()->format == any)
116                 CHECK(this->bias_pd_.set_format(x));
117             if (this->desc()->alg_kind == alg_kind::convolution_auto)
118                 CHECK(this->set_alg_kind(alg_kind::convolution_direct));
119             return status::success;
120         }
121
122         virtual bool is_gemm_conv_format() const {
123             using namespace mkldnn::impl::primitive_kind;
124             auto const &po = this->attr()->post_ops_;
125             auto is_relu = [&](int idx) {
126                 return po.entry_[idx].is_relu(true, false); };
127
128             switch (po.len_) {
129             case 0: return true;
130             case 1: return is_relu(0) || po.contain(sum, 0);
131             case 2: return po.contain(sum, 0) && is_relu(1);
132             default: return false;
133             }
134         }
135     };
136
137     _gemm_x8s8s32x_convolution_fwd_t(const pd_t *apd, const input_vector &inputs,
138            const output_vector &outputs)
139         : cpu_primitive_t(apd, inputs, outputs, true) {
140         pp_ker_ = new pp_ker_t(this->pd());
141     }
142     ~_gemm_x8s8s32x_convolution_fwd_t() {
143         delete pp_ker_;
144     }
145
146     typedef typename prec_traits<src_type>::type src_data_t;
147     typedef typename prec_traits<data_type::s8>::type wei_data_t;
148     typedef typename prec_traits<dst_type>::type dst_data_t;
149     typedef typename prec_traits<data_type::s32>::type acc_data_t;
150
151     virtual void execute(event_t *e) const {
152         execute_forward();
153         e->set_state(event_t::ready);
154     }
155
156 private:
157     const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
158     void execute_forward() const;
159     // XXX: this is throwaway code that will become unnecessary when we have a
160     // sufficiently advanced igemm jit generator that supports quantization,
161     // relu, and whatnot
162     class pp_ker_t : jit_generator {
163     public:
164         DECLARE_CPU_JIT_AUX_FUNCTIONS(
165         _gemm_x8s8s32x_convolution_fwd_t::pp_kernel);
166         pp_ker_t(const pd_t *pd);
167
168         void operator()(dst_data_t *dst, const acc_data_t *acc,
169             const char *bias, const float *scales,
170             float nslope, float sum_scale, float signed_scale,
171             int g, size_t start, size_t end);
172
173         size_t dst_os_stride_;
174
175     private:
176         void generate();
177
178         struct ker_args {
179             dst_data_t *dst;
180             const acc_data_t *acc;
181             const char *bias;
182             const float *scales;
183             float nslope;
184             float sum_scale;
185             float signed_scale;
186             size_t len;
187             size_t oc_offset;
188         };
189         void(*ker_)(const ker_args *args);
190
191         const jit_gemm_conv_conf_t jcp_;
192         size_t OC_;
193         size_t OS_;
194         data_type_t bias_data_type_;
195         size_t bias_data_type_size_;
196         size_t scale_idx_mult_;
197         round_mode_t rmode_;
198         bool do_bias_;
199         bool do_relu_;
200         bool do_sum_;
201         bool do_signed_scaling_;
202         size_t vlen_;
203     };
204
205
206     void execute_forward_thr(const int ithr, const int nthr,
207             const src_data_t *src_base, const wei_data_t *wei_base,
208             const char *bia_base, dst_data_t *dst_base,
209             const memory_tracking::grantor_t &scratchpad) const;
210
211     int nthr_;
212     pp_ker_t *pp_ker_;
213
214 };
215
216 template <data_type_t dst_type>
217 struct _gemm_u8s8s32x_convolution_bwd_data_t: public cpu_primitive_t {
218     struct pd_t: public cpu_convolution_bwd_data_pd_t{
219         pd_t(engine_t *engine,
220                 const convolution_desc_t *adesc, const primitive_attr_t *attr,
221                 const convolution_fwd_pd_t *hint_fwd_pd)
222             : cpu_convolution_bwd_data_pd_t(engine, adesc, attr, hint_fwd_pd)
223             , jcp_() {}
224
225         DECLARE_COMMON_PD_T(IGEMM_S8U8S32_IMPL_STR,
226                 _gemm_u8s8s32x_convolution_bwd_data_t<dst_type>);
227
228         virtual status_t init() override {
229             using namespace data_type;
230             using namespace memory_format;
231
232             assert(this->engine()->kind() == engine_kind::cpu);
233
234             bool ok = true
235                 && this->set_default_params() == status::success
236                 && this->desc()->prop_kind == prop_kind::backward_data
237                 && utils::one_of(this->desc()->alg_kind, alg_kind::convolution_auto,
238                            alg_kind::convolution_direct)
239                 && !this->has_zero_dim_memory()
240                 && this->desc()->diff_src_desc.data_type == dst_type
241                 && this->desc()->diff_dst_desc.data_type == u8
242                 && this->desc()->weights_desc.data_type == s8
243                 && IMPLICATION(this->with_bias(), utils::one_of(
244                             this->desc()->bias_desc.data_type, f32, s32, s8,
245                             u8))
246                 && this->desc()->accum_data_type == data_type::s32
247                 && utils::everyone_is(nhwc, this->diff_src_pd_.desc()->format,
248                         this->diff_dst_pd_.desc()->format)
249                 && this->weights_pd_.desc()->format == (this->with_groups()
250                         ? hwigo : hwio)
251                 && attr()->post_ops_.has_default_values();
252             if (!ok) return status::unimplemented;
253
254             auto scratchpad = scratchpad_registry().registrar();
255             return jit_gemm_convolution_utils::init_conf(jcp_, scratchpad,
256                     *this->desc(), this->diff_src_pd(), this->weights_pd(0),
257                     this->diff_dst_pd(), mkldnn_get_max_threads());
258         }
259
260         virtual bool support_bias() const override { return true; }
261
262         jit_gemm_conv_conf_t jcp_;
263
264     protected:
265         virtual status_t set_default_params() override {
266             using namespace memory_format;
267
268             if (this->diff_src_pd_.desc()->format == any)
269                 CHECK(this->diff_src_pd_.set_format(nhwc));
270             if (this->diff_dst_pd_.desc()->format == any)
271                 CHECK(this->diff_dst_pd_.set_format(nhwc));
272             if (this->weights_pd_.desc()->format == any)
273                 CHECK(this->weights_pd_.set_format(
274                             this->with_groups() ? hwigo : hwio));
275             if (bias_pd_.desc()->format == any)
276                 CHECK(bias_pd_.set_format(x));
277             if (this->desc()->alg_kind == alg_kind::convolution_auto)
278                 CHECK(this->set_alg_kind(alg_kind::convolution_direct));
279              return status::success;
280         }
281     };
282
283     _gemm_u8s8s32x_convolution_bwd_data_t(const pd_t *apd, const input_vector &inputs,
284            const output_vector &outputs)
285         : cpu_primitive_t(apd, inputs, outputs, true) {}
286     ~_gemm_u8s8s32x_convolution_bwd_data_t() {}
287
288     typedef typename prec_traits<data_type::u8>::type diff_dst_data_t;
289     typedef typename prec_traits<data_type::s8>::type wei_data_t;
290     typedef typename prec_traits<dst_type>::type diff_src_data_t;
291     typedef typename prec_traits<data_type::s32>::type acc_data_t;
292
293     virtual void execute(event_t *e) const {
294         execute_backward_data();
295         e->set_state(event_t::ready);
296     }
297
298 private:
299     void execute_backward_data() const;
300     void execute_backward_data_thr(const int ithr, const int nthr,
301             const diff_dst_data_t *diff_dst_base, const wei_data_t *wei_base,
302             const char *bia_base, diff_src_data_t *diff_src_base,
303             const memory_tracking::grantor_t &scratchpad) const;
304     const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
305 };
306
307 }
308 }
309 }
310
311 #endif