1 | /******************************************************************************* |
2 | * Copyright 2016-2018 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 SOFTMAX_PD_HPP |
18 | #define SOFTMAX_PD_HPP |
19 | |
20 | #include "mkldnn.h" |
21 | |
22 | #include "c_types_map.hpp" |
23 | #include "primitive_desc.hpp" |
24 | |
25 | namespace mkldnn { |
26 | namespace impl { |
27 | |
28 | struct softmax_fwd_pd_t; |
29 | |
30 | struct softmax_pd_t: public primitive_desc_t { |
31 | static constexpr auto base_pkind = primitive_kind::softmax; |
32 | |
33 | softmax_pd_t(engine_t *engine, |
34 | const softmax_desc_t *adesc, |
35 | const primitive_attr_t *attr, |
36 | const softmax_fwd_pd_t *hint_fwd_pd) |
37 | : primitive_desc_t(engine, attr, base_pkind) |
38 | , desc_(*adesc) |
39 | , hint_fwd_pd_(hint_fwd_pd) |
40 | , data_md_(desc_.data_desc) |
41 | {} |
42 | |
43 | const softmax_desc_t *desc() const { return &desc_; } |
44 | virtual const op_desc_t *op_desc() const override |
45 | { return reinterpret_cast<const op_desc_t *>(this->desc()); } |
46 | virtual void init_info() override { impl::init_info(this, this->info_); } |
47 | |
48 | virtual status_t query(query_t what, int idx, void *result) const override { |
49 | switch (what) { |
50 | case query::softmax_d: |
51 | *(const softmax_desc_t**)result = desc(); break; |
52 | default: return primitive_desc_t::query(what, idx, result); |
53 | } |
54 | return status::success; |
55 | } |
56 | |
57 | /* common softmax aux functions */ |
58 | |
59 | dim_t MB() const { return data_desc().dims[0]; } |
60 | dim_t C() const { return data_desc().dims[1]; } |
61 | dim_t D() const { return ndims() >= 5 ? data_desc().dims[ndims() - 3] : 1; } |
62 | dim_t H() const { return ndims() >= 4 ? data_desc().dims[ndims() - 2] : 1; } |
63 | dim_t W() const { return ndims() >= 3 ? data_desc().dims[ndims() - 1] : 1; } |
64 | |
65 | int ndims() const { return data_desc().ndims; } |
66 | |
67 | bool is_fwd() const { |
68 | return utils::one_of(desc_.prop_kind, prop_kind::forward_training, |
69 | prop_kind::forward_inference); |
70 | } |
71 | |
72 | protected: |
73 | softmax_desc_t desc_; |
74 | const softmax_fwd_pd_t *hint_fwd_pd_; |
75 | |
76 | memory_desc_t data_md_; |
77 | |
78 | private: |
79 | const memory_desc_t &data_desc() const { return desc_.data_desc; } |
80 | }; |
81 | |
82 | struct softmax_fwd_pd_t: public softmax_pd_t { |
83 | typedef softmax_fwd_pd_t base_class; |
84 | typedef softmax_fwd_pd_t hint_class; |
85 | |
86 | softmax_fwd_pd_t(engine_t *engine, |
87 | const softmax_desc_t *adesc, |
88 | const primitive_attr_t *attr, |
89 | const softmax_fwd_pd_t *hint_fwd_pd) |
90 | : softmax_pd_t(engine, adesc, attr, hint_fwd_pd) |
91 | {} |
92 | |
93 | virtual arg_usage_t arg_usage(primitive_arg_index_t arg) const override { |
94 | if (arg == MKLDNN_ARG_SRC) |
95 | return arg_usage_t::input; |
96 | |
97 | if (arg == MKLDNN_ARG_DST) |
98 | return arg_usage_t::output; |
99 | |
100 | if (arg == MKLDNN_ARG_WORKSPACE && (workspace_md() != nullptr)) |
101 | return arg_usage_t::output; |
102 | |
103 | return primitive_desc_t::arg_usage(arg); |
104 | } |
105 | |
106 | virtual const memory_desc_t *src_md(int index = 0) const override |
107 | { return index == 0 ? &data_md_ : nullptr; } |
108 | virtual const memory_desc_t *dst_md(int index = 0) const override |
109 | { return index == 0 ? &data_md_ : nullptr; } |
110 | |
111 | virtual int n_inputs() const override { return 1; } |
112 | virtual int n_outputs() const override |
113 | { return 1 + (workspace_md() != nullptr); } |
114 | }; |
115 | |
116 | struct softmax_bwd_pd_t: public softmax_pd_t { |
117 | typedef softmax_bwd_pd_t base_class; |
118 | typedef softmax_fwd_pd_t hint_class; |
119 | |
120 | softmax_bwd_pd_t(engine_t *engine, |
121 | const softmax_desc_t *adesc, |
122 | const primitive_attr_t *attr, |
123 | const softmax_fwd_pd_t *hint_fwd_pd) |
124 | : softmax_pd_t(engine, adesc, attr, hint_fwd_pd) |
125 | , diff_data_md_(desc_.diff_desc) |
126 | {} |
127 | |
128 | virtual arg_usage_t arg_usage(primitive_arg_index_t arg) const override { |
129 | if (utils::one_of(arg, MKLDNN_ARG_DST, MKLDNN_ARG_DIFF_DST)) |
130 | return arg_usage_t::input; |
131 | |
132 | if (arg == MKLDNN_ARG_DIFF_SRC) |
133 | return arg_usage_t::output; |
134 | |
135 | if (arg == MKLDNN_ARG_WORKSPACE && (workspace_md() != nullptr)) |
136 | return arg_usage_t::input; |
137 | |
138 | return primitive_desc_t::arg_usage(arg); |
139 | } |
140 | |
141 | virtual const memory_desc_t *dst_md(int index = 0) const override |
142 | { return index == 0 ? &data_md_ : nullptr; } |
143 | virtual const memory_desc_t *diff_dst_md(int index = 0) const override |
144 | { return index == 0 ? &diff_data_md_ : nullptr; } |
145 | virtual const memory_desc_t *diff_src_md(int index = 0) const override |
146 | { return index == 0 ? &diff_data_md_ : nullptr; } |
147 | |
148 | virtual int n_inputs() const override |
149 | { return 2 + (workspace_md() != nullptr); } |
150 | virtual int n_outputs() const override { return 1; } |
151 | |
152 | protected: |
153 | memory_desc_t diff_data_md_; |
154 | }; |
155 | |
156 | } |
157 | } |
158 | |
159 | #endif |
160 | |
161 | // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s |
162 | |