Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / src / cpu / jit_sse42_1x1_convolution.hpp
1 /*******************************************************************************
2 * Copyright 2017-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_SSE42_1x1_CONVOLUTION_HPP
18 #define CPU_JIT_SSE42_1x1_CONVOLUTION_HPP
19
20 #include "c_types_map.hpp"
21 #include "cpu_convolution_pd.hpp"
22 #include "cpu_engine.hpp"
23 #include "jit_sse42_1x1_conv_kernel_f32.hpp"
24 #include "mkldnn_thread.hpp"
25 #include "utils.hpp"
26 #include "jit_uni_depthwise.hpp"
27
28 namespace mkldnn {
29 namespace impl {
30 namespace cpu {
31
32 struct jit_sse42_1x1_convolution_fwd_t: public cpu_primitive_t {
33     // TODO: (Roma) Code duplication duplication! Remove with templates
34     //              (maybe...)!
35     struct pd_t: public cpu_convolution_fwd_pd_t {
36         pd_t(engine_t *engine,
37                 const convolution_desc_t *adesc,
38                 const primitive_attr_t *attr,
39                 const typename pd_t::base_class *hint_fwd_pd)
40             : cpu_convolution_fwd_pd_t(engine, adesc, attr, hint_fwd_pd)
41             , jcp_(), jcp_dw_() {}
42
43         DECLARE_COMMON_PD_T(
44                 JIT_IMPL_NAME_HELPER("jit_1x1:", sse42, ""),
45                 jit_sse42_1x1_convolution_fwd_t);
46
47         virtual status_t init() override {
48             using namespace prop_kind;
49             assert(this->engine()->kind() == engine_kind::cpu);
50             bool ok = true
51                 && this->set_default_params() == status::success
52                 && utils::one_of(this->desc()->prop_kind, forward_training,
53                         forward_inference)
54                 && utils::one_of(this->desc()->alg_kind,
55                            alg_kind::convolution_auto,
56                            alg_kind::convolution_direct)
57                 && !this->has_zero_dim_memory()
58                 && utils::everyone_is(data_type::f32,
59                         this->desc()->src_desc.data_type,
60                         this->desc()->weights_desc.data_type,
61                         this->desc()->dst_desc.data_type)
62                 && IMPLICATION(this->with_bias(),
63                         data_type::f32 == this->desc()->bias_desc.data_type);
64             if (!ok) return status::unimplemented;
65
66             status_t sts_1x1 = jit_sse42_1x1_conv_kernel_f32::init_conf(jcp_,
67                     *this->desc(),
68                     *this->src_pd_.desc(), *this->weights_pd_.desc(),
69                     *this->dst_pd_.desc(), *this->attr());
70             if (sts_1x1 != status::success) return sts_1x1;
71
72             if (jcp_.with_dw_conv) {
73                 status_t sts_dw = jit_uni_dw_conv_row_f32<sse42>::init_conf(jcp_, jcp_dw_, *this->attr());
74                 if (sts_dw != status::success) return sts_dw;
75             }
76
77             auto scratchpad = scratchpad_registry().registrar();
78             jit_sse42_1x1_conv_kernel_f32::init_scratchpad(scratchpad, jcp_, jcp_dw_);
79
80             return status::success;
81         }
82
83         jit_1x1_conv_conf_t jcp_;
84         jit_conv_conf_t jcp_dw_;
85
86     protected:
87         virtual status_t set_default_params() override {
88             using namespace memory_format;
89             if (this->src_pd_.desc()->format == any)
90                 CHECK(this->src_pd_.set_format(utils::pick(this->ndims() - 3,
91                     nCw8c, nChw8c)));
92             if (this->dst_pd_.desc()->format == any)
93                 CHECK(this->dst_pd_.set_format(utils::pick(this->ndims() - 3,
94                     nCw8c, nChw8c)));
95             if (this->weights_pd_.desc()->format == any)
96                 CHECK(this->weights_pd_.set_format(this->with_groups()
97                     ? utils::pick(this->ndims() - 3, gOIw8i8o, gOIhw8i8o)
98                     : utils::pick(this->ndims() - 3, OIw8i8o, OIhw8i8o)));
99             if (this->bias_pd_.desc()->format == any)
100                 CHECK(this->bias_pd_.set_format(x));
101             if (this->desc()->alg_kind == alg_kind::convolution_auto)
102                 CHECK(this->set_alg_kind(alg_kind::convolution_direct));
103             return status::success;
104         }
105     };
106
107     jit_sse42_1x1_convolution_fwd_t(const pd_t *apd,
108             const input_vector &inputs, const output_vector &outputs)
109         : cpu_primitive_t(apd, inputs, outputs)
110     {
111         kernel_ = new jit_sse42_1x1_conv_kernel_f32(pd()->jcp_, pd()->jcp_dw_, *pd()->attr());
112
113         if (pd()->jcp_.with_dw_conv) {
114             kernel_dw_ = new jit_uni_dw_conv_row_f32<sse42>(pd()->jcp_dw_, *pd()->attr(), pd()->jcp_dw_.ch_block);
115         }
116     }
117
118     ~jit_sse42_1x1_convolution_fwd_t() {
119         delete kernel_;
120
121         if (pd()->jcp_.with_dw_conv) {
122             delete kernel_dw_;
123         }
124     };
125
126     typedef typename prec_traits<data_type::f32>::type data_t;
127
128     virtual void execute(event_t *e) const {
129         if (pd()->jcp_.with_dw_conv)
130             execute_forward_with_dw_conv();
131         else
132             execute_forward();
133
134         e->set_state(event_t::ready);
135     }
136
137 private:
138     void execute_forward() const;
139     void execute_forward_with_dw_conv() const;
140     const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
141
142     jit_sse42_1x1_conv_kernel_f32 *kernel_;
143     jit_uni_dw_conv_row_f32<sse42> *kernel_dw_;
144 };
145
146 }
147 }
148 }
149
150 #endif