1 | /******************************************************************************* |
2 | * Copyright 2016-2022 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_X64_JIT_AVX512_COMMON_CONVOLUTION_HPP |
18 | #define CPU_X64_JIT_AVX512_COMMON_CONVOLUTION_HPP |
19 | |
20 | #include "common/c_types_map.hpp" |
21 | #include "common/dnnl_thread.hpp" |
22 | #include "common/memory_tracking.hpp" |
23 | #include "common/primitive.hpp" |
24 | #include "common/utils.hpp" |
25 | |
26 | #include "cpu/cpu_convolution_pd.hpp" |
27 | #include "cpu/x64/cpu_barrier.hpp" |
28 | #include "cpu/x64/cpu_reducer.hpp" |
29 | |
30 | #include "cpu/x64/jit_avx512_common_conv_kernel.hpp" |
31 | #include "cpu/x64/jit_transpose_utils.hpp" |
32 | |
33 | namespace dnnl { |
34 | namespace impl { |
35 | namespace cpu { |
36 | namespace x64 { |
37 | |
38 | template <impl::data_type_t src_type, impl::data_type_t wei_type = src_type, |
39 | impl::data_type_t dst_type = src_type> |
40 | struct jit_avx512_common_convolution_fwd_t : public primitive_t { |
41 | struct pd_t : public cpu_convolution_fwd_pd_t { |
42 | pd_t(const convolution_desc_t *adesc, const primitive_attr_t *attr, |
43 | const typename pd_t::base_class *hint_fwd_pd) |
44 | : cpu_convolution_fwd_pd_t(adesc, attr, hint_fwd_pd), jcp_() {} |
45 | |
46 | DECLARE_COMMON_PD_T(JIT_IMPL_NAME_HELPER("jit:" , avx512_core, "" ), |
47 | jit_avx512_common_convolution_fwd_t); |
48 | |
49 | status_t init(engine_t *engine) { |
50 | bool ok = true && is_fwd() |
51 | && set_default_alg_kind(alg_kind::convolution_direct) |
52 | && expect_data_types(src_type, wei_type, dst_type, dst_type, |
53 | data_type::undef) |
54 | && attr()->has_default_values( |
55 | primitive_attr_t::skip_mask_t::post_ops, dst_type) |
56 | && !has_zero_dim_memory(); |
57 | if (!ok) return status::unimplemented; |
58 | |
59 | CHECK(jit_avx512_common_conv_fwd_kernel::init_conf(jcp_, *desc(), |
60 | src_md_, weights_md_, dst_md_, bias_md_, attr_, |
61 | dnnl_get_max_threads())); |
62 | |
63 | auto scratchpad = scratchpad_registry().registrar(); |
64 | jit_avx512_common_conv_fwd_kernel::init_scratchpad( |
65 | scratchpad, jcp_); |
66 | |
67 | return status::success; |
68 | } |
69 | |
70 | jit_conv_conf_t jcp_; |
71 | }; |
72 | |
73 | jit_avx512_common_convolution_fwd_t(const pd_t *apd) : primitive_t(apd) {} |
74 | |
75 | typedef typename prec_traits<src_type>::type src_data_t; |
76 | typedef typename prec_traits<wei_type>::type wei_data_t; |
77 | typedef typename prec_traits<dst_type>::type dst_data_t; |
78 | |
79 | status_t init(engine_t *engine) override { |
80 | CHECK(safe_ptr_assign(kernel_, |
81 | new jit_avx512_common_conv_fwd_kernel( |
82 | pd()->jcp_, *pd()->attr(), *pd()->dst_md(0)))); |
83 | return kernel_->create_kernel(); |
84 | } |
85 | |
86 | status_t execute(const exec_ctx_t &ctx) const override { |
87 | if (pd()->ndims() == 3) |
88 | execute_forward_1d(ctx); |
89 | else if (pd()->ndims() == 4) |
90 | execute_forward_2d(ctx); |
91 | else if (pd()->ndims() == 5) |
92 | execute_forward_3d(ctx); |
93 | else |
94 | assert(false); |
95 | |
96 | if (pd()->wants_zero_pad_dst()) ctx.zero_pad_output(DNNL_ARG_DST); |
97 | return status::success; |
98 | } |
99 | |
100 | private: |
101 | void prepare_padded_bias(const dst_data_t *&bias, |
102 | const memory_tracking::grantor_t &scratchpad) const; |
103 | void execute_forward_1d(const exec_ctx_t &ctx) const; |
104 | void execute_forward_2d(const exec_ctx_t &ctx) const; |
105 | void execute_forward_3d(const exec_ctx_t &ctx) const; |
106 | const pd_t *pd() const { return (const pd_t *)primitive_t::pd().get(); } |
107 | |
108 | std::unique_ptr<jit_avx512_common_conv_fwd_kernel> kernel_; |
109 | }; |
110 | |
111 | template <impl::data_type_t diff_dst_type, |
112 | impl::data_type_t wei_type = diff_dst_type, |
113 | impl::data_type_t diff_src_type = diff_dst_type> |
114 | struct jit_avx512_common_convolution_bwd_data_t : public primitive_t { |
115 | struct pd_t : public cpu_convolution_bwd_data_pd_t { |
116 | pd_t(const convolution_desc_t *adesc, const primitive_attr_t *attr, |
117 | const convolution_fwd_pd_t *hint_fwd_pd) |
118 | : cpu_convolution_bwd_data_pd_t(adesc, attr, hint_fwd_pd), jcp_() {} |
119 | |
120 | DECLARE_COMMON_PD_T(JIT_IMPL_NAME_HELPER("jit:" , avx512_core, "" ), |
121 | jit_avx512_common_convolution_bwd_data_t); |
122 | |
123 | status_t init(engine_t *engine) { |
124 | bool ok = true && desc()->prop_kind == prop_kind::backward_data |
125 | && set_default_alg_kind(alg_kind::convolution_direct) |
126 | && expect_data_types(diff_src_type, wei_type, |
127 | data_type::undef, diff_dst_type, data_type::undef) |
128 | && attr()->has_default_values() && !has_zero_dim_memory(); |
129 | if (!ok) return status::unimplemented; |
130 | |
131 | status_t status |
132 | = jit_avx512_common_conv_bwd_data_kernel_f32::init_conf( |
133 | jcp_, *desc(), diff_src_md_, weights_md_, |
134 | diff_dst_md_, dnnl_get_max_threads()); |
135 | if (status != status::success) return status; |
136 | |
137 | auto scratchpad = scratchpad_registry().registrar(); |
138 | jit_avx512_common_conv_bwd_data_kernel_f32::init_scratchpad( |
139 | scratchpad, jcp_); |
140 | |
141 | return status::success; |
142 | } |
143 | |
144 | jit_conv_conf_t jcp_; |
145 | }; |
146 | |
147 | jit_avx512_common_convolution_bwd_data_t(const pd_t *apd) |
148 | : primitive_t(apd) {} |
149 | |
150 | typedef typename prec_traits<diff_dst_type>::type diff_dst_data_t; |
151 | typedef typename prec_traits<wei_type>::type wei_data_t; |
152 | typedef typename prec_traits<diff_src_type>::type diff_src_data_t; |
153 | |
154 | status_t init(engine_t *engine) override { |
155 | CHECK(safe_ptr_assign(kernel_, |
156 | new jit_avx512_common_conv_bwd_data_kernel_f32(pd()->jcp_))); |
157 | return kernel_->create_kernel(); |
158 | } |
159 | |
160 | status_t execute(const exec_ctx_t &ctx) const override { |
161 | if (pd()->ndims() == 3) |
162 | execute_backward_data_1d(ctx); |
163 | else if (pd()->ndims() == 4) |
164 | execute_backward_data_2d(ctx); |
165 | else if (pd()->ndims() == 5) |
166 | execute_backward_data_3d(ctx); |
167 | else |
168 | assert(false); |
169 | return status::success; |
170 | } |
171 | |
172 | private: |
173 | void execute_backward_data_1d(const exec_ctx_t &ctx) const; |
174 | void execute_backward_data_2d(const exec_ctx_t &ctx) const; |
175 | void execute_backward_data_3d(const exec_ctx_t &ctx) const; |
176 | const pd_t *pd() const { return (const pd_t *)primitive_t::pd().get(); } |
177 | |
178 | std::unique_ptr<jit_avx512_common_conv_bwd_data_kernel_f32> kernel_; |
179 | }; |
180 | |
181 | template <impl::data_type_t src_type, |
182 | impl::data_type_t diff_dst_type = src_type, |
183 | impl::data_type_t diff_weights_type = src_type> |
184 | struct jit_avx512_common_convolution_bwd_weights_t : public primitive_t { |
185 | struct pd_t : public cpu_convolution_bwd_weights_pd_t { |
186 | pd_t(const convolution_desc_t *adesc, const primitive_attr_t *attr, |
187 | const convolution_fwd_pd_t *hint_fwd_pd) |
188 | : cpu_convolution_bwd_weights_pd_t(adesc, attr, hint_fwd_pd) |
189 | , jcp_() {} |
190 | |
191 | DECLARE_COMMON_PD_T(JIT_IMPL_NAME_HELPER("jit:" , avx512_core, "" ), |
192 | jit_avx512_common_convolution_bwd_weights_t); |
193 | |
194 | status_t init(engine_t *engine) { |
195 | bool ok = true && desc()->prop_kind == prop_kind::backward_weights |
196 | && set_default_alg_kind(alg_kind::convolution_direct) |
197 | && expect_data_types(src_type, diff_weights_type, |
198 | diff_weights_type, diff_dst_type, data_type::undef) |
199 | && attr()->has_default_values() && !has_zero_dim_memory(); |
200 | if (!ok) return status::unimplemented; |
201 | |
202 | status_t status |
203 | = jit_avx512_common_conv_bwd_weights_kernel_f32::init_conf( |
204 | jcp_, *desc(), src_md_, diff_weights_md_, |
205 | diff_bias_md_, diff_dst_md_, |
206 | dnnl_get_max_threads()); |
207 | if (status != status::success) return status; |
208 | |
209 | init_balancers(); |
210 | |
211 | auto scratchpad = scratchpad_registry().registrar(); |
212 | jit_avx512_common_conv_bwd_weights_kernel_f32::init_scratchpad( |
213 | scratchpad, jcp_); |
214 | |
215 | auto reducer_bia_scratchpad = memory_tracking::registrar_t( |
216 | scratchpad, memory_tracking::names::prefix_reducer_bia); |
217 | reducer_bia_conf_.init_scratchpad(reducer_bia_scratchpad); |
218 | |
219 | return status; |
220 | } |
221 | |
222 | jit_conv_conf_t jcp_; |
223 | typename cpu_reducer_t<diff_weights_type>::conf_t reducer_bia_conf_; |
224 | |
225 | private: |
226 | void init_balancers() { |
227 | const size_t max_buffer_size = jcp_.nthr * 3 * 5 * 5 * 16 * 16; |
228 | if (with_bias()) { |
229 | reducer_bia_conf_.init(reduce_balancer_t(jcp_.nthr, |
230 | jcp_.oc_block, jcp_.ngroups * jcp_.nb_oc, jcp_.mb, |
231 | max_buffer_size, true)); |
232 | } |
233 | } |
234 | }; |
235 | |
236 | jit_avx512_common_convolution_bwd_weights_t(const pd_t *apd) |
237 | : primitive_t(apd) {} |
238 | |
239 | typedef typename prec_traits<src_type>::type src_data_t; |
240 | typedef typename prec_traits<diff_dst_type>::type diff_dst_data_t; |
241 | typedef typename prec_traits<diff_weights_type>::type diff_weights_data_t; |
242 | |
243 | status_t init(engine_t *engine) override; |
244 | |
245 | status_t execute(const exec_ctx_t &ctx) const override { |
246 | execute_backward_weights(ctx); |
247 | return status::success; |
248 | } |
249 | |
250 | private: |
251 | void execute_backward_weights(const exec_ctx_t &ctx) const; |
252 | void prepare_scratchpad_data(const exec_ctx_t &ctx) const; |
253 | struct thread_info_t; |
254 | void compute_diff_weights_nxc(const thread_info_t *) const; |
255 | void compute_diff_weights(const thread_info_t *) const; |
256 | void compute_diff_weights_2d(const thread_info_t *) const; |
257 | void compute_diff_weights_3d(const thread_info_t *) const; |
258 | void reduce_diff_weights(const thread_info_t *) const; |
259 | void reduce_diff_weights_3d(const thread_info_t *) const; |
260 | void compute_diff_bias(const thread_info_t *) const; |
261 | void reduce_diff_bias(const thread_info_t *) const; |
262 | |
263 | const pd_t *pd() const { return (const pd_t *)primitive_t::pd().get(); } |
264 | |
265 | int nthr_, nthr_mb_, nthr_g_, nthr_oc_b_, nthr_ic_b_; |
266 | |
267 | std::unique_ptr<jit_avx512_common_conv_bwd_weights_kernel_f32> kernel_; |
268 | std::unique_ptr<cpu_accumulator_1d_t<diff_weights_type>> acc_ker_; |
269 | std::unique_ptr<cpu_reducer_t<diff_weights_type>> reducer_bias_; |
270 | }; |
271 | |
272 | } // namespace x64 |
273 | } // namespace cpu |
274 | } // namespace impl |
275 | } // namespace dnnl |
276 | |
277 | #endif |
278 | |
279 | // vim: et ts=4 sw=4 cindent cino+=l0,\:4,N-s |
280 | |