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