1 /*******************************************************************************
2 * Copyright 2017-2019 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::one_of(this->src_pd_.desc()->format, nhwc, ndhwc)
70 && this->src_pd_.desc()->format == this->dst_pd_.desc()->format
71 && IMPLICATION(this->src_pd_.desc()->format == nhwc,
72 this->weights_pd_.desc()->format == (this->with_groups()
73 ? ((src_type == data_type::s8) ? hwigo_s8s8 : hwigo)
74 : ((src_type == data_type::s8) ? hwio_s8s8 : hwio)))
75 && IMPLICATION(this->src_pd_.desc()->format == ndhwc,
76 this->weights_pd_.desc()->format == (this->with_groups()
77 ? ((src_type == data_type::s8) ? dhwigo_s8s8 : dhwigo)
78 : ((src_type == data_type::s8) ? dhwio_s8s8 : dhwio)))
79 && this->is_gemm_conv_format();
80 if (!ok) return status::unimplemented;
82 auto scratchpad = scratchpad_registry().registrar();
83 return jit_gemm_convolution_utils::init_conf(jcp_, scratchpad,
84 *this->desc(), this->src_pd(), this->weights_pd(0),
85 this->dst_pd(), mkldnn_get_max_threads());
88 jit_gemm_conv_conf_t jcp_;
91 memory_format_t src_format() const {
92 using namespace memory_format;
93 const size_t ndims_sp = this->desc()->src_desc.ndims - 4;
94 return (utils::pick(ndims_sp, nhwc, ndhwc));
97 memory_format_t wei_format() const {
98 using namespace memory_format;
99 const size_t ndims_sp = this->desc()->src_desc.ndims - 4;
100 return this->with_groups()
101 ? (src_type == data_type::s8) ? utils::pick(ndims_sp, hwigo_s8s8, dhwigo_s8s8)
102 : utils::pick(ndims_sp, hwigo, dhwigo)
103 : (src_type == data_type::s8) ? utils::pick(ndims_sp, hwio_s8s8, dhwio_s8s8)
104 : utils::pick(ndims_sp, hwio, dhwio);
107 virtual status_t set_default_params() override {
108 using namespace memory_format;
109 if (this->src_pd_.desc()->format == any)
110 CHECK(this->src_pd_.set_format(src_format()));
111 if (this->dst_pd_.desc()->format == any)
112 CHECK(this->dst_pd_.set_format(src_format()));
113 if (this->weights_pd_.desc()->format == any)
114 CHECK(this->weights_pd_.set_format(wei_format()));
115 if (this->bias_pd_.desc()->format == any)
116 CHECK(this->bias_pd_.set_format(x));
117 if (this->desc()->alg_kind == alg_kind::convolution_auto)
118 CHECK(this->set_alg_kind(alg_kind::convolution_direct));
119 return status::success;
122 virtual bool is_gemm_conv_format() const {
123 using namespace mkldnn::impl::primitive_kind;
124 auto const &po = this->attr()->post_ops_;
125 auto is_relu = [&](int idx) {
126 return po.entry_[idx].is_relu(true, false); };
130 case 1: return is_relu(0) || po.contain(sum, 0);
131 case 2: return po.contain(sum, 0) && is_relu(1);
132 default: return false;
137 _gemm_x8s8s32x_convolution_fwd_t(const pd_t *apd, const input_vector &inputs,
138 const output_vector &outputs)
139 : cpu_primitive_t(apd, inputs, outputs, true) {
140 pp_ker_ = new pp_ker_t(this->pd());
142 ~_gemm_x8s8s32x_convolution_fwd_t() {
146 typedef typename prec_traits<src_type>::type src_data_t;
147 typedef typename prec_traits<data_type::s8>::type wei_data_t;
148 typedef typename prec_traits<dst_type>::type dst_data_t;
149 typedef typename prec_traits<data_type::s32>::type acc_data_t;
151 virtual void execute(event_t *e) const {
153 e->set_state(event_t::ready);
157 const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
158 void execute_forward() const;
159 // XXX: this is throwaway code that will become unnecessary when we have a
160 // sufficiently advanced igemm jit generator that supports quantization,
162 class pp_ker_t : jit_generator {
164 DECLARE_CPU_JIT_AUX_FUNCTIONS(
165 _gemm_x8s8s32x_convolution_fwd_t::pp_kernel);
166 pp_ker_t(const pd_t *pd);
168 void operator()(dst_data_t *dst, const acc_data_t *acc,
169 const char *bias, const float *scales,
170 float nslope, float sum_scale, float signed_scale,
171 int g, size_t start, size_t end);
173 size_t dst_os_stride_;
180 const acc_data_t *acc;
189 void(*ker_)(const ker_args *args);
191 const jit_gemm_conv_conf_t jcp_;
194 data_type_t bias_data_type_;
195 size_t bias_data_type_size_;
196 size_t scale_idx_mult_;
201 bool do_signed_scaling_;
206 void execute_forward_thr(const int ithr, const int nthr,
207 const src_data_t *src_base, const wei_data_t *wei_base,
208 const char *bia_base, dst_data_t *dst_base,
209 const memory_tracking::grantor_t &scratchpad) const;
216 template <data_type_t dst_type>
217 struct _gemm_u8s8s32x_convolution_bwd_data_t: public cpu_primitive_t {
218 struct pd_t: public cpu_convolution_bwd_data_pd_t{
219 pd_t(engine_t *engine,
220 const convolution_desc_t *adesc, const primitive_attr_t *attr,
221 const convolution_fwd_pd_t *hint_fwd_pd)
222 : cpu_convolution_bwd_data_pd_t(engine, adesc, attr, hint_fwd_pd)
225 DECLARE_COMMON_PD_T(IGEMM_S8U8S32_IMPL_STR,
226 _gemm_u8s8s32x_convolution_bwd_data_t<dst_type>);
228 virtual status_t init() override {
229 using namespace data_type;
230 using namespace memory_format;
232 assert(this->engine()->kind() == engine_kind::cpu);
235 && this->set_default_params() == status::success
236 && this->desc()->prop_kind == prop_kind::backward_data
237 && utils::one_of(this->desc()->alg_kind, alg_kind::convolution_auto,
238 alg_kind::convolution_direct)
239 && !this->has_zero_dim_memory()
240 && this->desc()->diff_src_desc.data_type == dst_type
241 && this->desc()->diff_dst_desc.data_type == u8
242 && this->desc()->weights_desc.data_type == s8
243 && IMPLICATION(this->with_bias(), utils::one_of(
244 this->desc()->bias_desc.data_type, f32, s32, s8,
246 && this->desc()->accum_data_type == data_type::s32
247 && utils::everyone_is(nhwc, this->diff_src_pd_.desc()->format,
248 this->diff_dst_pd_.desc()->format)
249 && this->weights_pd_.desc()->format == (this->with_groups()
251 && attr()->post_ops_.has_default_values();
252 if (!ok) return status::unimplemented;
254 auto scratchpad = scratchpad_registry().registrar();
255 return jit_gemm_convolution_utils::init_conf(jcp_, scratchpad,
256 *this->desc(), this->diff_src_pd(), this->weights_pd(0),
257 this->diff_dst_pd(), mkldnn_get_max_threads());
260 virtual bool support_bias() const override { return true; }
262 jit_gemm_conv_conf_t jcp_;
265 virtual status_t set_default_params() override {
266 using namespace memory_format;
268 if (this->diff_src_pd_.desc()->format == any)
269 CHECK(this->diff_src_pd_.set_format(nhwc));
270 if (this->diff_dst_pd_.desc()->format == any)
271 CHECK(this->diff_dst_pd_.set_format(nhwc));
272 if (this->weights_pd_.desc()->format == any)
273 CHECK(this->weights_pd_.set_format(
274 this->with_groups() ? hwigo : hwio));
275 if (bias_pd_.desc()->format == any)
276 CHECK(bias_pd_.set_format(x));
277 if (this->desc()->alg_kind == alg_kind::convolution_auto)
278 CHECK(this->set_alg_kind(alg_kind::convolution_direct));
279 return status::success;
283 _gemm_u8s8s32x_convolution_bwd_data_t(const pd_t *apd, const input_vector &inputs,
284 const output_vector &outputs)
285 : cpu_primitive_t(apd, inputs, outputs, true) {}
286 ~_gemm_u8s8s32x_convolution_bwd_data_t() {}
288 typedef typename prec_traits<data_type::u8>::type diff_dst_data_t;
289 typedef typename prec_traits<data_type::s8>::type wei_data_t;
290 typedef typename prec_traits<dst_type>::type diff_src_data_t;
291 typedef typename prec_traits<data_type::s32>::type acc_data_t;
293 virtual void execute(event_t *e) const {
294 execute_backward_data();
295 e->set_state(event_t::ready);
299 void execute_backward_data() const;
300 void execute_backward_data_thr(const int ithr, const int nthr,
301 const diff_dst_data_t *diff_dst_base, const wei_data_t *wei_base,
302 const char *bia_base, diff_src_data_t *diff_src_base,
303 const memory_tracking::grantor_t &scratchpad) const;
304 const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }