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 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) {}
41 DECLARE_COMMON_PD_T("ncsp_bnorm:any", ncsp_batch_normalization_fwd_t);
43 virtual status_t init() override {
44 using namespace data_type;
45 using namespace prop_kind;
47 assert(engine()->kind() == engine_kind::cpu);
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;
60 if (is_training() && fuse_bn_relu())
61 bn_init_default_ws(this, this->workspace_pd_, 8);
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);
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());
86 scratchpad.book(key_bnorm_tmp_mean, sizeof(data_t) * C());
87 scratchpad.book(key_bnorm_tmp_var, sizeof(data_t) * C());
93 typedef typename prec_traits<data_type::f32>::type data_t;
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() {}
100 virtual void execute(event_t *e) const {
102 e->set_state(event_t::ready);
106 void execute_forward() const;
107 const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
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) {}
118 DECLARE_COMMON_PD_T("ncsp_bnorm:any", ncsp_batch_normalization_bwd_t);
120 virtual status_t init() override {
121 using namespace data_type;
122 assert(engine()->kind() == engine_kind::cpu);
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;
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();
141 && hint_fwd_pd_->workspace_pd()
142 && memory_desc_wrapper(hint_fwd_pd_->workspace_pd()).size()
144 if (!ws_ok) return status::unimplemented;
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());
164 typedef typename prec_traits<data_type::f32>::type data_t;
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() {}
171 virtual void execute(event_t *e) const {
173 e->set_state(event_t::ready);
177 void execute_backward() const;
178 const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
187 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s