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#ifndef CPU_REF_LAYER_NORMALIZATION_HPP
18#define CPU_REF_LAYER_NORMALIZATION_HPP
19
20#include <assert.h>
21
22#include "common/c_types_map.hpp"
23#include "common/primitive.hpp"
24#include "common/type_helpers.hpp"
25#include "common/utils.hpp"
26
27#include "cpu/platform.hpp"
28
29#include "cpu/cpu_layer_normalization_pd.hpp"
30
31namespace dnnl {
32namespace impl {
33namespace cpu {
34
35struct ref_layer_normalization_fwd_t : public primitive_t {
36 struct pd_t : public cpu_layer_normalization_fwd_pd_t {
37 using cpu_layer_normalization_fwd_pd_t::
38 cpu_layer_normalization_fwd_pd_t;
39
40 DECLARE_COMMON_PD_T("ref:any", ref_layer_normalization_fwd_t);
41
42 status_t init(engine_t *engine) {
43 using namespace data_type;
44 using skip_mask_t = primitive_attr_t::skip_mask_t;
45
46 bool ok = is_fwd()
47 && utils::one_of(
48 src_md()->data_type, f32, bf16, f16, s8, u8)
49 && utils::one_of(
50 dst_md()->data_type, f32, bf16, f16, s8, u8)
51 && platform::has_data_type_support(src_md()->data_type)
52 && platform::has_data_type_support(dst_md()->data_type)
53 && stat_md()->data_type == f32
54 && check_scale_shift_data_type()
55 && attr()->has_default_values(skip_mask_t::scales_runtime)
56 && attr_scales_ok() && set_default_formats_common();
57 if (!ok) return status::unimplemented;
58
59 return status::success;
60 }
61 };
62
63 ref_layer_normalization_fwd_t(const pd_t *apd) : primitive_t(apd) {}
64
65 status_t execute(const exec_ctx_t &ctx) const override {
66 return execute_forward(ctx);
67 }
68
69private:
70 status_t execute_forward(const exec_ctx_t &ctx) const;
71 const pd_t *pd() const { return (const pd_t *)primitive_t::pd().get(); }
72};
73
74struct ref_layer_normalization_bwd_t : public primitive_t {
75 struct pd_t : public cpu_layer_normalization_bwd_pd_t {
76 using cpu_layer_normalization_bwd_pd_t::
77 cpu_layer_normalization_bwd_pd_t;
78
79 DECLARE_COMMON_PD_T("ref:any", ref_layer_normalization_bwd_t);
80
81 status_t init(engine_t *engine) {
82 using namespace data_type;
83 bool ok = is_bwd()
84 && utils::one_of(src_md()->data_type, f32, bf16, f16)
85 && utils::one_of(diff_dst_md()->data_type, f32, bf16, f16)
86 && utils::one_of(diff_src_md()->data_type, f32, bf16, f16)
87 && platform::has_data_type_support(src_md()->data_type)
88 && platform::has_data_type_support(diff_dst_md()->data_type)
89 && platform::has_data_type_support(diff_src_md()->data_type)
90 && stat_md()->data_type == f32
91 && check_scale_shift_data_type()
92 && attr()->has_default_values()
93 && set_default_formats_common();
94 if (!ok) return status::unimplemented;
95
96 return status::success;
97 }
98 };
99
100 ref_layer_normalization_bwd_t(const pd_t *apd) : primitive_t(apd) {}
101
102 status_t execute(const exec_ctx_t &ctx) const override {
103 return execute_backward(ctx);
104 }
105
106private:
107 status_t execute_backward(const exec_ctx_t &ctx) const;
108 const pd_t *pd() const { return (const pd_t *)primitive_t::pd().get(); }
109};
110
111} // namespace cpu
112} // namespace impl
113} // namespace dnnl
114
115#endif
116
117// vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s
118