updated readme files (#54)
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / src / cpu / jit_avx512_core_x8s8s32x_convolution.hpp
1 /*******************************************************************************
2 * Copyright 2016-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_CONVOLUTION_HPP
18 #define CPU_JIT_AVX512_CORE_X8S8S32X_CONVOLUTION_HPP
19
20 #include "c_types_map.hpp"
21 #include "cpu_convolution_pd.hpp"
22 #include "cpu_engine.hpp"
23 #include "jit_transpose_src_utils.hpp"
24 #include "cpu_reducer.hpp"
25 #include "cpu_barrier.hpp"
26
27 #include "jit_avx512_core_x8s8s32x_conv_kernel.hpp"
28
29 namespace mkldnn {
30 namespace impl {
31 namespace cpu {
32
33 template <bool with_relu, impl::data_type_t src_type, impl::data_type_t dst_type>
34 struct _jit_avx512_core_x8s8s32x_convolution_fwd_t : public cpu_primitive_t {
35     struct pd_t : public _cpu_convolution_fwd_pd_t<with_relu> {
36         pd_t(engine_t *engine, const typename pd_t::base_desc_t *adesc,
37                 const primitive_attr_t *attr,
38                 const typename pd_t::base_class *hint_fwd_pd)
39             : _cpu_convolution_fwd_pd_t<with_relu>(engine, adesc, attr,
40                     hint_fwd_pd)
41             , jcp_()
42         {
43         }
44         DECLARE_COMMON_PD_T(
45                 JIT_IMPL_NAME_HELPER("jit_int8:", avx512_core, ""),
46                 _jit_avx512_core_x8s8s32x_convolution_fwd_t<with_relu, src_type,
47                 dst_type>);
48
49         virtual status_t init() override
50         {
51             using namespace prop_kind;
52             assert(this->engine()->kind() == engine_kind::cpu);
53             bool ok = true
54                     && utils::one_of(this->cdesc_().prop_kind, forward_training,
55                                forward_inference)
56                     && this->cdesc_().alg_kind == alg_kind::convolution_direct
57                     && !this->has_zero_dim_memory()
58                     && this->cdesc_().src_desc.data_type == src_type
59                     && this->cdesc_().dst_desc.data_type == dst_type
60                     && IMPLICATION(this->with_bias(), utils::one_of(
61                             this->cdesc_().bias_desc.data_type, data_type::f32,
62                             data_type::s32, data_type::s8, data_type::u8))
63                     && this->cdesc_().accum_data_type == data_type::s32;
64             if (!ok)
65                 return status::unimplemented;
66
67             return jit_avx512_core_x8s8s32x_fwd_kernel::init_conf(
68                     jcp_, this->cdesc_(), this->src_pd_, this->weights_pd_,
69                     this->dst_pd_,this->bias_pd_, *this->attr(),
70                     mkldnn_get_max_threads(),
71                     with_relu, this->negative_slope());
72         }
73
74         jit_conv_conf_t jcp_;
75     };
76
77     _jit_avx512_core_x8s8s32x_convolution_fwd_t(const pd_t *pd,
78             const input_vector &inputs, const output_vector &outputs)
79         : cpu_primitive_t(&conf_, inputs, outputs), conf_(*pd)
80         , local_scales_(nullptr)
81     {
82         kernel_ = new jit_avx512_core_x8s8s32x_fwd_kernel(conf_.jcp_,
83                     *conf_.attr());
84         if (conf_.jcp_.signed_input && conf_.jcp_.ver != ver_vnni) {
85             size_t scales_size = (conf_.attr()->output_scales_.count_ == 1)
86                 ? 16
87                 : conf_.attr()->output_scales_.count_;
88             local_scales_ = (float *)malloc(sizeof(float) * scales_size, 64);
89             for (size_t i = 0; i < scales_size; i++) {
90                 local_scales_[i] = conf_.attr()->output_scales_.scales_[i] *
91                                         (1.f / conf_.jcp_.wei_adj_scale);
92             }
93         }
94     }
95
96     ~_jit_avx512_core_x8s8s32x_convolution_fwd_t() {
97         delete kernel_;
98         if (local_scales_) free(local_scales_);
99     };
100
101     typedef typename prec_traits<src_type>::type src_data_t;
102     typedef typename prec_traits<data_type::s8>::type wei_data_t;
103     typedef typename prec_traits<dst_type>::type dst_data_t;
104
105     virtual void execute(event_t *e)
106     {
107         execute_forward();
108         e->set_state(event_t::ready);
109     }
110
111 private:
112     void execute_forward();
113     pd_t conf_;
114     jit_avx512_core_x8s8s32x_fwd_kernel *kernel_;
115     float *local_scales_;
116 };
117
118 template <impl::data_type_t src_type, impl::data_type_t dst_type>
119 using jit_avx512_core_x8s8s32x_convolution_fwd_t =
120     _jit_avx512_core_x8s8s32x_convolution_fwd_t<false, src_type, dst_type>;
121
122 template <impl::data_type_t src_type, impl::data_type_t dst_type>
123 using jit_avx512_core_x8s8s32x_convolution_relu_t =
124     _jit_avx512_core_x8s8s32x_convolution_fwd_t<true, src_type, dst_type>;
125
126 }
127 }
128 }
129
130 #endif
131
132 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s