Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / src / cpu / jit_uni_binary_convolution.hpp
1 /*******************************************************************************
2 * Copyright 2019 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_JIT_UNI_BINARY_CONVOLUTION_HPP
18 #define CPU_JIT_UNI_BINARY_CONVOLUTION_HPP
19
20 #include "c_types_map.hpp"
21 #include "cpu_binary_convolution_pd.hpp"
22 #include "cpu_engine.hpp"
23 #include "cpu_reducer.hpp"
24 #include "jit_primitive_conf.hpp"
25 #include "jit_uni_bin_conv_kernel.hpp"
26 #include "mkldnn_thread.hpp"
27
28 namespace mkldnn {
29 namespace impl {
30 namespace cpu {
31
32 template <cpu_isa_t isa>
33 struct jit_uni_binary_convolution_fwd_t: public cpu_primitive_t {
34     struct pd_t: public _cpu_binary_convolution_fwd_pd_t {
35         pd_t(engine_t *engine,
36                 const binary_convolution_desc_t *adesc,
37                 const primitive_attr_t *attr,
38                 const typename pd_t::base_class *hint_fwd_pd)
39             : _cpu_binary_convolution_fwd_pd_t(engine, adesc, attr, hint_fwd_pd)
40             , jcp_(), jcp_dw_conv() {}
41
42         DECLARE_COMMON_PD_T(
43                 JIT_IMPL_NAME_HELPER("jit:", isa, ""),
44                 jit_uni_binary_convolution_fwd_t<isa>);
45
46         virtual status_t init() override {
47             using namespace prop_kind;
48             assert(this->engine()->kind() == engine_kind::cpu);
49             bool ok = true
50                 && this->set_default_params() == status::success
51                 && utils::one_of(this->cdesc_().prop_kind, forward_training, forward_inference)
52                 && this->cdesc_().alg_kind == alg_kind::binary_convolution_direct
53                 && utils::everyone_is(data_type::bin,
54                         this->cdesc_().src_desc.data_type,
55                         this->cdesc_().weights_desc.data_type)
56                 && utils::one_of(this->cdesc_().dst_desc.data_type,
57                         memory::data_type::f32,
58                         memory::data_type::bin);
59             if (!ok) return status::unimplemented;
60
61             status_t sts = jit_uni_bin_conv_fwd_kernel<isa>::init_conf(jcp_, *this->desc(),
62                     *this->src_pd_.desc(), *this->weights_pd_.desc(),
63                     *this->dst_pd_.desc(), *this->attr());
64             if (sts != status::success) return sts;
65
66             if (jcp_.with_dw_conv) {
67                 status_t sts_dw = jit_uni_dw_conv_row_f32<isa>::init_conf(jcp_, jcp_dw_conv, *this->attr());
68                 if (sts_dw != status::success) return sts_dw;
69             }
70
71             auto scratchpad = scratchpad_registry().registrar();
72             jit_uni_bin_conv_fwd_kernel<isa>::init_scratchpad(scratchpad, jcp_, jcp_dw_conv);
73
74             return status::success;
75         }
76
77         jit_bin_conv_conf_t jcp_;
78         jit_conv_conf_t jcp_dw_conv;
79
80     protected:
81         virtual status_t set_default_params() override {
82             using namespace memory_format;
83
84             auto desired_weights_format = isa == avx512_common ? OhIw16o32i : OhIw8o32i;
85
86             if (this->src_pd_.desc()->format == any)
87                 CHECK(this->src_pd_.set_format(nhwc));
88             if (this->dst_pd_.desc()->format == any)
89                 CHECK(this->dst_pd_.set_format(nhwc));
90             if (this->weights_pd_.desc()->format == any)
91                 CHECK(this->weights_pd_.set_format(desired_weights_format));
92             return status::success;
93         }
94     };
95
96     jit_uni_binary_convolution_fwd_t(const pd_t *apd, const input_vector &inputs,
97             const output_vector &outputs)
98         : cpu_primitive_t(apd, inputs, outputs) {
99         kernel_ = new jit_uni_bin_conv_fwd_kernel<isa>(pd()->jcp_, pd()->jcp_dw_conv, *pd()->attr());
100
101         if (pd()->jcp_.with_dw_conv) {
102             dw_conv_kernel_ = new jit_uni_dw_conv_row_f32<isa>(pd()->jcp_dw_conv, *pd()->attr(), pd()->jcp_dw_conv.oc);
103         }
104     }
105
106     ~jit_uni_binary_convolution_fwd_t() {
107         delete kernel_;
108
109         if (pd()->jcp_.with_dw_conv) {
110             delete dw_conv_kernel_;
111         }
112     };
113
114     virtual void execute(event_t *e) const {
115         if (pd()->jcp_.with_dw_conv)
116             execute_forward_with_dw_conv();
117         else
118             execute_forward();
119
120         e->set_state(event_t::ready);
121     }
122
123 private:
124     void execute_forward() const;
125     void execute_forward_with_dw_conv() const;
126
127     const pd_t *pd() const { return (const pd_t *)primitive_t::pd(); }
128
129     jit_uni_bin_conv_fwd_kernel<isa> *kernel_;
130     /* fuse with dw conv */
131     jit_uni_dw_conv_row_f32<isa> *dw_conv_kernel_;
132 };
133
134 }
135 }
136 }
137
138 #endif