Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / src / cpu / ref_pooling.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_POOLING_HPP
18 #define CPU_REF_POOLING_HPP
19
20 #include <assert.h>
21
22 #include "c_types_map.hpp"
23 #include "cpu_pooling_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, impl::data_type_t acc_type = data_type>
33 struct ref_pooling_fwd_t: public cpu_primitive_t {
34     struct pd_t: public cpu_pooling_fwd_pd_t {
35         pd_t(engine_t *engine, const pooling_desc_t *adesc,
36                 const primitive_attr_t *attr,
37                 const pooling_fwd_pd_t *hint_fwd_pd)
38             : cpu_pooling_fwd_pd_t(engine, adesc, attr, hint_fwd_pd) {}
39
40         DECLARE_COMMON_PD_T("ref:any", ref_pooling_fwd_t);
41
42         virtual status_t init() override {
43             using namespace prop_kind;
44             using namespace alg_kind;
45             assert(engine()->kind() == engine_kind::cpu);
46             bool ok = true
47                 && set_default_params() == status::success
48                 && utils::one_of(desc()->prop_kind, forward_training,
49                         forward_inference)
50                 && utils::one_of(desc()->alg_kind, pooling_max,
51                         pooling_avg_include_padding,
52                         pooling_avg_exclude_padding)
53                 && utils::everyone_is(data_type, src_pd()->desc()->data_type,
54                         dst_pd()->desc()->data_type)
55                 && desc()->accum_data_type == acc_type
56                 && attr()->has_default_values();
57             if (!ok) return status::unimplemented;
58
59             bool is_training = desc_.prop_kind == forward_training;
60             if (desc()->alg_kind == pooling_max && is_training) {
61                 auto indices_desc = *dst_pd()->desc();
62                 indices_desc.data_type = pooling_index_data_type(desc());
63                 ws_pd_ = cpu_memory_t::pd_t(engine_, &indices_desc);
64             }
65
66             return status::success;
67         }
68     };
69
70     ref_pooling_fwd_t(const pd_t *apd, const input_vector &inputs,
71             const output_vector &outputs)
72         : cpu_primitive_t(apd, inputs, outputs) {}
73
74     typedef typename prec_traits<data_type>::type data_t;
75     typedef typename prec_traits<acc_type>::type acc_data_t;
76
77     virtual void execute(event_t *e) const {
78         execute_forward();
79         e->set_state(event_t::ready);
80     }
81
82 private:
83     void execute_forward() const;
84     const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
85 };
86
87 template <impl::data_type_t data_type, impl::data_type_t acc_type = data_type>
88 struct ref_pooling_bwd_t: public cpu_primitive_t {
89     struct pd_t: public cpu_pooling_bwd_pd_t {
90         pd_t(engine_t *engine, const pooling_desc_t *adesc,
91                 const primitive_attr_t *attr,
92                 const pooling_fwd_pd_t *hint_fwd_pd)
93             : cpu_pooling_bwd_pd_t(engine, adesc, attr, hint_fwd_pd) {}
94
95         DECLARE_COMMON_PD_T("ref:any", ref_pooling_bwd_t);
96
97         virtual status_t init() override {
98             using namespace prop_kind;
99             using namespace alg_kind;
100             assert(engine()->kind() == engine_kind::cpu);
101             bool ok = true
102                 && set_default_params() == status::success
103                 && utils::one_of(desc()->prop_kind, backward_data)
104                 && utils::one_of(desc()->alg_kind, pooling_max,
105                         pooling_avg_include_padding,
106                         pooling_avg_exclude_padding)
107                 && utils::everyone_is(data_type, diff_dst_pd()->desc()->data_type,
108                         diff_src_pd()->desc()->data_type)
109                 && IMPLICATION(desc()->alg_kind == pooling_max,
110                         hint_fwd_pd_ && hint_fwd_pd_->workspace_pd()
111                         && hint_fwd_pd_->workspace_pd()->engine()->kind()
112                                 == engine_kind::cpu)
113                 && attr()->has_default_values();
114             if (!ok) return status::unimplemented;
115
116             if (desc()->alg_kind == pooling_max)
117                 ws_pd_ = *(cpu_memory_t::pd_t*)hint_fwd_pd_->workspace_pd();
118
119             return status::success;
120         }
121     };
122
123     ref_pooling_bwd_t(const pd_t *apd, const input_vector &inputs,
124             const output_vector &outputs)
125         : cpu_primitive_t(apd, inputs, outputs) {}
126     typedef typename prec_traits<data_type>::type data_t;
127     typedef typename prec_traits<acc_type>::type acc_data_t;
128
129     virtual void execute(event_t *e) const {
130         execute_backward();
131         e->set_state(event_t::ready);
132     }
133
134 private:
135     void execute_backward() const;
136     const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
137 };
138
139 }
140 }
141 }
142
143 #endif
144
145 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s