updated readme file due to moving CMake scripts to the root folder
[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 #include "gemm_inner_product_utils.hpp"
30
31 #include "cpu_inner_product_pd.hpp"
32
33 namespace mkldnn {
34 namespace impl {
35 namespace cpu {
36
37 template <impl::data_type_t src_type, impl::data_type_t dst_type>
38 struct gemm_x8s8s32x_inner_product_fwd_t: public cpu_primitive_t {
39     struct pd_t: public cpu_inner_product_fwd_pd_t {
40         pd_t(engine_t *engine, const inner_product_desc_t *adesc,
41                 const primitive_attr_t *attr,
42                 const inner_product_fwd_pd_t *hint_fwd_pd)
43             : cpu_inner_product_fwd_pd_t(engine, adesc, attr, hint_fwd_pd) {}
44
45         DECLARE_COMMON_PD_T(src_type == data_type::u8
46                 ? IGEMM_S8U8S32_IMPL_STR
47                 : IGEMM_S8S8S32_IMPL_STR,
48                 gemm_x8s8s32x_inner_product_fwd_t);
49
50         virtual status_t init() override {
51             using namespace utils;
52             using namespace data_type;
53
54             assert(engine()->kind() == engine_kind::cpu);
55
56             bool ok = true
57                 && this->set_default_params() == status::success
58                 && one_of(desc()->prop_kind, prop_kind::forward_training,
59                         prop_kind::forward_inference)
60                 && !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 == s8
64                 && IMPLICATION(this->with_bias(), utils::one_of(
65                             this->desc()->bias_desc.data_type, f32, s32, s8,
66                             u8))
67                 && attr()->post_ops_.len_ <= 1
68                 && IMPLICATION(attr()->post_ops_.len_,
69                         attr()->post_ops_.entry_[0].is_eltwise())
70                 && dense_gemm_consitency_check(src_pd(), weights_pd(),
71                         dst_pd());
72             if (!ok) return status::unimplemented;
73
74             dst_is_acc_ = one_of(dst_type, s32, f32);
75
76             init_scratchpad();
77
78             return status::success;
79         }
80
81         bool dst_is_acc_;
82
83     protected:
84         virtual status_t set_default_params() override {
85             using namespace memory_format;
86
87             if (this->src_pd_.desc()->format == any) {
88                 CHECK(this->src_pd_.set_format(
89                         utils::pick(ndims() - 2, nc, nwc, nhwc, ndhwc)));
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 (MB() > 1) {
95                     CHECK(this->weights_pd_.set_format(
96                         utils::pick(ndims() - 2, io, wio, hwio, dhwio)));
97                 } else {
98                     CHECK(this->weights_pd_.set_format(
99                         utils::pick(ndims() - 2, oi, owi, ohwi, odhwi)));
100                 }
101             }
102             if (this->bias_pd_.desc()->format == any)
103                 CHECK(this->bias_pd_.set_format(x));
104
105             return status::success;
106         }
107
108     private:
109         void init_scratchpad() {
110             if (!dst_is_acc_) {
111                 auto scratchpad = scratchpad_registry().registrar();
112                 scratchpad.book(
113                         memory_tracking::names::key_iprod_int_dat_in_acc_dt,
114                         sizeof(acc_data_t) * MB() * OC());
115             }
116         }
117     };
118
119     gemm_x8s8s32x_inner_product_fwd_t(const pd_t *apd,
120             const input_vector &inputs, const output_vector &outputs)
121         : cpu_primitive_t(apd, inputs, outputs, true) {
122         pp_kernel_ = new inner_product_utils::pp_kernel_t<data_type::s32,
123                 dst_type>(apd);
124     }
125     ~gemm_x8s8s32x_inner_product_fwd_t() { delete pp_kernel_; }
126
127     typedef typename prec_traits<dst_type>::type data_t;
128
129     typedef typename prec_traits<src_type>::type src_data_t;
130     typedef typename prec_traits<data_type::s8>::type wei_data_t;
131     typedef typename prec_traits<dst_type>::type dst_data_t;
132     typedef typename prec_traits<data_type::s32>::type acc_data_t;
133
134     virtual void execute(event_t *e) const {
135         execute_forward();
136         e->set_state(event_t::ready);
137     }
138
139 private:
140     void execute_forward() const;
141     const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
142
143     inner_product_utils::pp_kernel_t<data_type::s32, dst_type> *pp_kernel_;
144 };
145 }
146 }
147 }
148
149 #endif
150
151 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s