updated readme file due to moving CMake scripts to the root folder
[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 "cpu_isa_traits.hpp"
26 #include "type_helpers.hpp"
27 #include "utils.hpp"
28
29 namespace mkldnn {
30 namespace impl {
31 namespace cpu {
32
33 template <data_type_t data_type>
34 struct ref_batch_normalization_fwd_t : public cpu_primitive_t {
35     struct pd_t: public cpu_batch_normalization_fwd_pd_t {
36         pd_t(engine_t *engine, const batch_normalization_desc_t *adesc,
37                 const primitive_attr_t *attr,
38                 const batch_normalization_fwd_pd_t *hint_fwd_pd)
39             : cpu_batch_normalization_fwd_pd_t(engine, adesc, attr,
40                     hint_fwd_pd) {}
41
42         DECLARE_COMMON_PD_T("ref:any", ref_batch_normalization_fwd_t);
43
44         virtual status_t init() override {
45             using namespace data_type;
46             using namespace prop_kind;
47             assert(engine()->kind() == engine_kind::cpu);
48             bool ok = true
49                 && is_fwd()
50                 && !has_zero_dim_memory()
51                 && utils::one_of(desc()->prop_kind, forward_training,
52                         forward_inference)
53                 && desc()->data_desc.data_type == data_type
54                 && IMPLICATION(use_scaleshift(),
55                         desc()->data_scaleshift_desc.data_type == f32)
56                 && utils::everyone_is(f32,
57                         desc()->mean_desc.data_type,
58                         desc()->variance_desc.data_type)
59                 && IMPLICATION(data_type == bf16, mayiuse(avx512_core))
60                 && (attr()->has_default_values() || this->with_relu_post_op());
61             if (!ok) return status::unimplemented;
62
63             if (desc()->data_desc.data_type == data_type::s8 && !stats_is_src())
64                 return status::unimplemented;
65
66             if (stats_is_src() || is_training()) {
67                 memory_desc_t stats_d;
68                 dims_t stats_dims = { C() };
69                 mkldnn_memory_desc_init(
70                         &stats_d, 1, stats_dims, f32, memory_format::x);
71                 mean_pd_ = cpu_memory_t::pd_t(engine_, &stats_d);
72                 variance_pd_ = cpu_memory_t::pd_t(engine_, &stats_d);
73             }
74
75             if (is_training() && fuse_bn_relu())
76                 bn_init_default_ws(this, this->workspace_pd_, 8);
77
78             return status::success;
79         }
80     };
81
82     ref_batch_normalization_fwd_t(const pd_t *apd, const input_vector &inputs,
83             const output_vector &outputs)
84         : cpu_primitive_t(apd, inputs, outputs) {}
85
86     typedef typename prec_traits<data_type>::type data_t;
87
88     virtual void execute(event_t *e) const {
89         execute_forward();
90         e->set_state(event_t::ready);
91     }
92
93 private:
94     void execute_forward() const;
95     const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
96 };
97
98 template <data_type_t data_type>
99 struct ref_batch_normalization_bwd_t : public cpu_primitive_t {
100     struct pd_t: public cpu_batch_normalization_bwd_pd_t {
101         pd_t(engine_t *engine, const batch_normalization_desc_t *adesc,
102                 const primitive_attr_t *attr,
103                 const batch_normalization_fwd_pd_t *hint_fwd_pd)
104             : cpu_batch_normalization_bwd_pd_t(engine, adesc, attr,
105                     hint_fwd_pd) {}
106
107         DECLARE_COMMON_PD_T("ref:any", ref_batch_normalization_bwd_t);
108
109         virtual status_t init() override {
110             using namespace data_type;
111             using namespace prop_kind;
112             assert(engine()->kind() == engine_kind::cpu);
113             bool ok = true
114                 && is_bwd()
115                 && !has_zero_dim_memory()
116                 && utils::one_of(desc()->prop_kind, backward, backward_data)
117                 && utils::everyone_is(data_type, desc()->data_desc.data_type,
118                         desc()->diff_data_desc.data_type)
119                 && utils::everyone_is(f32,
120                         desc()->mean_desc.data_type,
121                         desc()->variance_desc.data_type)
122                 && IMPLICATION(use_scaleshift(),
123                         desc()->diff_data_scaleshift_desc.data_type == f32
124                         && desc()->data_scaleshift_desc.data_type == f32)
125                 && IMPLICATION(data_type == bf16, mayiuse(avx512_core))
126                 && attr()->has_default_values()
127                 && hint_fwd_pd_ != nullptr;
128             if (!ok) return status::unimplemented;
129
130             if (fuse_bn_relu()) {
131                 bn_init_default_ws(this, this->workspace_pd_, 8);
132                 const size_t this_ws_sz
133                     = memory_desc_wrapper(this->workspace_pd()).size();
134
135                 bool ws_ok = true
136                     && hint_fwd_pd_->workspace_pd()
137                     && memory_desc_wrapper(hint_fwd_pd_->workspace_pd()).size()
138                             == this_ws_sz;
139                 if (!ws_ok)
140                     return status::unimplemented;
141             }
142
143             bool stats_ok = true
144                 && hint_fwd_pd_->mean_pd()->desc()->ndims == 1
145                 && hint_fwd_pd_->mean_pd()->desc()->format == memory_format::x
146                 && hint_fwd_pd_->mean_pd()->desc()->data_type == f32
147                 && hint_fwd_pd_->variance_pd()->desc()->ndims == 1
148                 && hint_fwd_pd_->variance_pd()->desc()->format == memory_format::x
149                 && hint_fwd_pd_->variance_pd()->desc()->data_type == f32;
150             if (!stats_ok) return status::unimplemented;
151
152             return status::success;
153         }
154     };
155
156     ref_batch_normalization_bwd_t(const pd_t *apd, const input_vector &inputs,
157             const output_vector &outputs)
158         : cpu_primitive_t(apd, inputs, outputs) {}
159     typedef typename prec_traits<data_type>::type data_t;
160
161     virtual void execute(event_t *e) const {
162         execute_backward();
163         e->set_state(event_t::ready);
164     }
165
166 private:
167     void execute_backward() const;
168     const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
169 };
170
171 }
172 }
173 }
174
175 #endif
176
177 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s