Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / src / cpu / jit_uni_x8s8s32x_convolution.hpp
1 /*******************************************************************************
2 * Copyright 2018-2019 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_UNI_X8S8S32X_CONVOLUTION_HPP
18 #define CPU_JIT_UNI_X8S8S32X_CONVOLUTION_HPP
19
20 #include "c_types_map.hpp"
21 #include "cpu_convolution_pd.hpp"
22 #include "cpu_engine.hpp"
23 #include "cpu_reducer.hpp"
24 #include "jit_primitive_conf.hpp"
25 #include "jit_uni_x8s8s32x_conv_kernel.hpp"
26 #include "jit_generator.hpp"
27 #include "mkldnn_thread.hpp"
28 #include "jit_uni_depthwise.hpp"
29
30 namespace mkldnn {
31 namespace impl {
32 namespace cpu {
33
34 template <cpu_isa_t isa, impl::data_type_t src_type, impl::data_type_t dst_type>
35 struct _jit_uni_x8s8s32x_convolution_fwd_t: public cpu_primitive_t {
36     struct pd_t: public cpu_convolution_fwd_pd_t {
37         pd_t(engine_t *engine, 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:", isa, ""),
45                 _jit_uni_x8s8s32x_convolution_fwd_t<isa, src_type, dst_type>);
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                 && utils::one_of(this->desc()->prop_kind, forward_training,
52                         forward_inference)
53                 && this->desc()->alg_kind == alg_kind::convolution_direct
54                 && IMPLICATION(this->with_bias(), utils::one_of(
55                     this->desc()->bias_desc.data_type, data_type::f32,
56                     data_type::s32, data_type::s8, data_type::u8))
57                 && this->desc()->accum_data_type == data_type::s32
58                 && this->desc()->src_desc.data_type == src_type
59                 && this->desc()->dst_desc.data_type == dst_type;
60             if (!ok) return status::unimplemented;
61
62             status_t sts = jit_uni_x8s8s32x_conv_fwd_kernel<isa>::init_conf(jcp_, *this->desc(),
63                     this->src_pd_, this->weights_pd_,
64                     this->dst_pd_, this->bias_pd_, *this->attr());
65             if (sts != status::success) return sts;
66
67             if (jcp_.with_dw_conv) {
68                 status_t sts_dw = jit_uni_dw_conv_row_f32<isa>::init_conf(jcp_, jcp_dw_, *this->attr());
69                 if (sts_dw != status::success) return sts_dw;
70             }
71
72             auto scratchpad = scratchpad_registry().registrar();
73             jit_uni_x8s8s32x_conv_fwd_kernel<isa>::init_scratchpad(scratchpad, jcp_, jcp_dw_, *this->attr());
74
75             return status::success;
76         }
77
78         jit_conv_conf_t jcp_;
79         jit_conv_conf_t jcp_dw_;
80     };
81
82     _jit_uni_x8s8s32x_convolution_fwd_t(const pd_t *apd,
83             const input_vector &inputs, const output_vector &outputs)
84         : cpu_primitive_t(apd, inputs, outputs) {
85         kernel_ = new jit_uni_x8s8s32x_conv_fwd_kernel<isa>(pd()->jcp_, pd()->jcp_dw_, *pd()->attr());
86
87         if (pd()->jcp_.with_dw_conv) {
88             kernel_dw_ = new jit_uni_dw_conv_row_f32<isa>(pd()->jcp_dw_, *pd()->attr(), pd()->jcp_dw_.oc);
89         }
90     }
91
92     ~_jit_uni_x8s8s32x_convolution_fwd_t() {
93         delete kernel_;
94
95         if (pd()->jcp_.with_dw_conv) {
96             delete kernel_dw_;
97         }
98     };
99
100     typedef typename prec_traits<data_type::u8>::type src_data_t;
101     typedef typename prec_traits<data_type::s8>::type wei_data_t;
102     typedef typename prec_traits<data_type::f32>::type bia_data_t;
103     typedef typename prec_traits<dst_type>::type dst_data_t;
104
105     virtual void execute(event_t *e) const {
106         if (pd()->jcp_.with_dw_conv)
107             execute_forward_with_dw_conv();
108         else
109             execute_forward();
110
111         e->set_state(event_t::ready);
112     }
113
114 private:
115     void execute_forward() const;
116     void execute_forward_with_dw_conv() const;
117     const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
118     jit_uni_x8s8s32x_conv_fwd_kernel<isa> *kernel_;
119     jit_uni_dw_conv_row_f32<isa> *kernel_dw_;
120 };
121
122 template <impl::data_type_t src_type, impl::data_type_t dst_type>
123 using jit_avx2_x8s8s32x_convolution_fwd_t = _jit_uni_x8s8s32x_convolution_fwd_t<avx2, src_type, dst_type>;
124
125 template <impl::data_type_t src_type, impl::data_type_t dst_type>
126 using jit_sse42_x8s8s32x_convolution_fwd_t = _jit_uni_x8s8s32x_convolution_fwd_t<sse42, src_type, dst_type>;
127
128 }
129 }
130 }
131
132 #endif