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 bnrm_desc_init(batch_normalization_desc_t *bnrm_desc,
33 prop_kind_t prop_kind, const memory_desc_t *data_desc,
34 const memory_desc_t *diff_data_desc, float epsilon, unsigned flags) {
35 bool args_ok = true
36 && !any_null(bnrm_desc, data_desc)
37 && one_of(prop_kind, forward_training, forward_inference,
38 backward_data, backward)
39 && IMPLICATION(prop_kind & backward, diff_data_desc != nullptr);
40 if (!args_ok) return invalid_arguments;
41
42 auto bd = batch_normalization_desc_t();
43 bd.primitive_kind = primitive_kind::batch_normalization;
44 bd.prop_kind = prop_kind;
45
46 bd.data_desc = *data_desc;
47 bd.diff_data_desc = zero_md();
48 if ( one_of(bd.prop_kind,backward_data, backward) )
49 bd.diff_data_desc = *diff_data_desc;
50
51 dims_t scaleshift_dims = { 2, data_desc->dims[1] };
52 mkldnn_memory_desc_init_by_tag(&bd.data_scaleshift_desc, 2,
53 scaleshift_dims, data_type::f32, mkldnn_nc);
54 bd.diff_data_scaleshift_desc = zero_md();
55 if (bd.prop_kind == backward) {
56 bd.diff_data_scaleshift_desc = bd.data_scaleshift_desc;
57 }
58
59 dims_t stats_dims = { data_desc->dims[1] };
60 mkldnn_memory_desc_init_by_tag(&bd.mean_desc, 1, stats_dims,
61 data_type::f32, mkldnn_x);
62 bd.variance_desc = bd.mean_desc;
63 bd.batch_norm_epsilon = epsilon;
64
65 unsigned bnorm_flags =
66 mkldnn_use_global_stats | mkldnn_use_scaleshift | mkldnn_fuse_bn_relu;
67 if ((~bnorm_flags & flags) != 0) return invalid_arguments;
68
69 bd.flags = flags;
70
71 bool consistency = true
72 && utils::one_of(bd.data_desc.ndims, 2, 4, 5);
73 if (bd.prop_kind == backward_data)
74 consistency = consistency
75 && utils::one_of(bd.diff_data_desc.ndims, 2, 4, 5)
76 && array_cmp(bd.diff_data_desc.dims, bd.data_desc.dims,
77 bd.diff_data_desc.ndims);
78 if (!consistency) return invalid_arguments;
79
80 *bnrm_desc = bd;
81 return success;
82}
83}
84
85status_t mkldnn_batch_normalization_forward_desc_init(
86 batch_normalization_desc_t *bnrm_desc, prop_kind_t prop_kind,
87 const memory_desc_t *data_desc, float epsilon, unsigned flags) {
88 if (!one_of(prop_kind, forward_training, forward_inference))
89 return invalid_arguments;
90 return bnrm_desc_init(bnrm_desc, prop_kind, data_desc, nullptr,
91 epsilon, flags);
92}
93
94status_t mkldnn_batch_normalization_backward_desc_init(
95 batch_normalization_desc_t *bnrm_desc, prop_kind_t prop_kind,
96 const memory_desc_t *diff_data_desc, const memory_desc_t *data_desc,
97 float epsilon, unsigned flags) {
98 if (!one_of(prop_kind, backward, backward_data))
99 return invalid_arguments;
100 return bnrm_desc_init(bnrm_desc, prop_kind, data_desc, diff_data_desc,
101 epsilon, flags);
102}
103
104// vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s
105