1 /*******************************************************************************
2 * Copyright 2018 Intel Corporation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *******************************************************************************/
17 #ifndef CPU_NCSP_BATCH_NORMALIZATION_HPP
18 #define CPU_NCSP_BATCH_NORMALIZATION_HPP
22 #include "c_types_map.hpp"
23 #include "memory_tracking.hpp"
24 #include "type_helpers.hpp"
27 #include "cpu_batch_normalization_pd.hpp"
33 template <data_type_t data_type>
34 struct ncsp_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(
40 engine, adesc, attr, hint_fwd_pd) {}
42 DECLARE_COMMON_PD_T("ncsp_bnorm:any", ncsp_batch_normalization_fwd_t);
44 virtual status_t init() override {
45 using namespace data_type;
46 using namespace prop_kind;
47 assert(engine()->kind() == engine_kind::cpu);
51 && !has_zero_dim_memory()
52 && utils::one_of(desc()->prop_kind, forward_training,
54 && desc()->data_desc.data_type == data_type
55 && IMPLICATION(use_scaleshift(),
56 desc()->data_scaleshift_desc.data_type == f32)
57 && utils::everyone_is(f32,
58 desc()->mean_desc.data_type,
59 desc()->variance_desc.data_type)
60 && utils::one_of(data_pd_.desc()->format, memory_format::nchw,
61 memory_format::ncdhw, memory_format::nc)
62 && IMPLICATION(data_type == bf16, mayiuse(avx512_core))
63 && (attr()->has_default_values() || this->with_relu_post_op());
64 if (!ok) return status::unimplemented;
66 if (is_training() && fuse_bn_relu())
67 bn_init_default_ws(this, this->workspace_pd_, 8);
69 if (stats_is_src() || is_training()) {
70 memory_desc_t stats_d;
71 dims_t stats_dims = { C() };
72 mkldnn_memory_desc_init(
73 &stats_d, 1, stats_dims, f32, memory_format::x);
74 mean_pd_ = cpu_memory_t::pd_t(engine_, &stats_d);
75 variance_pd_ = cpu_memory_t::pd_t(engine_, &stats_d);
84 void init_scratchpad() {
85 using namespace memory_tracking::names;
86 auto scratchpad = scratchpad_registry().registrar();
87 if (!stats_is_src()) {
88 scratchpad.book(key_bnorm_reduction,
89 sizeof(acc_data_t) * C() * mkldnn_get_max_threads());
92 scratchpad.book(key_bnorm_tmp_mean, sizeof(acc_data_t) * C());
93 scratchpad.book(key_bnorm_tmp_var, sizeof(acc_data_t) * C());
97 if (data_type == data_type::bf16) {
98 const int simd_w = 16;
99 const bool has_spatial = utils::one_of(ndims(), 4, 5);
100 const int SP = has_spatial ? D() * H() * W() : 1;
102 const size_t bf16cvt_buf_sz = sizeof(acc_data_t) * nbufs
103 * mkldnn_get_max_threads() * utils::rnd_up(SP, simd_w);
104 scratchpad.book(key_bnorm_bf16cvt, bf16cvt_buf_sz);
109 typedef typename prec_traits<data_type>::type data_t;
110 typedef float acc_data_t;
112 ncsp_batch_normalization_fwd_t(const pd_t *apd, const input_vector &inputs,
113 const output_vector &outputs)
114 : cpu_primitive_t(apd, inputs, outputs) {}
116 ~ncsp_batch_normalization_fwd_t() {}
118 virtual void execute(event_t *e) const {
120 e->set_state(event_t::ready);
124 void execute_forward() const;
125 const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
128 template <data_type_t data_type>
129 struct ncsp_batch_normalization_bwd_t : public cpu_primitive_t {
130 struct pd_t : public cpu_batch_normalization_bwd_pd_t {
131 pd_t(engine_t *engine, const batch_normalization_desc_t *adesc,
132 const primitive_attr_t *attr,
133 const batch_normalization_fwd_pd_t *hint_fwd_pd)
134 : cpu_batch_normalization_bwd_pd_t(
135 engine, adesc, attr, hint_fwd_pd) {}
137 DECLARE_COMMON_PD_T("ncsp_bnorm:any", ncsp_batch_normalization_bwd_t);
139 virtual status_t init() override {
140 using namespace data_type;
141 using namespace prop_kind;
142 assert(engine()->kind() == engine_kind::cpu);
146 && !has_zero_dim_memory()
147 && utils::one_of(desc()->prop_kind, backward, backward_data)
148 && utils::everyone_is(data_type, desc()->data_desc.data_type,
149 desc()->diff_data_desc.data_type)
150 && utils::everyone_is(f32, desc()->mean_desc.data_type,
151 desc()->variance_desc.data_type)
152 && IMPLICATION(use_scaleshift(),
153 desc()->diff_data_scaleshift_desc.data_type == f32
154 && desc()->data_scaleshift_desc.data_type == f32)
155 && IMPLICATION(data_type == bf16, mayiuse(avx512_core))
156 && utils::one_of(data_pd_.desc()->format, memory_format::nchw,
157 memory_format::ncdhw, memory_format::nc)
158 && attr()->has_default_values()
159 && hint_fwd_pd_ != nullptr;
161 return status::unimplemented;
163 if (fuse_bn_relu()) {
164 bn_init_default_ws(this, this->workspace_pd_, 8);
165 const size_t this_ws_sz
166 = memory_desc_wrapper(this->workspace_pd()).size();
169 && hint_fwd_pd_->workspace_pd()
170 && memory_desc_wrapper(hint_fwd_pd_->workspace_pd()).size()
172 if (!ws_ok) return status::unimplemented;
177 return status::success;
181 void init_scratchpad() {
182 using namespace memory_tracking::names;
183 auto scratchpad = scratchpad_registry().registrar();
184 scratchpad.book(key_bnorm_reduction,
185 sizeof(acc_data_t) * 2 * C() * mkldnn_get_max_threads());
186 if (!(use_scaleshift() && desc()->prop_kind == prop_kind::backward))
187 scratchpad.book(key_bnorm_tmp_diff_ss,
188 sizeof(acc_data_t) * 2 * C());
190 if (data_type == data_type::bf16) {
191 const int simd_w = 16;
192 const bool has_spatial = utils::one_of(ndims(), 4, 5);
193 const int SP = has_spatial ? D() * H() * W() : 1;
194 const int nbufs = 2 + !use_global_stats();
195 const size_t bf16cvt_buf_sz = sizeof(acc_data_t) * nbufs
196 * mkldnn_get_max_threads() * utils::rnd_up(SP, simd_w);
197 scratchpad.book(key_bnorm_bf16cvt, bf16cvt_buf_sz);
202 typedef typename prec_traits<data_type>::type data_t;
203 typedef float acc_data_t;
205 ncsp_batch_normalization_bwd_t(const pd_t *apd, const input_vector &inputs,
206 const output_vector &outputs)
207 : cpu_primitive_t(apd, inputs, outputs) {}
209 ~ncsp_batch_normalization_bwd_t() {}
211 virtual void execute(event_t *e) const {
213 e->set_state(event_t::ready);
217 void execute_backward() const;
218 const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
227 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s