1 /*******************************************************************************
2 * Copyright 2016-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_BATCH_NORMALIZATION_FWD_PD_HPP
18 #define CPU_BATCH_NORMALIZATION_FWD_PD_HPP
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"
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) {
38 const size_t src_nelems
39 = memory_desc_wrapper(self->src_pd(0)).nelems(true);
40 const size_t bits_per_byte = 8;
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,
47 ws_pd = cpu_memory_t::pd_t(self->engine(), &ws_d);
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;
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)
61 , variance_pd_(engine_)
62 , scaleshift_pd_(engine_, &desc_.data_scaleshift_desc)
63 , workspace_pd_(engine_) {}
64 virtual ~cpu_batch_normalization_fwd_pd_t() {}
66 virtual const cpu_memory_pd_t *src_pd(int index = 0) const override {
67 if (index == 0) return &data_pd_;
69 if (index == 1) return &mean_pd_;
70 if (index == 2) return &variance_pd_;
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_;
84 virtual const cpu_memory_pd_t *weights_pd(int index = 0) const override
85 { return index == 0 ? &scaleshift_pd_ : nullptr; }
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;
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_;
99 virtual status_t init() = 0;
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;
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() {}
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_;
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; }
136 virtual const cpu_memory_pd_t *workspace_pd(int index = 0) const override
137 { return index == 0 && fuse_bn_relu() ? &workspace_pd_ : nullptr; }
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_;
148 virtual status_t init() = 0;
157 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s