Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / src / cpu / jit_uni_x8s8s32x_dw_convolution.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_UNI_X8S8S32X_DW_CONVOLUTION_HPP
18 #define CPU_JIT_UNI_X8S8S32X_DW_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_generator.hpp"
25 #include "jit_uni_x8s8s32x_dw_conv_kernel.hpp"
26
27 namespace mkldnn {
28 namespace impl {
29 namespace cpu {
30
31 template <cpu_isa_t isa, impl::data_type_t src_type, impl::data_type_t dst_type>
32 struct _jit_uni_x8s8s32x_dw_convolution_fwd_t: public cpu_primitive_t {
33     struct pd_t: public cpu_convolution_fwd_pd_t {
34         pd_t(engine_t *engine, 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,
38                 hint_fwd_pd)
39             , jcp_() {}
40
41         DECLARE_COMMON_PD_T(
42                 JIT_IMPL_NAME_HELPER("jit_dw:", isa, ""),
43                 _jit_uni_x8s8s32x_dw_convolution_fwd_t<isa, src_type, dst_type>);
44
45         virtual status_t init() override {
46             using namespace prop_kind;
47             assert(this->engine()->kind() == engine_kind::cpu);
48             bool ok = true
49                 && this->set_default_params() == status::success
50                 && utils::one_of(this->desc()->prop_kind, forward_training,
51                         forward_inference)
52                 && this->desc()->alg_kind == alg_kind::convolution_direct
53                 && this->desc()->dst_desc.data_type == dst_type
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             if (!ok) return status::unimplemented;
59
60             return jit_uni_x8s8s32x_dw_conv_fwd_kernel<isa>::init_conf(jcp_,
61                         *this->desc(),
62                         *this->src_pd_.desc(), *this->weights_pd_.desc(),
63                         *this->dst_pd_.desc(), *this->bias_pd_.desc(),
64                         *this->attr());
65         }
66
67         jit_conv_conf_t jcp_;
68
69     protected:
70         virtual status_t set_default_params() override {
71             using namespace memory_format;
72             auto desired_act_fmt = nhwc;
73             auto desired_wei_fmt = isa == avx512_common ? Goihw16g : Goihw8g;
74
75             if (this->src_pd_.desc()->format == any)
76                 CHECK(this->src_pd_.set_format(desired_act_fmt));
77             if (this->dst_pd_.desc()->format == any)
78                 CHECK(this->dst_pd_.set_format(desired_act_fmt));
79             if (this->weights_pd_.desc()->format == any)
80                 CHECK(this->weights_pd_.set_format(desired_wei_fmt));
81             if (this->bias_pd_.desc()->format == any)
82                 CHECK(this->bias_pd_.set_format(x));
83             return status::success;
84         }
85     };
86
87     _jit_uni_x8s8s32x_dw_convolution_fwd_t(const pd_t *apd,
88             const input_vector &inputs, const output_vector &outputs)
89         : cpu_primitive_t(apd, inputs, outputs)
90     {
91         kernel_ = new jit_uni_x8s8s32x_dw_conv_fwd_kernel<isa>(pd()->jcp_, *pd()->attr());
92     }
93
94     ~_jit_uni_x8s8s32x_dw_convolution_fwd_t() { delete kernel_; };
95
96     typedef typename prec_traits<data_type::u8>::type src_data_t;
97     typedef typename prec_traits<data_type::s8>::type wei_data_t;
98     typedef typename prec_traits<dst_type>::type dst_data_t;
99
100     virtual void execute(event_t *e) const {
101         execute_forward();
102         e->set_state(event_t::ready);
103     }
104
105 private:
106     void execute_forward() const ;
107     const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
108     jit_uni_x8s8s32x_dw_conv_fwd_kernel<isa> *kernel_;
109 };
110
111 template <impl::data_type_t src_type, impl::data_type_t dst_type>
112 using jit_avx2_x8s8s32x_dw_convolution_fwd_t = _jit_uni_x8s8s32x_dw_convolution_fwd_t<avx2, src_type, dst_type>;
113 template <impl::data_type_t src_type, impl::data_type_t dst_type>
114 using jit_sse42_x8s8s32x_dw_convolution_fwd_t = _jit_uni_x8s8s32x_dw_convolution_fwd_t<sse42, src_type, dst_type>;
115
116 }
117 }
118 }
119
120 #endif