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