Add a section of how to link IE with CMake project (#99)
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / src / cpu / gemm_u8s8s32x_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_U8S8S32X_INNER_PRODUCT_HPP
18 #define GEMM_U8S8S32X_INNER_PRODUCT_HPP
19
20 #include <assert.h>
21
22 #include "c_types_map.hpp"
23 #include "cpu_inner_product_pd.hpp"
24 #include "cpu_engine.hpp"
25 #include "type_helpers.hpp"
26 #include "utils.hpp"
27 #include "scratchpad.hpp"
28
29 #include "gemm/os_blas.hpp"
30
31 namespace mkldnn {
32 namespace impl {
33 namespace cpu {
34
35 template <impl::data_type_t dst_type>
36 struct gemm_u8s8s32x_inner_product_fwd_t: public cpu_primitive_t {
37     struct pd_t: public cpu_inner_product_fwd_pd_t {
38         pd_t(engine_t *engine, const inner_product_desc_t *adesc,
39                 const primitive_attr_t *attr,
40                 const inner_product_fwd_pd_t *hint_fwd_pd)
41             : cpu_inner_product_fwd_pd_t(engine, adesc, attr, hint_fwd_pd) {}
42
43         DECLARE_COMMON_PD_T("gemm:blas", gemm_u8s8s32x_inner_product_fwd_t);
44
45         virtual status_t init() override {
46             using namespace utils;
47             using namespace data_type;
48
49             assert(engine()->kind() == engine_kind::cpu);
50
51             bool ok = true
52 #if !USE_MKL_IGEMM
53                 && false
54 #endif
55                 && this->set_default_params() == status::success
56                 && one_of(desc()->prop_kind, prop_kind::forward_training,
57                         prop_kind::forward_inference)
58                 && !has_zero_dim_memory()
59                 && this->desc()->src_desc.data_type == u8
60                 && this->desc()->dst_desc.data_type == dst_type
61                 && this->desc()->weights_desc.data_type == s8
62                 && IMPLICATION(this->with_bias(), utils::one_of(
63                             this->desc()->bias_desc.data_type, f32, s32, s8,
64                             u8))
65                 && attr()->post_ops_.len_ <= 1
66                 && IMPLICATION(attr()->post_ops_.len_,
67                         attr()->post_ops_.entry_[0].is_relu(true, false))
68                 && dense_gemm_consitency_check(src_pd(), weights_pd(),
69                         dst_pd());
70             return ok ? status::success : status::unimplemented;
71         }
72
73     protected:
74         virtual status_t set_default_params() override {
75             using namespace memory_format;
76
77             if (this->src_pd_.desc()->format == any)
78             {
79                 if (ndims() == 4) CHECK(this->src_pd_.set_format(nhwc));
80                 else if (ndims() == 5) CHECK(this->src_pd_.set_format(ndhwc));
81                 else CHECK(this->src_pd_.set_format(nc));
82             }
83             if (this->dst_pd_.desc()->format == any)
84                 CHECK(this->dst_pd_.set_format(nc));
85             if (this->weights_pd_.desc()->format == any)
86             {
87                 if (ndims() == 4) CHECK(this->weights_pd_.set_format(hwio));
88                 else if (ndims() == 5) CHECK(this->weights_pd_.set_format(dhwio));
89                 else CHECK(this->weights_pd_.set_format(io));
90             }
91             if (this->bias_pd_.desc()->format == any)
92                 CHECK(this->bias_pd_.set_format(x));
93             return status::success;
94         }
95     };
96
97     gemm_u8s8s32x_inner_product_fwd_t(const pd_t *pd, const input_vector &inputs,
98             const output_vector &outputs)
99         : cpu_primitive_t(&conf_, inputs, outputs), conf_(*pd), dst_is_acc_(false),
100         scratchpad_(nullptr)
101     {
102         dst_is_acc_ = utils::one_of(dst_type, data_type::s32, data_type::f32);
103         if (!dst_is_acc_) {
104             size_t size = conf_.MB() * conf_.OC() * sizeof(acc_data_t);
105             scratchpad_ = create_scratchpad(size);
106         }
107     }
108     ~gemm_u8s8s32x_inner_product_fwd_t() { delete scratchpad_; };
109
110     typedef typename prec_traits<dst_type>::type data_t;
111
112     typedef typename prec_traits<data_type::u8>::type src_data_t;
113     typedef typename prec_traits<data_type::s8>::type wei_data_t;
114     typedef typename prec_traits<dst_type>::type dst_data_t;
115     typedef typename prec_traits<data_type::s32>::type acc_data_t;
116
117     virtual void execute(event_t *e) {
118         execute_forward();
119         e->set_state(event_t::ready);
120     }
121
122 private:
123     void execute_forward();
124     pd_t conf_;
125     bool dst_is_acc_;
126     scratchpad_t *scratchpad_;
127 };
128 }
129 }
130 }
131
132 #endif
133
134 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s