Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / src / common / depthwise.cpp
1 /*******************************************************************************
2 * Copyright 2018 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 #include <assert.h>
18 #include <mkldnn_types.h>
19 #include "mkldnn.h"
20
21 #include "c_types_map.hpp"
22 #include "type_helpers.hpp"
23 #include "utils.hpp"
24
25 using namespace mkldnn::impl;
26 using namespace mkldnn::impl::utils;
27 using namespace mkldnn::impl::status;
28 using namespace mkldnn::impl::prop_kind;
29 using namespace mkldnn::impl::alg_kind;
30 using namespace mkldnn::impl::types;
31
32 namespace {
33 status_t depthwise_desc_init(depthwise_desc_t *depthwise_desc, prop_kind_t prop_kind,
34         alg_kind_t alg_kind, const memory_desc_t *src_desc, const memory_desc_t *dst_desc,
35         const memory_desc_t *weights_desc, const memory_desc_t *bias_desc) {
36     bool args_ok = true
37         && !any_null(depthwise_desc, src_desc, dst_desc)
38         && one_of(prop_kind, forward_training, forward_inference)
39         && one_of(alg_kind, depthwise_scale_shift, depthwise_prelu);
40     if (!args_ok) return invalid_arguments;
41
42     auto dd = depthwise_desc_t();
43     dd.primitive_kind = primitive_kind::depthwise;
44     dd.prop_kind = prop_kind;
45     dd.alg_kind = alg_kind;
46     dd.src_desc = *src_desc;
47     dd.dst_desc = *dst_desc;
48     dd.weights_desc = *weights_desc;
49
50     const bool with_bias = bias_desc && bias_desc->format != memory_format::undef;
51     dd.bias_desc = with_bias ? *bias_desc : zero_md();
52
53     bool consistency = true
54         && memory_desc_wrapper(dd.src_desc).nelems()
55         && memory_desc_wrapper(dd.dst_desc).nelems();
56     if (!consistency) return invalid_arguments;
57
58     *depthwise_desc = dd;
59     return success;
60 }
61 }
62
63 status_t mkldnn_depthwise_forward_desc_init(depthwise_desc_t *depthwise_desc,
64         prop_kind_t prop_kind, alg_kind_t alg_kind,
65         const memory_desc_t *src_desc, const memory_desc_t *dst_desc, const memory_desc_t *weights_desc,
66         const memory_desc_t *bias_desc) {
67     if (!one_of(prop_kind, forward_training, forward_inference))
68         return invalid_arguments;
69     return depthwise_desc_init(depthwise_desc, prop_kind, alg_kind, src_desc, dst_desc,
70                                weights_desc, bias_desc);
71 }
72
73 status_t mkldnn_depthwise_forward_desc_init(depthwise_desc_t *depthwise_desc,
74         prop_kind_t prop_kind, alg_kind_t alg_kind,
75         const memory_desc_t *src_desc, const memory_desc_t *dst_desc, const memory_desc_t *weights_desc) {
76     if (!one_of(prop_kind, forward_training, forward_inference))
77         return invalid_arguments;
78     return depthwise_desc_init(depthwise_desc, prop_kind, alg_kind, src_desc, dst_desc,
79                                weights_desc, nullptr);
80 }