1 | /******************************************************************************* |
2 | * Copyright 2019-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_RESAMPLING_PD_HPP |
18 | #define COMMON_RESAMPLING_PD_HPP |
19 | |
20 | #include <assert.h> |
21 | |
22 | #include "oneapi/dnnl/dnnl.h" |
23 | |
24 | #include "c_types_map.hpp" |
25 | #include "primitive_desc.hpp" |
26 | #include "utils.hpp" |
27 | |
28 | namespace dnnl { |
29 | namespace impl { |
30 | |
31 | struct resampling_fwd_pd_t; |
32 | |
33 | struct resampling_pd_t : public primitive_desc_t { |
34 | static constexpr auto base_pkind = primitive_kind::resampling; |
35 | |
36 | resampling_pd_t(const resampling_desc_t *adesc, |
37 | const primitive_attr_t *attr, |
38 | const resampling_fwd_pd_t *hint_fwd_pd) |
39 | : primitive_desc_t(attr, base_pkind) |
40 | , desc_(*adesc) |
41 | , hint_fwd_pd_(hint_fwd_pd) {} |
42 | |
43 | const resampling_desc_t *desc() const { return &desc_; } |
44 | const op_desc_t *op_desc() const override { |
45 | return reinterpret_cast<const op_desc_t *>(this->desc()); |
46 | } |
47 | |
48 | status_t query(query_t what, int idx, void *result) const override { |
49 | switch (what) { |
50 | case query::prop_kind: |
51 | *(prop_kind_t *)result = desc()->prop_kind; |
52 | break; |
53 | case query::alg_kind: |
54 | *(alg_kind_t *)result = desc()->alg_kind; |
55 | break; |
56 | case query::factors: |
57 | *(const float **)result = desc()->factors; |
58 | break; |
59 | default: return primitive_desc_t::query(what, idx, result); |
60 | } |
61 | return status::success; |
62 | } |
63 | |
64 | /* common resampling aux functions */ |
65 | |
66 | dim_t MB() const { return src_desc().dims[0]; } |
67 | dim_t C() const { return src_desc().dims[1]; } |
68 | dim_t ID() const { return ndims() >= 5 ? src_desc().dims[ndims() - 3] : 1; } |
69 | dim_t IH() const { return ndims() >= 4 ? src_desc().dims[ndims() - 2] : 1; } |
70 | dim_t IW() const { return ndims() >= 3 ? src_desc().dims[ndims() - 1] : 1; } |
71 | dim_t OD() const { return ndims() >= 5 ? dst_desc().dims[ndims() - 3] : 1; } |
72 | dim_t OH() const { return ndims() >= 4 ? dst_desc().dims[ndims() - 2] : 1; } |
73 | dim_t OW() const { return ndims() >= 3 ? dst_desc().dims[ndims() - 1] : 1; } |
74 | |
75 | float FD() const { return ndims() >= 5 ? desc()->factors[ndims() - 5] : 1; } |
76 | float FH() const { return ndims() >= 4 ? desc()->factors[ndims() - 4] : 1; } |
77 | float FW() const { return ndims() >= 3 ? desc()->factors[ndims() - 3] : 1; } |
78 | |
79 | int ndims() const { return src_desc().ndims; } |
80 | |
81 | bool is_fwd() const { |
82 | return utils::one_of(desc_.prop_kind, prop_kind::forward_training, |
83 | prop_kind::forward_inference); |
84 | } |
85 | |
86 | bool has_zero_dim_memory() const { |
87 | return memory_desc_wrapper(src_desc()).has_zero_dim(); |
88 | } |
89 | |
90 | int n_inputs() const override { return 1 + n_binary_po_inputs(); } |
91 | int n_outputs() const override { return 1; } |
92 | |
93 | protected: |
94 | resampling_desc_t desc_; |
95 | const resampling_fwd_pd_t *hint_fwd_pd_; |
96 | |
97 | private: |
98 | const memory_desc_t &src_desc() const { |
99 | return is_fwd() ? desc_.src_desc : desc_.diff_src_desc; |
100 | } |
101 | const memory_desc_t &dst_desc() const { |
102 | return is_fwd() ? desc_.dst_desc : desc_.diff_dst_desc; |
103 | } |
104 | }; |
105 | |
106 | struct resampling_fwd_pd_t : public resampling_pd_t { |
107 | typedef resampling_fwd_pd_t base_class; |
108 | typedef resampling_fwd_pd_t hint_class; |
109 | |
110 | resampling_fwd_pd_t(const resampling_desc_t *adesc, |
111 | const primitive_attr_t *attr, |
112 | const resampling_fwd_pd_t *hint_fwd_pd) |
113 | : resampling_pd_t(adesc, attr, hint_fwd_pd) |
114 | , src_md_(desc_.src_desc) |
115 | , dst_md_(desc_.dst_desc) {} |
116 | |
117 | arg_usage_t arg_usage(int arg) const override { |
118 | if (arg == DNNL_ARG_SRC) return arg_usage_t::input; |
119 | |
120 | if (arg == DNNL_ARG_DST) return arg_usage_t::output; |
121 | |
122 | return primitive_desc_t::arg_usage(arg); |
123 | } |
124 | |
125 | const memory_desc_t *arg_md(int arg) const override { |
126 | switch (arg) { |
127 | case DNNL_ARG_SRC: return src_md(0); |
128 | case DNNL_ARG_DST: return dst_md(0); |
129 | default: return resampling_pd_t::arg_md(arg); |
130 | } |
131 | } |
132 | |
133 | const memory_desc_t *src_md(int index = 0) const override { |
134 | return index == 0 ? &src_md_ : &glob_zero_md; |
135 | } |
136 | const memory_desc_t *dst_md(int index = 0) const override { |
137 | return index == 0 ? &dst_md_ : &glob_zero_md; |
138 | } |
139 | |
140 | protected: |
141 | memory_desc_t src_md_; |
142 | memory_desc_t dst_md_; |
143 | |
144 | virtual status_t set_default_params( |
145 | format_tag_t src_tag_hint = format_tag::undef) { |
146 | if (dst_md()->format_kind != format_kind::any) return status::success; |
147 | |
148 | if (src_md()->format_kind != format_kind::blocked) |
149 | return status::unimplemented; |
150 | |
151 | if (src_tag_hint != format_tag::undef) { |
152 | return memory_desc_init_by_tag(dst_md_, src_tag_hint); |
153 | } else { |
154 | return memory_desc_init_by_blocking_desc( |
155 | dst_md_, src_md_.format_desc.blocking); |
156 | } |
157 | } |
158 | }; |
159 | |
160 | struct resampling_bwd_pd_t : public resampling_pd_t { |
161 | typedef resampling_bwd_pd_t base_class; |
162 | typedef resampling_fwd_pd_t hint_class; |
163 | |
164 | resampling_bwd_pd_t(const resampling_desc_t *adesc, |
165 | const primitive_attr_t *attr, |
166 | const resampling_fwd_pd_t *hint_fwd_pd) |
167 | : resampling_pd_t(adesc, attr, hint_fwd_pd) |
168 | , diff_src_md_(desc_.diff_src_desc) |
169 | , diff_dst_md_(desc_.diff_dst_desc) {} |
170 | |
171 | arg_usage_t arg_usage(int arg) const override { |
172 | if (arg == DNNL_ARG_DIFF_DST) return arg_usage_t::input; |
173 | |
174 | if (arg == DNNL_ARG_DIFF_SRC) return arg_usage_t::output; |
175 | |
176 | return primitive_desc_t::arg_usage(arg); |
177 | } |
178 | |
179 | const memory_desc_t *arg_md(int arg) const override { |
180 | switch (arg) { |
181 | case DNNL_ARG_DIFF_SRC: return diff_src_md(0); |
182 | case DNNL_ARG_DIFF_DST: return diff_dst_md(0); |
183 | default: return resampling_pd_t::arg_md(arg); |
184 | } |
185 | } |
186 | |
187 | const memory_desc_t *diff_src_md(int index = 0) const override { |
188 | return index == 0 ? &diff_src_md_ : &glob_zero_md; |
189 | } |
190 | const memory_desc_t *diff_dst_md(int index = 0) const override { |
191 | return index == 0 ? &diff_dst_md_ : &glob_zero_md; |
192 | } |
193 | |
194 | protected: |
195 | memory_desc_t diff_src_md_; |
196 | memory_desc_t diff_dst_md_; |
197 | |
198 | virtual status_t set_default_params() { |
199 | if (diff_dst_md()->format_kind == format_kind::any && hint_fwd_pd_) { |
200 | status_t status = memory_desc_init_by_md_and_dt(diff_dst_md_, |
201 | *hint_fwd_pd_->dst_md(0), diff_dst_md_.data_type); |
202 | if (status != status::success) return status; |
203 | } |
204 | |
205 | if (diff_src_md()->format_kind != format_kind::any) |
206 | return status::success; |
207 | |
208 | if (diff_dst_md()->format_kind != format_kind::blocked) |
209 | return status::unimplemented; |
210 | return memory_desc_init_by_blocking_desc( |
211 | diff_src_md_, diff_dst_md_.format_desc.blocking); |
212 | } |
213 | }; |
214 | |
215 | } // namespace impl |
216 | } // namespace dnnl |
217 | #endif |
218 | |