1/*******************************************************************************
2* Copyright 2021-2022 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_REF_INNER_PRODUCT_INT8_HPP
18#define CPU_REF_INNER_PRODUCT_INT8_HPP
19
20#include <assert.h>
21
22#include "common/c_types_map.hpp"
23#include "common/primitive.hpp"
24#include "common/type_helpers.hpp"
25#include "common/utils.hpp"
26
27#include "cpu/cpu_inner_product_pd.hpp"
28#include "cpu/primitive_attr_postops.hpp"
29
30namespace dnnl {
31namespace impl {
32namespace cpu {
33
34struct ref_inner_product_int8_fwd_t : public primitive_t {
35 struct pd_t : public cpu_inner_product_fwd_pd_t {
36 using cpu_inner_product_fwd_pd_t::cpu_inner_product_fwd_pd_t;
37
38 DECLARE_COMMON_PD_T("ref:any", ref_inner_product_int8_fwd_t);
39
40 status_t init(engine_t *engine) {
41 using namespace data_type;
42 using smask_t = primitive_attr_t::skip_mask_t;
43 const auto src_type = src_md(0)->data_type;
44 const auto wei_type = weights_md(0)->data_type;
45 const auto bia_type = weights_md(1)->data_type;
46 const auto dst_type = dst_md(0)->data_type;
47
48 const bool allow_all_tags = true; // ref should support all tags
49
50 bool ok = is_fwd() && utils::one_of(src_type, s8, u8)
51 && wei_type == s8
52 && IMPLICATION(with_bias(),
53 utils::one_of(bia_type, f32, bf16, s32, s8, u8))
54 && utils::one_of(dst_type, f32, bf16, s32, s8, u8)
55 && IMPLICATION(with_bias(),
56 platform::has_data_type_support(bia_type))
57 && platform::has_data_type_support(dst_type)
58 && set_default_params(allow_all_tags) == status::success
59 && attr()->has_default_values(smask_t::scales_runtime
60 | smask_t::post_ops | smask_t::sum_dt)
61 && attr()->post_ops_.check_sum_consistent_dt(dst_type)
62 && arg_scales_mask_ok()
63 && attr_.set_default_formats(dst_md(0)) == status::success;
64 return ok ? status::success : status::unimplemented;
65 }
66
67 private:
68 bool arg_scales_mask_ok() const {
69 const std::vector<int> supported_args
70 = {DNNL_ARG_SRC, DNNL_ARG_WEIGHTS, DNNL_ARG_DST};
71 bool ok = attr()->scales_.has_default_values(supported_args);
72
73 for (auto arg : supported_args) {
74 int mask = attr()->scales_.get(arg).mask_;
75 if (arg == DNNL_ARG_WEIGHTS)
76 ok = ok && (mask == 0 || mask == (1 << 0));
77 else
78 ok = ok && (mask == 0);
79 }
80 return ok;
81 }
82 };
83
84 ref_inner_product_int8_fwd_t(const pd_t *apd) : primitive_t(apd) {}
85
86 status_t init(engine_t *engine) override {
87 ref_post_ops
88 = utils::make_unique<ref_post_ops_t>(pd()->attr()->post_ops_);
89 if (!ref_post_ops) return status::out_of_memory;
90 return status::success;
91 }
92
93 status_t execute(const exec_ctx_t &ctx) const override {
94 return execute_forward(ctx);
95 }
96
97private:
98 status_t execute_forward(const exec_ctx_t &ctx) const;
99 const pd_t *pd() const { return (const pd_t *)primitive_t::pd().get(); }
100 std::unique_ptr<ref_post_ops_t> ref_post_ops;
101};
102
103} // namespace cpu
104} // namespace impl
105} // namespace dnnl
106
107#endif
108
109// vim: et ts=4 sw=4 cindent cino+=l0,\:4,N-s
110