1 | /******************************************************************************* |
2 | * Copyright 2018-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_SHUFFLE_PD_HPP |
18 | #define COMMON_SHUFFLE_PD_HPP |
19 | |
20 | #include "oneapi/dnnl/dnnl.h" |
21 | |
22 | #include "c_types_map.hpp" |
23 | #include "primitive_desc.hpp" |
24 | |
25 | namespace dnnl { |
26 | namespace impl { |
27 | |
28 | struct shuffle_pd_t : public primitive_desc_t { |
29 | static constexpr auto base_pkind = primitive_kind::shuffle; |
30 | |
31 | typedef shuffle_pd_t base_class; |
32 | typedef shuffle_pd_t hint_class; |
33 | |
34 | const shuffle_desc_t *desc() const { return &desc_; } |
35 | const op_desc_t *op_desc() const override { |
36 | return reinterpret_cast<const op_desc_t *>(this->desc()); |
37 | } |
38 | |
39 | status_t query(query_t what, int idx, void *result) const override { |
40 | switch (what) { |
41 | case query::prop_kind: |
42 | *(prop_kind_t *)result = desc()->prop_kind; |
43 | break; |
44 | case query::axis_s32: *(int *)result = desc()->axis; break; |
45 | case query::group_size_s64: |
46 | *(dim_t *)result = desc()->group_size; |
47 | break; |
48 | default: return primitive_desc_t::query(what, idx, result); |
49 | } |
50 | return status::success; |
51 | } |
52 | |
53 | arg_usage_t arg_usage(int arg) const override { |
54 | if (is_fwd()) { |
55 | if (arg == DNNL_ARG_SRC) return arg_usage_t::input; |
56 | |
57 | if (arg == DNNL_ARG_DST) return arg_usage_t::output; |
58 | } else { |
59 | if (arg == DNNL_ARG_DIFF_DST) return arg_usage_t::input; |
60 | |
61 | if (arg == DNNL_ARG_DIFF_SRC) return arg_usage_t::output; |
62 | } |
63 | |
64 | return primitive_desc_t::arg_usage(arg); |
65 | } |
66 | |
67 | const memory_desc_t *arg_md(int arg) const override { |
68 | switch (arg) { |
69 | case DNNL_ARG_SRC: return src_md(0); |
70 | case DNNL_ARG_DST: return dst_md(0); |
71 | case DNNL_ARG_DIFF_SRC: return diff_src_md(0); |
72 | case DNNL_ARG_DIFF_DST: return diff_dst_md(0); |
73 | default: return primitive_desc_t::arg_md(arg); |
74 | } |
75 | } |
76 | |
77 | const memory_desc_t *src_md(int index = 0) const override { |
78 | return index == 0 && is_fwd() ? &src_md_ : &glob_zero_md; |
79 | } |
80 | const memory_desc_t *dst_md(int index = 0) const override { |
81 | return index == 0 && is_fwd() ? &dst_md_ : &glob_zero_md; |
82 | } |
83 | |
84 | const memory_desc_t *diff_src_md(int index = 0) const override { |
85 | return index == 0 && !is_fwd() ? &src_md_ : &glob_zero_md; |
86 | } |
87 | const memory_desc_t *diff_dst_md(int index = 0) const override { |
88 | return index == 0 && !is_fwd() ? &dst_md_ : &glob_zero_md; |
89 | } |
90 | |
91 | int n_inputs() const override { return 1; } |
92 | int n_outputs() const override { return 1; } |
93 | |
94 | /* shuffle aux functions */ |
95 | |
96 | dim_t MB() const { return data_md()->dims[0]; } |
97 | dim_t C() const { return ndims() >= 2 ? data_md()->dims[1] : 1; } |
98 | dim_t D() const { return ndims() >= 5 ? data_md()->dims[ndims() - 3] : 1; } |
99 | dim_t H() const { return ndims() >= 4 ? data_md()->dims[ndims() - 2] : 1; } |
100 | dim_t W() const { return ndims() >= 3 ? data_md()->dims[ndims() - 1] : 1; } |
101 | |
102 | int ndims() const { return data_md()->ndims; } |
103 | |
104 | int axis() const { return desc_.axis; } |
105 | dim_t group_size() const { return desc_.group_size; } |
106 | dim_t axis_size() const { return data_md()->dims[axis()]; } |
107 | |
108 | bool is_fwd() const { |
109 | return utils::one_of(desc_.prop_kind, prop_kind::forward_training, |
110 | prop_kind::forward_inference); |
111 | } |
112 | |
113 | std::vector<memory_desc_t> hint_mds(bool is_hint) const override { |
114 | assert(IMPLICATION(is_hint, is_fwd())); |
115 | if (!is_hint && is_fwd()) return {}; |
116 | if (is_hint && is_fwd()) return {*dst_md(0)}; |
117 | return hint_mds_; |
118 | } |
119 | |
120 | protected: |
121 | shuffle_desc_t desc_; |
122 | const shuffle_pd_t *hint_fwd_pd_; |
123 | memory_desc_t src_md_; |
124 | memory_desc_t dst_md_; |
125 | |
126 | shuffle_pd_t(const shuffle_desc_t *adesc, const primitive_attr_t *attr, |
127 | const shuffle_pd_t *hint_fwd_pd) |
128 | : primitive_desc_t(attr, base_pkind) |
129 | , desc_(*adesc) |
130 | , hint_fwd_pd_(hint_fwd_pd) |
131 | , src_md_(desc_.src_desc) |
132 | , dst_md_(desc_.dst_desc) { |
133 | if (hint_fwd_pd_) hint_mds_.push_back(*hint_fwd_pd_->dst_md(0)); |
134 | } |
135 | |
136 | bool set_default_formats_common() { |
137 | // `src_md_.format_kind == format_kind::any` may be true only for |
138 | // backward prop kind. |
139 | return IMPLICATION(src_md_.format_kind == format_kind::any, |
140 | hint_fwd_pd_ |
141 | ? memory_desc_init_by_md_and_dt(src_md_, |
142 | hint_mds(/* is_hint = */ false)[0], |
143 | src_md_.data_type) |
144 | == status::success |
145 | : memory_desc_init_by_strides(src_md_, nullptr) |
146 | == status::success) |
147 | && IMPLICATION(dst_md_.format_kind == format_kind::any, |
148 | memory_desc_init_by_md_and_dt( |
149 | dst_md_, src_md_, dst_md_.data_type) |
150 | == status::success); |
151 | } |
152 | |
153 | private: |
154 | std::vector<memory_desc_t> hint_mds_; |
155 | |
156 | const memory_desc_t *data_md() const { |
157 | return is_fwd() ? src_md() : diff_src_md(); |
158 | } |
159 | }; |
160 | |
161 | } // namespace impl |
162 | } // namespace dnnl |
163 | |
164 | #endif |
165 | |
166 | // vim: et ts=4 sw=4 cindent cino+=l0,\:4,N-s |
167 | |