1/*******************************************************************************
2* Copyright 2019-2022 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 "oneapi/dnnl/dnnl.h"
19#include "opdesc.hpp"
20#include "primitive_desc_iface.hpp"
21
22#include "c_types_map.hpp"
23#include "type_helpers.hpp"
24#include "utils.hpp"
25
26using namespace dnnl::impl;
27using namespace dnnl::impl::utils;
28using namespace dnnl::impl::status;
29using namespace dnnl::impl::prop_kind;
30using namespace dnnl::impl::types;
31
32namespace {
33status_t lnorm_desc_init(layer_normalization_desc_t *lnorm_desc,
34 prop_kind_t prop_kind, const memory_desc_t *src_desc,
35 const memory_desc_t *dst_desc, const memory_desc_t *stat_desc,
36 const memory_desc_t *diff_src_desc, const memory_desc_t *diff_dst_desc,
37 float epsilon, unsigned flags) {
38 bool args_ok = !any_null(lnorm_desc, src_desc) && 2 <= src_desc->ndims
39 && src_desc->ndims <= 5
40 && (flags
41 & ~(normalization_flags::use_global_stats
42 | normalization_flags::use_scale
43 | normalization_flags::use_shift))
44 == 0;
45 if (!args_ok) return invalid_arguments;
46
47 bool is_fwd
48 = prop_kind == forward_training || prop_kind == forward_inference;
49 args_ok = IMPLICATION(is_fwd, dst_desc != nullptr)
50 && IMPLICATION(!is_fwd, !any_null(diff_src_desc, diff_dst_desc))
51 && IMPLICATION(is_fwd, !memory_desc_wrapper(src_desc).format_any());
52 if (!args_ok) return invalid_arguments;
53
54 auto ld = layer_normalization_desc_t();
55 ld.primitive_kind = primitive_kind::layer_normalization;
56 ld.prop_kind = prop_kind;
57
58 bool runtime_dims_or_strides
59 = memory_desc_wrapper(src_desc).has_runtime_dims_or_strides()
60 || memory_desc_wrapper(dst_desc).has_runtime_dims_or_strides()
61 || (stat_desc
62 && memory_desc_wrapper(stat_desc)
63 .has_runtime_dims_or_strides());
64 if (!is_fwd)
65 runtime_dims_or_strides = runtime_dims_or_strides
66 || memory_desc_wrapper(diff_src_desc)
67 .has_runtime_dims_or_strides()
68 || memory_desc_wrapper(diff_dst_desc)
69 .has_runtime_dims_or_strides();
70 if (runtime_dims_or_strides) return unimplemented;
71
72 ld.src_desc = *src_desc;
73 if (is_fwd) ld.dst_desc = *dst_desc;
74 if (!is_fwd) ld.diff_src_desc = *diff_src_desc;
75 if (!is_fwd) ld.diff_dst_desc = *diff_dst_desc;
76
77 if (stat_desc)
78 ld.stat_desc = *stat_desc;
79 else
80 CHECK(memory_desc_init_by_tag(ld.stat_desc, ld.src_desc.ndims - 1,
81 ld.src_desc.dims, data_type::f32, format_tag::any));
82
83 int ndims = src_desc->ndims;
84 ld.data_scaleshift_desc = zero_md();
85 if (flags
86 & (normalization_flags::use_scale
87 | normalization_flags::use_shift)) {
88 dims_t scaleshift_dims = {src_desc->dims[ndims - 1]};
89 memory_desc_init_by_tag(ld.data_scaleshift_desc, 1, scaleshift_dims,
90 data_type::f32, dnnl_x);
91 } else {
92 dims_t scaleshift_dims = {2, src_desc->dims[ndims - 1]};
93 memory_desc_init_by_tag(ld.data_scaleshift_desc, 2, scaleshift_dims,
94 data_type::f32, dnnl_nc);
95 }
96 if (ld.prop_kind == backward) {
97 ld.diff_data_scaleshift_desc = ld.data_scaleshift_desc;
98 }
99
100 ld.layer_norm_epsilon = epsilon;
101
102 ld.flags = flags;
103
104 if (is_fwd) {
105 bool consistency = ld.src_desc.ndims == ld.dst_desc.ndims
106 && array_cmp(
107 ld.src_desc.dims, ld.dst_desc.dims, ld.src_desc.ndims);
108 if (!consistency) return invalid_arguments;
109 } else {
110 bool consistency = ld.diff_src_desc.ndims == ld.src_desc.ndims
111 && array_cmp(ld.diff_src_desc.dims, ld.src_desc.dims,
112 ld.diff_src_desc.ndims)
113 && ld.diff_src_desc.ndims == ld.diff_dst_desc.ndims
114 && array_cmp(ld.diff_src_desc.dims, ld.diff_dst_desc.dims,
115 ld.diff_src_desc.ndims)
116 && ld.src_desc.ndims == ld.stat_desc.ndims + 1
117 && array_cmp(ld.stat_desc.dims, ld.src_desc.dims,
118 ld.stat_desc.ndims);
119 if (!consistency) return invalid_arguments;
120 }
121
122 *lnorm_desc = ld;
123 return success;
124}
125} // namespace
126
127status_t dnnl_layer_normalization_forward_primitive_desc_create(
128 primitive_desc_iface_t **primitive_desc_iface, engine_t *engine,
129 prop_kind_t prop_kind, const memory_desc_t *src_desc,
130 const memory_desc_t *dst_desc, const memory_desc_t *stat_desc,
131 float epsilon, unsigned flags, const primitive_attr_t *attr) {
132 if (!one_of(prop_kind, forward_training, forward_inference))
133 return invalid_arguments;
134
135 auto lnorm_desc = layer_normalization_desc_t();
136 CHECK(lnorm_desc_init(&lnorm_desc, prop_kind, src_desc, dst_desc, stat_desc,
137 nullptr, nullptr, epsilon, flags));
138 return primitive_desc_create(primitive_desc_iface, engine,
139 (const op_desc_t *)&lnorm_desc, nullptr, attr);
140}
141
142status_t dnnl_layer_normalization_backward_primitive_desc_create(
143 primitive_desc_iface_t **primitive_desc_iface, engine_t *engine,
144 prop_kind_t prop_kind, const memory_desc_t *diff_src_desc,
145 const memory_desc_t *diff_dst_desc, const memory_desc_t *src_desc,
146 const memory_desc_t *stat_desc, float epsilon, unsigned flags,
147 const primitive_desc_iface_t *hint_fwd_pd,
148 const primitive_attr_t *attr) {
149 if (!one_of(prop_kind, backward, backward_data)) return invalid_arguments;
150
151 auto lnorm_desc = layer_normalization_desc_t();
152 CHECK(lnorm_desc_init(&lnorm_desc, prop_kind, src_desc, nullptr, stat_desc,
153 diff_src_desc, diff_dst_desc, epsilon, flags));
154 return primitive_desc_create(primitive_desc_iface, engine,
155 (const op_desc_t *)&lnorm_desc, hint_fwd_pd, attr);
156}
157
158// vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s
159