updated readme file due to moving CMake scripts to the root folder
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / src / cpu / gemm_inner_product.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_GEMM_INNER_PRODUCT_HPP
18 #define CPU_GEMM_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 "gemm/gemm.hpp"
28 #include "gemm_inner_product_utils.hpp"
29
30 namespace mkldnn {
31 namespace impl {
32 namespace cpu {
33
34 template <impl::data_type_t data_type>
35 struct gemm_inner_product_fwd_t: public cpu_primitive_t {
36     struct pd_t: public cpu_inner_product_fwd_pd_t {
37         pd_t(engine_t *engine, const inner_product_desc_t *adesc,
38                 const primitive_attr_t *attr,
39                 const inner_product_fwd_pd_t *hint_fwd_pd)
40             : cpu_inner_product_fwd_pd_t(engine, adesc, attr, hint_fwd_pd) {}
41
42         DECLARE_COMMON_PD_T(GEMM_IMPL_STR, gemm_inner_product_fwd_t);
43
44         virtual status_t init() override {
45             using namespace utils;
46             assert(engine()->kind() == engine_kind::cpu);
47
48             bool ok = true
49                 && this->set_default_params() == status::success
50                 && one_of(desc()->prop_kind, prop_kind::forward_training,
51                         prop_kind::forward_inference)
52                 && !has_zero_dim_memory()
53                 && everyone_is(data_type, desc()->src_desc.data_type,
54                         desc()->weights_desc.data_type,
55                         desc()->dst_desc.data_type)
56                 && IMPLICATION(this->with_bias(),
57                         data_type == desc()->bias_desc.data_type)
58                 && attr()->post_ops_.len_ <= 1
59                 && IMPLICATION(attr()->post_ops_.len_ == 1,
60                         attr()->post_ops_.entry_[0].is_eltwise())
61                 && dense_gemm_consitency_check(src_pd(), weights_pd(),
62                         dst_pd());
63             return ok ? status::success : status::unimplemented;
64         }
65     };
66
67     gemm_inner_product_fwd_t(const pd_t *apd, const input_vector &inputs,
68             const output_vector &outputs)
69         : cpu_primitive_t(apd, inputs, outputs) {
70         bool has_bias = pd()->with_bias(),
71              has_eltwise = pd()->attr()->post_ops_.len_ == 1,
72              has_scale = !pd()->attr()->output_scales_.has_default_values();
73         postops_in_ip_ = has_bias || has_eltwise || has_scale;
74         pp_kernel_ = new inner_product_utils::pp_kernel_t<data_type, data_type>(
75                 apd);
76     }
77     ~gemm_inner_product_fwd_t() { delete pp_kernel_; }
78
79     typedef typename prec_traits<data_type>::type data_t;
80
81     virtual void execute(event_t *e) const {
82         execute_forward();
83         e->set_state(event_t::ready);
84     }
85
86 private:
87     void execute_forward() const;
88     const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
89
90     inner_product_utils::pp_kernel_t<data_type, data_type> *pp_kernel_;
91     bool postops_in_ip_;
92 };
93
94 template <impl::data_type_t data_type>
95 struct gemm_inner_product_bwd_data_t: public cpu_primitive_t {
96     struct pd_t: public cpu_inner_product_bwd_data_pd_t {
97         pd_t(engine_t *engine, const inner_product_desc_t *adesc,
98                 const primitive_attr_t *attr,
99                 const inner_product_fwd_pd_t *hint_fwd_pd)
100             : cpu_inner_product_bwd_data_pd_t(engine, adesc, attr,
101                     hint_fwd_pd) {}
102
103         DECLARE_COMMON_PD_T(GEMM_IMPL_STR, gemm_inner_product_bwd_data_t);
104
105         virtual status_t init() override {
106             using namespace utils;
107             assert(engine()->kind() == engine_kind::cpu);
108
109             bool ok = true
110                 && this->set_default_params() == status::success
111                 && desc()->prop_kind == prop_kind::backward_data
112                 && !has_zero_dim_memory()
113                 && everyone_is(data_type, desc()->diff_src_desc.data_type,
114                         desc()->weights_desc.data_type,
115                         desc()->diff_dst_desc.data_type)
116                 && attr()->has_default_values()
117                 && dense_gemm_consitency_check(diff_src_pd(), weights_pd(),
118                         diff_dst_pd());
119             return ok ? status::success : status::unimplemented;
120         }
121     };
122
123     gemm_inner_product_bwd_data_t(const pd_t *apd, const input_vector &inputs,
124             const output_vector &outputs)
125         : cpu_primitive_t(apd, inputs, outputs) {}
126     typedef typename prec_traits<data_type>::type data_t;
127
128     virtual void execute(event_t *e) const {
129         execute_backward_data();
130         e->set_state(event_t::ready);
131     }
132
133 private:
134     void execute_backward_data() const;
135     const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
136 };
137
138 template <impl::data_type_t data_type>
139 struct gemm_inner_product_bwd_weights_t: public cpu_primitive_t {
140     struct pd_t: public cpu_inner_product_bwd_weights_pd_t {
141         pd_t(engine_t *engine, const inner_product_desc_t *adesc,
142                 const primitive_attr_t *attr,
143                 const inner_product_fwd_pd_t *hint_fwd_pd)
144             : cpu_inner_product_bwd_weights_pd_t(engine, adesc, attr,
145                     hint_fwd_pd) {}
146
147         DECLARE_COMMON_PD_T(GEMM_IMPL_STR, gemm_inner_product_bwd_weights_t);
148
149         virtual status_t init() override {
150             using namespace utils;
151             assert(engine()->kind() == engine_kind::cpu);
152             bool ok = true
153                 && this->set_default_params() == status::success
154                 && desc()->prop_kind == prop_kind::backward_weights
155                 && !has_zero_dim_memory()
156                 && everyone_is(data_type, desc()->src_desc.data_type,
157                         desc()->diff_weights_desc.data_type,
158                         desc()->diff_dst_desc.data_type)
159                 && attr()->has_default_values()
160                 && dense_gemm_consitency_check(src_pd(), diff_weights_pd(),
161                         diff_dst_pd());
162
163             return ok ? status::success : status::unimplemented;
164         }
165     };
166
167     gemm_inner_product_bwd_weights_t(const pd_t *apd, const input_vector &inputs,
168             const output_vector &outputs)
169         : cpu_primitive_t(apd, inputs, outputs) {}
170     typedef typename prec_traits<data_type>::type data_t;
171
172     virtual void execute(event_t *e) const {
173         execute_backward_weights();
174         e->set_state(event_t::ready);
175     }
176
177 private:
178     void execute_backward_weights() const;
179     const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
180 };
181
182 }
183 }
184 }
185
186 #endif
187
188 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s
189