Add a section of how to link IE with CMake project (#99)
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / src / cpu / jit_avx512_core_u8s8s32x_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
33 namespace mkldnn {
34 namespace impl {
35 namespace cpu {
36
37 typedef enum {
38     no_last_block = 0x1U,
39     last_ic_block = 0x2U,
40     last_sp_block = 0x4U,
41     last_ic
42 } ker_block_t;
43
44 struct jit_avx512_core_u8s8s32x_deconv_fwd_kernel : public jit_generator {
45     DECLARE_CPU_JIT_AUX_FUNCTIONS(jit_avx512_core_u8s8s32x_deconv_fwd_ker_t);
46
47     jit_avx512_core_u8s8s32x_deconv_fwd_kernel(jit_conv_conf_t ajcp,
48             const primitive_attr_t &attr) : jcp(ajcp), attr_(attr) {
49         generate();
50         jit_ker = (void (*)(jit_deconv_call_s *))getCode();
51     }
52
53     static status_t init_conf(jit_conv_conf_t &jcp,
54             const deconvolution_desc_t &cd,
55             cpu_memory_t::pd_t &src_pd,
56             cpu_memory_t::pd_t &weights_pd,
57             cpu_memory_t::pd_t &dst_pd,
58             const bool with_bias,
59             cpu_memory_t::pd_t &bias_pd,
60             const primitive_attr_t &attr);
61
62     jit_conv_conf_t jcp;
63     const primitive_attr_t &attr_;
64     void (*jit_ker)(jit_deconv_call_s *);
65 private:
66     using reg64_t = const Xbyak::Reg64;
67     using zmm_t = const Xbyak::Zmm;
68     using xmm_t = const Xbyak::Xmm;
69
70     reg64_t reg_src = r8;
71     reg64_t reg_filt = r9;
72     reg64_t reg_dst = r10;
73     reg64_t param1 = abi_param1;
74     reg64_t reg_kh = abi_not_param1;
75     reg64_t reg_nur_w = rbx;
76     reg64_t reg_bias = rdx;
77     reg64_t reg_icb = reg_bias;
78     reg64_t reg_ptr_scales = rax;
79     reg64_t reg_oc_blocks = rsi;
80
81     reg64_t reg_scratch = r14;
82     reg64_t aux_reg_src = r11;
83     reg64_t aux_reg_filt = r12;
84     reg64_t reg_kj = rax;
85
86     Xbyak::Opmask ktail_mask = Xbyak::Opmask(2);
87     zmm_t zmm_tmp = zmm_t(29);
88     zmm_t zmm_one = zmm_t(30);
89     zmm_t zmm_zero = zmm_t(31);
90     zmm_t zmm_wei = zmm_t(31);
91
92     zmm_t zmm_out(int i_ur, int i_oc) {
93         int idx = i_ur * jcp.nb_oc_blocking + i_oc;
94         assert(idx < 31);
95         return zmm_t(idx);
96     }
97     zmm_t zmm_inp(int i_ic, int nb_x_blocking) {
98         int idx = i_ic + nb_x_blocking * jcp.ur_w;
99         assert(idx < 31);
100         return zmm_t(idx);
101     }
102
103     int get_ow_start(int ki, int l_overflow) {
104         int res = (jcp.ow - 1 + jcp.r_pad) % jcp.stride_w
105                 + l_overflow * jcp.stride_w
106                 - (jcp.kw - 1 - ki) * (jcp.dilate_w + 1);
107         while (res < 0)
108             res += jcp.stride_w;
109         return res;
110     }
111
112     int get_ow_end(int ur_w, int ki, int r_overflow) {
113         if (utils::one_of(ur_w, jcp.ow, jcp.ur_w_tail))
114                 ur_w += nstl::min(0, jcp.r_pad);
115         int res = (ur_w - 1 + jcp.l_pad) % jcp.stride_w
116             + r_overflow * jcp.stride_w - ki * (jcp.dilate_w + 1);
117         while (res < 0)
118             res += jcp.stride_w;
119         return ur_w - res;
120     }
121
122     void prepare_output(int ur_w);
123     void store_output(int ur_w, bool last_oc_block);
124     void compute_ker(int ur_w, int pad_l, int pad_r, ker_block_t last_ker_block);
125     void compute_loop(int ur_w, int pad_l, int pad_r, bool last_block);
126     void generate();
127     void cvt2ps(data_type_t type_in, zmm_t zmm_in, const Xbyak::Operand &op,
128         bool mask_flag);
129 };
130
131 template <impl::data_type_t dst_type>
132 struct _jit_avx512_core_u8s8s32x_deconvolution_fwd_t : public cpu_primitive_t {
133     struct pd_t : public cpu_deconvolution_fwd_pd_t {
134         pd_t(engine_t *engine,
135                 const deconvolution_desc_t *adesc,
136                 const primitive_attr_t *attr,
137                 const deconvolution_fwd_pd_t *hint_fwd_pd)
138             : cpu_deconvolution_fwd_pd_t(engine, adesc, attr, hint_fwd_pd) {}
139
140         DECLARE_COMMON_PD_T(JIT_IMPL_NAME_HELPER("jit_deconvolution:", avx512_core, ""),
141                 _jit_avx512_core_u8s8s32x_deconvolution_fwd_t<dst_type>);
142
143         virtual status_t init() override {
144             assert(this->engine()->kind() == engine_kind::cpu);
145
146             bool ok = true
147                 && utils::one_of(this->desc()->prop_kind, prop_kind::forward_training,
148                             prop_kind::forward_inference)
149                 && this->desc()->alg_kind & alg_kind::deconvolution_direct
150                 && this->desc()->dst_desc.data_type == dst_type
151                 && IMPLICATION(this->with_bias(), utils::one_of(
152                             this->desc()->bias_desc.data_type, data_type::f32,
153                             data_type::s32, data_type::s8, data_type::u8))
154                 && this->desc()->accum_data_type == data_type::s32;
155             if (!ok) return status::unimplemented;
156
157             /*TODO: support signed input and postops */
158             return jit_avx512_core_u8s8s32x_deconv_fwd_kernel::init_conf(
159                     jcp_, *this->desc(), this->src_pd_,
160                     this->weights_pd_, this->dst_pd_,
161                     this->with_bias(), this->bias_pd_,
162                     *this->attr());
163         }
164         jit_conv_conf_t jcp_;
165     };
166
167     _jit_avx512_core_u8s8s32x_deconvolution_fwd_t(const pd_t *pd,
168            const input_vector &inputs, const output_vector &outputs)
169        : cpu_primitive_t(&conf_, inputs, outputs), conf_(*pd) {
170            kernel_ = new jit_avx512_core_u8s8s32x_deconv_fwd_kernel(conf_.jcp_,
171                    *conf_.attr());
172        }
173
174     ~_jit_avx512_core_u8s8s32x_deconvolution_fwd_t() {
175         delete kernel_;
176     }
177
178     typedef typename prec_traits<data_type::u8>::type src_data_t;
179     typedef typename prec_traits<data_type::s8>::type wei_data_t;
180     typedef typename prec_traits<dst_type>::type dst_data_t;
181
182     virtual void execute(event_t *e)
183     {
184         execute_forward();
185         e->set_state(event_t::ready);
186     }
187
188 private:
189     void execute_forward();
190     pd_t conf_;
191     jit_avx512_core_u8s8s32x_deconv_fwd_kernel *kernel_;
192 };
193
194
195 }
196 }
197 }
198
199 #endif
200
201 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s