Add a section of how to link IE with CMake project (#99)
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / src / cpu / gemm_x8s8s32x_inner_product.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 GEMM_X8S8S32X_INNER_PRODUCT_HPP
18 #define GEMM_X8S8S32X_INNER_PRODUCT_HPP
19
20 #include <assert.h>
21
22 #include "c_types_map.hpp"
23 #include "memory_tracking.hpp"
24 #include "type_helpers.hpp"
25 #include "utils.hpp"
26
27 #include "gemm/gemm.hpp"
28 #include "jit_generator.hpp"
29
30 #include "cpu_inner_product_pd.hpp"
31
32 namespace mkldnn {
33 namespace impl {
34 namespace cpu {
35
36 template <impl::data_type_t src_type, impl::data_type_t dst_type>
37 struct gemm_x8s8s32x_inner_product_fwd_t: public cpu_primitive_t {
38     struct pd_t: public cpu_inner_product_fwd_pd_t {
39         pd_t(engine_t *engine, const inner_product_desc_t *adesc,
40                 const primitive_attr_t *attr,
41                 const inner_product_fwd_pd_t *hint_fwd_pd)
42             : cpu_inner_product_fwd_pd_t(engine, adesc, attr, hint_fwd_pd) {}
43
44         DECLARE_COMMON_PD_T(src_type == data_type::u8
45                 ? IGEMM_S8U8S32_IMPL_STR
46                 : IGEMM_S8S8S32_IMPL_STR,
47                 gemm_x8s8s32x_inner_product_fwd_t);
48
49         virtual status_t init() override {
50             using namespace utils;
51             using namespace data_type;
52
53             assert(engine()->kind() == engine_kind::cpu);
54
55             bool ok = true
56                 && this->set_default_params() == status::success
57                 && one_of(desc()->prop_kind, prop_kind::forward_training,
58                         prop_kind::forward_inference)
59                 && !has_zero_dim_memory()
60                 && this->desc()->src_desc.data_type == src_type
61                 && this->desc()->dst_desc.data_type == dst_type
62                 && this->desc()->weights_desc.data_type == s8
63                 && IMPLICATION(this->with_bias(), utils::one_of(
64                             this->desc()->bias_desc.data_type, f32, s32, s8,
65                             u8))
66                 && attr()->post_ops_.len_ <= 1
67                 && IMPLICATION(attr()->post_ops_.len_,
68                         attr()->post_ops_.entry_[0].is_relu(true, false))
69                 && dense_gemm_consitency_check(src_pd(), weights_pd(),
70                         dst_pd());
71             if (!ok) return status::unimplemented;
72
73             dst_is_acc_ = one_of(dst_type, s32, f32);
74
75             init_scratchpad();
76
77             return status::success;
78         }
79
80         bool dst_is_acc_;
81
82     protected:
83         virtual status_t set_default_params() override {
84             using namespace memory_format;
85
86             if (this->src_pd_.desc()->format == any) {
87                 if (ndims() == 4) CHECK(this->src_pd_.set_format(nhwc));
88                 else if (ndims() == 5) CHECK(this->src_pd_.set_format(ndhwc));
89                 else CHECK(this->src_pd_.set_format(nc));
90             }
91             if (this->dst_pd_.desc()->format == any)
92                 CHECK(this->dst_pd_.set_format(nc));
93             if (this->weights_pd_.desc()->format == any) {
94                 if (ndims() == 4) CHECK(this->weights_pd_.set_format(hwio));
95                 else if (ndims() == 5) CHECK(this->weights_pd_.set_format(dhwio));
96                 else CHECK(this->weights_pd_.set_format(io));
97             }
98             if (this->bias_pd_.desc()->format == any)
99                 CHECK(this->bias_pd_.set_format(x));
100
101             return status::success;
102         }
103
104     private:
105         void init_scratchpad() {
106             if (!dst_is_acc_) {
107                 auto scratchpad = scratchpad_registry().registrar();
108                 scratchpad.book(
109                         memory_tracking::names::key_iprod_int_dat_in_acc_dt,
110                         sizeof(acc_data_t) * MB() * OC());
111             }
112         }
113     };
114
115     gemm_x8s8s32x_inner_product_fwd_t(const pd_t *apd, const input_vector &inputs,
116             const output_vector &outputs)
117         : cpu_primitive_t(apd, inputs, outputs, true)
118     { pp_kernel_ = new pp_kernel_t(apd, pd()->dst_is_acc_); }
119     ~gemm_x8s8s32x_inner_product_fwd_t() { delete pp_kernel_; }
120
121     typedef typename prec_traits<dst_type>::type data_t;
122
123     typedef typename prec_traits<src_type>::type src_data_t;
124     typedef typename prec_traits<data_type::s8>::type wei_data_t;
125     typedef typename prec_traits<dst_type>::type dst_data_t;
126     typedef typename prec_traits<data_type::s32>::type acc_data_t;
127
128     virtual void execute(event_t *e) const {
129         execute_forward();
130         e->set_state(event_t::ready);
131     }
132
133 private:
134     // XXX: this is throwaway code that will become unnecessary when we have a
135     // sufficiently advanced igemm jit generator that supports quantization,
136     // relu, and whatnot
137     class pp_kernel_t: jit_generator {
138     public:
139         DECLARE_CPU_JIT_AUX_FUNCTIONS(
140                 gemm_x8s8s32x_inner_product_fwd_t::pp_kernel);
141         pp_kernel_t(const pd_t *pd, bool dst_is_acc);
142
143         void operator()(dst_data_t *dst, const acc_data_t *acc,
144                 const char *bias, const float *scales, float nslope,
145                 size_t start, size_t end);
146     private:
147         void generate();
148
149         struct ker_args {
150             dst_data_t *dst;
151             const acc_data_t *acc;
152             const char *bias;
153             const float *scales;
154             float nslope;
155             size_t len;
156             size_t oc_offset;
157         };
158         void (*ker_)(const ker_args *args);
159
160         size_t OC_;
161         data_type_t bias_data_type_;
162         size_t bias_data_type_size_;
163         size_t scale_idx_mult_;
164         round_mode_t rmode_;
165         bool do_bias_;
166         bool do_relu_;
167     };
168
169     void execute_forward() const;
170     const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
171
172     pp_kernel_t *pp_kernel_;
173 };
174 }
175 }
176 }
177
178 #endif
179
180 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s