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 CPU_REF_LRN_HPP |
18 | #define CPU_REF_LRN_HPP |
19 | |
20 | #include <assert.h> |
21 | |
22 | #include "common/c_types_map.hpp" |
23 | #include "common/primitive.hpp" |
24 | #include "common/type_helpers.hpp" |
25 | #include "common/utils.hpp" |
26 | |
27 | #include "cpu/platform.hpp" |
28 | |
29 | #include "cpu/cpu_lrn_pd.hpp" |
30 | |
31 | namespace dnnl { |
32 | namespace impl { |
33 | namespace cpu { |
34 | |
35 | template <data_type_t d_type> |
36 | struct ref_lrn_fwd_t : public primitive_t { |
37 | struct pd_t : public cpu_lrn_fwd_pd_t { |
38 | using cpu_lrn_fwd_pd_t::cpu_lrn_fwd_pd_t; |
39 | |
40 | DECLARE_COMMON_PD_T("ref:any" , ref_lrn_fwd_t); |
41 | |
42 | status_t init(engine_t *engine) { |
43 | using namespace format_tag; |
44 | using namespace data_type; |
45 | |
46 | const memory_desc_wrapper src_d(src_md()); |
47 | const memory_desc_wrapper dst_d(dst_md()); |
48 | |
49 | bool ok = is_fwd() |
50 | && utils::everyone_is( |
51 | d_type, src_md()->data_type, dst_md()->data_type) |
52 | && platform::has_data_type_support(d_type) |
53 | && attr()->has_default_values() |
54 | && set_default_formats_common() && src_d == dst_d; |
55 | if (!ok) return status::unimplemented; |
56 | |
57 | dat_tag_ = memory_desc_matches_one_of_tag( |
58 | *src_md(), nChw16c, nChw8c, nchw, nhwc); |
59 | |
60 | return status::success; |
61 | } |
62 | |
63 | format_tag_t dat_tag_; |
64 | }; |
65 | |
66 | ref_lrn_fwd_t(const pd_t *apd) : primitive_t(apd) {} |
67 | typedef typename prec_traits<d_type>::type data_t; |
68 | |
69 | status_t execute(const exec_ctx_t &ctx) const override { |
70 | using namespace format_tag; |
71 | switch (pd()->dat_tag_) { |
72 | case nChw16c: return execute_forward<nChw16c>(ctx); break; |
73 | case nChw8c: return execute_forward<nChw8c>(ctx); break; |
74 | case nchw: return execute_forward<nchw>(ctx); break; |
75 | case nhwc: return execute_forward<nhwc>(ctx); break; |
76 | default: return execute_forward<any>(ctx); |
77 | } |
78 | return status::success; |
79 | } |
80 | |
81 | private: |
82 | template <format_tag_t tag> |
83 | status_t execute_forward(const exec_ctx_t &ctx) const; |
84 | const pd_t *pd() const { return (const pd_t *)primitive_t::pd().get(); } |
85 | }; |
86 | |
87 | template <impl::data_type_t d_type> |
88 | struct ref_lrn_bwd_t : public primitive_t { |
89 | struct pd_t : public cpu_lrn_bwd_pd_t { |
90 | using cpu_lrn_bwd_pd_t::cpu_lrn_bwd_pd_t; |
91 | |
92 | DECLARE_COMMON_PD_T("ref:any" , ref_lrn_bwd_t); |
93 | |
94 | status_t init(engine_t *engine) { |
95 | using namespace format_tag; |
96 | using namespace data_type; |
97 | |
98 | const memory_desc_wrapper diff_src_d(diff_src_md()); |
99 | const memory_desc_wrapper diff_dst_d(diff_dst_md()); |
100 | |
101 | bool ok = !is_fwd() |
102 | && utils::everyone_is(d_type, src_md()->data_type, |
103 | diff_src_md()->data_type, diff_dst_md()->data_type) |
104 | && platform::has_data_type_support(d_type) |
105 | && attr()->has_default_values() |
106 | && set_default_formats_common() && diff_dst_d == diff_src_d; |
107 | if (!ok) return status::unimplemented; |
108 | |
109 | dat_tag_ = memory_desc_matches_one_of_tag( |
110 | *src_md(), nChw16c, nChw8c, nchw, nhwc); |
111 | |
112 | return status::success; |
113 | } |
114 | |
115 | format_tag_t dat_tag_; |
116 | }; |
117 | |
118 | ref_lrn_bwd_t(const pd_t *apd) : primitive_t(apd) {} |
119 | typedef typename prec_traits<d_type>::type data_t; |
120 | |
121 | status_t execute(const exec_ctx_t &ctx) const override { |
122 | using namespace format_tag; |
123 | switch (pd()->dat_tag_) { |
124 | case nChw16c: return execute_backward<nChw16c>(ctx); break; |
125 | case nChw8c: return execute_backward<nChw8c>(ctx); break; |
126 | case nchw: return execute_backward<nchw>(ctx); break; |
127 | case nhwc: return execute_backward<nhwc>(ctx); break; |
128 | default: return execute_backward<any>(ctx); |
129 | } |
130 | return status::success; |
131 | } |
132 | |
133 | private: |
134 | template <format_tag_t tag> |
135 | status_t execute_backward(const exec_ctx_t &ctx) const; |
136 | const pd_t *pd() const { return (const pd_t *)primitive_t::pd().get(); } |
137 | }; |
138 | |
139 | } // namespace cpu |
140 | } // namespace impl |
141 | } // namespace dnnl |
142 | |
143 | #endif |
144 | |
145 | // vim: et ts=4 sw=4 cindent cino+=l0,\:4,N-s |
146 | |