Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / src / cpu / ref_lrn.hpp
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 CPU_REF_LRN_FWD_HPP
18 #define CPU_REF_LRN_FWD_HPP
19
20 #include <assert.h>
21
22 #include "c_types_map.hpp"
23 #include "cpu_lrn_pd.hpp"
24 #include "cpu_engine.hpp"
25 #include "type_helpers.hpp"
26 #include "utils.hpp"
27
28 namespace mkldnn {
29 namespace impl {
30 namespace cpu {
31
32 template <impl::data_type_t data_type>
33 struct ref_lrn_fwd_t: public cpu_primitive_t {
34     struct pd_t: public cpu_lrn_fwd_pd_t {
35         pd_t(engine_t *engine, const lrn_desc_t *adesc,
36                 const primitive_attr_t *attr, const lrn_fwd_pd_t *hint_fwd_pd)
37             : cpu_lrn_fwd_pd_t(engine, adesc, attr, hint_fwd_pd) {}
38
39         DECLARE_COMMON_PD_T("ref:any", ref_lrn_fwd_t);
40
41         virtual status_t init() override {
42             using namespace prop_kind;
43             using namespace alg_kind;
44             assert(engine()->kind() == engine_kind::cpu);
45             bool ok = true
46                 && utils::one_of(desc()->prop_kind, forward_training,
47                         forward_inference)
48                 && utils::one_of(desc()->alg_kind, lrn_across_channels,
49                         lrn_within_channel)
50                 && utils::everyone_is(data_type, desc()->data_desc.data_type)
51                 && attr()->has_default_values();
52             if (!ok) return status::unimplemented;
53
54             if (desc_.prop_kind == forward_training) { ws_pd_ = data_pd_; }
55
56             return status::success;
57         }
58     };
59
60     ref_lrn_fwd_t(const pd_t *apd, const input_vector &inputs,
61             const output_vector &outputs)
62         : cpu_primitive_t(apd, inputs, outputs) {}
63     typedef typename prec_traits<data_type>::type data_t;
64
65     virtual void execute(event_t *e) const {
66         using namespace memory_format;
67         switch (pd()->src_pd()->desc()->format) {
68         case nChw16c: execute_forward<nChw16c>(); break;
69         case nChw8c: execute_forward<nChw8c>(); break;
70         case nchw: execute_forward<nchw>(); break;
71         case nhwc: execute_forward<nhwc>(); break;
72         // XXX: fix compatibility with 0.14
73         // mkldnn_any is used to call ref code for arbitrary format
74         default: execute_forward<mkldnn_any>();
75         }
76         e->set_state(event_t::ready);
77     }
78
79 private:
80     template<memory_format_t fmt>void execute_forward() const;
81     const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
82 };
83
84 template <impl::data_type_t data_type>
85 struct ref_lrn_bwd_t: public cpu_primitive_t {
86     struct pd_t: public cpu_lrn_bwd_pd_t {
87         pd_t(engine_t *engine, const lrn_desc_t *adesc,
88                 const primitive_attr_t *attr, const lrn_fwd_pd_t *hint_fwd_pd)
89             : cpu_lrn_bwd_pd_t(engine, adesc, attr, hint_fwd_pd) {}
90
91         DECLARE_COMMON_PD_T("ref:any", ref_lrn_bwd_t);
92
93         virtual status_t init() override {
94             using namespace prop_kind;
95             using namespace alg_kind;
96             assert(engine()->kind() == engine_kind::cpu);
97             bool ok = true
98                 && utils::one_of(desc()->prop_kind, backward_data)
99                 && utils::one_of(desc()->alg_kind, lrn_across_channels
100                         /*, lrn_within_channel */) // not supported yet
101                 && utils::everyone_is(data_type, desc()->data_desc.data_type)
102                 && attr()->has_default_values();
103             if (!ok) return status::unimplemented;
104
105             return status::success;
106         }
107     };
108
109     ref_lrn_bwd_t(const pd_t *apd, const input_vector &inputs,
110             const output_vector &outputs)
111         : cpu_primitive_t(apd, inputs, outputs) {}
112     typedef typename prec_traits<data_type>::type data_t;
113
114     virtual void execute(event_t *e) const {
115         using namespace memory_format;
116         switch (pd()->src_pd()->desc()->format) {
117         case nChw16c: execute_backward<nChw16c>(); break;
118         case nChw8c: execute_backward<nChw8c>(); break;
119         case nchw: execute_backward<nchw>(); break;
120         case nhwc: execute_backward<nhwc>(); break;
121         // XXX: fix compatibility with 0.14
122         // mkldnn_any is used to call ref code for arbitrary format
123         default: execute_backward<mkldnn_any>();
124         }
125         e->set_state(event_t::ready);
126     }
127
128 private:
129     template<memory_format_t fmt>void execute_backward() const;
130     const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
131 };
132
133 }
134 }
135 }
136
137 #endif
138
139 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s