Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / src / cpu / jit_avx512_core_fp32_wino_conv_2x3.hpp
1 /*******************************************************************************
2 * Copyright 2018 Intel Corporation
3 *
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
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
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 *******************************************************************************/
16
17 #ifndef CPU_JIT_AVX512_CORE_FP32_WINO_CONV_2x3_HPP
18 #define CPU_JIT_AVX512_CORE_FP32_WINO_CONV_2x3_HPP
19
20 #include <assert.h>
21
22 #include "c_types_map.hpp"
23 #include "cpu_convolution_pd.hpp"
24 #include "cpu_engine.hpp"
25 #include "mkldnn_thread.hpp"
26 #include "type_helpers.hpp"
27 #include "utils.hpp"
28
29 #include "jit_primitive_conf.hpp"
30 #include "jit_generator.hpp"
31
32 namespace mkldnn {
33 namespace impl {
34 namespace cpu {
35
36 struct jit_avx512_core_fp32_wino_conv_2x3_fwd_ker_t;
37 struct jit_avx512_core_fp32_wino_conv_2x3_src_trans_t;
38 struct jit_avx512_core_fp32_wino_conv_2x3_dst_trans_t;
39
40 struct jit_avx512_core_fp32_wino_conv_2x3_fwd_t : public cpu_primitive_t {
41     struct pd_t : public cpu_convolution_fwd_pd_t {
42         pd_t(engine_t *engine, const convolution_desc_t *adesc,
43                 const primitive_attr_t *attr,
44                 const typename pd_t::base_class *hint_fwd_pd)
45             : cpu_convolution_fwd_pd_t(engine, adesc, attr, hint_fwd_pd)
46             , jcp_() {}
47
48         DECLARE_COMMON_PD_T(
49                 JIT_IMPL_NAME_HELPER("jit_fp32_wino_2x3:", avx512_core, ""),
50                 jit_avx512_core_fp32_wino_conv_2x3_fwd_t);
51
52         virtual status_t init() override {
53             using namespace prop_kind;
54             using namespace memory_format;
55             assert(this->engine()->kind() == engine_kind::cpu);
56             bool ok = true && this->set_default_params() == status::success
57                     && utils::one_of(this->desc()->prop_kind, forward_inference)
58                     && utils::one_of(this->desc()->alg_kind,
59                                alg_kind::convolution_auto,
60                                alg_kind::convolution_winograd)
61                     && this->desc()->src_desc.data_type == data_type::f32
62                     && this->desc()->dst_desc.data_type == data_type::f32
63                     && this->desc()->weights_desc.data_type == data_type::f32
64                     && IMPLICATION(this->with_bias(),
65                                utils::one_of(this->desc()->bias_desc.data_type,
66                                        data_type::f32));
67             if (!ok)
68                 return status::unimplemented;
69
70             memory_desc_t expect_wei_md = *(this->weights_pd_.desc());
71             status_t jit_conf_result = jit_conf(expect_wei_md);
72             if (jit_conf_result != success) return jit_conf_result;
73
74             cpu_memory_t::pd_t new_weights_pd(this->engine_, &expect_wei_md);
75             if (this->weights_pd_.desc()->format == any)
76                 this->weights_pd_ = new_weights_pd;
77             if (!this->weights_pd_.is_equal(&new_weights_pd))
78                 return unimplemented;
79
80             init_scratchpad();
81
82             if (this->desc()->alg_kind == alg_kind::convolution_auto)
83                CHECK(this->set_alg_kind(alg_kind::convolution_winograd));
84
85             return success;
86         }
87
88         jit_conv_conf_2x3_wino_t jcp_;
89
90     protected:
91         status_t jit_conf(memory_desc_t& expect_wei_md);
92
93         void init_scratchpad() {
94             using namespace memory_tracking::names;
95
96             auto scratchpad = this->scratchpad_registry().registrar();
97
98             int wino_size_offset = (jcp_.yb / 2) * (jcp_.xb / 2) + jcp_.xb;
99
100             size_t V_sz = (size_t)jcp_.ic * 16 * wino_size_offset * jcp_.nthr;
101             scratchpad.book(key_wino_V, sizeof(float) * V_sz, PAGE_4K);
102
103             size_t M_sz = (size_t)jcp_.oc * 16 * wino_size_offset * jcp_.nthr;
104             scratchpad.book(key_wino_M, sizeof(float) * M_sz, PAGE_4K);
105
106             if (wants_padded_bias()) {
107                 assert(jcp_.ngroups == 1);
108                 scratchpad.book(key_conv_padded_bias, sizeof(float) * jcp_.oc);
109             }
110         }
111
112         virtual status_t set_default_params() override {
113             using namespace memory_format;
114             if (this->src_pd_.desc()->format == any)
115                 CHECK(this->src_pd_.set_format(nChw16c));
116             if (this->dst_pd_.desc()->format == any)
117                 CHECK(this->dst_pd_.set_format(nChw16c));
118             if (this->bias_pd_.desc()->format == any)
119                 CHECK(this->bias_pd_.set_format(x));
120             return status::success;
121         }
122     };
123
124     jit_avx512_core_fp32_wino_conv_2x3_fwd_t(const pd_t *apd,
125             const input_vector &inputs, const output_vector &outputs);
126
127     ~jit_avx512_core_fp32_wino_conv_2x3_fwd_t();
128
129     virtual void execute(event_t *e) const {
130         execute_forward();
131         e->set_state(event_t::ready);
132     }
133
134 private:
135     void execute_forward() const;
136     void execute_forward_small_mb() const;
137     void execute_forward_mbN() const;
138     const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
139
140     jit_avx512_core_fp32_wino_conv_2x3_fwd_ker_t *kernel_;
141     jit_avx512_core_fp32_wino_conv_2x3_src_trans_t *src_trans_;
142     jit_avx512_core_fp32_wino_conv_2x3_dst_trans_t *dst_trans_;
143 };
144
145 }
146 }
147 }
148
149 #endif