jit_avx2_convolution.hpp 15 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
/*******************************************************************************
* Copyright 2016-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_JIT_AVX2_CONVOLUTION_HPP
#define CPU_JIT_AVX2_CONVOLUTION_HPP

#include "c_types_map.hpp"
21 22 23 24
#include "memory_tracking.hpp"
#include "mkldnn_thread.hpp"
#include "utils.hpp"

openvino-pushbot's avatar
openvino-pushbot committed
25 26
#include "cpu_convolution_pd.hpp"
#include "cpu_reducer.hpp"
27

openvino-pushbot's avatar
openvino-pushbot committed
28 29 30 31 32 33 34
#include "jit_avx2_conv_kernel_f32.hpp"
#include "jit_uni_depthwise.hpp"

namespace mkldnn {
namespace impl {
namespace cpu {

35 36
struct jit_avx2_convolution_fwd_t: public cpu_primitive_t {
    struct pd_t: public cpu_convolution_fwd_pd_t {
openvino-pushbot's avatar
openvino-pushbot committed
37
        pd_t(engine_t *engine,
38
                const convolution_desc_t *adesc,
openvino-pushbot's avatar
openvino-pushbot committed
39 40
                const primitive_attr_t *attr,
                const typename pd_t::base_class *hint_fwd_pd)
41 42
            : cpu_convolution_fwd_pd_t(engine, adesc, attr, hint_fwd_pd)
            , jcp_(), jcp_dw_() {}
openvino-pushbot's avatar
openvino-pushbot committed
43 44 45

        DECLARE_COMMON_PD_T(
                JIT_IMPL_NAME_HELPER("jit:", avx2, ""),
46
                jit_avx2_convolution_fwd_t);
openvino-pushbot's avatar
openvino-pushbot committed
47 48 49 50 51 52

        virtual status_t init() override {
            using namespace prop_kind;
            assert(this->engine()->kind() == engine_kind::cpu);
            bool ok = true
                && this->set_default_params() == status::success
53
                && utils::one_of(this->desc()->prop_kind, forward_training,
openvino-pushbot's avatar
openvino-pushbot committed
54
                        forward_inference)
55 56 57
                && utils::one_of(this->desc()->alg_kind,
                        alg_kind::convolution_auto,
                        alg_kind::convolution_direct)
Alexey Suhov's avatar
Alexey Suhov committed
58
                && !this->has_zero_dim_memory()
openvino-pushbot's avatar
openvino-pushbot committed
59
                && utils::everyone_is(data_type::f32,
60 61 62
                        this->desc()->src_desc.data_type,
                        this->desc()->weights_desc.data_type,
                        this->desc()->dst_desc.data_type)
63
                && IMPLICATION(this->with_bias(),
64
                        data_type::f32 == this->desc()->bias_desc.data_type);
openvino-pushbot's avatar
openvino-pushbot committed
65 66
            if (!ok) return status::unimplemented;

67 68 69 70 71 72


            status_t sts = jit_avx2_conv_fwd_kernel_f32::init_conf(jcp_,
                    *this->desc(), *this->src_pd_.desc(),
                    *this->weights_pd_.desc(), *this->dst_pd_.desc(),
                    *this->attr());
openvino-pushbot's avatar
openvino-pushbot committed
73 74 75
            if (sts != status::success) return sts;

            if (jcp_.with_dw_conv) {
76
                status_t sts_dw = jit_uni_dw_conv_row_f32<avx2>::init_conf(jcp_, jcp_dw_, *this->attr());
openvino-pushbot's avatar
openvino-pushbot committed
77 78 79
                if (sts_dw != status::success) return sts_dw;
            }

80 81 82
            auto scratchpad = scratchpad_registry().registrar();
            jit_avx2_conv_fwd_kernel_f32::init_scratchpad(scratchpad, jcp_, jcp_dw_);

openvino-pushbot's avatar
openvino-pushbot committed
83 84 85 86
            return status::success;
        }

        jit_conv_conf_t jcp_;
87
        jit_conv_conf_t jcp_dw_;
openvino-pushbot's avatar
openvino-pushbot committed
88 89 90 91 92

    protected:
        virtual status_t set_default_params() override {
            using namespace memory_format;

Alexey Suhov's avatar
Alexey Suhov committed
93 94
            const int simd_w = 8;
            const bool flat = this->IC() < simd_w;
openvino-pushbot's avatar
openvino-pushbot committed
95
            if (this->src_pd_.desc()->format == any)
96 97 98
                CHECK(this->src_pd_.set_format(flat
                    ? utils::pick(this->ndims() - 3, ncw, nchw, ncdhw)
                    : utils::pick(this->ndims() - 3, nCw8c, nChw8c, nCdhw8c)));
openvino-pushbot's avatar
openvino-pushbot committed
99
            if (this->dst_pd_.desc()->format == any)
100 101
                CHECK(this->dst_pd_.set_format(
                    utils::pick(this->ndims() - 3, nCw8c, nChw8c, nCdhw8c)));
openvino-pushbot's avatar
openvino-pushbot committed
102
            if (this->weights_pd_.desc()->format == any)
103 104 105 106 107 108
                CHECK(this->weights_pd_.set_format(this->with_groups()
                    ? utils::pick(2 * this->ndims() - 6 + flat, gOIw8i8o,
                        gOwi8o, gOIhw8i8o, gOhwi8o, gOIdhw8i8o, gOdhwi8o)
                    : utils::pick(2 * this->ndims() - 6 + flat, OIw8i8o, Owi8o,
                        OIhw8i8o, Ohwi8o, OIdhw8i8o, Odhwi8o)));

openvino-pushbot's avatar
openvino-pushbot committed
109 110
            if (this->bias_pd_.desc()->format == any)
                CHECK(this->bias_pd_.set_format(x));
111 112
            if (this->desc()->alg_kind == alg_kind::convolution_auto)
                CHECK(this->set_alg_kind(alg_kind::convolution_direct));
openvino-pushbot's avatar
openvino-pushbot committed
113 114 115 116
            return status::success;
        }
    };

117
    jit_avx2_convolution_fwd_t(const pd_t *apd, const input_vector &inputs,
openvino-pushbot's avatar
openvino-pushbot committed
118
            const output_vector &outputs)
119
        : cpu_primitive_t(apd, inputs, outputs)
openvino-pushbot's avatar
openvino-pushbot committed
120
    {
121
        kernel_ = new jit_avx2_conv_fwd_kernel_f32(pd()->jcp_, pd()->jcp_dw_, *pd()->attr());
Alexey Suhov's avatar
Alexey Suhov committed
122

123 124
        if (pd()->jcp_.with_dw_conv) {
            kernel_dw_ = new jit_uni_dw_conv_row_f32<avx2>(pd()->jcp_dw_, *pd()->attr(), pd()->jcp_dw_.ch_block);
openvino-pushbot's avatar
openvino-pushbot committed
125 126 127
        }
    }

128
    ~jit_avx2_convolution_fwd_t() {
openvino-pushbot's avatar
openvino-pushbot committed
129 130
        delete kernel_;

131
        if (pd()->jcp_.with_dw_conv) {
openvino-pushbot's avatar
openvino-pushbot committed
132 133 134 135 136 137
            delete kernel_dw_;
        }
    };

    typedef typename prec_traits<data_type::f32>::type data_t;

138 139 140
    virtual void execute(event_t *e) const {
        if (pd()->jcp_.with_dw_conv)
            execute_forward_with_dw_conv();
openvino-pushbot's avatar
openvino-pushbot committed
141 142 143 144 145 146 147
        else
            execute_forward();

        e->set_state(event_t::ready);
    }

private:
148 149 150
    void execute_forward() const;
    void execute_forward_with_dw_conv() const;
    const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
openvino-pushbot's avatar
openvino-pushbot committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175

    jit_avx2_conv_fwd_kernel_f32 *kernel_;
    jit_uni_dw_conv_row_f32<avx2> *kernel_dw_;
};

struct jit_avx2_convolution_bwd_data_t: public cpu_primitive_t {
    struct pd_t: public cpu_convolution_bwd_data_pd_t {
        pd_t(engine_t *engine,
                const convolution_desc_t *adesc,
                const primitive_attr_t *attr,
                const convolution_fwd_pd_t *hint_fwd_pd)
            : cpu_convolution_bwd_data_pd_t(engine, adesc, attr, hint_fwd_pd)
            , jcp_()
        {}

        DECLARE_COMMON_PD_T(
                JIT_IMPL_NAME_HELPER("jit:", avx2, ""),
                jit_avx2_convolution_bwd_data_t);

        virtual status_t init() override {
            using namespace prop_kind;
            assert(this->engine()->kind() == engine_kind::cpu);
            bool ok = true
                && this->set_default_params() == status::success
                && utils::one_of(this->desc()->prop_kind, backward_data)
176 177
                && utils::one_of(this->desc()->alg_kind, alg_kind::convolution_auto,
                           alg_kind::convolution_direct)
Alexey Suhov's avatar
Alexey Suhov committed
178
                && !this->has_zero_dim_memory()
openvino-pushbot's avatar
openvino-pushbot committed
179 180 181 182 183 184
                && utils::everyone_is(data_type::f32,
                        this->desc()->diff_src_desc.data_type,
                        this->desc()->weights_desc.data_type,
                        this->desc()->diff_dst_desc.data_type);
            if (!ok) return status::unimplemented;

185 186
            status_t status = jit_avx2_conv_bwd_data_kernel_f32::init_conf(
                    jcp_, *this->desc(), *this->diff_src_pd_.desc(),
openvino-pushbot's avatar
openvino-pushbot committed
187
                    *this->weights_pd_.desc(), *this->diff_dst_pd_.desc());
188 189 190 191 192 193 194
            if (status != status::success) return status;

            auto scratchpad = scratchpad_registry().registrar();
            jit_avx2_conv_bwd_data_kernel_f32::init_scratchpad(scratchpad,
                    jcp_);

            return status::success;
openvino-pushbot's avatar
openvino-pushbot committed
195 196 197 198 199 200 201 202 203
        }

        jit_conv_conf_t jcp_;

    protected:
        virtual status_t set_default_params() override {
            using namespace memory_format;

            if (this->diff_src_pd_.desc()->format == any)
204 205
                CHECK(this->diff_src_pd_.set_format(
                    utils::pick(this->ndims() - 3, nCw8c, nChw8c, nCdhw8c)));
openvino-pushbot's avatar
openvino-pushbot committed
206
            if (this->diff_dst_pd_.desc()->format == any)
207 208
                CHECK(this->diff_dst_pd_.set_format(
                    utils::pick(this->ndims() - 3, nCw8c, nChw8c, nCdhw8c)));
openvino-pushbot's avatar
openvino-pushbot committed
209
            if (this->weights_pd_.desc()->format == any)
210 211 212 213 214
                CHECK(this->weights_pd_.set_format(this->with_groups()
                    ? utils::pick(this->ndims() - 3, gOIw8o8i, gOIhw8o8i,
                        gOIdhw8o8i)
                    : utils::pick(this->ndims() - 3, OIw8o8i, OIhw8o8i,
                        OIdhw8o8i)));
215 216
            if (this->desc()->alg_kind == alg_kind::convolution_auto)
                CHECK(this->set_alg_kind(alg_kind::convolution_direct));
openvino-pushbot's avatar
openvino-pushbot committed
217 218 219 220
            return status::success;
        }
    };

221
    jit_avx2_convolution_bwd_data_t(const pd_t *apd, const input_vector &inputs,
openvino-pushbot's avatar
openvino-pushbot committed
222
            const output_vector &outputs)
223 224 225
        : cpu_primitive_t(apd, inputs, outputs)
    { kernel_ = new jit_avx2_conv_bwd_data_kernel_f32(pd()->jcp_); }
    ~jit_avx2_convolution_bwd_data_t() { delete kernel_; }
openvino-pushbot's avatar
openvino-pushbot committed
226 227 228

    typedef typename prec_traits<data_type::f32>::type data_t;

229 230
    virtual void execute(event_t *e) const {
        switch (pd()->desc()->prop_kind) {
openvino-pushbot's avatar
openvino-pushbot committed
231 232 233 234 235 236 237 238 239 240
        case prop_kind::backward_data:
            execute_backward_data();
            break;
        default:
            assert(!"invalid prop_kind");
        }
        e->set_state(event_t::ready);
    }

private:
241 242 243
    void execute_backward_data() const;
    const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }

openvino-pushbot's avatar
openvino-pushbot committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
    jit_avx2_conv_bwd_data_kernel_f32 *kernel_;
};

struct jit_avx2_convolution_bwd_weights_t: public cpu_primitive_t {
    struct pd_t: public  cpu_convolution_bwd_weights_pd_t {
        pd_t(engine_t *engine, const convolution_desc_t *adesc,
                const primitive_attr_t *attr,
                const convolution_fwd_pd_t *hint_fwd_pd)
            : cpu_convolution_bwd_weights_pd_t(engine, adesc, attr, hint_fwd_pd)
            , jcp_() {}

        DECLARE_COMMON_PD_T(
                JIT_IMPL_NAME_HELPER("jit:", avx2, ""),
                jit_avx2_convolution_bwd_weights_t);

        virtual status_t init() override {
            assert(this->engine()->kind() == engine_kind::cpu);
            bool ok = true
                && this->set_default_params() == status::success
                && this->desc()->prop_kind == prop_kind::backward_weights
264 265
                && utils::one_of(this->desc()->alg_kind, alg_kind::convolution_auto,
                           alg_kind::convolution_direct)
Alexey Suhov's avatar
Alexey Suhov committed
266
                && !this->has_zero_dim_memory()
openvino-pushbot's avatar
openvino-pushbot committed
267 268 269 270 271 272
                && utils::everyone_is(data_type::f32,
                        this->desc()->src_desc.data_type,
                        this->desc()->diff_dst_desc.data_type,
                        this->desc()->diff_weights_desc.data_type);
            if (!ok) return status::unimplemented;

273 274
            status_t status = jit_avx2_conv_bwd_weights_kernel_f32::init_conf(
                    jcp_, *this->desc(), *this->src_pd_.desc(),
openvino-pushbot's avatar
openvino-pushbot committed
275 276
                    *this->diff_weights_pd_.desc(),
                    *this->diff_dst_pd_.desc());
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
            if (status != status::success) return status;

            init_balancers();

            auto scratchpad = scratchpad_registry().registrar();
            jit_avx2_conv_bwd_weights_kernel_f32::init_scratchpad(scratchpad,
                    jcp_);

            auto reducer_bia_scratchpad = memory_tracking::registrar_t(
                    scratchpad, memory_tracking::names::prefix_reducer_bia);
            reducer_bia_conf_.init_scratchpad(reducer_bia_scratchpad);

            auto reducer_wei_scratchpad = memory_tracking::registrar_t(
                    scratchpad, memory_tracking::names::prefix_reducer_wei);
            reducer_wei_conf_.init_scratchpad(reducer_wei_scratchpad);

            return status::success;
openvino-pushbot's avatar
openvino-pushbot committed
294 295 296
        }

        jit_conv_conf_t jcp_;
297 298
        cpu_reducer_t<data_type::f32>::conf_t reducer_bia_conf_;
        cpu_reducer_t<data_type::f32>::conf_t reducer_wei_conf_;
openvino-pushbot's avatar
openvino-pushbot committed
299 300 301 302 303 304 305

    protected:
        virtual status_t set_default_params() override {
            using namespace memory_format;
            const bool flat = this->IC() == 3;

            if (this->src_pd_.desc()->format == any)
306 307 308
                CHECK(this->src_pd_.set_format(flat
                    ? utils::pick(this->ndims() - 3, ncw, nchw, ncdhw)
                    : utils::pick(this->ndims() - 3, nCw8c, nChw8c, nCdhw8c)));
openvino-pushbot's avatar
openvino-pushbot committed
309
            if (this->diff_dst_pd_.desc()->format == any)
310 311
                CHECK(this->diff_dst_pd_.set_format(
                    utils::pick(this->ndims() - 3, nCw8c, nChw8c, nCdhw8c)));
openvino-pushbot's avatar
openvino-pushbot committed
312
            if (this->diff_weights_pd_.desc()->format == any)
313 314 315 316 317
                CHECK(this->diff_weights_pd_.set_format(this->with_groups()
                    ? utils::pick(2 * this->ndims() - 6 + flat, gOIw8i8o,
                        gOwi8o, gOIhw8i8o, gOhwi8o, gOIdhw8i8o, gOdhwi8o)
                    : utils::pick(2 * this->ndims() - 6 + flat, OIw8i8o, Owi8o,
                        OIhw8i8o, Ohwi8o, OIdhw8i8o, Odhwi8o)));
openvino-pushbot's avatar
openvino-pushbot committed
318 319
            if (this->diff_bias_pd_.desc()->format == any)
                CHECK(this->diff_bias_pd_.set_format(x));
320 321
            if (this->desc()->alg_kind == alg_kind::convolution_auto)
                CHECK(this->set_alg_kind(alg_kind::convolution_direct));
openvino-pushbot's avatar
openvino-pushbot committed
322 323
            return status::success;
        }
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341

    private:
        void init_balancers() {
            const int max_threads = mkldnn_get_max_threads();
            const size_t max_buffer_size = 1<<21; /* just a heuristic */

            if(with_bias()) {
                reducer_bia_conf_.init(reduce_balancer_t(max_threads,
                            jcp_.oc_block, jcp_.ngroups * jcp_.nb_oc, jcp_.mb,
                            max_buffer_size));
            }

            reducer_wei_conf_.init(reduce_balancer_t(max_threads,
                        jcp_.kd * jcp_.kh * jcp_.kw
                        * jcp_.ic_block * jcp_.oc_block,
                        jcp_.ngroups * jcp_.nb_ic * jcp_.nb_oc,
                        jcp_.mb * jcp_.od, max_buffer_size));
        }
