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