Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / src / cpu / jit_avx512_core_x8s8s32x_deconvolution.hpp
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 #ifndef CPU_JIT_AVX512_CORE_U8S8S32X_DECONVOLUTION_HPP
18 #define CPU_JIT_AVX512_CORE_U8S8S32X_DECONVOLUTION_HPP
19
20
21 #include "c_types_map.hpp"
22 #include "cpu_engine.hpp"
23 #include "cpu_memory.hpp"
24 #include "mkldnn_thread.hpp"
25 #include "type_helpers.hpp"
26 #include "utils.hpp"
27 #include "nstl.hpp"
28
29 #include "cpu_deconvolution_pd.hpp"
30 #include "jit_generator.hpp"
31 #include "jit_primitive_conf.hpp"
32 #include "jit_uni_eltwise.hpp"
33
34 namespace mkldnn {
35 namespace impl {
36 namespace cpu {
37
38 typedef enum {
39     no_last_block = 0x1U,
40     last_ic_block = 0x2U,
41     last_sp_block = 0x4U,
42 } ker_block_t;
43
44 struct jit_avx512_core_x8s8s32x_deconv_fwd_kernel : public jit_generator {
45     DECLARE_CPU_JIT_AUX_FUNCTIONS(jit_avx512_core_x8s8s32x_deconv_fwd_ker_t);
46
47     jit_avx512_core_x8s8s32x_deconv_fwd_kernel(
48             jit_conv_conf_t ajcp, const primitive_attr_t &attr)
49         : jcp(ajcp), attr_(attr), eltwise_injector_(nullptr) {
50         if (jcp.with_eltwise)
51             eltwise_injector_ = new jit_uni_eltwise_injector_f32<avx512_common>(
52                     this, jcp.eltwise);
53         generate();
54         jit_ker = (void (*)(jit_deconv_call_s *))getCode();
55     }
56
57     ~jit_avx512_core_x8s8s32x_deconv_fwd_kernel() {
58             delete eltwise_injector_;
59     }
60
61     static bool post_ops_ok(jit_conv_conf_t &jcp,
62             const primitive_attr_t &attr);
63
64     static status_t init_conf(jit_conv_conf_t &jcp,
65             const deconvolution_desc_t &cd,
66             cpu_memory_t::pd_t &src_pd,
67             cpu_memory_t::pd_t &weights_pd,
68             cpu_memory_t::pd_t &dst_pd,
69             const bool with_bias,
70             cpu_memory_t::pd_t &bias_pd,
71             const primitive_attr_t &attr);
72
73     static void init_scratchpad(memory_tracking::registrar_t &scratchpad,
74             const jit_conv_conf_t &jcp, const primitive_attr_t &attr);
75
76     jit_conv_conf_t jcp;
77     const primitive_attr_t &attr_;
78     void (*jit_ker)(jit_deconv_call_s *);
79 private:
80     jit_uni_eltwise_injector_f32<avx512_common> *eltwise_injector_;
81     using reg64_t = const Xbyak::Reg64;
82     using zmm_t = const Xbyak::Zmm;
83     using xmm_t = const Xbyak::Xmm;
84
85     reg64_t reg_src = r8;
86     reg64_t reg_filt = r9;
87     reg64_t reg_dst = r10;
88     reg64_t param1 = abi_param1;
89     reg64_t reg_kh = abi_not_param1;
90     reg64_t reg_nur_w = rbx;
91     reg64_t reg_bias = rdx;
92     reg64_t reg_icb = reg_bias;
93     reg64_t reg_ptr_scales = rax;
94     reg64_t reg_oc_blocks = rsi;
95
96     reg64_t aux_reg_src = r11;
97     reg64_t aux_reg_filt = r12;
98
99     reg64_t reg_compensation = r14;
100     reg64_t reg_scratch = r14;
101     reg64_t reg_ptr_sum_scale = r11;
102     reg64_t reg_bias_alpha = abi_not_param1;
103     reg64_t reg_overflow = rax;
104     reg64_t reg_comp_strides = reg_overflow;
105
106     Xbyak::Opmask ktail_mask = Xbyak::Opmask(2);
107     zmm_t zmm_tmp = zmm_t(28);
108     zmm_t zmm_one = zmm_t(29);
109     /* used during write-out section of store_output */
110     zmm_t zmm_zero = zmm_t(31);
111     zmm_t zmm_wei = zmm_t(31);
112
113     /* signed input */
114     zmm_t zmm_shift = zmm_t(30);
115     zmm_t zmm_comp = zmm_t(30);
116     zmm_t zmm_bias = zmm_t(31);
117     zmm_t zmm_prev_dst = zmm_t(31);
118
119     zmm_t zmm_out(int i_ur, int i_oc) {
120         int idx = i_ur * jcp.nb_oc_blocking + i_oc;
121         assert(idx < 31);
122         return zmm_t(idx);
123     }
124     zmm_t zmm_inp(int i_ic, int nb_x_blocking) {
125         int idx = i_ic + nb_x_blocking * jcp.ur_w;
126         assert(idx < 31);
127         return zmm_t(idx);
128     }
129     zmm_t zmm_bias_alpha() {
130         return zmm_t(jcp.nb_oc_blocking * jcp.ur_w);
131     }
132     xmm_t xmm_bias_alpha() {
133         return xmm_t(jcp.nb_oc_blocking * jcp.ur_w);
134     }
135
136     int get_ow_start(int ki, int l_overflow) {
137         int res = (jcp.ow - 1 + jcp.r_pad) % jcp.stride_w
138                 + l_overflow * jcp.stride_w
139                 - (jcp.kw - 1 - ki) * (jcp.dilate_w + 1);
140         while (res < 0)
141             res += jcp.stride_w;
142         return res;
143     }
144
145     int get_ow_end(int ur_w, int ki, int r_overflow) {
146         if (utils::one_of(ur_w, jcp.ow, jcp.ur_w_tail))
147                 ur_w += nstl::min(0, jcp.r_pad); // remove negative padding
148         int res = (ur_w - 1 + jcp.l_pad) % jcp.stride_w
149             + r_overflow * jcp.stride_w - ki * (jcp.dilate_w + 1);
150         while (res < 0)
151             res += jcp.stride_w;
152         return ur_w - res;
153     }
154     bool maybe_eltwise(int position);
155     void compute_eltwise(int ur_w);
156     void prepare_output(int ur_w);
157     void store_output(int ur_w, bool last_oc_block);
158     void compute_ker(int ur_w, int l_overflow, int r_overflow,
159              ker_block_t last_ic_block_flag, bool h_padded = false);
160     void kh_loop(int ur_w, int pad_l, int pad_r, ker_block_t last_ker_block);
161     void icb_loop(int ur_w, int pad_l, int pad_r, bool last_block);
162     void generate();
163     void cvt2ps(data_type_t type_in, zmm_t zmm_in, const Xbyak::Operand &op,
164         bool mask_flag);
165 };
166
167 template <impl::data_type_t src_type, impl::data_type_t dst_type>
168 struct _jit_avx512_core_x8s8s32x_deconvolution_fwd_t : public cpu_primitive_t {
169     struct pd_t : public cpu_deconvolution_fwd_pd_t {
170         pd_t(engine_t *engine,
171                 const deconvolution_desc_t *adesc,
172                 const primitive_attr_t *attr,
173                 const deconvolution_fwd_pd_t *hint_fwd_pd)
174             : cpu_deconvolution_fwd_pd_t(engine, adesc, attr, hint_fwd_pd) {}
175
176         DECLARE_COMMON_PD_T(JIT_IMPL_NAME_HELPER("jit_deconvolution:", avx512_core, ""),
177                 _jit_avx512_core_x8s8s32x_deconvolution_fwd_t<src_type, dst_type>);
178
179         virtual status_t init() override {
180             assert(this->engine()->kind() == engine_kind::cpu);
181
182             bool ok = true
183                 && utils::one_of(this->desc()->prop_kind, prop_kind::forward_training,
184                             prop_kind::forward_inference)
185                 && this->desc()->alg_kind & alg_kind::deconvolution_direct
186                 && this->desc()->src_desc.data_type == src_type
187                 && this->desc()->dst_desc.data_type == dst_type
188                 && IMPLICATION(this->with_bias(), utils::one_of(
189                             this->desc()->bias_desc.data_type, data_type::f32,
190                             data_type::s32, data_type::s8, data_type::u8))
191                 && this->desc()->accum_data_type == data_type::s32;
192             if (!ok) return status::unimplemented;
193
194             status_t status = jit_avx512_core_x8s8s32x_deconv_fwd_kernel::init_conf(
195                     jcp_, *this->desc(), this->src_pd_,
196                     this->weights_pd_, this->dst_pd_,
197                     this->with_bias(), this->bias_pd_,
198                     *this->attr());
199
200             if (status != status::success) return status;
201
202             auto scratchpad = scratchpad_registry().registrar();
203             jit_avx512_core_x8s8s32x_deconv_fwd_kernel::init_scratchpad(scratchpad,
204                     jcp_, *this->attr());
205
206             return status::success;
207         }
208         jit_conv_conf_t jcp_;
209     };
210
211     _jit_avx512_core_x8s8s32x_deconvolution_fwd_t(const pd_t *apd,
212            const input_vector &inputs, const output_vector &outputs)
213        : cpu_primitive_t(apd, inputs, outputs) {
214            kernel_ = new jit_avx512_core_x8s8s32x_deconv_fwd_kernel(pd()->jcp_,
215                    *pd()->attr());
216        }
217
218     ~_jit_avx512_core_x8s8s32x_deconvolution_fwd_t() {
219         delete kernel_;
220     }
221
222     typedef typename prec_traits<src_type>::type src_data_t;
223     typedef typename prec_traits<data_type::s8>::type wei_data_t;
224     typedef typename prec_traits<dst_type>::type dst_data_t;
225
226     virtual void execute(event_t *e) const
227     {
228         execute_forward();
229         e->set_state(event_t::ready);
230     }
231
232 private:
233     void execute_forward() const;
234     const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
235     jit_avx512_core_x8s8s32x_deconv_fwd_kernel *kernel_;
236 };
237
238
239 }
240 }
241 }
242
243 #endif
244
245 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s