openvino-pushbot's avatar
openvino-pushbot committed
342 343
    };

344
    jit_avx2_convolution_bwd_weights_t(const pd_t *apd,
openvino-pushbot's avatar
openvino-pushbot committed
345
            const input_vector &inputs, const output_vector &outputs)
346
        : cpu_primitive_t(apd, inputs, outputs)
openvino-pushbot's avatar
openvino-pushbot committed
347 348
        , kernel_(nullptr), reducer_weights_(nullptr), reducer_bias_(nullptr)
    {
349 350 351 352 353
        kernel_ = new jit_avx2_conv_bwd_weights_kernel_f32(pd()->jcp_);
        reducer_bias_ =
            new cpu_reducer_t<data_type::f32>(pd()->reducer_bia_conf_);
        reducer_weights_ =
            new cpu_reducer_t<data_type::f32>(pd()->reducer_wei_conf_);
openvino-pushbot's avatar
openvino-pushbot committed
354
    }
355

Alexey Suhov's avatar
Alexey Suhov committed
356 357
    ~jit_avx2_convolution_bwd_weights_t() {
        delete kernel_;
358 359
        delete reducer_weights_;
        delete reducer_bias_;
360
    }
openvino-pushbot's avatar
openvino-pushbot committed
361 362 363

    typedef typename prec_traits<data_type::f32>::type data_t;

364
    virtual void execute(event_t *e) const {
openvino-pushbot's avatar
openvino-pushbot committed
365 366 367 368 369
        execute_backward_weights();
        e->set_state(event_t::ready);
    }

private:
370 371 372
    void execute_backward_weights() const;
    const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }

openvino-pushbot's avatar
openvino-pushbot committed
373 374 375 376 377 378 379 380 381 382 383
    jit_avx2_conv_bwd_weights_kernel_f32 *kernel_;
    cpu_reducer_t<data_type::f32> *reducer_weights_, *reducer_bias_;
};

}
}
}

#endif

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