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