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 | && IMPLICATION(prop_kind == backward_data, diff_data_desc != nullptr); |
43 | if (!args_ok) return invalid_arguments; |
44 | |
45 | auto ed = eltwise_desc_t(); |
46 | ed.primitive_kind = primitive_kind::eltwise; |
47 | ed.prop_kind = prop_kind; |
48 | ed.alg_kind = alg_kind; |
49 | |
50 | ed.data_desc = *data_desc; |
51 | ed.diff_data_desc = |
52 | (ed.prop_kind == backward_data) ? *diff_data_desc : zero_md(); |
53 | |
54 | ed.alpha = alpha; |
55 | ed.beta = beta; |
56 | |
57 | bool consistency = true |
58 | && IMPLICATION(ed.prop_kind == backward_data, |
59 | array_cmp(ed.diff_data_desc.dims, ed.data_desc.dims, |
60 | ed.diff_data_desc.ndims)); |
61 | if (!consistency) return invalid_arguments; |
62 | |
63 | *eltwise_desc = ed; |
64 | return success; |
65 | } |
66 | } |
67 | |
68 | status_t mkldnn_eltwise_forward_desc_init(eltwise_desc_t *eltwise_desc, |
69 | prop_kind_t prop_kind, alg_kind_t alg_kind, |
70 | const memory_desc_t *data_desc, float alpha, float beta) { |
71 | if (!one_of(prop_kind, forward_training, forward_inference)) |
72 | return invalid_arguments; |
73 | return eltwise_desc_init(eltwise_desc, prop_kind, alg_kind, data_desc, |
74 | nullptr, alpha, beta); |
75 | } |
76 | |
77 | status_t mkldnn_eltwise_backward_desc_init(eltwise_desc_t *eltwise_desc, |
78 | alg_kind_t alg_kind, const memory_desc_t *diff_data_desc, |
79 | const memory_desc_t *data_desc, float alpha, float beta) { |
80 | return eltwise_desc_init(eltwise_desc, backward_data, alg_kind, data_desc, |
81 | diff_data_desc, alpha, beta); |
82 | } |
83 | |
84 | // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s |
85 | |