1 /*******************************************************************************
2 * Copyright 2017-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_CONVOLUTION_HPP
18 #define GEMM_X8S8S32X_CONVOLUTION_HPP
20 #include "c_types_map.hpp"
21 #include "memory_tracking.hpp"
23 #include "cpu_convolution_pd.hpp"
24 #include "cpu_engine.hpp"
25 #include "jit_primitive_conf.hpp"
26 #include "jit_generator.hpp"
27 #include "gemm_convolution_utils.hpp"
29 #include "gemm/gemm.hpp"
35 template <data_type_t src_type, data_type_t dst_type>
36 struct _gemm_x8s8s32x_convolution_fwd_t: public cpu_primitive_t {
37 struct pd_t: public cpu_convolution_fwd_pd_t {
38 pd_t(engine_t *engine, const convolution_desc_t *adesc,
39 const primitive_attr_t *attr,
40 const typename pd_t::base_class *hint_fwd_pd)
41 : cpu_convolution_fwd_pd_t(engine, adesc, attr, hint_fwd_pd)
44 DECLARE_COMMON_PD_T(IGEMM_S8U8S32_IMPL_STR,
45 _gemm_x8s8s32x_convolution_fwd_t<src_type, dst_type>);
47 virtual status_t init() override {
48 using namespace data_type;
49 using namespace memory_format;
51 assert(this->engine()->kind() == engine_kind::cpu);
54 && this->set_default_params() == status::success
55 && utils::one_of(this->desc()->prop_kind,
56 prop_kind::forward_training,
57 prop_kind::forward_inference)
58 && utils::one_of(this->desc()->alg_kind,
59 alg_kind::convolution_auto,
60 alg_kind::convolution_direct)
61 && !this->has_zero_dim_memory()
62 && this->desc()->src_desc.data_type == src_type
63 && this->desc()->dst_desc.data_type == dst_type
64 && this->desc()->weights_desc.data_type == s8
65 && IMPLICATION(this->with_bias(), utils::one_of(
66 this->desc()->bias_desc.data_type, f32, s32, s8,
68 && this->desc()->accum_data_type == data_type::s32
69 && utils::everyone_is(nhwc, this->src_pd_.desc()->format,
70 this->dst_pd_.desc()->format)
71 && this->weights_pd_.desc()->format == (this->with_groups()
72 ? ((src_type == data_type::s8) ? hwigo_s8s8 : hwigo)
73 : ((src_type == data_type::s8) ? hwio_s8s8 : hwio))
74 && this->is_gemm_conv_format();
75 if (!ok) return status::unimplemented;
77 auto scratchpad = scratchpad_registry().registrar();
78 return jit_gemm_convolution_utils::init_conf(jcp_, scratchpad,
79 *this->desc(), this->src_pd(), this->weights_pd(0),
80 this->dst_pd(), mkldnn_get_max_threads());
83 jit_gemm_conv_conf_t jcp_;
86 virtual status_t set_default_params() override {
87 using namespace memory_format;
88 const bool is_sign_input =
89 this->desc()->src_desc.data_type == data_type::s8;
91 if (this->src_pd_.desc()->format == any)
92 CHECK(this->src_pd_.set_format(nhwc));
93 if (this->dst_pd_.desc()->format == any)
94 CHECK(this->dst_pd_.set_format(nhwc));
95 if (this->weights_pd_.desc()->format == any)
96 CHECK(this->weights_pd_.set_format(this->with_groups()
97 ? (is_sign_input ? hwigo_s8s8 : hwigo)
98 : (is_sign_input ? hwio_s8s8 : hwio)));
99 if (this->bias_pd_.desc()->format == any)
100 CHECK(this->bias_pd_.set_format(x));
101 if (this->desc()->alg_kind == alg_kind::convolution_auto)
102 CHECK(this->set_alg_kind(alg_kind::convolution_direct));
103 return status::success;
106 virtual bool is_gemm_conv_format() const {
107 using namespace mkldnn::impl::primitive_kind;
108 auto const &po = this->attr()->post_ops_;
109 auto is_relu = [&](int idx) {
110 return po.entry_[idx].is_relu(true, false); };
114 case 1: return is_relu(0) || po.contain(sum, 0);
115 case 2: return po.contain(sum, 0) && is_relu(1);
116 default: return false;
122 _gemm_x8s8s32x_convolution_fwd_t(const pd_t *apd, const input_vector &inputs,
123 const output_vector &outputs)
124 : cpu_primitive_t(apd, inputs, outputs, true) {
125 pp_ker_ = new pp_ker_t(apd);
127 ~_gemm_x8s8s32x_convolution_fwd_t() {
131 typedef typename prec_traits<src_type>::type src_data_t;
132 typedef typename prec_traits<data_type::s8>::type wei_data_t;
133 typedef typename prec_traits<dst_type>::type dst_data_t;
134 typedef typename prec_traits<data_type::s32>::type acc_data_t;
136 virtual void execute(event_t *e) const {
138 e->set_state(event_t::ready);
142 const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
143 void execute_forward() const;
144 // XXX: this is throwaway code that will become unnecessary when we have a
145 // sufficiently advanced igemm jit generator that supports quantization,
147 class pp_ker_t : jit_generator {
149 DECLARE_CPU_JIT_AUX_FUNCTIONS(
150 _gemm_x8s8s32x_convolution_fwd_t::pp_kernel);
151 pp_ker_t(const pd_t *pd);
153 void operator()(dst_data_t *dst, const acc_data_t *acc,
154 const char *bias, const float *scales,
155 float nslope, float sum_scale, float signed_scale,
156 int g, size_t start, size_t end);
162 const acc_data_t *acc;
171 void(*ker_)(const ker_args *args);
173 const jit_gemm_conv_conf_t jcp_;
176 data_type_t bias_data_type_;
177 size_t bias_data_type_size_;
178 size_t scale_idx_mult_;
183 bool do_signed_scaling_;
184 size_t dst_os_stride_;
189 void execute_forward_thr(const int ithr, const int nthr,
190 const src_data_t *src_base, const wei_data_t *wei_base,
191 const char *bia_base, dst_data_t *dst_base,
192 const memory_tracking::grantor_t &scratchpad) const;
199 template <data_type_t dst_type>
200 struct _gemm_u8s8s32x_convolution_bwd_data_t: public cpu_primitive_t {
201 struct pd_t: public cpu_convolution_bwd_data_pd_t{
202 pd_t(engine_t *engine,
203 const convolution_desc_t *adesc, const primitive_attr_t *attr,
204 const convolution_fwd_pd_t *hint_fwd_pd)
205 : cpu_convolution_bwd_data_pd_t(engine, adesc, attr, hint_fwd_pd)
208 DECLARE_COMMON_PD_T(IGEMM_S8U8S32_IMPL_STR,
209 _gemm_u8s8s32x_convolution_bwd_data_t<dst_type>);
211 virtual status_t init() override {
212 using namespace data_type;
213 using namespace memory_format;
215 assert(this->engine()->kind() == engine_kind::cpu);
218 && this->set_default_params() == status::success
219 && this->desc()->prop_kind == prop_kind::backward_data
220 && utils::one_of(this->desc()->alg_kind, alg_kind::convolution_auto,
221 alg_kind::convolution_direct)
222 && !this->has_zero_dim_memory()
223 && this->desc()->diff_src_desc.data_type == dst_type
224 && this->desc()->diff_dst_desc.data_type == u8
225 && this->desc()->weights_desc.data_type == s8
226 && IMPLICATION(this->with_bias(), utils::one_of(
227 this->desc()->bias_desc.data_type, f32, s32, s8,
229 && this->desc()->accum_data_type == data_type::s32
230 && utils::everyone_is(nhwc, this->diff_src_pd_.desc()->format,
231 this->diff_dst_pd_.desc()->format)
232 && this->weights_pd_.desc()->format == (this->with_groups()
234 && attr()->post_ops_.has_default_values();
235 if (!ok) return status::unimplemented;
237 auto scratchpad = scratchpad_registry().registrar();
238 return jit_gemm_convolution_utils::init_conf(jcp_, scratchpad,
239 *this->desc(), this->diff_src_pd(), this->weights_pd(0),
240 this->diff_dst_pd(), mkldnn_get_max_threads());
243 virtual bool support_bias() const override { return true; }
245 jit_gemm_conv_conf_t jcp_;
248 virtual status_t set_default_params() override {
249 using namespace memory_format;
251 if (this->diff_src_pd_.desc()->format == any)
252 CHECK(this->diff_src_pd_.set_format(nhwc));
253 if (this->diff_dst_pd_.desc()->format == any)
254 CHECK(this->diff_dst_pd_.set_format(nhwc));
255 if (this->weights_pd_.desc()->format == any)
256 CHECK(this->weights_pd_.set_format(
257 this->with_groups() ? hwigo : hwio));
258 if (bias_pd_.desc()->format == any)
259 CHECK(bias_pd_.set_format(x));
260 if (this->desc()->alg_kind == alg_kind::convolution_auto)
261 CHECK(this->set_alg_kind(alg_kind::convolution_direct));
262 return status::success;
266 _gemm_u8s8s32x_convolution_bwd_data_t(const pd_t *apd, const input_vector &inputs,
267 const output_vector &outputs)
268 : cpu_primitive_t(apd, inputs, outputs, true) {}
269 ~_gemm_u8s8s32x_convolution_bwd_data_t() {}
271 typedef typename prec_traits<data_type::u8>::type diff_dst_data_t;
272 typedef typename prec_traits<data_type::s8>::type wei_data_t;
273 typedef typename prec_traits<dst_type>::type diff_src_data_t;
274 typedef typename prec_traits<data_type::s32>::type acc_data_t;
276 virtual void execute(event_t *e) const {
277 execute_backward_data();
278 e->set_state(event_t::ready);
282 void execute_backward_data() const;
283 void execute_backward_data_thr(const int ithr, const int nthr,
284 const diff_dst_data_t *diff_dst_base, const wei_data_t *wei_base,
285 const char *bia_base, diff_src_data_t *diff_src_base,
286 const memory_tracking::grantor_t &scratchpad) const;
287 const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }