Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / src / cpu / ncsp_batch_normalization.hpp
1 /*******************************************************************************
2 * Copyright 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_NCSP_BATCH_NORMALIZATION_HPP
18 #define CPU_NCSP_BATCH_NORMALIZATION_HPP
19
20 #include <assert.h>
21
22 #include "c_types_map.hpp"
23 #include "memory_tracking.hpp"
24 #include "type_helpers.hpp"
25 #include "utils.hpp"
26
27 #include "cpu_batch_normalization_pd.hpp"
28
29 namespace mkldnn {
30 namespace impl {
31 namespace cpu {
32
33 struct ncsp_batch_normalization_fwd_t : public cpu_primitive_t {
34     struct pd_t : public cpu_batch_normalization_fwd_pd_t {
35         pd_t(engine_t *engine, const batch_normalization_desc_t *adesc,
36                 const primitive_attr_t *attr,
37                 const batch_normalization_fwd_pd_t *hint_fwd_pd)
38             : cpu_batch_normalization_fwd_pd_t(
39                       engine, adesc, attr, hint_fwd_pd) {}
40
41         DECLARE_COMMON_PD_T("ncsp_bnorm:any", ncsp_batch_normalization_fwd_t);
42
43         virtual status_t init() override {
44             using namespace data_type;
45             using namespace prop_kind;
46
47             assert(engine()->kind() == engine_kind::cpu);
48
49             bool ok = true
50                 && is_fwd()
51                 && !has_zero_dim_memory()
52                 && desc()->data_desc.data_type == f32
53                 && IMPLICATION(use_scaleshift(),
54                         desc()->data_scaleshift_desc.data_type == f32)
55                 && utils::one_of(data_pd_.desc()->format, memory_format::nchw,
56                         memory_format::ncdhw, memory_format::nc)
57                 && (attr()->has_default_values() || this->with_relu_post_op());
58             if (!ok) return status::unimplemented;
59
60             if (is_training() && fuse_bn_relu())
61                 bn_init_default_ws(this, this->workspace_pd_, 8);
62
63             if (stats_is_src() || is_training()) {
64                 memory_desc_t stats_d;
65                 dims_t stats_dims = { C() };
66                 mkldnn_memory_desc_init(&stats_d, 1, stats_dims,
67                         data_type::f32, memory_format::x);
68                 mean_pd_ = cpu_memory_t::pd_t(engine_, &stats_d);
69                 variance_pd_ = cpu_memory_t::pd_t(engine_, &stats_d);
70             }
71
72             init_scratchpad();
73
74             return success;
75         }
76
77     private:
78         void init_scratchpad() {
79             using namespace memory_tracking::names;
80             auto scratchpad = scratchpad_registry().registrar();
81             if (!stats_is_src()) {
82                 scratchpad.book(key_bnorm_reduction,
83                         sizeof(data_t) * C() * mkldnn_get_max_threads());
84
85                 if (!is_training()) {
86                     scratchpad.book(key_bnorm_tmp_mean, sizeof(data_t) * C());
87                     scratchpad.book(key_bnorm_tmp_var, sizeof(data_t) * C());
88                 }
89             }
90         }
91     };
92
93     typedef typename prec_traits<data_type::f32>::type data_t;
94
95     ncsp_batch_normalization_fwd_t(const pd_t *apd, const input_vector &inputs,
96             const output_vector &outputs)
97         : cpu_primitive_t(apd, inputs, outputs) {}
98     ~ncsp_batch_normalization_fwd_t() {}
99
100     virtual void execute(event_t *e) const {
101         execute_forward();
102         e->set_state(event_t::ready);
103     }
104
105 private:
106     void execute_forward() const;
107     const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
108 };
109
110 struct ncsp_batch_normalization_bwd_t : public cpu_primitive_t {
111     struct pd_t : public cpu_batch_normalization_bwd_pd_t {
112         pd_t(engine_t *engine, const batch_normalization_desc_t *adesc,
113                 const primitive_attr_t *attr,
114                 const batch_normalization_fwd_pd_t *hint_fwd_pd)
115             : cpu_batch_normalization_bwd_pd_t(
116                     engine, adesc, attr, hint_fwd_pd) {}
117
118         DECLARE_COMMON_PD_T("ncsp_bnorm:any", ncsp_batch_normalization_bwd_t);
119
120         virtual status_t init() override {
121             using namespace data_type;
122             assert(engine()->kind() == engine_kind::cpu);
123
124             bool ok = true
125                 && is_bwd()
126                 && !has_zero_dim_memory()
127                 && desc()->data_desc.data_type == f32
128                 && IMPLICATION(use_scaleshift(),
129                         desc()->data_scaleshift_desc.data_type == f32)
130                 && utils::one_of(data_pd_.desc()->format, memory_format::nchw,
131                         memory_format::ncdhw, memory_format::nc)
132                 && attr()->has_default_values();
133             if (!ok) return status::unimplemented;
134
135             if (fuse_bn_relu()) {
136                 bn_init_default_ws(this, this->workspace_pd_, 8);
137                 const size_t this_ws_sz
138                     = memory_desc_wrapper(this->workspace_pd()).size();
139
140                 bool ws_ok = true
141                     && hint_fwd_pd_->workspace_pd()
142                     && memory_desc_wrapper(hint_fwd_pd_->workspace_pd()).size()
143                     == this_ws_sz;
144                 if (!ws_ok) return status::unimplemented;
145             }
146
147             init_scratchpad();
148
149             return success;
150         }
151
152     private:
153         void init_scratchpad() {
154             using namespace memory_tracking::names;
155             auto scratchpad = scratchpad_registry().registrar();
156             scratchpad.book(key_bnorm_reduction,
157                     sizeof(data_t) * 2 * C() * mkldnn_get_max_threads());
158             if (!(use_scaleshift() && desc()->prop_kind == prop_kind::backward))
159                 scratchpad.book(key_bnorm_tmp_diff_ss,
160                         sizeof(data_t) * 2 * C());
161         }
162     };
163
164     typedef typename prec_traits<data_type::f32>::type data_t;
165
166     ncsp_batch_normalization_bwd_t(const pd_t *apd, const input_vector &inputs,
167             const output_vector &outputs)
168         : cpu_primitive_t(apd, inputs, outputs) {}
169     ~ncsp_batch_normalization_bwd_t() {}
170
171     virtual void execute(event_t *e) const {
172         execute_backward();
173         e->set_state(event_t::ready);
174     }
175
176 private:
177     void execute_backward() const;
178     const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
179 };
180
181 }
182 }
183 }
184
185 #endif
186
187 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s