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 COMMON_POOLING_PD_HPP |
18 | #define COMMON_POOLING_PD_HPP |
19 | |
20 | #include "oneapi/dnnl/dnnl.h" |
21 | |
22 | #include "c_types_map.hpp" |
23 | #include "primitive_desc.hpp" |
24 | #include "type_helpers.hpp" |
25 | #include "utils.hpp" |
26 | |
27 | namespace dnnl { |
28 | namespace impl { |
29 | |
30 | struct pooling_fwd_pd_t; |
31 | |
32 | struct pooling_pd_t : public primitive_desc_t { |
33 | static constexpr auto base_pkind = primitive_kind::pooling; |
34 | |
35 | const pooling_desc_t *desc() const { return &desc_; } |
36 | const op_desc_t *op_desc() const override { |
37 | return reinterpret_cast<const op_desc_t *>(this->desc()); |
38 | } |
39 | |
40 | status_t query(query_t what, int idx, void *result) const override { |
41 | switch (what) { |
42 | case query::prop_kind: |
43 | *(prop_kind_t *)result = desc()->prop_kind; |
44 | break; |
45 | case query::primitive_kind: |
46 | *(primitive_kind_t *)result = desc_.primitive_kind; |
47 | break; |
48 | case query::alg_kind: |
49 | *(alg_kind_t *)result = desc()->alg_kind; |
50 | break; |
51 | case query::kernel: |
52 | *(const dims_t **)result = &desc()->kernel; |
53 | break; |
54 | case query::strides: |
55 | *(const dims_t **)result = &desc()->strides; |
56 | break; |
57 | case query::dilations: |
58 | *(const dims_t **)result = &desc()->dilation; |
59 | break; |
60 | case query::padding_l: |
61 | *(const dims_t **)result = &desc()->padding[0]; |
62 | break; |
63 | case query::padding_r: |
64 | *(const dims_t **)result = &desc()->padding[1]; |
65 | break; |
66 | default: return primitive_desc_t::query(what, idx, result); |
67 | } |
68 | return status::success; |
69 | } |
70 | |
71 | /* common pooling aux functions */ |
72 | |
73 | dim_t MB() const { return src_desc().dims[0]; } |
74 | dim_t IC() const { return src_desc().dims[1]; } |
75 | dim_t OC() const { return IC(); } |
76 | |
77 | dim_t ID() const { return ndims() >= 5 ? src_desc().dims[ndims() - 3] : 1; } |
78 | dim_t IH() const { return ndims() >= 4 ? src_desc().dims[ndims() - 2] : 1; } |
79 | dim_t IW() const { return src_desc().dims[ndims() - 1]; } |
80 | |
81 | dim_t OD() const { return ndims() >= 5 ? dst_desc().dims[ndims() - 3] : 1; } |
82 | dim_t OH() const { return ndims() >= 4 ? dst_desc().dims[ndims() - 2] : 1; } |
83 | dim_t OW() const { return dst_desc().dims[ndims() - 1]; } |
84 | |
85 | dim_t KD() const { return ndims() >= 5 ? desc_.kernel[ndims() - 5] : 1; } |
86 | dim_t KH() const { return ndims() >= 4 ? desc_.kernel[ndims() - 4] : 1; } |
87 | dim_t KW() const { return desc_.kernel[ndims() - 3]; } |
88 | |
89 | dim_t KSD() const { return ndims() >= 5 ? desc_.strides[ndims() - 5] : 1; } |
90 | dim_t KSH() const { return ndims() >= 4 ? desc_.strides[ndims() - 4] : 1; } |
91 | dim_t KSW() const { return desc_.strides[ndims() - 3]; } |
92 | |
93 | dim_t KDD() const { |
94 | return (ndims() >= 5 ? desc_.dilation[ndims() - 5] : 0); |
95 | } |
96 | dim_t KDH() const { |
97 | return (ndims() >= 4 ? desc_.dilation[ndims() - 4] : 0); |
98 | } |
99 | dim_t KDW() const { return desc_.dilation[ndims() - 3]; } |
100 | |
101 | dim_t padFront() const { |
102 | return ndims() >= 5 ? desc_.padding[0][ndims() - 5] : 0; |
103 | } |
104 | dim_t padBack() const { |
105 | return ndims() >= 5 ? desc_.padding[1][ndims() - 5] : 0; |
106 | } |
107 | dim_t padT() const { |
108 | return ndims() >= 4 ? desc_.padding[0][ndims() - 4] : 0; |
109 | } |
110 | dim_t padB() const { |
111 | return ndims() >= 4 ? desc_.padding[1][ndims() - 4] : 0; |
112 | } |
113 | dim_t padL() const { return desc_.padding[0][ndims() - 3]; } |
114 | dim_t padR() const { return desc_.padding[1][ndims() - 3]; } |
115 | |
116 | int ndims() const { return src_desc().ndims; } |
117 | int spatial_ndims() const { return ndims() - 2; } |
118 | |
119 | bool is_dilated() const { return KDD() != 0 || KDH() != 0 || KDW() != 0; } |
120 | |
121 | bool has_zero_dim_memory() const { |
122 | return memory_desc_wrapper(src_desc()).has_zero_dim(); |
123 | } |
124 | |
125 | bool is_fwd() const { |
126 | return utils::one_of(desc_.prop_kind, prop_kind::forward_training, |
127 | prop_kind::forward_inference); |
128 | } |
129 | |
130 | const memory_desc_t *invariant_src_md() const { |
131 | return is_fwd() ? src_md() : diff_src_md(); |
132 | } |
133 | |
134 | const memory_desc_t *invariant_dst_md() const { |
135 | return is_fwd() ? dst_md() : diff_dst_md(); |
136 | } |
137 | |
138 | protected: |
139 | pooling_desc_t desc_; |
140 | const pooling_fwd_pd_t *hint_fwd_pd_; |
141 | |
142 | memory_desc_t ws_md_; |
143 | |
144 | pooling_pd_t(const pooling_desc_t *adesc, const primitive_attr_t *attr, |
145 | const pooling_fwd_pd_t *hint_fwd_pd) |
146 | : primitive_desc_t(attr, base_pkind) |
147 | , desc_(*adesc) |
148 | , hint_fwd_pd_(hint_fwd_pd) |
149 | , ws_md_() {} |
150 | |
151 | void init_default_ws(data_type_t dt = data_type::undef) { |
152 | ws_md_ = is_fwd() ? *dst_md() : *diff_dst_md(); |
153 | ws_md_.data_type = (dt != data_type::undef) ? dt : indices_data_type(); |
154 | } |
155 | |
156 | data_type_t indices_data_type() const { |
157 | /* the simplest way to express 256... */ |
158 | const int u8_max = nstl::numeric_limits< |
159 | typename prec_traits<data_type::u8>::type>::max(); |
160 | return utils::array_product(desc()->kernel, spatial_ndims()) <= u8_max |
161 | ? data_type::u8 |
162 | : data_type::s32; |
163 | } |
164 | |
165 | private: |
166 | const memory_desc_t &src_desc() const { |
167 | return is_fwd() ? desc_.src_desc : desc_.diff_src_desc; |
168 | } |
169 | const memory_desc_t &dst_desc() const { |
170 | return is_fwd() ? desc_.dst_desc : desc_.diff_dst_desc; |
171 | } |
172 | }; |
173 | |
174 | struct pooling_fwd_pd_t : public pooling_pd_t { |
175 | typedef pooling_fwd_pd_t base_class; |
176 | typedef pooling_fwd_pd_t hint_class; |
177 | |
178 | arg_usage_t arg_usage(int arg) const override { |
179 | if (arg == DNNL_ARG_SRC) return arg_usage_t::input; |
180 | |
181 | if (arg == DNNL_ARG_DST) return arg_usage_t::output; |
182 | |
183 | if (arg == DNNL_ARG_WORKSPACE && (!types::is_zero_md(workspace_md()))) |
184 | return arg_usage_t::output; |
185 | |
186 | return primitive_desc_t::arg_usage(arg); |
187 | } |
188 | |
189 | const memory_desc_t *arg_md(int arg) const override { |
190 | switch (arg) { |
191 | case DNNL_ARG_SRC: return src_md(0); |
192 | case DNNL_ARG_DST: return dst_md(0); |
193 | default: return pooling_pd_t::arg_md(arg); |
194 | } |
195 | } |
196 | |
197 | const memory_desc_t *src_md(int index = 0) const override { |
198 | return index == 0 ? &src_md_ : &glob_zero_md; |
199 | } |
200 | const memory_desc_t *dst_md(int index = 0) const override { |
201 | return index == 0 ? &dst_md_ : &glob_zero_md; |
202 | } |
203 | const memory_desc_t *workspace_md(int index = 0) const override { |
204 | return index == 0 && !types::is_zero_md(&ws_md_) ? &ws_md_ |
205 | : &glob_zero_md; |
206 | } |
207 | |
208 | int n_inputs() const override { return 1 + n_binary_po_inputs(); } |
209 | int n_outputs() const override { |
210 | return 1 + (!types::is_zero_md(workspace_md())); |
211 | } |
212 | |
213 | std::vector<memory_desc_t> hint_mds(bool is_hint) const override { |
214 | if (!is_hint) return {}; |
215 | return {*dst_md(0)}; |
216 | } |
217 | |
218 | protected: |
219 | memory_desc_t src_md_; |
220 | memory_desc_t dst_md_; |
221 | |
222 | pooling_fwd_pd_t(const pooling_desc_t *adesc, const primitive_attr_t *attr, |
223 | const pooling_fwd_pd_t *hint_fwd_pd) |
224 | : pooling_pd_t(adesc, attr, hint_fwd_pd) |
225 | , src_md_(desc_.src_desc) |
226 | , dst_md_(desc_.dst_desc) {} |
227 | |
228 | virtual status_t set_default_params() { |
229 | if (dst_md()->format_kind != format_kind::any) return status::success; |
230 | |
231 | if (src_md()->format_kind != format_kind::blocked) |
232 | return status::unimplemented; |
233 | |
234 | return memory_desc_init_by_blocking_desc( |
235 | dst_md_, src_md_.format_desc.blocking); |
236 | } |
237 | }; |
238 | |
239 | struct pooling_bwd_pd_t : public pooling_pd_t { |
240 | typedef pooling_bwd_pd_t base_class; |
241 | typedef pooling_fwd_pd_t hint_class; |
242 | |
243 | arg_usage_t arg_usage(int arg) const override { |
244 | if (arg == DNNL_ARG_DIFF_DST) return arg_usage_t::input; |
245 | |
246 | if (arg == DNNL_ARG_DIFF_SRC) return arg_usage_t::output; |
247 | |
248 | if (arg == DNNL_ARG_WORKSPACE && (!types::is_zero_md(workspace_md()))) |
249 | return arg_usage_t::input; |
250 | |
251 | return primitive_desc_t::arg_usage(arg); |
252 | } |
253 | |
254 | const memory_desc_t *arg_md(int arg) const override { |
255 | switch (arg) { |
256 | case DNNL_ARG_DIFF_SRC: return diff_src_md(0); |
257 | case DNNL_ARG_DIFF_DST: return diff_dst_md(0); |
258 | default: return pooling_pd_t::arg_md(arg); |
259 | } |
260 | } |
261 | |
262 | const memory_desc_t *diff_src_md(int index = 0) const override { |
263 | return index == 0 ? &diff_src_md_ : &glob_zero_md; |
264 | } |
265 | const memory_desc_t *diff_dst_md(int index = 0) const override { |
266 | return index == 0 ? &diff_dst_md_ : &glob_zero_md; |
267 | } |
268 | const memory_desc_t *workspace_md(int index = 0) const override { |
269 | return index == 0 && !types::is_zero_md(&ws_md_) ? &ws_md_ |
270 | : &glob_zero_md; |
271 | } |
272 | |
273 | int n_inputs() const override { |
274 | return 1 + (!types::is_zero_md(workspace_md())); |
275 | } |
276 | int n_outputs() const override { return 1; } |
277 | |
278 | std::vector<memory_desc_t> hint_mds(bool is_hint) const override { |
279 | assert(!is_hint); |
280 | MAYBE_UNUSED(is_hint); |
281 | return hint_mds_; |
282 | } |
283 | |
284 | protected: |
285 | memory_desc_t diff_src_md_; |
286 | memory_desc_t diff_dst_md_; |
287 | |
288 | pooling_bwd_pd_t(const pooling_desc_t *adesc, const primitive_attr_t *attr, |
289 | const pooling_fwd_pd_t *hint_fwd_pd) |
290 | : pooling_pd_t(adesc, attr, hint_fwd_pd) |
291 | , diff_src_md_(desc_.diff_src_desc) |
292 | , diff_dst_md_(desc_.diff_dst_desc) { |
293 | if (hint_fwd_pd_) |
294 | hint_mds_ = hint_fwd_pd_->hint_mds(true /* is_hint */); |
295 | } |
296 | |
297 | virtual status_t set_default_params() { |
298 | if (diff_dst_md()->format_kind == format_kind::any) { |
299 | status_t status = status::success; |
300 | if (hint_fwd_pd_) |
301 | status = memory_desc_init_by_md_and_dt(diff_dst_md_, |
302 | hint_mds(false /* is_hint */)[0], |
303 | diff_dst_md_.data_type); |
304 | else |
305 | status = memory_desc_init_by_strides(diff_dst_md_, nullptr); |
306 | if (status != status::success) return status; |
307 | } |
308 | |
309 | if (diff_src_md()->format_kind != format_kind::any) |
310 | return status::success; |
311 | |
312 | if (diff_dst_md()->format_kind != format_kind::blocked) |
313 | return status::unimplemented; |
314 | |
315 | return memory_desc_init_by_blocking_desc( |
316 | diff_src_md_, diff_dst_md_.format_desc.blocking); |
317 | } |
318 | |
319 | private: |
320 | std::vector<memory_desc_t> hint_mds_; |
321 | }; |
322 | |
323 | } // namespace impl |
324 | } // namespace dnnl |
325 | |
326 | #endif |
327 | |
328 | // vim: et ts=4 sw=4 cindent cino+=l0,\:4,N-s |
329 | |