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_SOFTMAX_PD_HPP
18#define COMMON_SOFTMAX_PD_HPP
19
20#include "oneapi/dnnl/dnnl.h"
21
22#include "c_types_map.hpp"
23#include "primitive_desc.hpp"
24
25namespace dnnl {
26namespace impl {
27
28struct softmax_fwd_pd_t;
29
30struct softmax_pd_t : public primitive_desc_t {
31 static constexpr auto base_pkind = primitive_kind::softmax;
32
33 const softmax_desc_t *desc() const { return &desc_; }
34 const op_desc_t *op_desc() const override {
35 return reinterpret_cast<const op_desc_t *>(this->desc());
36 }
37
38 status_t query(query_t what, int idx, void *result) const override {
39 switch (what) {
40 case query::prop_kind:
41 *(prop_kind_t *)result = desc()->prop_kind;
42 break;
43 case query::primitive_kind:
44 *(primitive_kind_t *)result = desc()->primitive_kind;
45 break;
46 case query::alg_kind:
47 *(alg_kind_t *)result = desc()->alg_kind;
48 break;
49 case query::axis_s32: *(int *)result = desc()->softmax_axis; break;
50 default: return primitive_desc_t::query(what, idx, result);
51 }
52 return status::success;
53 }
54
55 /* common softmax aux functions */
56
57 dim_t MB() const { return dst_desc().dims[0]; }
58 dim_t C() const { return dst_desc().dims[1]; }
59 dim_t D() const { return ndims() >= 5 ? dst_desc().dims[ndims() - 3] : 1; }
60 dim_t H() const { return ndims() >= 4 ? dst_desc().dims[ndims() - 2] : 1; }
61 dim_t W() const { return ndims() >= 3 ? dst_desc().dims[ndims() - 1] : 1; }
62
63 dim_t outer_size() const {
64 return utils::array_product(dst_desc().dims, axis());
65 }
66 dim_t axis_size(bool padded = false) const {
67 return padded ? dst_desc().padded_dims[axis()]
68 : dst_desc().dims[axis()];
69 }
70 dim_t inner_size() const {
71 return utils::array_product(
72 dst_desc().dims + axis() + 1, ndims() - 1 - axis());
73 }
74
75 dim_t outer_stride() const {
76 const memory_desc_wrapper dst_d(dst_desc());
77 return axis() > 0 ? dst_d.blocking_desc().strides[axis() - 1] : 1;
78 }
79
80 int axis() const { return desc_.softmax_axis; }
81 int ndims() const { return dst_desc().ndims; }
82
83 bool is_fwd() const {
84 return utils::one_of(desc_.prop_kind, prop_kind::forward_training,
85 prop_kind::forward_inference);
86 }
87
88 bool has_zero_dim_memory() const {
89 return memory_desc_wrapper(dst_desc()).has_zero_dim();
90 }
91
92 alg_kind_t alg_kind() const { return desc()->alg_kind; }
93 bool is_softmax() const { return alg_kind() == alg_kind::softmax_accurate; }
94 bool is_logsoftmax() const { return alg_kind() == alg_kind::softmax_log; }
95
96protected:
97 softmax_desc_t desc_;
98 const softmax_fwd_pd_t *hint_fwd_pd_;
99
100 memory_desc_t dst_md_;
101
102 softmax_pd_t(const softmax_desc_t *adesc, const primitive_attr_t *attr,
103 const softmax_fwd_pd_t *hint_fwd_pd)
104 : primitive_desc_t(attr, base_pkind)
105 , desc_(*adesc)
106 , hint_fwd_pd_(hint_fwd_pd)
107 , dst_md_(desc_.dst_desc) {}
108
109private:
110 const memory_desc_t &dst_desc() const { return dst_md_; }
111};
112
113struct softmax_fwd_pd_t : public softmax_pd_t {
114 typedef softmax_fwd_pd_t base_class;
115 typedef softmax_fwd_pd_t hint_class;
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 if (arg == DNNL_ARG_WORKSPACE && (!types::is_zero_md(workspace_md())))
123 return arg_usage_t::output;
124
125 return primitive_desc_t::arg_usage(arg);
126 }
127
128 const memory_desc_t *arg_md(int arg) const override {
129 switch (arg) {
130 case DNNL_ARG_SRC: return src_md(0);
131 case DNNL_ARG_DST: return dst_md(0);
132 default: return softmax_pd_t::arg_md(arg);
133 }
134 }
135
136 const memory_desc_t *src_md(int index = 0) const override {
137 return index == 0 ? &src_md_ : &glob_zero_md;
138 }
139 const memory_desc_t *dst_md(int index = 0) const override {
140 return index == 0 ? &dst_md_ : &glob_zero_md;
141 }
142
143 int n_inputs() const override { return 1; }
144 int n_outputs() const override {
145 return 1 + (!types::is_zero_md(workspace_md()));
146 }
147
148protected:
149 memory_desc_t src_md_;
150
151 softmax_fwd_pd_t(const softmax_desc_t *adesc, const primitive_attr_t *attr,
152 const softmax_fwd_pd_t *hint_fwd_pd)
153 : softmax_pd_t(adesc, attr, hint_fwd_pd), src_md_(desc_.src_desc) {}
154
155 status_t set_default_formats() {
156 if (dst_md()->format_kind != format_kind::any) return status::success;
157
158 if (src_md()->format_kind != format_kind::blocked)
159 return status::unimplemented;
160
161 return memory_desc_init_by_blocking_desc(
162 dst_md_, src_md_.format_desc.blocking);
163 }
164
165 bool attr_scales_ok() const {
166 const auto &scales = attr()->scales_;
167 bool ok = true;
168 for (const auto &e : scales.scales_) {
169 ok = ok && e.second.mask_ == 0;
170 }
171 return ok;
172 }
173};
174
175struct softmax_bwd_pd_t : public softmax_pd_t {
176 typedef softmax_bwd_pd_t base_class;
177 typedef softmax_fwd_pd_t hint_class;
178
179 arg_usage_t arg_usage(int arg) const override {
180 if (utils::one_of(arg, DNNL_ARG_DST, DNNL_ARG_DIFF_DST))
181 return arg_usage_t::input;
182
183 if (arg == DNNL_ARG_DIFF_SRC) return arg_usage_t::output;
184
185 if (arg == DNNL_ARG_WORKSPACE && (!types::is_zero_md(workspace_md())))
186 return arg_usage_t::input;
187
188 return primitive_desc_t::arg_usage(arg);
189 }
190
191 const memory_desc_t *arg_md(int arg) const override {
192 switch (arg) {
193 case DNNL_ARG_DST: return dst_md(0);
194 case DNNL_ARG_DIFF_SRC: return diff_src_md(0);
195 case DNNL_ARG_DIFF_DST: return diff_dst_md(0);
196 default: return softmax_pd_t::arg_md(arg);
197 }
198 }
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 *diff_dst_md(int index = 0) const override {
204 return index == 0 ? &diff_dst_md_ : &glob_zero_md;
205 }
206 const memory_desc_t *diff_src_md(int index = 0) const override {
207 return index == 0 ? &diff_src_md_ : &glob_zero_md;
208 }
209
210 int n_inputs() const override {
211 return 2 + (!types::is_zero_md(workspace_md()));
212 }
213 int n_outputs() const override { return 1; }
214
215protected:
216 memory_desc_t diff_src_md_;
217 memory_desc_t diff_dst_md_;
218
219 softmax_bwd_pd_t(const softmax_desc_t *adesc, const primitive_attr_t *attr,
220 const softmax_fwd_pd_t *hint_fwd_pd)
221 : softmax_pd_t(adesc, attr, hint_fwd_pd)
222 , diff_src_md_(desc_.diff_src_desc)
223 , diff_dst_md_(desc_.diff_dst_desc) {}
224
225 status_t set_default_formats() {
226 status_t st = status::invalid_arguments;
227 if (diff_dst_md_.format_kind == format_kind::any) {
228 st = memory_desc_init_by_md_and_dt(
229 diff_dst_md_, dst_md_, diff_dst_md_.data_type);
230 if (st != status::success) return st;
231 }
232 if (diff_src_md_.format_kind == format_kind::any) {
233 st = memory_desc_init_by_md_and_dt(
234 diff_src_md_, diff_dst_md_, diff_src_md_.data_type);
235 if (st != status::success) return st;
236 }
237 return status::success;
238 }
239};
240
241} // namespace impl
242} // namespace dnnl
243
244#endif
245
246// vim: et ts=4 sw=4 cindent cino+=l0,\:4,N-s
247