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 CPU_JIT_SSE42_CONVOLUTION_HPP
18 #define CPU_JIT_SSE42_CONVOLUTION_HPP
20 #include "c_types_map.hpp"
21 #include "cpu_convolution_pd.hpp"
22 #include "cpu_engine.hpp"
23 #include "jit_primitive_conf.hpp"
24 #include "jit_sse42_conv_kernel_f32.hpp"
25 #include "jit_uni_depthwise.hpp"
31 struct jit_sse42_convolution_fwd_t: public cpu_primitive_t {
32 struct pd_t: public cpu_convolution_fwd_pd_t {
33 pd_t(engine_t *engine,
34 const convolution_desc_t *adesc,
35 const primitive_attr_t *attr,
36 const typename pd_t::base_class *hint_fwd_pd)
37 : cpu_convolution_fwd_pd_t(engine, adesc, attr, hint_fwd_pd)
38 , jcp_(), jcp_dw_() {}
41 JIT_IMPL_NAME_HELPER("jit:", sse42, ""),
42 jit_sse42_convolution_fwd_t);
44 virtual status_t init() override {
45 using namespace prop_kind;
46 assert(this->engine()->kind() == engine_kind::cpu);
48 && this->set_default_params() == status::success
49 && utils::one_of(this->desc()->prop_kind, forward_training,
51 && utils::one_of(this->desc()->alg_kind,
52 alg_kind::convolution_auto,
53 alg_kind::convolution_direct)
54 && !this->has_zero_dim_memory()
55 && utils::everyone_is(data_type::f32,
56 this->desc()->src_desc.data_type,
57 this->desc()->weights_desc.data_type,
58 this->desc()->dst_desc.data_type)
59 && IMPLICATION(this->with_bias(),
60 data_type::f32 == this->desc()->bias_desc.data_type);
61 if (!ok) return status::unimplemented;
63 status_t status = jit_sse42_conv_fwd_kernel_f32::init_conf(jcp_, *this->desc(),
64 *this->src_pd_.desc(), *this->weights_pd_.desc(),
65 *this->dst_pd_.desc(), *this->attr());
66 if (status != status::success) return status;
68 if (jcp_.with_dw_conv) {
69 status_t sts_dw = jit_uni_dw_conv_row_f32<sse42>::init_conf(jcp_, jcp_dw_, *this->attr());
70 if (sts_dw != status::success) return sts_dw;
73 auto scratchpad = scratchpad_registry().registrar();
74 jit_sse42_conv_fwd_kernel_f32::init_scratchpad(scratchpad, jcp_, jcp_dw_);
76 return status::success;
80 jit_conv_conf_t jcp_dw_;
83 virtual status_t set_default_params() override {
84 using namespace memory_format;
86 const bool flat = this->IC() == 3 || this->IC() == 1;
87 if (this->src_pd_.desc()->format == any)
88 CHECK(this->src_pd_.set_format(flat
89 ? utils::pick(this->ndims() - 3, ncw, nchw)
90 : utils::pick(this->ndims() - 3, nCw8c, nChw8c)));
91 if (this->dst_pd_.desc()->format == any)
92 CHECK(this->dst_pd_.set_format(utils::pick(this->ndims() - 3,
94 if (this->weights_pd_.desc()->format == any)
95 CHECK(this->weights_pd_.set_format(this->with_groups()
96 ? utils::pick(2 * this->ndims() - 6 + flat, gOIw8i8o,
97 gOwi8o, gOIhw8i8o, gOhwi8o)
98 : utils::pick(2 * this->ndims() - 6 + flat, OIw8i8o, Owi8o,
100 if (this->bias_pd_.desc()->format == any)
101 CHECK(this->bias_pd_.set_format(x));
102 if (this->desc()->alg_kind == alg_kind::convolution_auto)
103 CHECK(this->set_alg_kind(alg_kind::convolution_direct));
104 return status::success;
108 jit_sse42_convolution_fwd_t(const pd_t *apd, const input_vector &inputs,
109 const output_vector &outputs)
110 : cpu_primitive_t(apd, inputs, outputs)
112 kernel_ = new jit_sse42_conv_fwd_kernel_f32(pd()->jcp_, pd()->jcp_dw_, *pd()->attr());
114 if (pd()->jcp_.with_dw_conv) {
115 kernel_dw_ = new jit_uni_dw_conv_row_f32<sse42>(pd()->jcp_dw_, *pd()->attr(), pd()->jcp_dw_.ch_block);
119 ~jit_sse42_convolution_fwd_t() {
122 if (pd()->jcp_.with_dw_conv) {
127 typedef typename prec_traits<data_type::f32>::type data_t;
129 virtual void execute(event_t *e) const {
130 if (pd()->jcp_.with_dw_conv)
131 execute_forward_with_dw_conv();
135 e->set_state(event_t::ready);
139 void execute_forward() const;
140 void execute_forward_with_dw_conv() const;
141 const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
143 jit_sse42_conv_fwd_kernel_f32 *kernel_;
144 jit_uni_dw_conv_row_f32<sse42> *kernel_dw_;