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