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_U8S8S32X_INNER_PRODUCT_HPP
18 #define GEMM_U8S8S32X_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"
27 #include "scratchpad.hpp"
29 #include "gemm/os_blas.hpp"
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) {}
43 DECLARE_COMMON_PD_T("gemm:blas", gemm_u8s8s32x_inner_product_fwd_t);
45 virtual status_t init() override {
46 using namespace utils;
47 using namespace data_type;
49 assert(engine()->kind() == engine_kind::cpu);
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,
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(),
70 return ok ? status::success : status::unimplemented;
74 virtual status_t set_default_params() override {
75 using namespace memory_format;
77 if (this->src_pd_.desc()->format == any)
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));
83 if (this->dst_pd_.desc()->format == any)
84 CHECK(this->dst_pd_.set_format(nc));
85 if (this->weights_pd_.desc()->format == any)
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));
91 if (this->bias_pd_.desc()->format == any)
92 CHECK(this->bias_pd_.set_format(x));
93 return status::success;
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),
102 dst_is_acc_ = utils::one_of(dst_type, data_type::s32, data_type::f32);
104 size_t size = conf_.MB() * conf_.OC() * sizeof(acc_data_t);
105 scratchpad_ = create_scratchpad(size);
108 ~gemm_u8s8s32x_inner_product_fwd_t() { delete scratchpad_; };
110 typedef typename prec_traits<dst_type>::type data_t;
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;
117 virtual void execute(event_t *e) {
119 e->set_state(event_t::ready);
123 void execute_forward();
126 scratchpad_t *scratchpad_;
134 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s