ncsp_batch_normalization.hpp 8.55 KB
Newer Older
openvino-pushbot's avatar
openvino-pushbot committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*******************************************************************************
* Copyright 2018 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/

#ifndef CPU_NCSP_BATCH_NORMALIZATION_HPP
#define CPU_NCSP_BATCH_NORMALIZATION_HPP

#include <assert.h>

#include "c_types_map.hpp"
23
#include "memory_tracking.hpp"
openvino-pushbot's avatar
openvino-pushbot committed
24 25 26
#include "type_helpers.hpp"
#include "utils.hpp"

27 28
#include "cpu_batch_normalization_pd.hpp"

openvino-pushbot's avatar
openvino-pushbot committed
29 30 31 32
namespace mkldnn {
namespace impl {
namespace cpu {

33
template <data_type_t data_type>
openvino-pushbot's avatar
openvino-pushbot committed
34 35 36 37 38 39 40 41 42 43 44 45
struct ncsp_batch_normalization_fwd_t : public cpu_primitive_t {
    struct pd_t : public cpu_batch_normalization_fwd_pd_t {
        pd_t(engine_t *engine, const batch_normalization_desc_t *adesc,
                const primitive_attr_t *attr,
                const batch_normalization_fwd_pd_t *hint_fwd_pd)
            : cpu_batch_normalization_fwd_pd_t(
                      engine, adesc, attr, hint_fwd_pd) {}

        DECLARE_COMMON_PD_T("ncsp_bnorm:any", ncsp_batch_normalization_fwd_t);

        virtual status_t init() override {
            using namespace data_type;
46
            using namespace prop_kind;
openvino-pushbot's avatar
openvino-pushbot committed
47
            assert(engine()->kind() == engine_kind::cpu);
48

Alexey Suhov's avatar
Alexey Suhov committed
49 50 51
            bool ok = true
                && is_fwd()
                && !has_zero_dim_memory()
52 53 54
                && utils::one_of(desc()->prop_kind, forward_training,
                        forward_inference)
                && desc()->data_desc.data_type == data_type
55
                && IMPLICATION(use_scaleshift(),
Alexey Suhov's avatar
Alexey Suhov committed
56
                        desc()->data_scaleshift_desc.data_type == f32)
57 58 59
                && utils::everyone_is(f32,
                        desc()->mean_desc.data_type,
                        desc()->variance_desc.data_type)
Alexey Suhov's avatar
Alexey Suhov committed
60 61
                && utils::one_of(data_pd_.desc()->format, memory_format::nchw,
                        memory_format::ncdhw, memory_format::nc)
62
                && IMPLICATION(data_type == bf16, mayiuse(avx512_core))
Alexey Suhov's avatar
Alexey Suhov committed
63
                && (attr()->has_default_values() || this->with_relu_post_op());
64
            if (!ok) return status::unimplemented;
openvino-pushbot's avatar
openvino-pushbot committed
65

66
            if (is_training() && fuse_bn_relu())
openvino-pushbot's avatar
openvino-pushbot committed
67 68 69 70 71
                bn_init_default_ws(this, this->workspace_pd_, 8);

            if (stats_is_src() || is_training()) {
                memory_desc_t stats_d;
                dims_t stats_dims = { C() };
72 73
                mkldnn_memory_desc_init(
                        &stats_d, 1, stats_dims, f32, memory_format::x);
openvino-pushbot's avatar
openvino-pushbot committed
74 75 76 77
                mean_pd_ = cpu_memory_t::pd_t(engine_, &stats_d);
                variance_pd_ = cpu_memory_t::pd_t(engine_, &stats_d);
            }

78 79
            init_scratchpad();

openvino-pushbot's avatar
openvino-pushbot committed
80 81
            return success;
        }
82 83 84 85 86 87 88

    private:
        void init_scratchpad() {
            using namespace memory_tracking::names;
            auto scratchpad = scratchpad_registry().registrar();
            if (!stats_is_src()) {
                scratchpad.book(key_bnorm_reduction,
89
                        sizeof(acc_data_t) * C() * mkldnn_get_max_threads());
90 91

                if (!is_training()) {
92 93
                    scratchpad.book(key_bnorm_tmp_mean, sizeof(acc_data_t) * C());
                    scratchpad.book(key_bnorm_tmp_var, sizeof(acc_data_t) * C());
94 95
                }
            }
96 97 98 99 100 101 102 103 104 105

            if (data_type == data_type::bf16) {
                const int simd_w = 16;
                const bool has_spatial = utils::one_of(ndims(), 4, 5);
                const int SP = has_spatial ? D() * H() * W() : 1;
                const int nbufs = 2;
                const size_t bf16cvt_buf_sz = sizeof(acc_data_t) * nbufs
                    * mkldnn_get_max_threads() * utils::rnd_up(SP, simd_w);
                scratchpad.book(key_bnorm_bf16cvt, bf16cvt_buf_sz);
            }
106
        }
openvino-pushbot's avatar
openvino-pushbot committed
107 108
    };

109 110
    typedef typename prec_traits<data_type>::type data_t;
    typedef float acc_data_t;
openvino-pushbot's avatar
openvino-pushbot committed
111

112 113 114
    ncsp_batch_normalization_fwd_t(const pd_t *apd, const input_vector &inputs,
            const output_vector &outputs)
        : cpu_primitive_t(apd, inputs, outputs) {}
115

116
    ~ncsp_batch_normalization_fwd_t() {}
openvino-pushbot's avatar
openvino-pushbot committed
117

118
    virtual void execute(event_t *e) const {
openvino-pushbot's avatar
openvino-pushbot committed
119 120 121 122 123
        execute_forward();
        e->set_state(event_t::ready);
    }

private:
124 125
    void execute_forward() const;
    const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
openvino-pushbot's avatar
openvino-pushbot committed
126 127
};

128
template <data_type_t data_type>
openvino-pushbot's avatar
openvino-pushbot committed
129 130 131 132 133 134
struct ncsp_batch_normalization_bwd_t : public cpu_primitive_t {
    struct pd_t : public cpu_batch_normalization_bwd_pd_t {
        pd_t(engine_t *engine, const batch_normalization_desc_t *adesc,
                const primitive_attr_t *attr,
                const batch_normalization_fwd_pd_t *hint_fwd_pd)
            : cpu_batch_normalization_bwd_pd_t(
135
                    engine, adesc, attr, hint_fwd_pd) {}
openvino-pushbot's avatar
openvino-pushbot committed
136 137 138 139 140

