Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 1 | /* |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 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 "BatchNormalizationLayer.h" |
| 25 | |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 26 | #include "ActivationLayer.h" |
| 27 | |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 28 | #include "tests/validation/FixedPoint.h" |
| 29 | #include "tests/validation/Helpers.h" |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 30 | #include "tests/validation/reference/Permute.h" |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 31 | |
| 32 | namespace arm_compute |
| 33 | { |
| 34 | namespace test |
| 35 | { |
| 36 | namespace validation |
| 37 | { |
| 38 | namespace reference |
| 39 | { |
| 40 | // Batch Normalization Layer for fixed point type |
| 41 | template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type *> |
| 42 | SimpleTensor<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 Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 43 | ActivationLayerInfo act_info, int fixed_point_position) |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 44 | { |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 45 | ARM_COMPUTE_ERROR_ON_MSG(src.data_layout() == DataLayout::NHWC, "Unsupported NHWC format"); |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 46 | ARM_COMPUTE_UNUSED(act_info); |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 47 | 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 Lee | c1294fa | 2017-11-17 11:47:41 +0000 | [diff] [blame] | 52 | const int upper_dims = src.shape().total_size() / (cols * rows * depth); |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 53 | |
| 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 |
| 85 | template <typename T, typename std::enable_if<is_floating_point<T>::value, int>::type *> |
| 86 | SimpleTensor<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 Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 87 | ActivationLayerInfo act_info, int fixed_point_position) |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 88 | { |
| 89 | ARM_COMPUTE_UNUSED(fixed_point_position); |
| 90 | |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 91 | 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 Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 94 | |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 95 | 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 Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 99 | |
| 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 Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 110 | const float numerator = perm_src[pos] - mean[i]; |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 111 | const float x_bar = numerator / denominator; |
| 112 | result[pos] = beta[i] + x_bar * gamma[i]; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 117 | |
| 118 | if(act_info.enabled()) |
| 119 | { |
| 120 | result = activation_layer(result, act_info); |
| 121 | } |
| 122 | |
Michele Di Giorgio | 0cbb927 | 2018-03-01 16:56:48 +0000 | [diff] [blame] | 123 | if(is_nhwc) |
| 124 | { |
| 125 | result = permute(result, PermutationVector(2U, 0U, 1U)); |
| 126 | } |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 127 | return result; |
| 128 | } |
| 129 | template SimpleTensor<float> batch_normalization_layer(const SimpleTensor<float> &src, const SimpleTensor<float> &mean, const SimpleTensor<float> &var, const SimpleTensor<float> &beta, |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 130 | const SimpleTensor<float> &gamma, float epsilon, ActivationLayerInfo act_info, int fixed_point_position); |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 131 | template 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 Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 132 | const SimpleTensor<int8_t> &gamma, float epsilon, ActivationLayerInfo act_info, int fixed_point_position); |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 133 | template 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 Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 134 | const SimpleTensor<int16_t> &gamma, float epsilon, ActivationLayerInfo act_info, int fixed_point_position); |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 135 | template SimpleTensor<half> batch_normalization_layer(const SimpleTensor<half> &src, const SimpleTensor<half> &mean, const SimpleTensor<half> &var, |
| 136 | const SimpleTensor<half> &beta, |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 137 | const SimpleTensor<half> &gamma, float epsilon, ActivationLayerInfo act_info, int fixed_point_position); |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 138 | |
| 139 | } // namespace reference |
| 140 | } // namespace validation |
| 141 | } // namespace test |
| 142 | } // namespace arm_compute |