1 /*
2 * Copyright (c) 2019-2020 Arm Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24 #include "FuseBatchNormalization.h"
25 #include "tests/validation/Helpers.h"
26
27 namespace arm_compute
28 {
29 namespace test
30 {
31 namespace validation
32 {
33 namespace reference
34 {
35 template <typename T>
fuse_batch_normalization_dwc_layer(const SimpleTensor<T> & w,const SimpleTensor<T> & mean,const SimpleTensor<T> & var,SimpleTensor<T> & w_fused,SimpleTensor<T> & b_fused,const SimpleTensor<T> & b,const SimpleTensor<T> & beta,const SimpleTensor<T> & gamma,float epsilon)36 void fuse_batch_normalization_dwc_layer(const SimpleTensor<T> &w, const SimpleTensor<T> &mean, const SimpleTensor<T> &var, SimpleTensor<T> &w_fused, SimpleTensor<T> &b_fused, const SimpleTensor<T> &b,
37 const SimpleTensor<T> &beta, const SimpleTensor<T> &gamma, float epsilon)
38 {
39 const auto *w_data = w.data();
40 const auto *b_data = b.data();
41
42 auto *w_fused_data = w_fused.data();
43 auto *b_fused_data = b_fused.data();
44
45 const unsigned int width = w.shape()[0];
46 const unsigned int height = w.shape()[1];
47 const unsigned int dim2 = w.shape()[2];
48
49 #if defined(_OPENMP)
50 #pragma omp parallel for
51 #endif /* _OPENMP */
52 for(unsigned int b = 0; b < dim2; ++b)
53 {
54 const auto mean_val = mean.data()[b];
55 const auto var_val = var.data()[b];
56 const auto beta_val = beta.data()[b];
57 const auto gamma_val = gamma.data()[b];
58
59 for(unsigned int i = 0; i < width * height; ++i)
60 {
61 unsigned int index = i + b * width * height;
62
63 w_fused_data[index] = (gamma_val * (w_data[index])) / sqrt(var_val + epsilon);
64 }
65
66 b_fused_data[b] = (b_data[b] - mean_val) / sqrt(var_val + epsilon) * gamma_val + beta_val;
67 }
68 }
69
70 template <typename T>
fuse_batch_normalization_conv_layer(const SimpleTensor<T> & w,const SimpleTensor<T> & mean,const SimpleTensor<T> & var,SimpleTensor<T> & w_fused,SimpleTensor<T> & b_fused,const SimpleTensor<T> & b,const SimpleTensor<T> & beta,const SimpleTensor<T> & gamma,float epsilon)71 void fuse_batch_normalization_conv_layer(const SimpleTensor<T> &w, const SimpleTensor<T> &mean, const SimpleTensor<T> &var, SimpleTensor<T> &w_fused, SimpleTensor<T> &b_fused,
72 const SimpleTensor<T> &b,
73 const SimpleTensor<T> &beta, const SimpleTensor<T> &gamma, float epsilon)
74 {
75 const auto *w_data = w.data();
76 const auto *b_data = b.data();
77
78 auto *w_fused_data = w_fused.data();
79 auto *b_fused_data = b_fused.data();
80
81 const unsigned int width = w.shape()[0];
82 const unsigned int height = w.shape()[1];
83 const unsigned int dim2 = w.shape()[2];
84 const unsigned int dim3 = w.shape()[3];
85
86 for(unsigned int b = 0; b < dim3; ++b)
87 {
88 const auto mean_val = mean.data()[b];
89 const auto var_val = var.data()[b];
90 const auto beta_val = beta.data()[b];
91 const auto gamma_val = gamma.data()[b];
92
93 for(unsigned int i = 0; i < width * height * dim2; ++i)
94 {
95 unsigned int index = i + b * width * height * dim2;
96
97 w_fused_data[index] = (gamma_val * (w_data[index])) / sqrt(var_val + epsilon);
98 }
99
100 b_fused_data[b] = (b_data[b] - mean_val) / sqrt(var_val + epsilon) * gamma_val + beta_val;
101 }
102 }
103
104 template void fuse_batch_normalization_dwc_layer(const SimpleTensor<float> &w, const SimpleTensor<float> &mean, const SimpleTensor<float> &var, SimpleTensor<float> &w_fused,
105 SimpleTensor<float> &b_fused, const SimpleTensor<float> &b, const SimpleTensor<float> &beta, const SimpleTensor<float> &gamma, float epsilon);
106 template void fuse_batch_normalization_dwc_layer(const SimpleTensor<half> &w, const SimpleTensor<half> &mean, const SimpleTensor<half> &var, SimpleTensor<half> &w_fused, SimpleTensor<half> &b_fused,
107 const SimpleTensor<half> &b, const SimpleTensor<half> &beta, const SimpleTensor<half> &gamma, float epsilon);
108 template void fuse_batch_normalization_conv_layer(const SimpleTensor<float> &w, const SimpleTensor<float> &mean, const SimpleTensor<float> &var, SimpleTensor<float> &w_fused,
109 SimpleTensor<float> &b_fused, const SimpleTensor<float> &b, const SimpleTensor<float> &beta, const SimpleTensor<float> &gamma, float epsilon);
110 template void fuse_batch_normalization_conv_layer(const SimpleTensor<half> &w, const SimpleTensor<half> &mean, const SimpleTensor<half> &var, SimpleTensor<half> &w_fused, SimpleTensor<half> &b_fused,
111 const SimpleTensor<half> &b, const SimpleTensor<half> &beta, const SimpleTensor<half> &gamma, float epsilon);
112 } // namespace reference
113 } // namespace validation
114 } // namespace test
115 } // namespace arm_compute
116