Laurent Carlier | 749294b | 2020-06-01 09:03:17 +0100 | [diff] [blame] | 1 | // |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | |
Matteo Martincigh | 2fc70c5 | 2019-06-05 14:12:48 +0100 | [diff] [blame] | 6 | #include "RefNormalizationWorkload.hpp" |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 7 | |
Matthew Bentham | f48afc6 | 2020-01-15 17:55:08 +0000 | [diff] [blame] | 8 | #include <armnn/Logging.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 9 | #include <armnn/Tensor.hpp> |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 10 | #include <armnnUtils/DataLayoutIndexed.hpp> |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 11 | #include <armnn/utility/NumericCast.hpp> |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 12 | |
Matteo Martincigh | 2fc70c5 | 2019-06-05 14:12:48 +0100 | [diff] [blame] | 13 | #include <Profiling.hpp> |
| 14 | |
Matthew Bentham | f48afc6 | 2020-01-15 17:55:08 +0000 | [diff] [blame] | 15 | #include "RefWorkloadUtils.hpp" |
| 16 | #include "Decoders.hpp" |
| 17 | #include "Encoders.hpp" |
| 18 | |
Matteo Martincigh | 2fc70c5 | 2019-06-05 14:12:48 +0100 | [diff] [blame] | 19 | using namespace armnn; |
Matteo Martincigh | 2135015 | 2018-11-28 16:22:22 +0000 | [diff] [blame] | 20 | using namespace armnnUtils; |
| 21 | |
Matteo Martincigh | 2fc70c5 | 2019-06-05 14:12:48 +0100 | [diff] [blame] | 22 | namespace |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 23 | { |
| 24 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 25 | // Helper function to compute "Within" normalization using Krichevsky 2012: Local Brightness Normalization. |
Matteo Martincigh | 2fc70c5 | 2019-06-05 14:12:48 +0100 | [diff] [blame] | 26 | void NormalizeWithinUingLbr(Decoder<float>& inputData, |
| 27 | Encoder<float>& outputData, |
| 28 | const TensorShape& tensorShape, |
| 29 | uint32_t norm_size, |
| 30 | float alpha, |
| 31 | float beta, |
| 32 | float kappa) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 33 | { |
| 34 | const unsigned int batchSize = tensorShape[0]; |
| 35 | const unsigned int depth = tensorShape[1]; |
| 36 | const unsigned int rows = tensorShape[2]; |
| 37 | const unsigned int cols = tensorShape[3]; |
| 38 | |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 39 | int radius = armnn::numeric_cast<int>(norm_size / 2u); /* Strong Assumption on rounding Mode */ |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 40 | |
| 41 | for (unsigned int n = 0; n < batchSize; n++) |
| 42 | { |
| 43 | for (unsigned int c = 0; c < depth; c++) |
| 44 | { |
| 45 | for (unsigned int h = 0; h < rows; h++) |
| 46 | { |
| 47 | for (unsigned int w = 0; w < cols; w++) |
| 48 | { |
| 49 | float accumulated_scale = 0.0; |
| 50 | for (int y = -radius; y <= radius; y++) |
| 51 | { |
| 52 | for (int x = -radius; x <= radius; x++) |
| 53 | { |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 54 | int i = armnn::numeric_cast<int>(w) + x; |
| 55 | int j = armnn::numeric_cast<int>(h) + y; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 56 | |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 57 | if ((i < 0) || (i >= armnn::numeric_cast<int>(cols))) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 58 | { |
| 59 | continue; |
| 60 | } |
| 61 | |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 62 | if ((j < 0) || (j >= armnn::numeric_cast<int>(rows))) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 63 | { |
| 64 | continue; |
| 65 | } |
| 66 | |
Matteo Martincigh | 2fc70c5 | 2019-06-05 14:12:48 +0100 | [diff] [blame] | 67 | unsigned int inputIndex = n * cols * rows * depth + |
| 68 | c * cols * rows + |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 69 | armnn::numeric_cast<unsigned int>(j) * cols + |
| 70 | armnn::numeric_cast<unsigned int>(i); |
Matteo Martincigh | 2fc70c5 | 2019-06-05 14:12:48 +0100 | [diff] [blame] | 71 | inputData[inputIndex]; |
| 72 | float inval = inputData.Get(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 73 | |
| 74 | accumulated_scale += inval*inval; |
| 75 | } |
| 76 | } |
Matteo Martincigh | 2fc70c5 | 2019-06-05 14:12:48 +0100 | [diff] [blame] | 77 | |
| 78 | unsigned int index = n * cols * rows * depth + |
| 79 | c * cols * rows + |
| 80 | h * cols + |
| 81 | w; |
| 82 | inputData[index]; |
| 83 | outputData[index]; |
| 84 | outputData.Set(inputData.Get() / (powf((kappa + (accumulated_scale * alpha)), beta))); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 91 | // Helper function to compute "Across" normalization using Krichevsky 2012: Local Brightness Normalization. |
Matteo Martincigh | 2fc70c5 | 2019-06-05 14:12:48 +0100 | [diff] [blame] | 92 | void NormalizeAcrossUingLbr(Decoder<float>& inputData, |
| 93 | Encoder<float>& outputData, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 94 | const TensorShape& tensorShape, |
| 95 | uint32_t norm_size, |
| 96 | float alpha, |
| 97 | float beta, |
Matteo Martincigh | 8e6f92d | 2018-10-18 08:45:39 +0100 | [diff] [blame] | 98 | float kappa, |
| 99 | DataLayout dataLayout) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 100 | { |
Matteo Martincigh | 8e6f92d | 2018-10-18 08:45:39 +0100 | [diff] [blame] | 101 | DataLayoutIndexed dataLayoutIndexed(dataLayout); |
| 102 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 103 | const unsigned int batchSize = tensorShape[0]; |
Matteo Martincigh | 8e6f92d | 2018-10-18 08:45:39 +0100 | [diff] [blame] | 104 | const unsigned int depth = tensorShape[dataLayoutIndexed.GetChannelsIndex()]; |
| 105 | const unsigned int rows = tensorShape[dataLayoutIndexed.GetHeightIndex()]; |
| 106 | const unsigned int cols = tensorShape[dataLayoutIndexed.GetWidthIndex()]; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 107 | |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 108 | int radius = armnn::numeric_cast<int>(norm_size / 2u); /* Strong Assumption on rounding Mode */ |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 109 | |
| 110 | for (unsigned int n = 0; n < batchSize; n++) |
| 111 | { |
| 112 | for (unsigned int c = 0; c < depth; c++) |
| 113 | { |
| 114 | for (unsigned int h = 0; h < rows; h++) |
| 115 | { |
| 116 | for (unsigned int w = 0; w < cols; w++) |
| 117 | { |
| 118 | float accumulated_scale = 0.0; |
| 119 | for (int z = -radius; z <= radius; z++) |
| 120 | { |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 121 | int k = armnn::numeric_cast<int>(c) + z; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 122 | |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 123 | if ((k < 0) || (k >= armnn::numeric_cast<int>(depth))) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 124 | { |
| 125 | continue; |
| 126 | } |
| 127 | |
Matteo Martincigh | 2fc70c5 | 2019-06-05 14:12:48 +0100 | [diff] [blame] | 128 | unsigned inputIndex = dataLayoutIndexed.GetIndex(tensorShape, |
| 129 | n, |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 130 | armnn::numeric_cast<unsigned int>(k), |
Matteo Martincigh | 2fc70c5 | 2019-06-05 14:12:48 +0100 | [diff] [blame] | 131 | h, |
| 132 | w); |
| 133 | |
| 134 | inputData[inputIndex]; |
| 135 | float inval = inputData.Get(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 136 | |
Matteo Martincigh | 8e6f92d | 2018-10-18 08:45:39 +0100 | [diff] [blame] | 137 | accumulated_scale += inval * inval; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 138 | } |
Matteo Martincigh | 8e6f92d | 2018-10-18 08:45:39 +0100 | [diff] [blame] | 139 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 140 | float scale = kappa + (accumulated_scale * alpha); |
| 141 | scale = powf(scale, -beta); |
Matteo Martincigh | 8e6f92d | 2018-10-18 08:45:39 +0100 | [diff] [blame] | 142 | |
Matteo Martincigh | 2fc70c5 | 2019-06-05 14:12:48 +0100 | [diff] [blame] | 143 | unsigned index = dataLayoutIndexed.GetIndex(tensorShape, n, c, h, w); |
| 144 | |
| 145 | inputData[index]; |
| 146 | outputData[index]; |
| 147 | outputData.Set(scale * inputData.Get()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
Matteo Martincigh | 2fc70c5 | 2019-06-05 14:12:48 +0100 | [diff] [blame] | 154 | } // Anonymous namespace |
| 155 | |
| 156 | namespace armnn |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 157 | { |
Matteo Martincigh | 2fc70c5 | 2019-06-05 14:12:48 +0100 | [diff] [blame] | 158 | |
| 159 | RefNormalizationWorkload::RefNormalizationWorkload(const NormalizationQueueDescriptor& descriptor, |
| 160 | const WorkloadInfo& info) |
| 161 | : BaseWorkload(descriptor, info) |
| 162 | {} |
| 163 | |
| 164 | void RefNormalizationWorkload::Execute() const |
| 165 | { |
| 166 | ARMNN_SCOPED_PROFILING_EVENT(Compute::CpuRef, "RefNormalizationWorkload_Execute"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 167 | |
| 168 | const TensorInfo& inputInfo = GetTensorInfo(m_Data.m_Inputs[0]); |
| 169 | |
Matteo Martincigh | 2fc70c5 | 2019-06-05 14:12:48 +0100 | [diff] [blame] | 170 | auto inputDecoder = MakeDecoder<float>(inputInfo, m_Data.m_Inputs[0]->Map()); |
| 171 | auto outputEncoder = MakeEncoder<float>(inputInfo, m_Data.m_Outputs[0]->Map()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 172 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 173 | if (NormalizationAlgorithmMethod::LocalBrightness == m_Data.m_Parameters.m_NormMethodType) |
| 174 | { |
| 175 | if (NormalizationAlgorithmChannel::Within == m_Data.m_Parameters.m_NormChannelType) |
| 176 | { |
Matteo Martincigh | 2fc70c5 | 2019-06-05 14:12:48 +0100 | [diff] [blame] | 177 | NormalizeWithinUingLbr(*inputDecoder, |
| 178 | *outputEncoder, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 179 | inputInfo.GetShape(), |
| 180 | m_Data.m_Parameters.m_NormSize, |
| 181 | m_Data.m_Parameters.m_Alpha, |
| 182 | m_Data.m_Parameters.m_Beta, |
| 183 | m_Data.m_Parameters.m_K); |
| 184 | } |
| 185 | else if (NormalizationAlgorithmChannel::Across == m_Data.m_Parameters.m_NormChannelType) |
| 186 | { |
Matteo Martincigh | 2fc70c5 | 2019-06-05 14:12:48 +0100 | [diff] [blame] | 187 | NormalizeAcrossUingLbr(*inputDecoder, |
| 188 | *outputEncoder, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 189 | inputInfo.GetShape(), |
| 190 | m_Data.m_Parameters.m_NormSize, |
| 191 | m_Data.m_Parameters.m_Alpha, |
| 192 | m_Data.m_Parameters.m_Beta, |
Matteo Martincigh | 8e6f92d | 2018-10-18 08:45:39 +0100 | [diff] [blame] | 193 | m_Data.m_Parameters.m_K, |
| 194 | m_Data.m_Parameters.m_DataLayout); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 195 | } |
| 196 | else |
| 197 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 198 | ARMNN_LOG(warning) << "Illegal NORMALIZATION mode in normalization_f32"; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 199 | return; |
| 200 | } |
| 201 | } |
| 202 | else |
| 203 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 204 | ARMNN_LOG(warning) << "Lcr method (Jarret 2009: Local Contrast Normalization) not supported yet."; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 205 | return; |
| 206 | } |
| 207 | } |
| 208 | |
Matteo Martincigh | 2fc70c5 | 2019-06-05 14:12:48 +0100 | [diff] [blame] | 209 | } // namespace armnn |