1 /*******************************************************************************
2 * Copyright 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 GEMM_X8S8S32X_INNER_PRODUCT_HPP
18 #define GEMM_X8S8S32X_INNER_PRODUCT_HPP
22 #include "c_types_map.hpp"
23 #include "memory_tracking.hpp"
24 #include "type_helpers.hpp"
27 #include "gemm/gemm.hpp"
28 #include "jit_generator.hpp"
29 #include "gemm_inner_product_utils.hpp"
31 #include "cpu_inner_product_pd.hpp"
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) {}
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);
50 virtual status_t init() override {
51 using namespace utils;
52 using namespace data_type;
54 assert(engine()->kind() == engine_kind::cpu);
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,
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(),
72 if (!ok) return status::unimplemented;
74 dst_is_acc_ = one_of(dst_type, s32, f32);
78 return status::success;
84 virtual status_t set_default_params() override {
85 using namespace memory_format;
87 if (this->src_pd_.desc()->format == any) {
88 CHECK(this->src_pd_.set_format(
89 utils::pick(ndims() - 2, nc, nwc, nhwc, ndhwc)));
91 if (this->dst_pd_.desc()->format == any)
92 CHECK(this->dst_pd_.set_format(nc));
93 if (this->weights_pd_.desc()->format == any) {
95 CHECK(this->weights_pd_.set_format(
96 utils::pick(ndims() - 2, io, wio, hwio, dhwio)));
98 CHECK(this->weights_pd_.set_format(
99 utils::pick(ndims() - 2, oi, owi, ohwi, odhwi)));
102 if (this->bias_pd_.desc()->format == any)
103 CHECK(this->bias_pd_.set_format(x));
105 return status::success;
109 void init_scratchpad() {
111 auto scratchpad = scratchpad_registry().registrar();
113 memory_tracking::names::key_iprod_int_dat_in_acc_dt,
114 sizeof(acc_data_t) * MB() * OC());
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,
125 ~gemm_x8s8s32x_inner_product_fwd_t() { delete pp_kernel_; }
127 typedef typename prec_traits<dst_type>::type data_t;
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;
134 virtual void execute(event_t *e) const {
136 e->set_state(event_t::ready);
140 void execute_forward() const;
141 const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
143 inner_product_utils::pp_kernel_t<data_type::s32, dst_type> *pp_kernel_;
151 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s