blob: ae309d909316a769d823314693ff4090f78ece64 [file] [log] [blame]
Sanghoon Lee96883782017-09-15 14:10:48 +01001/*
Giorgio Arena11674872018-02-07 15:38:12 +00002 * Copyright (c) 2017-2018 ARM Limited.
Sanghoon Lee96883782017-09-15 14:10:48 +01003 *
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 "BatchNormalizationLayer.h"
25
Giorgio Arena11674872018-02-07 15:38:12 +000026#include "ActivationLayer.h"
27
Sanghoon Lee96883782017-09-15 14:10:48 +010028#include "tests/validation/FixedPoint.h"
29#include "tests/validation/Helpers.h"
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +000030#include "tests/validation/reference/Permute.h"
Sanghoon Lee96883782017-09-15 14:10:48 +010031
32namespace arm_compute
33{
34namespace test
35{
36namespace validation
37{
38namespace reference
39{
40// Batch Normalization Layer for fixed point type
41template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type *>
42SimpleTensor<T> batch_normalization_layer(const SimpleTensor<T> &src, const SimpleTensor<T> &mean, const SimpleTensor<T> &var, const SimpleTensor<T> &beta, const SimpleTensor<T> &gamma, float epsilon,
Giorgio Arena11674872018-02-07 15:38:12 +000043 ActivationLayerInfo act_info, int fixed_point_position)
Sanghoon Lee96883782017-09-15 14:10:48 +010044{
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +000045 ARM_COMPUTE_ERROR_ON_MSG(src.data_layout() == DataLayout::NHWC, "Unsupported NHWC format");
Giorgio Arena11674872018-02-07 15:38:12 +000046 ARM_COMPUTE_UNUSED(act_info);
Sanghoon Lee96883782017-09-15 14:10:48 +010047 SimpleTensor<T> result(src.shape(), src.data_type());
48
49 const auto cols = static_cast<int>(src.shape()[0]);
50 const auto rows = static_cast<int>(src.shape()[1]);
51 const auto depth = static_cast<int>(src.shape()[2]);
Sanghoon Leec1294fa2017-11-17 11:47:41 +000052 const int upper_dims = src.shape().total_size() / (cols * rows * depth);
Sanghoon Lee96883782017-09-15 14:10:48 +010053
54 for(int r = 0; r < upper_dims; ++r)
55 {
56 for(int i = 0; i < depth; ++i)
57 {
58 for(int k = 0; k < rows; ++k)
59 {
60 for(int l = 0; l < cols; ++l)
61 {
62 const int pos = l + k * cols + i * rows * cols + r * cols * rows * depth;
63
64 fixed_point_arithmetic::fixed_point<T> src_qs(src[pos], fixed_point_position, true);
65 fixed_point_arithmetic::fixed_point<T> var_qs(var[i], fixed_point_position, true);
66 fixed_point_arithmetic::fixed_point<T> mean_qs(mean[i], fixed_point_position, true);
67 fixed_point_arithmetic::fixed_point<T> beta_qs(beta[i], fixed_point_position, true);
68 fixed_point_arithmetic::fixed_point<T> gamma_qs(gamma[i], fixed_point_position, true);
69 fixed_point_arithmetic::fixed_point<T> epsilon_qs(epsilon, fixed_point_position);
70
71 auto denominator = fixed_point_arithmetic::inv_sqrt(var_qs + epsilon_qs);
72 auto numerator = src_qs - mean_qs;
73 auto x_bar = numerator * denominator;
74 x_bar = beta_qs + x_bar * gamma_qs;
75 result[pos] = x_bar.raw();
76 }
77 }
78 }
79 }
80
81 return result;
82}
83
84// Batch Normalization Layer for floating point type
85template <typename T, typename std::enable_if<is_floating_point<T>::value, int>::type *>
86SimpleTensor<T> batch_normalization_layer(const SimpleTensor<T> &src, const SimpleTensor<T> &mean, const SimpleTensor<T> &var, const SimpleTensor<T> &beta, const SimpleTensor<T> &gamma, float epsilon,
Giorgio Arena11674872018-02-07 15:38:12 +000087 ActivationLayerInfo act_info, int fixed_point_position)
Sanghoon Lee96883782017-09-15 14:10:48 +010088{
89 ARM_COMPUTE_UNUSED(fixed_point_position);
90
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +000091 const bool is_nhwc = src.data_layout() == DataLayout::NHWC;
92 const SimpleTensor<T> perm_src = (is_nhwc) ? permute(src, PermutationVector(1U, 2U, 0U)) : src;
93 SimpleTensor<T> result(perm_src.shape(), perm_src.data_type());
Sanghoon Lee96883782017-09-15 14:10:48 +010094
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +000095 const auto cols = static_cast<int>(perm_src.shape()[0]);
96 const auto rows = static_cast<int>(perm_src.shape()[1]);
97 const auto depth = static_cast<int>(perm_src.shape()[2]);
98 const int upper_dims = perm_src.shape().total_size() / (cols * rows * depth);
Sanghoon Lee96883782017-09-15 14:10:48 +010099
100 for(int r = 0; r < upper_dims; ++r)
101 {
102 for(int i = 0; i < depth; ++i)
103 {
104 for(int k = 0; k < rows; ++k)
105 {
106 for(int l = 0; l < cols; ++l)
107 {
108 const int pos = l + k * cols + i * rows * cols + r * cols * rows * depth;
109 const float denominator = sqrt(var[i] + epsilon);
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +0000110 const float numerator = perm_src[pos] - mean[i];
Sanghoon Lee96883782017-09-15 14:10:48 +0100111 const float x_bar = numerator / denominator;
112 result[pos] = beta[i] + x_bar * gamma[i];
113 }
114 }
115 }
116 }
Giorgio Arena11674872018-02-07 15:38:12 +0000117
118 if(act_info.enabled())
119 {
120 result = activation_layer(result, act_info);
121 }
122
Michele Di Giorgio0cbb9272018-03-01 16:56:48 +0000123 if(is_nhwc)
124 {
125 result = permute(result, PermutationVector(2U, 0U, 1U));
126 }
Sanghoon Lee96883782017-09-15 14:10:48 +0100127 return result;
128}
129template SimpleTensor<float> batch_normalization_layer(const SimpleTensor<float> &src, const SimpleTensor<float> &mean, const SimpleTensor<float> &var, const SimpleTensor<float> &beta,
Giorgio Arena11674872018-02-07 15:38:12 +0000130 const SimpleTensor<float> &gamma, float epsilon, ActivationLayerInfo act_info, int fixed_point_position);
Sanghoon Lee96883782017-09-15 14:10:48 +0100131template SimpleTensor<int8_t> batch_normalization_layer(const SimpleTensor<int8_t> &src, const SimpleTensor<int8_t> &mean, const SimpleTensor<int8_t> &var, const SimpleTensor<int8_t> &beta,
Giorgio Arena11674872018-02-07 15:38:12 +0000132 const SimpleTensor<int8_t> &gamma, float epsilon, ActivationLayerInfo act_info, int fixed_point_position);
Sanghoon Lee96883782017-09-15 14:10:48 +0100133template SimpleTensor<int16_t> batch_normalization_layer(const SimpleTensor<int16_t> &src, const SimpleTensor<int16_t> &mean, const SimpleTensor<int16_t> &var, const SimpleTensor<int16_t> &beta,
Giorgio Arena11674872018-02-07 15:38:12 +0000134 const SimpleTensor<int16_t> &gamma, float epsilon, ActivationLayerInfo act_info, int fixed_point_position);
Sanghoon Lee96883782017-09-15 14:10:48 +0100135template SimpleTensor<half> batch_normalization_layer(const SimpleTensor<half> &src, const SimpleTensor<half> &mean, const SimpleTensor<half> &var,
136 const SimpleTensor<half> &beta,
Giorgio Arena11674872018-02-07 15:38:12 +0000137 const SimpleTensor<half> &gamma, float epsilon, ActivationLayerInfo act_info, int fixed_point_position);
Sanghoon Lee96883782017-09-15 14:10:48 +0100138
139} // namespace reference
140} // namespace validation
141} // namespace test
142} // namespace arm_compute