Publishing R3
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / src / cpu / jit_avx512_core_u8s8s32x_1x1_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_AVX512_CORE_U8S8S32X_1X1_CONVOLUTION_HPP
18 #define CPU_JIT_AVX512_CORE_U8S8S32X_1X1_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 "mkldnn_thread.hpp"
25 #include "utils.hpp"
26
27 #include "jit_uni_1x1_conv_utils.hpp"
28 #include "jit_avx512_core_u8s8s32x_1x1_conv_kernel.hpp"
29
30 namespace mkldnn {
31 namespace impl {
32 namespace cpu {
33
34 template <bool with_relu, impl::data_type_t dst_type>
35 struct _jit_avx512_core_u8s8s32x_1x1_convolution_fwd_t : public cpu_primitive_t {
36     struct pd_t: public _cpu_convolution_fwd_pd_t<with_relu> {
37         pd_t(engine_t *engine,
38                 const typename pd_t::base_desc_t *adesc,
39                 const primitive_attr_t *attr,
40                 const typename pd_t::base_class *hint_fwd_pd)
41             : _cpu_convolution_fwd_pd_t<with_relu>(engine, adesc, attr,
42                     hint_fwd_pd)
43             , jcp_(), rtus_() {}
44
45         DECLARE_COMMON_PD_T(
46                 JIT_IMPL_NAME_HELPER("jit_1x1:", avx512_core, ""),
47                 _jit_avx512_core_u8s8s32x_1x1_convolution_fwd_t<with_relu,
48                 dst_type>);
49
50         virtual status_t init() override {
51             using namespace prop_kind;
52             using namespace utils;
53             assert(this->engine()->kind() == engine_kind::cpu);
54             bool ok = true
55                 && this->set_default_params() == status::success
56                 && utils::one_of(this->cdesc_().prop_kind, forward_training,
57                         forward_inference)
58                 && this->cdesc_().alg_kind == alg_kind::convolution_direct
59                 && this->cdesc_().src_desc.data_type == data_type::u8
60                 && this->cdesc_().dst_desc.data_type == dst_type
61                 && this->cdesc_().weights_desc.data_type == data_type::s8
62                 && utils::implication(this->with_bias(), utils::one_of(
63                             this->cdesc_().bias_desc.data_type, data_type::f32,
64                             data_type::s32, data_type::s8, data_type::u8))
65                 && this->cdesc_().accum_data_type == data_type::s32;
66
67             if (!ok) return status::unimplemented;
68
69             const convolution_desc_t *conv_d = &this->cdesc_();
70             const memory_desc_t *src_d = this->src_pd_.desc();
71             rtus_prepare(this, conv_d, src_d, this->dst_pd_.desc());
72             return jit_avx512_core_u8s8s32x_1x1_conv_kernel::init_conf(jcp_,
73                     *conv_d, *src_d, *this->weights_pd_.desc(),
74                     *this->dst_pd_.desc(), *this->bias_pd_.desc(), *this->attr(),
75                     with_relu, this->negative_slope(),
76                     omp_get_max_threads(), rtus_.reduce_src_);
77         }
78
79         jit_1x1_conv_conf_t jcp_;
80         struct reduce_to_unit_stride_t {
81             convolution_desc_t conv_d_;
82             bool reduce_src_;
83         } rtus_;
84
85       protected:
86         virtual status_t set_default_params() override {
87             using namespace memory_format;
88             if (this->src_pd_.desc()->format == any)
89                 CHECK(this->src_pd_.set_format(nhwc));
90             if (this->dst_pd_.desc()->format == any)
91                 CHECK(this->dst_pd_.set_format(nhwc));
92             if (this->weights_pd_.desc()->format == any)
93                 CHECK(this->weights_pd_.set_format(this->with_groups()
94                                         ? gOIhw4i16o4i : OIhw4i16o4i));
95             if (this->bias_pd_.desc()->format == any)
96                 CHECK(this->bias_pd_.set_format(x));
97             return status::success;
98         }
99     };
100
101     template <cpu_isa_t isa, typename conv_t>
102     friend void init_rtus_driver(conv_t *self);
103     _jit_avx512_core_u8s8s32x_1x1_convolution_fwd_t(const pd_t *pd,
104                                           const input_vector &inputs,
105                                           const output_vector &outputs)
106         : cpu_primitive_t(&conf_, inputs, outputs), conf_(*pd)
107         , kernel_(nullptr), rtus_driver_(nullptr), ws_per_thread_(0)
108         , scratch_(nullptr)
109     {
110         kernel_ = new jit_avx512_core_u8s8s32x_1x1_conv_kernel(conf_.jcp_,
111                     *conf_.attr());
112
113         ws_size_ = conf_.jcp_.mb * conf_.jcp_.oc * conf_.jcp_.ow * conf_.jcp_.oh;
114         ws_ = (acc_data_t *)malloc(ws_size_ * sizeof(acc_data_t), 64);
115         init_rtus_driver<avx512_common>(this);
116     }
117     ~_jit_avx512_core_u8s8s32x_1x1_convolution_fwd_t() {
118         delete kernel_;
119         delete rtus_driver_;
120         free(ws_);
121         free(scratch_);
122     }
123
124     typedef typename prec_traits<data_type::u8>::type src_data_t;
125     typedef typename prec_traits<data_type::s8>::type wei_data_t;
126     typedef typename prec_traits<dst_type>::type dst_data_t;
127     typedef typename prec_traits<data_type::s32>::type acc_data_t;
128
129     virtual void execute(event_t *e) {
130         execute_forward();
131         e->set_state(event_t::ready);
132     }
133
134   private:
135     void execute_forward();
136     pd_t conf_;
137     jit_avx512_core_u8s8s32x_1x1_conv_kernel *kernel_;
138
139     rtus_driver_t<avx512_common> *rtus_driver_;
140     size_t ws_per_thread_;
141     src_data_t *scratch_;
142     acc_data_t *ws_;
143     size_t ws_size_;
144 };
145
146 template <impl::data_type_t dst_type>
147 using jit_avx512_core_u8s8s32x_1x1_convolution_fwd_t =
148     _jit_avx512_core_u8s8s32x_1x1_convolution_fwd_t<false, dst_type>;
149
150 template <impl::data_type_t dst_type>
151 using jit_avx512_core_u8s8s32x_1x1_convolution_relu_t =
152     _jit_avx512_core_u8s8s32x_1x1_convolution_fwd_t<true, dst_type>;
153
154 }
155 }
156 }
157
158 #endif