Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / src / cpu / ref_batch_normalization.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_BATCH_NORMALIZATION_FWD_HPP
18 #define CPU_REF_BATCH_NORMALIZATION_FWD_HPP
19
20 #include <assert.h>
21
22 #include "c_types_map.hpp"
23 #include "cpu_batch_normalization_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_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(engine, adesc, attr,
39                     hint_fwd_pd) {}
40
41         DECLARE_COMMON_PD_T("ref:any", ref_batch_normalization_fwd_t);
42
43         virtual status_t init() override {
44             using namespace prop_kind;
45             assert(engine()->kind() == engine_kind::cpu);
46             bool ok = true
47                 && utils::one_of(desc()->prop_kind, forward_training,
48                         forward_inference)
49                 && utils::everyone_is(data_type, desc()->data_desc.data_type,
50                         desc()->data_scaleshift_desc.data_type)
51                 && (attr()->has_default_values() || this->with_relu_post_op());
52             if (!ok) return status::unimplemented;
53
54             if (stats_is_src() || is_training()) {
55                 memory_desc_t stats_d;
56                 dims_t stats_dims = { C() };
57                 mkldnn_memory_desc_init(&stats_d, 1, stats_dims, data_type,
58                         memory_format::x);
59                 mean_pd_ = cpu_memory_t::pd_t(engine_, &stats_d);
60                 variance_pd_ = cpu_memory_t::pd_t(engine_, &stats_d);
61             }
62
63             if (is_training() && fuse_bn_relu())
64                 bn_init_default_ws(this, this->workspace_pd_, 8);
65
66             return status::success;
67         }
68     };
69
70     ref_batch_normalization_fwd_t(const pd_t *apd, const input_vector &inputs,
71             const output_vector &outputs)
72         : cpu_primitive_t(apd, inputs, outputs) {}
73     typedef typename prec_traits<data_type>::type data_t;
74
75     virtual void execute(event_t *e) const {
76         execute_forward();
77         e->set_state(event_t::ready);
78     }
79
80 private:
81     void execute_forward() const;
82     const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
83 };
84
85 template <impl::data_type_t data_type>
86 struct ref_batch_normalization_bwd_t: public cpu_primitive_t {
87     struct pd_t: public cpu_batch_normalization_bwd_pd_t {
88         pd_t(engine_t *engine, const batch_normalization_desc_t *adesc,
89                 const primitive_attr_t *attr,
90                 const batch_normalization_fwd_pd_t *hint_fwd_pd)
91             : cpu_batch_normalization_bwd_pd_t(engine, adesc, attr,
92                     hint_fwd_pd) {}
93
94         DECLARE_COMMON_PD_T("ref:any", ref_batch_normalization_bwd_t);
95
96         virtual status_t init() override {
97             using namespace prop_kind;
98             assert(engine()->kind() == engine_kind::cpu);
99             bool ok = true
100                 && utils::one_of(desc()->prop_kind, backward, backward_data)
101                 && utils::everyone_is(data_type, desc()->data_desc.data_type,
102                         desc()->diff_data_desc.data_type,
103                         desc()->data_desc.data_type,
104                         desc()->data_scaleshift_desc.data_type)
105                 && attr()->has_default_values()
106                 && hint_fwd_pd_ != nullptr;
107             if (!ok) return status::unimplemented;
108
109             if (fuse_bn_relu()) {
110                 bn_init_default_ws(this, this->workspace_pd_, 8);
111                 const size_t this_ws_sz
112                     = memory_desc_wrapper(this->workspace_pd()).size();
113
114                 bool ws_ok = true
115                     && hint_fwd_pd_->workspace_pd()
116                     && memory_desc_wrapper(hint_fwd_pd_->workspace_pd()).size()
117                             == this_ws_sz;
118                 if (!ws_ok)
119                     return status::unimplemented;
120             }
121
122             bool stats_ok = true
123                 && hint_fwd_pd_->mean_pd()->desc()->ndims == 1
124                 && hint_fwd_pd_->mean_pd()->desc()->format == memory_format::x
125                 && hint_fwd_pd_->mean_pd()->desc()->data_type == data_type
126                 && hint_fwd_pd_->variance_pd()->desc()->ndims == 1
127                 && hint_fwd_pd_->variance_pd()->desc()->format == memory_format::x
128                 && hint_fwd_pd_->variance_pd()->desc()->data_type == data_type;
129             if (!stats_ok) return status::unimplemented;
130
131             return status::success;
132         }
133     };
134
135     ref_batch_normalization_bwd_t(const pd_t *apd, const input_vector &inputs,
136             const output_vector &outputs)
137         : cpu_primitive_t(apd, inputs, outputs) {}
138     typedef typename prec_traits<data_type>::type data_t;
139
140     virtual void execute(event_t *e) const {
141         execute_backward();
142         e->set_state(event_t::ready);
143     }
144
145 private:
146     void execute_backward() const;
147     const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
148 };
149
150 }
151 }
152 }
153
154 #endif
155
156 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s