updated readme file due to moving CMake scripts to the root folder
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / src / cpu / cpu_batch_normalization_pd.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_BATCH_NORMALIZATION_FWD_PD_HPP
18 #define CPU_BATCH_NORMALIZATION_FWD_PD_HPP
19
20 #include <assert.h>
21
22 #include "c_types_map.hpp"
23 #include "batch_normalization_pd.hpp"
24 #include "cpu_engine.hpp"
25 #include "cpu_memory.hpp"
26 #include "cpu_primitive.hpp"
27 #include "type_helpers.hpp"
28 #include "utils.hpp"
29
30 namespace mkldnn {
31 namespace impl {
32 namespace cpu {
33
34 namespace {
35 template <typename pd_t> inline void bn_init_default_ws(const pd_t *self,
36         cpu_memory_t::pd_t &ws_pd, size_t bits_per_element) {
37     memory_desc_t ws_d;
38     const size_t src_nelems
39         = memory_desc_wrapper(self->src_pd(0)).nelems(true);
40     const size_t bits_per_byte = 8;
41     const size_t ws_sz
42         = utils::div_up(src_nelems * bits_per_element, bits_per_byte);
43     dim_t mb = memory_desc_wrapper(self->src_pd(0)).dims()[0];
44     dims_t ws_dims = { mb, (dim_t)( ws_sz / mb ) };
45     mkldnn_memory_desc_init(&ws_d, 2, ws_dims, impl::data_type::u8,
46             memory_format::nc);
47     ws_pd = cpu_memory_t::pd_t(self->engine(), &ws_d);
48 }
49 }
50
51 struct cpu_batch_normalization_fwd_pd_t: public batch_normalization_fwd_pd_t {
52     using cpu_memory_pd_t = cpu_memory_t::pd_t;
53
54     cpu_batch_normalization_fwd_pd_t(engine_t *engine,
55             const batch_normalization_desc_t *adesc,
56             const primitive_attr_t *attr,
57             const batch_normalization_fwd_pd_t *hint_fwd_pd)
58         : batch_normalization_fwd_pd_t(engine, adesc, attr, hint_fwd_pd)
59         , data_pd_(engine_, &desc_.data_desc)
60         , mean_pd_(engine_)
61         , variance_pd_(engine_)
62         , scaleshift_pd_(engine_, &desc_.data_scaleshift_desc)
63         , workspace_pd_(engine_) {}
64     virtual ~cpu_batch_normalization_fwd_pd_t() {}
65
66     virtual const cpu_memory_pd_t *src_pd(int index = 0) const override {
67         if (index == 0) return &data_pd_;
68         if (stats_is_src()) {
69             if (index == 1) return &mean_pd_;
70             if (index == 2) return &variance_pd_;
71         }
72         return nullptr;
73     }
74
75     virtual const cpu_memory_pd_t *dst_pd(int index = 0) const override {
76         if (index == 0)  return &data_pd_;
77         if (!stats_is_src() && is_training()) {
78             if (index == 1) return &mean_pd_;
79             if (index == 2) return &variance_pd_;
80         }
81         return nullptr;
82     }
83
84     virtual const cpu_memory_pd_t *weights_pd(int index = 0) const override
85     { return index == 0 ? &scaleshift_pd_ : nullptr; }
86
87     virtual const cpu_memory_pd_t *workspace_pd(int index = 0) const override {
88         return index == 0 && is_training() && fuse_bn_relu()
89             ? &workspace_pd_ : nullptr;
90     }
91
92 protected:
93     cpu_memory_pd_t data_pd_;
94     cpu_memory_pd_t mean_pd_;
95     cpu_memory_pd_t variance_pd_;
96     cpu_memory_pd_t scaleshift_pd_;
97     cpu_memory_pd_t workspace_pd_;
98
99     virtual status_t init() = 0;
100 };
101
102 struct cpu_batch_normalization_bwd_pd_t: public batch_normalization_bwd_pd_t {
103     using cpu_memory_pd_t = cpu_memory_t::pd_t;
104
105     cpu_batch_normalization_bwd_pd_t(engine_t *engine,
106             const batch_normalization_desc_t *adesc,
107             const primitive_attr_t *attr,
108             const batch_normalization_fwd_pd_t *hint_fwd_pd)
109         : batch_normalization_bwd_pd_t(engine, adesc, attr, hint_fwd_pd)
110         , data_pd_(engine_, &desc_.data_desc)
111         , mean_pd_(engine_, &desc_.mean_desc)
112         , variance_pd_(engine_, &desc_.variance_desc)
113         , diff_data_pd_(engine_, &desc_.diff_data_desc)
114         , scaleshift_pd_(engine_, &desc_.data_scaleshift_desc)
115         , diff_scaleshift_pd_(engine_, &desc_.diff_data_scaleshift_desc)
116         , workspace_pd_(engine_) {}
117     virtual ~cpu_batch_normalization_bwd_pd_t() {}
118
119     virtual const cpu_memory_pd_t *src_pd(int index = 0) const override {
120         if (index == 0) return &data_pd_;
121         if (index == 1) return &mean_pd_;
122         if (index == 2) return &variance_pd_;
123
124         return nullptr;
125     }
126
127     virtual const cpu_memory_pd_t *diff_dst_pd(int index = 0) const override
128     { return index == 0 ? &diff_data_pd_ : nullptr; }
129     virtual const cpu_memory_pd_t *weights_pd(int index = 0) const override
130     { return index == 0 ? &scaleshift_pd_ : nullptr; }
131     virtual const cpu_memory_pd_t *diff_weights_pd(int index = 0) const
132         override { return index == 0 ? &diff_scaleshift_pd_ : nullptr; }
133     virtual const cpu_memory_pd_t *diff_src_pd(int index = 0) const override
134     { return index == 0 ? &diff_data_pd_ : nullptr; }
135
136     virtual const cpu_memory_pd_t *workspace_pd(int index = 0) const override
137     { return index == 0 && fuse_bn_relu() ? &workspace_pd_ : nullptr; }
138
139 protected:
140     cpu_memory_pd_t data_pd_;
141     cpu_memory_pd_t mean_pd_;
142     cpu_memory_pd_t variance_pd_;
143     cpu_memory_pd_t diff_data_pd_;
144     cpu_memory_pd_t scaleshift_pd_;
145     cpu_memory_pd_t diff_scaleshift_pd_;
146     cpu_memory_pd_t workspace_pd_;
147
148     virtual status_t init() = 0;
149 };
150
151 }
152 }
153 }
154
155 #endif
156
157 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s