1 /*******************************************************************************
2 * Copyright 2016-2018 Intel Corporation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *******************************************************************************/
17 #ifndef CPU_REF_INNER_PRODUCT_HPP
18 #define CPU_REF_INNER_PRODUCT_HPP
22 #include "c_types_map.hpp"
23 #include "cpu_inner_product_pd.hpp"
24 #include "cpu_engine.hpp"
25 #include "type_helpers.hpp"
32 template <impl::data_type_t src_type, impl::data_type_t wei_type = src_type,
33 impl::data_type_t dst_type = src_type,
34 impl::data_type_t acc_type = dst_type>
35 struct ref_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) {}
42 DECLARE_COMMON_PD_T("ref:any", ref_inner_product_fwd_t);
44 virtual status_t init() override {
45 using namespace prop_kind;
46 assert(engine()->kind() == engine_kind::cpu);
48 && this->set_default_params() == status::success
49 && utils::one_of(desc()->prop_kind, forward_training,
51 && desc()->src_desc.data_type == src_type
52 && desc()->weights_desc.data_type == wei_type
53 && desc()->accum_data_type == acc_type
54 && desc()->dst_desc.data_type == dst_type
55 && utils::implication(this->with_bias(),
56 desc()->bias_desc.data_type == dst_type)
57 && attr()->has_default_values();
58 return ok ? status::success : status::unimplemented;
62 ref_inner_product_fwd_t(const pd_t *pd, const input_vector &inputs,
63 const output_vector &outputs)
64 : cpu_primitive_t(&conf_, inputs, outputs), conf_(*pd) {}
66 typedef typename prec_traits<src_type>::type src_data_t;
67 typedef typename prec_traits<wei_type>::type wei_data_t;
68 typedef typename prec_traits<dst_type>::type dst_data_t;
69 typedef typename prec_traits<acc_type>::type acc_data_t;
71 virtual void execute(event_t *e) {
72 switch (conf_.desc()->prop_kind) {
73 case prop_kind::forward_training:
74 case prop_kind::forward_inference:
78 assert(!"invalid prop_kind");
80 e->set_state(event_t::ready);
84 void execute_forward();
88 template <impl::data_type_t diff_src_type, impl::data_type_t wei_type,
89 impl::data_type_t diff_dst_type,
90 impl::data_type_t acc_type = diff_src_type>
91 struct ref_inner_product_bwd_data_t: public cpu_primitive_t {
92 struct pd_t: public cpu_inner_product_bwd_data_pd_t {
93 pd_t(engine_t *engine, const inner_product_desc_t *adesc,
94 const primitive_attr_t *attr,
95 const inner_product_fwd_pd_t *hint_fwd_pd)
96 : cpu_inner_product_bwd_data_pd_t(engine, adesc, attr, hint_fwd_pd)
99 DECLARE_COMMON_PD_T("ref:any", ref_inner_product_bwd_data_t);
101 virtual status_t init() override {
102 using namespace prop_kind;
103 assert(engine()->kind() == engine_kind::cpu);
105 && this->set_default_params() == status::success
106 && utils::one_of(this->desc()->prop_kind, backward,
108 && desc()->diff_src_desc.data_type == diff_src_type
109 && desc()->weights_desc.data_type == wei_type
110 && desc()->accum_data_type == acc_type
111 && desc()->diff_dst_desc.data_type == diff_dst_type
112 && attr()->has_default_values();
113 return ok ? status::success : status::unimplemented;
117 ref_inner_product_bwd_data_t(const pd_t *pd, const input_vector &inputs,
118 const output_vector &outputs)
119 : cpu_primitive_t(&conf_, inputs, outputs), conf_(*pd) {}
121 typedef typename prec_traits<diff_src_type>::type diff_src_data_t;
122 typedef typename prec_traits<wei_type>::type wei_data_t;
123 typedef typename prec_traits<diff_dst_type>::type diff_dst_data_t;
124 typedef typename prec_traits<acc_type>::type acc_data_t;
126 virtual void execute(event_t *e) {
127 switch (conf_.desc()->prop_kind) {
128 case prop_kind::backward:
129 case prop_kind::backward_data:
130 execute_backward_data();
133 assert(!"invalid prop_kind");
135 e->set_state(event_t::ready);
139 void execute_backward_data();
143 template <impl::data_type_t data_type>
144 struct ref_inner_product_bwd_weights_t: public cpu_primitive_t {
145 struct pd_t: public cpu_inner_product_bwd_weights_pd_t {
146 pd_t(engine_t *engine, const inner_product_desc_t *adesc,
147 const primitive_attr_t *attr,
148 const inner_product_fwd_pd_t *hint_fwd_pd)
149 : cpu_inner_product_bwd_weights_pd_t(engine, adesc, attr,
152 DECLARE_COMMON_PD_T("ref:any", ref_inner_product_bwd_weights_t);
154 virtual status_t init() override {
155 using namespace prop_kind;
156 assert(engine()->kind() == engine_kind::cpu);
158 && this->set_default_params() == status::success
159 && utils::one_of(this->desc()->prop_kind, backward,
161 && utils::everyone_is(data_type,
162 this->desc()->src_desc.data_type,
163 this->desc()->diff_dst_desc.data_type,
164 this->desc()->diff_weights_desc.data_type)
165 && utils::implication(this->with_bias(),
166 data_type == this->desc()->diff_bias_desc.data_type)
167 && attr()->has_default_values();
168 return ok ? status::success : status::unimplemented;
172 ref_inner_product_bwd_weights_t(const pd_t *pd, const input_vector &inputs,
173 const output_vector &outputs)
174 : cpu_primitive_t(&conf_, inputs, outputs), conf_(*pd) {}
175 typedef typename prec_traits<data_type>::type data_t;
177 virtual void execute(event_t *e) {
178 switch (conf_.desc()->prop_kind) {
179 case prop_kind::backward:
180 case prop_kind::backward_weights:
181 execute_backward_weights();
184 assert(!"invalid prop_kind");
186 e->set_state(event_t::ready);
190 void execute_backward_weights();
200 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s