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 ELTWISE_PD_HPP |
18 | #define ELTWISE_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 eltwise_fwd_pd_t; |
29 | |
30 | struct eltwise_pd_t: public primitive_desc_t { |
31 | static constexpr auto base_pkind = primitive_kind::eltwise; |
32 | |
33 | eltwise_pd_t(mkldnn::impl::engine_t *engine, |
34 | const eltwise_desc_t *adesc, |
35 | const primitive_attr_t *attr, |
36 | const eltwise_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 eltwise_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::eltwise_d: |
51 | *(const eltwise_desc_t**)result = desc(); break; |
52 | default: return primitive_desc_t::query(what, idx, result); |
53 | } |
54 | return status::success; |
55 | } |
56 | |
57 | /* common eltwise 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 | bool has_zero_dim_memory() const |
73 | { return memory_desc_wrapper(desc_.data_desc).has_zero_dim(); } |
74 | |
75 | protected: |
76 | eltwise_desc_t desc_; |
77 | const eltwise_fwd_pd_t *hint_fwd_pd_; |
78 | |
79 | memory_desc_t data_md_; |
80 | |
81 | private: |
82 | const memory_desc_t &data_desc() const { return desc_.data_desc; } |
83 | }; |
84 | |
85 | struct eltwise_fwd_pd_t: public eltwise_pd_t { |
86 | typedef eltwise_fwd_pd_t base_class; |
87 | typedef eltwise_fwd_pd_t hint_class; |
88 | |
89 | eltwise_fwd_pd_t(mkldnn::impl::engine_t *engine, |
90 | const eltwise_desc_t *adesc, |
91 | const primitive_attr_t *attr, |
92 | const eltwise_fwd_pd_t *hint_fwd_pd) |
93 | : eltwise_pd_t(engine, adesc, attr, hint_fwd_pd) |
94 | {} |
95 | |
96 | virtual arg_usage_t arg_usage(primitive_arg_index_t arg) const override { |
97 | if (arg == MKLDNN_ARG_SRC) |
98 | return arg_usage_t::input; |
99 | |
100 | if (arg == MKLDNN_ARG_DST) |
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 { return 1; } |
113 | |
114 | bool is_zero_preserved() const |
115 | { return math::eltwise_fwd_preserves_zero(desc_.alg_kind); } |
116 | }; |
117 | |
118 | struct eltwise_bwd_pd_t: public eltwise_pd_t { |
119 | typedef eltwise_bwd_pd_t base_class; |
120 | typedef eltwise_fwd_pd_t hint_class; |
121 | |
122 | eltwise_bwd_pd_t(engine_t *engine, |
123 | const eltwise_desc_t *adesc, |
124 | const primitive_attr_t *attr, |
125 | const eltwise_fwd_pd_t *hint_fwd_pd) |
126 | : eltwise_pd_t(engine, adesc, attr, hint_fwd_pd) |
127 | , diff_data_md_(desc_.diff_data_desc) |
128 | {} |
129 | |
130 | virtual arg_usage_t arg_usage(primitive_arg_index_t arg) const override { |
131 | if (utils::one_of(arg, MKLDNN_ARG_SRC, MKLDNN_ARG_DIFF_DST)) |
132 | return arg_usage_t::input; |
133 | |
134 | if (arg == MKLDNN_ARG_DIFF_SRC) |
135 | return arg_usage_t::output; |
136 | |
137 | return primitive_desc_t::arg_usage(arg); |
138 | } |
139 | |
140 | virtual const memory_desc_t *src_md(int index = 0) const override |
141 | { return index == 0 ? &data_md_ : nullptr; } |
142 | virtual const memory_desc_t *diff_dst_md(int index = 0) const override |
143 | { return index == 0 ? &diff_data_md_ : nullptr; } |
144 | virtual const memory_desc_t *diff_src_md(int index = 0) const override |
145 | { return index == 0 ? &diff_data_md_ : nullptr; } |
146 | |
147 | virtual int n_inputs() const override { return 2; } |
148 | virtual int n_outputs() const override { return 1; } |
149 | |
150 | bool is_zero_preserved() const { return true; } |
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 | |