Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / src / cpu / jit_avx512_core_x8s8s32x_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_X8S8S32X_1X1_CONVOLUTION_HPP
18 #define CPU_JIT_AVX512_CORE_X8S8S32X_1X1_CONVOLUTION_HPP
19
20 #include "c_types_map.hpp"
21 #include "memory_tracking.hpp"
22 #include "mkldnn_thread.hpp"
23 #include "utils.hpp"
24
25 #include "cpu_convolution_pd.hpp"
26 #include "cpu_engine.hpp"
27
28 #include "jit_avx512_core_x8s8s32x_1x1_conv_kernel.hpp"
29 #include "jit_uni_1x1_conv_utils.hpp"
30
31 namespace mkldnn {
32 namespace impl {
33 namespace cpu {
34
35 template<impl::data_type_t src_type, impl::data_type_t dst_type>
36 struct jit_avx512_core_x8s8s32x_1x1_convolution_fwd_t : public cpu_primitive_t {
37     struct pd_t: public cpu_convolution_fwd_pd_t {
38         pd_t(engine_t *engine, const convolution_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(engine, adesc, attr, hint_fwd_pd)
42             , jcp_(), rtus_() {}
43
44         DECLARE_COMMON_PD_T(
45                 JIT_IMPL_NAME_HELPER("jit_int8_1x1:", avx512_core, ""),
46                 jit_avx512_core_x8s8s32x_1x1_convolution_fwd_t<
47                 src_type, dst_type>);
48
49         virtual status_t init() override {
50             using namespace prop_kind;
51             using namespace utils;
52             assert(this->engine()->kind() == engine_kind::cpu);
53             bool ok = true
54                 && this->set_default_params() == status::success
55                 && utils::one_of(this->desc()->prop_kind, forward_training,
56                         forward_inference)
57                 && utils::one_of(this->desc()->alg_kind,
58                         alg_kind::convolution_auto,
59                         alg_kind::convolution_direct)
60                 && !this->has_zero_dim_memory()
61                 && this->desc()->src_desc.data_type == src_type
62                 && this->desc()->dst_desc.data_type == dst_type
63                 && this->desc()->weights_desc.data_type == data_type::s8
64                 && IMPLICATION(this->with_bias(), utils::one_of(
65                             this->desc()->bias_desc.data_type, data_type::f32,
66                             data_type::s32, data_type::s8, data_type::u8))
67                 && this->desc()->accum_data_type == data_type::s32;
68             if (!ok) return status::unimplemented;
69
70             const convolution_desc_t *conv_d = this->desc();
71             const memory_desc_t *src_d = this->src_pd_.desc();
72             rtus_prepare(this, conv_d, src_d, this->dst_pd_.desc());
73
74             status_t status =
75                 jit_avx512_core_x8s8s32x_1x1_conv_kernel::init_conf(jcp_,
76                         *conv_d, *src_d, *this->weights_pd_.desc(),
77                         *this->dst_pd_.desc(), *this->bias_pd_.desc(),
78                         *this->attr(), mkldnn_get_max_threads(),
79                         rtus_.reduce_src_);
80             if (status != status::success) return status;
81
82             auto scratchpad = scratchpad_registry().registrar();
83             jit_avx512_core_x8s8s32x_1x1_conv_kernel::init_scratchpad(
84                     scratchpad, jcp_, *this->attr());
85
86             rtus_prepare_space_info(this, scratchpad);
87
88             return status::success;
89         }
90
91         jit_1x1_conv_conf_t jcp_;
92         reduce_to_unit_stride_t rtus_;
93
94     protected:
95         virtual status_t set_default_params() override {
96             using namespace memory_format;
97             bool is_sign_input =
98                 this->desc()->src_desc.data_type == data_type::s8;
99
100             if (this->src_pd_.desc()->format == any)
101                 CHECK(this->src_pd_.set_format(nhwc));
102             if (this->dst_pd_.desc()->format == any)
103                 CHECK(this->dst_pd_.set_format(nhwc));
104             if (this->weights_pd_.desc()->format == any)
105                 CHECK(this->weights_pd_.set_format(this->with_groups()
106                     ? (is_sign_input ? gOIhw4i16o4i_s8s8 : gOIhw4i16o4i)
107                     : (is_sign_input ? OIhw4i16o4i_s8s8 : OIhw4i16o4i)));
108             if (this->bias_pd_.desc()->format == any)
109                 CHECK(this->bias_pd_.set_format(x));
110             if (this->desc()->alg_kind == alg_kind::convolution_auto)
111                 CHECK(this->set_alg_kind(alg_kind::convolution_direct));
112
113             return status::success;
114         }
115     };
116
117     template <cpu_isa_t isa, typename conv_t>
118     friend void init_rtus_driver(conv_t *self);
119
120     jit_avx512_core_x8s8s32x_1x1_convolution_fwd_t(const pd_t *apd,
121             const input_vector &inputs, const output_vector &outputs)
122         : cpu_primitive_t(apd, inputs, outputs)
123         , kernel_(nullptr), rtus_driver_(nullptr)
124     {
125         kernel_ = new jit_avx512_core_x8s8s32x_1x1_conv_kernel(pd()->jcp_,
126                     *pd()->attr());
127         init_rtus_driver<avx512_common>(this);
128     }
129
130     ~jit_avx512_core_x8s8s32x_1x1_convolution_fwd_t() {
131         delete kernel_;
132         delete rtus_driver_;
133     }
134
135     typedef typename prec_traits<src_type>::type src_data_t;
136     typedef typename prec_traits<data_type::s8>::type wei_data_t;
137     typedef typename prec_traits<dst_type>::type dst_data_t;
138     typedef typename prec_traits<data_type::s32>::type acc_data_t;
139
140     virtual void execute(event_t *e) const {
141         execute_forward();
142         e->set_state(event_t::ready);
143     }
144
145   private:
146     void execute_forward() const;
147     void execute_forward_thr(const int ithr, const int nthr,
148             const src_data_t *src, const wei_data_t *weights,
149             const char *bias, dst_data_t *dst,
150             const memory_tracking::grantor_t &scratchpad) const;
151     const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
152
153     jit_avx512_core_x8s8s32x_1x1_conv_kernel *kernel_;
154     rtus_driver_t<avx512_common> *rtus_driver_;
155 };
156
157 }
158 }
159 }
160
161 #endif