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"
30 #include "cpu_inner_product_pd.hpp"
36 template <impl::data_type_t src_type, impl::data_type_t dst_type>
37 struct gemm_x8s8s32x_inner_product_fwd_t: public cpu_primitive_t {
38 struct pd_t: public cpu_inner_product_fwd_pd_t {
39 pd_t(engine_t *engine, const inner_product_desc_t *adesc,
40 const primitive_attr_t *attr,
41 const inner_product_fwd_pd_t *hint_fwd_pd)
42 : cpu_inner_product_fwd_pd_t(engine, adesc, attr, hint_fwd_pd) {}
44 DECLARE_COMMON_PD_T(src_type == data_type::u8
45 ? IGEMM_S8U8S32_IMPL_STR
46 : IGEMM_S8S8S32_IMPL_STR,
47 gemm_x8s8s32x_inner_product_fwd_t);
49 virtual status_t init() override {
50 using namespace utils;
51 using namespace data_type;
53 assert(engine()->kind() == engine_kind::cpu);
56 && this->set_default_params() == status::success
57 && one_of(desc()->prop_kind, prop_kind::forward_training,
58 prop_kind::forward_inference)
59 && !has_zero_dim_memory()
60 && this->desc()->src_desc.data_type == src_type
61 && this->desc()->dst_desc.data_type == dst_type
62 && this->desc()->weights_desc.data_type == s8
63 && IMPLICATION(this->with_bias(), utils::one_of(
64 this->desc()->bias_desc.data_type, f32, s32, s8,
66 && attr()->post_ops_.len_ <= 1
67 && IMPLICATION(attr()->post_ops_.len_,
68 attr()->post_ops_.entry_[0].is_relu(true, false))
69 && dense_gemm_consitency_check(src_pd(), weights_pd(),
71 if (!ok) return status::unimplemented;
73 dst_is_acc_ = one_of(dst_type, s32, f32);
77 return status::success;
83 virtual status_t set_default_params() override {
84 using namespace memory_format;
86 if (this->src_pd_.desc()->format == any) {
87 if (ndims() == 4) CHECK(this->src_pd_.set_format(nhwc));
88 else if (ndims() == 5) CHECK(this->src_pd_.set_format(ndhwc));
89 else CHECK(this->src_pd_.set_format(nc));
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 (ndims() == 4) CHECK(this->weights_pd_.set_format(hwio));
95 else if (ndims() == 5) CHECK(this->weights_pd_.set_format(dhwio));
96 else CHECK(this->weights_pd_.set_format(io));
98 if (this->bias_pd_.desc()->format == any)
99 CHECK(this->bias_pd_.set_format(x));
101 return status::success;
105 void init_scratchpad() {
107 auto scratchpad = scratchpad_registry().registrar();
109 memory_tracking::names::key_iprod_int_dat_in_acc_dt,
110 sizeof(acc_data_t) * MB() * OC());
115 gemm_x8s8s32x_inner_product_fwd_t(const pd_t *apd, const input_vector &inputs,
116 const output_vector &outputs)
117 : cpu_primitive_t(apd, inputs, outputs, true)
118 { pp_kernel_ = new pp_kernel_t(apd, pd()->dst_is_acc_); }
119 ~gemm_x8s8s32x_inner_product_fwd_t() { delete pp_kernel_; }
121 typedef typename prec_traits<dst_type>::type data_t;
123 typedef typename prec_traits<src_type>::type src_data_t;
124 typedef typename prec_traits<data_type::s8>::type wei_data_t;
125 typedef typename prec_traits<dst_type>::type dst_data_t;
126 typedef typename prec_traits<data_type::s32>::type acc_data_t;
128 virtual void execute(event_t *e) const {
130 e->set_state(event_t::ready);
134 // XXX: this is throwaway code that will become unnecessary when we have a
135 // sufficiently advanced igemm jit generator that supports quantization,
137 class pp_kernel_t: jit_generator {
139 DECLARE_CPU_JIT_AUX_FUNCTIONS(
140 gemm_x8s8s32x_inner_product_fwd_t::pp_kernel);
141 pp_kernel_t(const pd_t *pd, bool dst_is_acc);
143 void operator()(dst_data_t *dst, const acc_data_t *acc,
144 const char *bias, const float *scales, float nslope,
145 size_t start, size_t end);
151 const acc_data_t *acc;
158 void (*ker_)(const ker_args *args);
161 data_type_t bias_data_type_;
162 size_t bias_data_type_size_;
163 size_t scale_idx_mult_;
169 void execute_forward() const;
170 const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
172 pp_kernel_t *pp_kernel_;
180 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s