Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / src / cpu / jit_avx512_core_u8s8s32x_wino_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_WINO_CONVOLUTION_HPP
18 #define CPU_JIT_AVX512_CORE_U8S8S32X_WINO_CONVOLUTION_HPP
19
20 #include <assert.h>
21
22 #include "c_types_map.hpp"
23 #include "cpu_convolution_pd.hpp"
24 #include "cpu_engine.hpp"
25 #include "mkldnn_thread.hpp"
26 #include "type_helpers.hpp"
27 #include "utils.hpp"
28
29 #include "jit_primitive_conf.hpp"
30
31 #include "jit_generator.hpp"
32
33 namespace mkldnn {
34 namespace impl {
35 namespace cpu {
36
37 struct jit_avx512_core_u8s8s32x_wino_conv_fwd_ker_t;
38 struct jit_avx512_core_u8s8s32x_wino_conv_src_trans_t;
39 struct jit_avx512_core_u8s8s32x_wino_conv_dst_trans_t;
40
41 template <data_type_t dst_data_type>
42 struct jit_avx512_core_u8s8s32x_wino_convolution_fwd_t : public cpu_primitive_t {
43     struct pd_t : public cpu_convolution_fwd_pd_t {
44         pd_t(engine_t *engine, const convolution_desc_t *adesc,
45                 const primitive_attr_t *attr,
46                 const typename pd_t::base_class *hint_fwd_pd)
47             :  cpu_convolution_fwd_pd_t(engine, adesc, attr, hint_fwd_pd)
48             , jcp_()
49         {}
50         DECLARE_COMMON_PD_T(
51                 JIT_IMPL_NAME_HELPER("jit_int8_wino:", avx512_core, ""),
52                 jit_avx512_core_u8s8s32x_wino_convolution_fwd_t<dst_data_type>);
53
54         virtual status_t init() override {
55             using namespace prop_kind;
56             using namespace memory_format;
57             assert(this->engine()->kind() == engine_kind::cpu);
58             bool ok = true
59                 && this->set_default_params() == status::success
60                 && utils::one_of(this->desc()->prop_kind,
61                                     forward_training, forward_inference)
62                 && utils::one_of(this->desc()->alg_kind,
63                            alg_kind::convolution_auto,
64                            alg_kind::convolution_winograd)
65                 && !this->has_zero_dim_memory()
66                 && this->desc()->src_desc.data_type == data_type::u8
67                 && this->desc()->dst_desc.data_type == dst_data_type
68                 && this->desc()->weights_desc.data_type == data_type::s8
69                 && IMPLICATION(this->with_bias(),
70                     utils::one_of(this->desc()->bias_desc.data_type,
71                                                 data_type::f32, data_type::s32,
72                                                 data_type::s8, data_type::u8))
73                 && this->desc()->accum_data_type == data_type::s32;
74
75             if (!ok) return status::unimplemented;
76
77             status_t status = jit_conf();
78             if (status != status::success) return status;
79
80             init_scratchpad();
81
82             if (status == status::success
83                     && this->desc()->alg_kind == alg_kind::convolution_auto)
84                 this->set_alg_kind(alg_kind::convolution_winograd);
85             return status;
86         }
87
88         jit_conv_conf_2x3_wino_t jcp_;
89
90     protected:
91         status_t jit_conf();
92         void init_scratchpad();
93
94         virtual status_t set_default_params() override {
95             using namespace memory_format;
96             if (this->src_pd_.desc()->format == any)
97                 CHECK(this->src_pd_.set_format(nhwc));
98             if (this->dst_pd_.desc()->format == any)
99                 CHECK(this->dst_pd_.set_format(nhwc));
100             if (this->bias_pd_.desc()->format == any)
101                 CHECK(this->bias_pd_.set_format(x));
102             return status::success;
103         }
104     };
105
106     typedef typename prec_traits<data_type::u8>::type src_data_t;
107     typedef typename prec_traits<data_type::s8>::type wei_data_t;
108     typedef typename prec_traits<data_type::s32>::type acc_data_t;
109     typedef typename prec_traits<dst_data_type>::type dst_data_t;
110
111     jit_avx512_core_u8s8s32x_wino_convolution_fwd_t(const pd_t *apd,
112             const input_vector &inputs, const output_vector &outputs);
113     ~jit_avx512_core_u8s8s32x_wino_convolution_fwd_t();
114
115     virtual void execute(event_t *e) const {
116         execute_forward();
117         e->set_state(event_t::ready);
118     }
119
120 private:
121     const float *adjust_oscales(const memory_tracking::grantor_t &scratchpad)
122         const;
123     void execute_forward() const;
124     void execute_forward_small_mb() const;
125     void execute_forward_mbN() const;
126     const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
127
128     jit_avx512_core_u8s8s32x_wino_conv_fwd_ker_t *kernel_;
129     jit_avx512_core_u8s8s32x_wino_conv_src_trans_t *src_trans_;
130     jit_avx512_core_u8s8s32x_wino_conv_dst_trans_t *dst_trans_;
131 };
132
133 }
134 }
135 }
136
137 #endif
138
139 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s