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
24using namespace mkldnn::impl;
25using namespace mkldnn::impl::utils;
26using namespace mkldnn::impl::status;
27using namespace mkldnn::impl::prop_kind;
28using namespace mkldnn::impl::alg_kind;
29using namespace mkldnn::impl::types;
30
31namespace {
32status_t lrn_desc_init(lrn_desc_t *lrn_desc,
33 prop_kind_t prop_kind, alg_kind_t alg_kind,
34 const memory_desc_t *data_desc, const memory_desc_t *diff_data_desc,
35 dim_t local_size, float alpha, float beta, float k) {
36 bool args_ok = true
37 && !any_null(lrn_desc, data_desc)
38 && one_of(alg_kind, lrn_within_channel, lrn_across_channels)
39 && one_of(prop_kind, forward_training, forward_inference, backward_data)
40 && IMPLICATION(prop_kind == backward_data, diff_data_desc != nullptr);
41 if (!args_ok) return invalid_arguments;
42
43 auto ld = lrn_desc_t();
44 ld.primitive_kind = primitive_kind::lrn;
45 ld.prop_kind = prop_kind;
46 ld.alg_kind = alg_kind;
47
48 const bool is_fwd = one_of(prop_kind, forward_training, forward_inference);
49
50 ld.data_desc = *data_desc;
51 if (!is_fwd)
52 ld.diff_data_desc = *diff_data_desc;
53 else
54 ld.diff_data_desc = zero_md();
55 ld.local_size = local_size;
56 ld.lrn_alpha = alpha;
57 ld.lrn_beta = beta;
58 ld.lrn_k = k;
59
60 bool consistency = true
61 && ld.data_desc.ndims == 4;
62 if (ld.prop_kind == backward_data)
63 consistency = consistency
64 && ld.diff_data_desc.ndims == 4
65 && array_cmp(ld.diff_data_desc.dims, ld.data_desc.dims, 4);
66 if (!consistency) return invalid_arguments;
67
68 *lrn_desc = ld;
69 return success;
70}
71}
72
73status_t mkldnn_lrn_forward_desc_init(lrn_desc_t *lrn_desc,
74 prop_kind_t prop_kind, alg_kind_t alg_kind,
75 const memory_desc_t *data_desc, dim_t local_size, float alpha,
76 float beta, float k) {
77 if (!one_of(prop_kind, forward_training, forward_inference))
78 return invalid_arguments;
79 return lrn_desc_init(lrn_desc, prop_kind, alg_kind, data_desc, nullptr,
80 local_size, alpha, beta, k);
81}
82
83status_t mkldnn_lrn_backward_desc_init(lrn_desc_t *lrn_desc,
84 alg_kind_t alg_kind, const memory_desc_t *data_desc,
85 const memory_desc_t *diff_data_desc, dim_t local_size, float alpha,
86 float beta, float k) {
87 return lrn_desc_init(lrn_desc, backward_data, alg_kind, data_desc,
88 diff_data_desc, local_size, alpha, beta, k);
89}
90
91// vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s
92