Moritz Pflanzer | 6db73ce | 2017-07-19 10:18:42 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 Arm Limited. |
Moritz Pflanzer | 6db73ce | 2017-07-19 10:18:42 +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 "NormalizationLayer.h" |
| 25 | |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/Types.h" |
Moritz Pflanzer | 6db73ce | 2017-07-19 10:18:42 +0100 | [diff] [blame] | 27 | |
| 28 | namespace arm_compute |
| 29 | { |
| 30 | namespace test |
| 31 | { |
| 32 | namespace validation |
| 33 | { |
| 34 | namespace reference |
| 35 | { |
Isabella Gottardi | 651540f | 2018-09-13 15:33:35 +0100 | [diff] [blame] | 36 | template <typename T> |
Moritz Pflanzer | 6db73ce | 2017-07-19 10:18:42 +0100 | [diff] [blame] | 37 | SimpleTensor<T> normalization_layer(const SimpleTensor<T> &src, NormalizationLayerInfo info) |
| 38 | { |
| 39 | // Create reference |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 40 | SimpleTensor<T> dst{ src.shape(), src.data_type(), 1 }; |
Moritz Pflanzer | 6db73ce | 2017-07-19 10:18:42 +0100 | [diff] [blame] | 41 | |
| 42 | // Compute reference |
| 43 | const uint32_t norm_size = info.norm_size(); |
| 44 | NormType type = info.type(); |
| 45 | float beta = info.beta(); |
| 46 | uint32_t kappa = info.kappa(); |
| 47 | |
| 48 | const int cols = src.shape()[0]; |
| 49 | const int rows = src.shape()[1]; |
| 50 | const int depth = src.shape()[2]; |
| 51 | int upper_dims = src.shape().total_size() / (cols * rows); |
| 52 | |
| 53 | float coeff = info.scale_coeff(); |
| 54 | int radius_cols = norm_size / 2; |
| 55 | |
| 56 | // IN_MAP_1D and CROSS_MAP normalize over a single axis only |
| 57 | int radius_rows = (NormType::IN_MAP_2D == type) ? norm_size / 2 : 0; |
| 58 | |
Michele Di Giorgio | 9d3a831 | 2018-11-20 12:31:24 +0000 | [diff] [blame] | 59 | if(info.is_cross_map()) |
Moritz Pflanzer | 6db73ce | 2017-07-19 10:18:42 +0100 | [diff] [blame] | 60 | { |
| 61 | // Remove also depth from upper dimensions since it is the dimension we |
| 62 | // want to use for normalization |
| 63 | upper_dims /= depth; |
| 64 | |
| 65 | for(int r = 0; r < upper_dims; ++r) |
| 66 | { |
| 67 | for(int i = 0; i < rows; ++i) |
| 68 | { |
| 69 | for(int k = 0; k < cols; ++k) |
| 70 | { |
| 71 | for(int l = 0; l < depth; ++l) |
| 72 | { |
| 73 | float accumulated_scale = 0.f; |
| 74 | |
| 75 | for(int j = -radius_cols; j <= radius_cols; ++j) |
| 76 | { |
| 77 | const int z = l + j; |
| 78 | |
| 79 | if(z >= 0 && z < depth) |
| 80 | { |
| 81 | const T value = src[k + i * cols + z * rows * cols + r * cols * rows * depth]; |
| 82 | accumulated_scale += value * value; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | dst[k + i * cols + l * rows * cols + r * cols * rows * depth] = kappa + accumulated_scale * coeff; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | for(int r = 0; r < upper_dims; ++r) |
| 95 | { |
| 96 | for(int i = 0; i < rows; ++i) |
| 97 | { |
| 98 | for(int k = 0; k < cols; ++k) |
| 99 | { |
| 100 | float accumulated_scale = 0.f; |
| 101 | |
| 102 | for(int j = -radius_rows; j <= radius_rows; ++j) |
| 103 | { |
| 104 | const int y = i + j; |
| 105 | for(int l = -radius_cols; l <= radius_cols; ++l) |
| 106 | { |
| 107 | const int x = k + l; |
| 108 | |
| 109 | if((x >= 0 && y >= 0) && (x < cols && y < rows)) |
| 110 | { |
| 111 | const T value = src[x + y * cols + r * cols * rows]; |
| 112 | accumulated_scale += value * value; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | dst[k + i * cols + r * cols * rows] = kappa + accumulated_scale * coeff; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | if(beta == 1.f) |
| 124 | { |
| 125 | for(int i = 0; i < dst.num_elements(); ++i) |
| 126 | { |
| 127 | dst[i] = src[i] / dst[i]; |
| 128 | } |
| 129 | } |
| 130 | else if(beta == 0.5f) |
| 131 | { |
| 132 | for(int i = 0; i < dst.num_elements(); ++i) |
| 133 | { |
| 134 | dst[i] = src[i] / std::sqrt(dst[i]); |
| 135 | } |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | for(int i = 0; i < dst.num_elements(); ++i) |
| 140 | { |
| 141 | dst[i] = src[i] * std::exp(std::log(dst[i]) * -beta); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | return dst; |
| 146 | } |
| 147 | |
Moritz Pflanzer | 6db73ce | 2017-07-19 10:18:42 +0100 | [diff] [blame] | 148 | template SimpleTensor<float> normalization_layer(const SimpleTensor<float> &src, NormalizationLayerInfo info); |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 149 | template SimpleTensor<half> normalization_layer(const SimpleTensor<half> &src, NormalizationLayerInfo info); |
Moritz Pflanzer | 6db73ce | 2017-07-19 10:18:42 +0100 | [diff] [blame] | 150 | } // namespace reference |
| 151 | } // namespace validation |
| 152 | } // namespace test |
| 153 | } // namespace arm_compute |