Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / src / common / eltwise.cpp
1 /*******************************************************************************
2 * Copyright 2016-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.h"
19
20 #include "c_types_map.hpp"
21 #include "type_helpers.hpp"
22 #include "utils.hpp"
23
24 using namespace mkldnn::impl;
25 using namespace mkldnn::impl::utils;
26 using namespace mkldnn::impl::status;
27 using namespace mkldnn::impl::prop_kind;
28 using namespace mkldnn::impl::alg_kind;
29 using namespace mkldnn::impl::types;
30
31 namespace {
32 status_t eltwise_desc_init(eltwise_desc_t *eltwise_desc, prop_kind_t prop_kind,
33         alg_kind_t alg_kind, const memory_desc_t *data_desc,
34         const memory_desc_t *diff_data_desc, float alpha, float beta) {
35     bool args_ok = true
36         && !any_null(eltwise_desc, data_desc)
37         && one_of(prop_kind, forward_training, forward_inference,
38                 backward_data)
39         && one_of(alg_kind, eltwise_relu, eltwise_tanh, eltwise_elu,
40                   eltwise_square, eltwise_abs, eltwise_sqrt, eltwise_linear,
41                   eltwise_bounded_relu, eltwise_soft_relu, eltwise_logistic,
42                   eltwise_clamp, eltwise_exp, eltwise_not)
43         && IMPLICATION(prop_kind == backward_data, diff_data_desc != nullptr);
44     if (!args_ok) return invalid_arguments;
45
46     auto ed = eltwise_desc_t();
47     ed.primitive_kind = primitive_kind::eltwise;
48     ed.prop_kind = prop_kind;
49     ed.alg_kind = alg_kind;
50
51     ed.data_desc = *data_desc;
52     ed.diff_data_desc =
53         (ed.prop_kind == backward_data) ? *diff_data_desc : zero_md();
54
55     ed.alpha = alpha;
56     ed.beta = beta;
57
58     bool consistency = true
59         && IMPLICATION(ed.prop_kind == backward_data,
60                 array_cmp(ed.diff_data_desc.dims, ed.data_desc.dims,
61                     ed.diff_data_desc.ndims));
62     if (!consistency) return invalid_arguments;
63
64     *eltwise_desc = ed;
65     return success;
66 }
67 }
68
69 status_t mkldnn_eltwise_forward_desc_init(eltwise_desc_t *eltwise_desc,
70         prop_kind_t prop_kind, alg_kind_t alg_kind,
71         const memory_desc_t *data_desc, float alpha, float beta) {
72     if (!one_of(prop_kind, forward_training, forward_inference))
73         return invalid_arguments;
74     return eltwise_desc_init(eltwise_desc, prop_kind, alg_kind, data_desc,
75             nullptr, alpha, beta);
76 }
77
78 status_t mkldnn_eltwise_backward_desc_init(eltwise_desc_t *eltwise_desc,
79         alg_kind_t alg_kind, const memory_desc_t *diff_data_desc,
80         const memory_desc_t *data_desc, float alpha, float beta) {
81     return eltwise_desc_init(eltwise_desc, backward_data, alg_kind, data_desc,
82             diff_data_desc, alpha, beta);
83 }
84
85 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s