1 | /******************************************************************************* |
2 | * Copyright 2019-2021 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_SIMPLE_RESAMPLING_HPP |
18 | #define CPU_SIMPLE_RESAMPLING_HPP |
19 | |
20 | #include <cassert> |
21 | #include <functional> |
22 | |
23 | #include "common/c_types_map.hpp" |
24 | #include "common/primitive.hpp" |
25 | #include "common/type_helpers.hpp" |
26 | #include "common/utils.hpp" |
27 | |
28 | #include "cpu/platform.hpp" |
29 | #include "cpu/primitive_attr_postops.hpp" |
30 | |
31 | #include "cpu/cpu_resampling_pd.hpp" |
32 | #include "cpu/resampling_utils.hpp" |
33 | |
34 | namespace dnnl { |
35 | namespace impl { |
36 | namespace cpu { |
37 | |
38 | struct simple_resampling_base_t { |
39 | simple_resampling_base_t(const resampling_pd_t *pd) : pd_(pd) {} |
40 | virtual ~simple_resampling_base_t() = default; |
41 | |
42 | virtual status_t init() = 0; |
43 | virtual status_t execute(const exec_ctx_t &ctx) const = 0; |
44 | |
45 | protected: |
46 | const resampling_pd_t *pd_; |
47 | |
48 | dim_t nsp_outer_ = 0; |
49 | dim_t stride_d_ = 0; |
50 | dim_t stride_h_ = 0; |
51 | dim_t stride_w_ = 0; |
52 | dim_t inner_stride_ = 0; |
53 | }; |
54 | |
55 | struct simple_resampling_fwd_t : public primitive_t { |
56 | struct pd_t : public cpu_resampling_fwd_pd_t { |
57 | using cpu_resampling_fwd_pd_t::cpu_resampling_fwd_pd_t; |
58 | |
59 | DECLARE_COMMON_PD_T("simple:any" , simple_resampling_fwd_t); |
60 | |
61 | status_t init(engine_t *engine) { |
62 | using namespace format_tag; |
63 | using namespace data_type; |
64 | using sm = primitive_attr_t::skip_mask_t; |
65 | |
66 | const bool ok = is_fwd() && !has_zero_dim_memory() |
67 | && platform::has_data_type_support(src_md()->data_type) |
68 | && platform::has_data_type_support(dst_md()->data_type) |
69 | && set_default_params() == status::success |
70 | && attr()->has_default_values( |
71 | sm::post_ops, dst_md()->data_type) |
72 | && attr_.set_default_formats(dst_md(0)) == status::success; |
73 | if (!ok) return status::unimplemented; |
74 | |
75 | format_tag_t dat_tag = memory_desc_matches_one_of_tag(*src_md(), |
76 | nCw8c, nChw8c, nCdhw8c, nCw16c, nChw16c, nCdhw16c, ncw, |
77 | nchw, ncdhw, nwc, nhwc, ndhwc); |
78 | if (!memory_desc_matches_tag(*dst_md(), dat_tag)) |
79 | return status::unimplemented; |
80 | |
81 | return status::success; |
82 | } |
83 | }; |
84 | |
85 | simple_resampling_fwd_t(const pd_t *apd); |
86 | status_t init(engine_t *engine) override; |
87 | ~simple_resampling_fwd_t() = default; |
88 | |
89 | status_t execute(const exec_ctx_t &ctx) const override; |
90 | |
91 | private: |
92 | const pd_t *pd() const { return (const pd_t *)primitive_t::pd().get(); } |
93 | |
94 | std::unique_ptr<simple_resampling_base_t> kernel_; |
95 | }; |
96 | |
97 | struct simple_resampling_bwd_t : public primitive_t { |
98 | struct pd_t : public cpu_resampling_bwd_pd_t { |
99 | using cpu_resampling_bwd_pd_t::cpu_resampling_bwd_pd_t; |
100 | |
101 | DECLARE_COMMON_PD_T("simple:any" , simple_resampling_bwd_t); |
102 | |
103 | status_t init(engine_t *engine) { |
104 | using namespace format_tag; |
105 | using namespace data_type; |
106 | const bool ok = !is_fwd() && !has_zero_dim_memory() |
107 | && platform::has_data_type_support(diff_dst_md()->data_type) |
108 | && platform::has_data_type_support(diff_src_md()->data_type) |
109 | && set_default_params() == status::success |
110 | && attr()->has_default_values(); |
111 | if (!ok) return status::unimplemented; |
112 | |
113 | format_tag_t dat_tag = memory_desc_matches_one_of_tag( |
114 | *diff_src_md(), nCw8c, nChw8c, nCdhw8c, nCw16c, nChw16c, |
115 | nCdhw16c, ncw, nchw, ncdhw, nwc, nhwc, ndhwc); |
116 | if (!memory_desc_matches_tag(*diff_dst_md(), dat_tag)) |
117 | return status::unimplemented; |
118 | |
119 | return status::success; |
120 | } |
121 | }; |
122 | |
123 | simple_resampling_bwd_t(const pd_t *apd); |
124 | status_t init(engine_t *engine) override; |
125 | ~simple_resampling_bwd_t() = default; |
126 | |
127 | status_t execute(const exec_ctx_t &ctx) const override; |
128 | |
129 | private: |
130 | const pd_t *pd() const { return (const pd_t *)primitive_t::pd().get(); } |
131 | |
132 | std::unique_ptr<simple_resampling_base_t> kernel_; |
133 | }; |
134 | |
135 | } // namespace cpu |
136 | } // namespace impl |
137 | } // namespace dnnl |
138 | |
139 | #endif |
140 | |