        DECLARE_COMMON_PD_T("ncsp_bnorm:any", ncsp_batch_normalization_bwd_t);

        virtual status_t init() override {
            using namespace data_type;
141
            using namespace prop_kind;
openvino-pushbot's avatar
openvino-pushbot committed
142
            assert(engine()->kind() == engine_kind::cpu);
143

Alexey Suhov's avatar
Alexey Suhov committed
144 145 146
            bool ok = true
                && is_bwd()
                && !has_zero_dim_memory()
147 148 149 150 151
                && utils::one_of(desc()->prop_kind, backward, backward_data)
                && utils::everyone_is(data_type, desc()->data_desc.data_type,
                        desc()->diff_data_desc.data_type)
                && utils::everyone_is(f32, desc()->mean_desc.data_type,
                        desc()->variance_desc.data_type)
152
                && IMPLICATION(use_scaleshift(),
153 154 155
                        desc()->diff_data_scaleshift_desc.data_type == f32
                        && desc()->data_scaleshift_desc.data_type == f32)
                && IMPLICATION(data_type == bf16, mayiuse(avx512_core))
Alexey Suhov's avatar
Alexey Suhov committed
156 157
                && utils::one_of(data_pd_.desc()->format, memory_format::nchw,
                        memory_format::ncdhw, memory_format::nc)
158 159 160 161
                && attr()->has_default_values()
                && hint_fwd_pd_ != nullptr;
            if (!ok)
                return status::unimplemented;
openvino-pushbot's avatar
openvino-pushbot committed
162 163 164 165

            if (fuse_bn_relu()) {
                bn_init_default_ws(this, this->workspace_pd_, 8);
                const size_t this_ws_sz
166 167 168 169 170 171 172
                    = memory_desc_wrapper(this->workspace_pd()).size();

                bool ws_ok = true
                    && hint_fwd_pd_->workspace_pd()
                    && memory_desc_wrapper(hint_fwd_pd_->workspace_pd()).size()
                    == this_ws_sz;
                if (!ws_ok) return status::unimplemented;
openvino-pushbot's avatar
openvino-pushbot committed
173 174
            }

175 176
            init_scratchpad();

177
            return status::success;
openvino-pushbot's avatar
openvino-pushbot committed
178
        }
179 180 181 182 183 184

    private:
        void init_scratchpad() {
            using namespace memory_tracking::names;
            auto scratchpad = scratchpad_registry().registrar();
            scratchpad.book(key_bnorm_reduction,
185
                    sizeof(acc_data_t) * 2 * C() * mkldnn_get_max_threads());
186 187
            if (!(use_scaleshift() && desc()->prop_kind == prop_kind::backward))
                scratchpad.book(key_bnorm_tmp_diff_ss,
188 189 190 191 192 193 194 195 196 197 198
                        sizeof(acc_data_t) * 2 * C());

            if (data_type == data_type::bf16) {
                const int simd_w = 16;
                const bool has_spatial = utils::one_of(ndims(), 4, 5);
                const int SP = has_spatial ? D() * H() * W() : 1;
                const int nbufs = 2 + !use_global_stats();
                const size_t bf16cvt_buf_sz = sizeof(acc_data_t) * nbufs
                    * mkldnn_get_max_threads() * utils::rnd_up(SP, simd_w);
                scratchpad.book(key_bnorm_bf16cvt, bf16cvt_buf_sz);
            }
199
        }
openvino-pushbot's avatar
openvino-pushbot committed
200 201
    };

202 203
    typedef typename prec_traits<data_type>::type data_t;
    typedef float acc_data_t;
openvino-pushbot's avatar
openvino-pushbot committed
204

205 206 207
    ncsp_batch_normalization_bwd_t(const pd_t *apd, const input_vector &inputs,
            const output_vector &outputs)
        : cpu_primitive_t(apd, inputs, outputs) {}
208

209 210 211
    ~ncsp_batch_normalization_bwd_t() {}

    virtual void execute(event_t *e) const {
openvino-pushbot's avatar
openvino-pushbot committed
212 213 214 215 216
        execute_backward();
        e->set_state(event_t::ready);
    }

private:
217 218
    void execute_backward() const;
    const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
openvino-pushbot's avatar
openvino-pushbot committed
219
};
220

openvino-pushbot's avatar
openvino-pushbot committed
221 222 223 224 225 226 227
}
}
}

#endif

// vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s