Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 1 | /* |
Viet-Hoa Do | a3e57c2 | 2023-03-13 16:20:04 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2023 Arm Limited. |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +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 | #ifndef ARM_COMPUTE_TEST_FULLY_CONNECTED_LAYER_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_FULLY_CONNECTED_LAYER_FIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
| 29 | #include "arm_compute/core/Utils.h" |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 30 | #include "tests/AssetsLibrary.h" |
| 31 | #include "tests/Globals.h" |
| 32 | #include "tests/IAccessor.h" |
| 33 | #include "tests/RawTensor.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 34 | #include "tests/framework/Asserts.h" |
| 35 | #include "tests/framework/Fixture.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 36 | #include "tests/validation/Helpers.h" |
Giorgio Arena | 1856ff7 | 2020-02-07 13:46:45 +0000 | [diff] [blame] | 37 | #include "tests/validation/reference/ActivationLayer.h" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 38 | #include "tests/validation/reference/FullyConnectedLayer.h" |
| 39 | #include "tests/validation/reference/Utils.h" |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 40 | |
| 41 | #include <random> |
| 42 | |
| 43 | namespace arm_compute |
| 44 | { |
| 45 | namespace test |
| 46 | { |
| 47 | namespace validation |
| 48 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 49 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 50 | class FullyConnectedLayerValidationGenericFixture : public framework::Fixture |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 51 | { |
| 52 | public: |
Sang-Hoon Park | b66aa3b | 2020-01-10 14:44:13 +0000 | [diff] [blame] | 53 | using TDecay = typename std::decay<T>::type; |
| 54 | using TBias = typename std::conditional < (std::is_same<TDecay, uint8_t>::value || std::is_same<TDecay, int8_t>::value), int32_t, T >::type; |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 55 | |
| 56 | public: |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 57 | void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, bool transpose_weights, bool reshape_weights, |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 58 | DataType data_type, QuantizationInfo quantization_info, ActivationLayerInfo activation_info, bool mixed_layout = false) |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 59 | { |
| 60 | ARM_COMPUTE_UNUSED(weights_shape); |
| 61 | ARM_COMPUTE_UNUSED(bias_shape); |
| 62 | |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 63 | _mixed_layout = mixed_layout; |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 64 | _data_type = data_type; |
| 65 | _bias_data_type = is_data_type_quantized_asymmetric(data_type) ? DataType::S32 : data_type; |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 66 | _quantization_info = quantization_info; |
Giorgio Arena | 1856ff7 | 2020-02-07 13:46:45 +0000 | [diff] [blame] | 67 | _activation_info = activation_info; |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 68 | |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 69 | _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, transpose_weights, reshape_weights); |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 70 | _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | protected: |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 74 | void mix_layout(FunctionType &layer, TensorType &src, TensorType &dst) |
| 75 | { |
| 76 | const DataLayout data_layout = src.info()->data_layout(); |
| 77 | // Test Multi DataLayout graph cases, when the data layout changes after configure |
| 78 | src.info()->set_data_layout(data_layout == DataLayout::NCHW ? DataLayout::NHWC : DataLayout::NCHW); |
| 79 | dst.info()->set_data_layout(data_layout == DataLayout::NCHW ? DataLayout::NHWC : DataLayout::NCHW); |
| 80 | |
| 81 | // Compute Convolution function |
| 82 | layer.run(); |
| 83 | |
| 84 | // Reinstating original data layout for the test suite to properly check the values |
| 85 | src.info()->set_data_layout(data_layout); |
| 86 | dst.info()->set_data_layout(data_layout); |
| 87 | } |
| 88 | |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 89 | template <typename U> |
| 90 | void fill(U &&tensor, int i) |
| 91 | { |
Michele Di Giorgio | 9c70037 | 2020-01-08 11:33:44 +0000 | [diff] [blame] | 92 | if(_data_type == DataType::QASYMM8) |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 93 | { |
Pablo Tello | 29cab36 | 2022-03-10 17:05:34 +0000 | [diff] [blame] | 94 | std::uniform_int_distribution<uint32_t> distribution(0, 30); |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 95 | library->fill(tensor, distribution, i); |
| 96 | } |
Michele Di Giorgio | 9c70037 | 2020-01-08 11:33:44 +0000 | [diff] [blame] | 97 | else if(_data_type == DataType::QASYMM8_SIGNED) |
| 98 | { |
Pablo Tello | 29cab36 | 2022-03-10 17:05:34 +0000 | [diff] [blame] | 99 | std::uniform_int_distribution<int32_t> distribution(-15, 15); |
Michele Di Giorgio | 9c70037 | 2020-01-08 11:33:44 +0000 | [diff] [blame] | 100 | library->fill(tensor, distribution, i); |
| 101 | } |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 102 | else if(_data_type == DataType::S32) |
| 103 | { |
| 104 | std::uniform_int_distribution<int32_t> distribution(-50, 50); |
| 105 | library->fill(tensor, distribution, i); |
| 106 | } |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 107 | else if(_data_type == DataType::F16) |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 108 | { |
Giorgio Arena | 33b103b | 2021-01-08 10:37:15 +0000 | [diff] [blame] | 109 | arm_compute::utils::uniform_real_distribution_16bit<half> distribution(-1.0f, 1.0f); |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 110 | library->fill(tensor, distribution, i); |
| 111 | } |
| 112 | else if(_data_type == DataType::F32) |
| 113 | { |
| 114 | std::uniform_real_distribution<float> distribution(-1.0f, 1.0f); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 115 | library->fill(tensor, distribution, i); |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | library->fill_tensor_uniform(tensor, i); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | TensorType compute_target(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, bool transpose_weights, |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 124 | bool reshape_weights) |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 125 | { |
| 126 | TensorShape reshaped_weights_shape(weights_shape); |
| 127 | |
| 128 | // Test actions depending on the target settings |
| 129 | // |
| 130 | // | reshape | !reshape |
| 131 | // -----------+-----------+--------------------------- |
| 132 | // transpose | | *** |
| 133 | // -----------+-----------+--------------------------- |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 134 | // !transpose | transpose | transpose |
| 135 | // | | |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 136 | // |
| 137 | // ***: That combination is invalid. But we can ignore the transpose flag and handle all !reshape the same |
| 138 | if(!reshape_weights || !transpose_weights) |
| 139 | { |
| 140 | const size_t shape_x = reshaped_weights_shape.x(); |
| 141 | reshaped_weights_shape.set(0, reshaped_weights_shape.y()); |
| 142 | reshaped_weights_shape.set(1, shape_x); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | // Create tensors |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 146 | TensorType src = create_tensor<TensorType>(input_shape, _data_type, 1, _quantization_info); |
| 147 | TensorType weights = create_tensor<TensorType>(reshaped_weights_shape, _data_type, 1, _quantization_info); |
| 148 | TensorType bias = create_tensor<TensorType>(bias_shape, _bias_data_type, 1, _quantization_info); |
| 149 | TensorType dst = create_tensor<TensorType>(output_shape, _data_type, 1, _quantization_info); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 150 | |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 151 | // Create Fully Connected layer info |
| 152 | FullyConnectedLayerInfo fc_info; |
| 153 | fc_info.transpose_weights = transpose_weights; |
| 154 | fc_info.are_weights_reshaped = !reshape_weights; |
Giorgio Arena | 1856ff7 | 2020-02-07 13:46:45 +0000 | [diff] [blame] | 155 | fc_info.activation_info = _activation_info; |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 156 | |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 157 | // Create and configure function. |
| 158 | FunctionType fc; |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 159 | fc.configure(&src, &weights, &bias, &dst, fc_info); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 160 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 161 | ARM_COMPUTE_ASSERT(src.info()->is_resizable()); |
| 162 | ARM_COMPUTE_ASSERT(weights.info()->is_resizable()); |
| 163 | ARM_COMPUTE_ASSERT(bias.info()->is_resizable()); |
| 164 | ARM_COMPUTE_ASSERT(dst.info()->is_resizable()); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 165 | |
Giorgio Arena | 63825e8 | 2021-03-25 14:54:50 +0000 | [diff] [blame] | 166 | add_padding_x({ &src, &weights, &bias, &dst }); |
| 167 | |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 168 | // Allocate tensors |
| 169 | src.allocator()->allocate(); |
| 170 | weights.allocator()->allocate(); |
| 171 | bias.allocator()->allocate(); |
| 172 | dst.allocator()->allocate(); |
| 173 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 174 | ARM_COMPUTE_ASSERT(!src.info()->is_resizable()); |
| 175 | ARM_COMPUTE_ASSERT(!weights.info()->is_resizable()); |
| 176 | ARM_COMPUTE_ASSERT(!bias.info()->is_resizable()); |
| 177 | ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 178 | |
| 179 | // Fill tensors |
| 180 | fill(AccessorType(src), 0); |
| 181 | fill(AccessorType(bias), 2); |
| 182 | |
| 183 | if(!reshape_weights || !transpose_weights) |
| 184 | { |
| 185 | TensorShape tmp_shape(weights_shape); |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 186 | RawTensor tmp(tmp_shape, _data_type, 1); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 187 | |
| 188 | // Fill with original shape |
| 189 | fill(tmp, 1); |
| 190 | |
| 191 | // Transpose elementwise |
| 192 | tmp = transpose(tmp); |
| 193 | |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 194 | AccessorType weights_accessor(weights); |
| 195 | |
| 196 | for(int i = 0; i < tmp.num_elements(); ++i) |
| 197 | { |
| 198 | Coordinates coord = index2coord(tmp.shape(), i); |
| 199 | std::copy_n(static_cast<const RawTensor::value_type *>(tmp(coord)), |
| 200 | tmp.element_size(), |
| 201 | static_cast<RawTensor::value_type *>(weights_accessor(coord))); |
| 202 | } |
| 203 | } |
| 204 | else |
| 205 | { |
| 206 | fill(AccessorType(weights), 1); |
| 207 | } |
| 208 | |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 209 | if(_mixed_layout) |
| 210 | { |
| 211 | mix_layout(fc, src, dst); |
| 212 | } |
| 213 | else |
| 214 | { |
| 215 | // Compute NEFullyConnectedLayer function |
| 216 | fc.run(); |
| 217 | } |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 218 | |
| 219 | return dst; |
| 220 | } |
| 221 | |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 222 | SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape) |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 223 | { |
| 224 | // Create reference |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 225 | SimpleTensor<T> src{ input_shape, _data_type, 1, _quantization_info }; |
| 226 | SimpleTensor<T> weights{ weights_shape, _data_type, 1, _quantization_info }; |
| 227 | SimpleTensor<TBias> bias{ bias_shape, _bias_data_type, 1, _quantization_info }; |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 228 | |
| 229 | // Fill reference |
| 230 | fill(src, 0); |
| 231 | fill(weights, 1); |
| 232 | fill(bias, 2); |
| 233 | |
Giorgio Arena | 63e0beb | 2021-09-24 14:04:27 +0100 | [diff] [blame] | 234 | return reference::activation_layer(reference::fully_connected_layer<T>(src, weights, bias, output_shape, _quantization_info), _activation_info, _quantization_info); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 235 | } |
| 236 | |
Giorgio Arena | 1856ff7 | 2020-02-07 13:46:45 +0000 | [diff] [blame] | 237 | TensorType _target{}; |
| 238 | SimpleTensor<T> _reference{}; |
| 239 | DataType _data_type{}; |
| 240 | DataType _bias_data_type{}; |
Giorgio Arena | 63825e8 | 2021-03-25 14:54:50 +0000 | [diff] [blame] | 241 | bool _mixed_layout{ false }; |
Giorgio Arena | 1856ff7 | 2020-02-07 13:46:45 +0000 | [diff] [blame] | 242 | QuantizationInfo _quantization_info{}; |
| 243 | ActivationLayerInfo _activation_info{}; |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 244 | }; |
| 245 | |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 246 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool mixed_layout = false> |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 247 | class FullyConnectedLayerValidationFixture : public FullyConnectedLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 248 | { |
| 249 | public: |
Giorgio Arena | 1856ff7 | 2020-02-07 13:46:45 +0000 | [diff] [blame] | 250 | void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, bool transpose_weights, bool reshape_weights, DataType data_type, |
| 251 | ActivationLayerInfo activation_info) |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 252 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 253 | FullyConnectedLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, transpose_weights, |
| 254 | reshape_weights, data_type, |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 255 | QuantizationInfo(), activation_info, mixed_layout); |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 256 | } |
| 257 | }; |
| 258 | |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 259 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool mixed_layout = false> |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 260 | class FullyConnectedLayerValidationQuantizedFixture : public FullyConnectedLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 261 | { |
| 262 | public: |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 263 | void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, bool transpose_weights, bool reshape_weights, DataType data_type, |
Giorgio Arena | 1856ff7 | 2020-02-07 13:46:45 +0000 | [diff] [blame] | 264 | QuantizationInfo quantization_info, ActivationLayerInfo activation_info) |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 265 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 266 | FullyConnectedLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, transpose_weights, |
| 267 | reshape_weights, data_type, |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 268 | quantization_info, activation_info, mixed_layout); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 269 | } |
| 270 | }; |
Georgios Pinitas | 72ee9b4 | 2021-05-10 22:26:37 +0100 | [diff] [blame] | 271 | |
| 272 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Giorgio Arena | 63e0beb | 2021-09-24 14:04:27 +0100 | [diff] [blame] | 273 | class FullyConnectedWithDynamicTensorsFixture : public framework::Fixture |
Georgios Pinitas | 72ee9b4 | 2021-05-10 22:26:37 +0100 | [diff] [blame] | 274 | { |
| 275 | private: |
| 276 | template <typename U> |
| 277 | void fill(U &&tensor, int i) |
| 278 | { |
| 279 | if(_data_type == DataType::F16) |
| 280 | { |
| 281 | arm_compute::utils::uniform_real_distribution_16bit<half> distribution(-1.0f, 1.0f); |
| 282 | library->fill(tensor, distribution, i); |
| 283 | } |
| 284 | else if(_data_type == DataType::F32) |
| 285 | { |
| 286 | std::uniform_real_distribution<float> distribution(-1.0f, 1.0f); |
| 287 | library->fill(tensor, distribution, i); |
| 288 | } |
Giorgio Arena | 63e0beb | 2021-09-24 14:04:27 +0100 | [diff] [blame] | 289 | else if(_data_type == DataType::QASYMM8) |
| 290 | { |
Pablo Tello | 29cab36 | 2022-03-10 17:05:34 +0000 | [diff] [blame] | 291 | std::uniform_int_distribution<uint32_t> distribution(0, 30); |
Giorgio Arena | 63e0beb | 2021-09-24 14:04:27 +0100 | [diff] [blame] | 292 | library->fill(tensor, distribution, i); |
| 293 | } |
| 294 | else if(_data_type == DataType::S32) |
| 295 | { |
| 296 | std::uniform_int_distribution<int32_t> distribution(-50, 50); |
| 297 | library->fill(tensor, distribution, i); |
| 298 | } |
Georgios Pinitas | 72ee9b4 | 2021-05-10 22:26:37 +0100 | [diff] [blame] | 299 | else |
| 300 | { |
| 301 | library->fill_tensor_uniform(tensor, i); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | void fill_transposed_weights(TensorType &weights, TensorShape weights_shape, int seed) |
| 306 | { |
| 307 | RawTensor tmp(weights_shape, _data_type, 1); |
| 308 | |
| 309 | // Fill with original shape |
| 310 | fill(tmp, seed); |
| 311 | |
| 312 | // Transpose elementwise |
| 313 | tmp = transpose(tmp); |
| 314 | |
| 315 | AccessorType weights_accessor(weights); |
| 316 | |
| 317 | for(int i = 0; i < tmp.num_elements(); ++i) |
| 318 | { |
| 319 | Coordinates coord = index2coord(tmp.shape(), i); |
| 320 | std::copy_n(static_cast<const RawTensor::value_type *>(tmp(coord)), |
| 321 | tmp.element_size(), |
| 322 | static_cast<RawTensor::value_type *>(weights_accessor(coord))); |
| 323 | } |
| 324 | } |
| 325 | |
Viet-Hoa Do | a3e57c2 | 2023-03-13 16:20:04 +0000 | [diff] [blame] | 326 | void validate_with_tolerance(TensorType &target, SimpleTensor<float> &ref) |
Georgios Pinitas | 72ee9b4 | 2021-05-10 22:26:37 +0100 | [diff] [blame] | 327 | { |
Viet-Hoa Do | a3e57c2 | 2023-03-13 16:20:04 +0000 | [diff] [blame] | 328 | constexpr RelativeTolerance<float> rel_tolerance_f32(0.01f); |
| 329 | constexpr AbsoluteTolerance<float> abs_tolerance_f32(0.001f); |
| 330 | validate(AccessorType(target), ref, rel_tolerance_f32, 0, abs_tolerance_f32); |
| 331 | } |
| 332 | |
Viet-Hoa Do | a3e57c2 | 2023-03-13 16:20:04 +0000 | [diff] [blame] | 333 | void validate_with_tolerance(TensorType &target, SimpleTensor<half_float::half> &ref) |
| 334 | { |
Mohammed Suhail Munshi | a2bb80e | 2023-06-19 14:57:57 +0100 | [diff] [blame] | 335 | constexpr AbsoluteTolerance<float> abs_tolerance_f16(0.3f); |
Viet-Hoa Do | a3e57c2 | 2023-03-13 16:20:04 +0000 | [diff] [blame] | 336 | const RelativeTolerance<half_float::half> rel_tolerance_f16(half_float::half(0.2f)); |
Mohammed Suhail Munshi | a2bb80e | 2023-06-19 14:57:57 +0100 | [diff] [blame] | 337 | constexpr float tolerance_num_f16 = 0.07f; |
Viet-Hoa Do | a3e57c2 | 2023-03-13 16:20:04 +0000 | [diff] [blame] | 338 | |
| 339 | validate(AccessorType(target), ref, rel_tolerance_f16, tolerance_num_f16, abs_tolerance_f16); |
| 340 | } |
Viet-Hoa Do | a3e57c2 | 2023-03-13 16:20:04 +0000 | [diff] [blame] | 341 | |
| 342 | void validate_with_tolerance(TensorType &target, SimpleTensor<uint8_t> &ref) |
| 343 | { |
| 344 | constexpr AbsoluteTolerance<uint32_t> tolerance_qasymm8(1); |
| 345 | validate(AccessorType(target), ref, tolerance_qasymm8); |
| 346 | } |
| 347 | |
| 348 | void validate_with_tolerance(TensorType &target, SimpleTensor<int8_t> &ref) |
| 349 | { |
| 350 | constexpr AbsoluteTolerance<uint32_t> tolerance_qasymm8_signed(1); |
| 351 | validate(AccessorType(target), ref, tolerance_qasymm8_signed); |
Georgios Pinitas | 72ee9b4 | 2021-05-10 22:26:37 +0100 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | public: |
Giorgio Arena | 63e0beb | 2021-09-24 14:04:27 +0100 | [diff] [blame] | 355 | using TDecay = typename std::decay<T>::type; |
| 356 | using TBias = typename std::conditional < (std::is_same<TDecay, uint8_t>::value || std::is_same<TDecay, int8_t>::value), int32_t, T >::type; |
| 357 | |
Georgios Pinitas | 72ee9b4 | 2021-05-10 22:26:37 +0100 | [diff] [blame] | 358 | void setup(TensorShape src_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape dst_shape, |
Mohammed Suhail Munshi | a2bb80e | 2023-06-19 14:57:57 +0100 | [diff] [blame] | 359 | DataType data_type, ActivationLayerInfo activation_info, bool constant_weights, bool constant_bias, bool weights_reshaped, bool remove_bias = false) |
Georgios Pinitas | 72ee9b4 | 2021-05-10 22:26:37 +0100 | [diff] [blame] | 360 | { |
| 361 | _data_type = data_type; |
| 362 | |
Mohammed Suhail Munshi | a2bb80e | 2023-06-19 14:57:57 +0100 | [diff] [blame] | 363 | const bool is_quantized = is_data_type_quantized(data_type); |
Giorgio Arena | 63e0beb | 2021-09-24 14:04:27 +0100 | [diff] [blame] | 364 | const DataType bias_data_type = (is_quantized) ? DataType::S32 : data_type; |
| 365 | |
| 366 | const QuantizationInfo src_qinfo = is_quantized ? QuantizationInfo(0.1f, 10) : QuantizationInfo(); |
| 367 | const QuantizationInfo weights_qinfo = is_quantized ? QuantizationInfo(0.3f, 20) : QuantizationInfo(); |
| 368 | const QuantizationInfo dst_qinfo = is_quantized ? QuantizationInfo(0.2f, 5) : QuantizationInfo(); |
| 369 | |
Mohammed Suhail Munshi | a2bb80e | 2023-06-19 14:57:57 +0100 | [diff] [blame] | 370 | // Configure TensorInfo Objects |
Giorgio Arena | 63e0beb | 2021-09-24 14:04:27 +0100 | [diff] [blame] | 371 | const TensorInfo src_info(src_shape, 1, data_type, src_qinfo); |
Mohammed Suhail Munshi | a2bb80e | 2023-06-19 14:57:57 +0100 | [diff] [blame] | 372 | const TensorInfo dst_info(dst_shape, 1, data_type, dst_qinfo); |
| 373 | TensorInfo bias_info(bias_shape, 1, bias_data_type); |
| 374 | TensorInfo wei_info(weights_shape, 1, data_type, weights_qinfo); |
Georgios Pinitas | 72ee9b4 | 2021-05-10 22:26:37 +0100 | [diff] [blame] | 375 | |
Viet-Hoa Do | a3e57c2 | 2023-03-13 16:20:04 +0000 | [diff] [blame] | 376 | if(!constant_weights && weights_reshaped) |
Giorgio Arena | 63e0beb | 2021-09-24 14:04:27 +0100 | [diff] [blame] | 377 | { |
| 378 | const TensorShape tr_weights_shape{ weights_shape[1], weights_shape[0] }; |
| 379 | wei_info.set_tensor_shape(tr_weights_shape); |
| 380 | } |
| 381 | wei_info.set_are_values_constant(constant_weights); |
Giorgio Arena | 63e0beb | 2021-09-24 14:04:27 +0100 | [diff] [blame] | 382 | bias_info.set_are_values_constant(constant_bias); |
Georgios Pinitas | 72ee9b4 | 2021-05-10 22:26:37 +0100 | [diff] [blame] | 383 | |
Mohammed Suhail Munshi | a2bb80e | 2023-06-19 14:57:57 +0100 | [diff] [blame] | 384 | // Initialise Tensors |
| 385 | _src.allocator()->init(src_info); |
| 386 | _weights.allocator()->init(wei_info); |
| 387 | if(!remove_bias) |
| 388 | _bias.allocator()->init(bias_info); |
Georgios Pinitas | 72ee9b4 | 2021-05-10 22:26:37 +0100 | [diff] [blame] | 389 | _dst.allocator()->init(dst_info); |
| 390 | |
| 391 | // Configure FC layer and mark the weights as non constant |
| 392 | FullyConnectedLayerInfo fc_info; |
Giorgio Arena | 63e0beb | 2021-09-24 14:04:27 +0100 | [diff] [blame] | 393 | fc_info.activation_info = activation_info; |
| 394 | if(!constant_weights) |
| 395 | { |
Viet-Hoa Do | a3e57c2 | 2023-03-13 16:20:04 +0000 | [diff] [blame] | 396 | fc_info.are_weights_reshaped = weights_reshaped; |
| 397 | fc_info.transpose_weights = !weights_reshaped; |
Giorgio Arena | 63e0beb | 2021-09-24 14:04:27 +0100 | [diff] [blame] | 398 | } |
Georgios Pinitas | 72ee9b4 | 2021-05-10 22:26:37 +0100 | [diff] [blame] | 399 | FunctionType fc; |
Mohammed Suhail Munshi | a2bb80e | 2023-06-19 14:57:57 +0100 | [diff] [blame] | 400 | fc.configure(&_src, &_weights, (remove_bias) ? nullptr : &_bias, &_dst, fc_info); |
Georgios Pinitas | 72ee9b4 | 2021-05-10 22:26:37 +0100 | [diff] [blame] | 401 | |
| 402 | // Allocate all the tensors |
| 403 | _src.allocator()->allocate(); |
| 404 | _weights.allocator()->allocate(); |
Mohammed Suhail Munshi | a2bb80e | 2023-06-19 14:57:57 +0100 | [diff] [blame] | 405 | if(!remove_bias) |
| 406 | _bias.allocator()->allocate(); |
Georgios Pinitas | 72ee9b4 | 2021-05-10 22:26:37 +0100 | [diff] [blame] | 407 | _dst.allocator()->allocate(); |
| 408 | |
| 409 | // Run multiple iterations with different inputs |
| 410 | constexpr int num_iterations = 5; |
| 411 | int randomizer_offset = 0; |
Giorgio Arena | 63e0beb | 2021-09-24 14:04:27 +0100 | [diff] [blame] | 412 | |
| 413 | // Create reference tensors |
| 414 | SimpleTensor<T> src{ src_shape, data_type, 1, src_qinfo }; |
| 415 | SimpleTensor<T> weights{ weights_shape, data_type, 1, weights_qinfo }; |
| 416 | SimpleTensor<TBias> bias{ bias_shape, bias_data_type }; |
| 417 | |
| 418 | // Fill weights and/or bias if they remain constant |
| 419 | if(constant_weights) |
| 420 | { |
| 421 | fill(AccessorType(_weights), 1); |
| 422 | fill(weights, 1); |
| 423 | } |
Mohammed Suhail Munshi | a2bb80e | 2023-06-19 14:57:57 +0100 | [diff] [blame] | 424 | if(constant_bias && !remove_bias) |
Giorgio Arena | 63e0beb | 2021-09-24 14:04:27 +0100 | [diff] [blame] | 425 | { |
| 426 | fill(AccessorType(_bias), 2); |
| 427 | fill(bias, 2); |
| 428 | } |
Mohammed Suhail Munshi | a2bb80e | 2023-06-19 14:57:57 +0100 | [diff] [blame] | 429 | // To remove bias, fill with 0 |
| 430 | if(remove_bias && is_quantized) |
| 431 | { |
| 432 | library->fill_tensor_value(bias, 0); |
| 433 | } |
| 434 | else if(remove_bias) |
| 435 | { |
| 436 | library->fill_tensor_value(bias, (float)0.0); |
| 437 | } |
Giorgio Arena | 63e0beb | 2021-09-24 14:04:27 +0100 | [diff] [blame] | 438 | |
Georgios Pinitas | 72ee9b4 | 2021-05-10 22:26:37 +0100 | [diff] [blame] | 439 | for(int i = 0; i < num_iterations; ++i) |
| 440 | { |
| 441 | // Run target |
| 442 | { |
| 443 | fill(AccessorType(_src), randomizer_offset); |
Giorgio Arena | 63e0beb | 2021-09-24 14:04:27 +0100 | [diff] [blame] | 444 | if(!constant_weights) |
| 445 | { |
Viet-Hoa Do | a3e57c2 | 2023-03-13 16:20:04 +0000 | [diff] [blame] | 446 | if(weights_reshaped) |
| 447 | { |
| 448 | fill_transposed_weights(_weights, weights_shape, randomizer_offset + 1); |
| 449 | } |
| 450 | else |
| 451 | { |
| 452 | fill(AccessorType(_weights), randomizer_offset + 1); |
| 453 | } |
Giorgio Arena | 63e0beb | 2021-09-24 14:04:27 +0100 | [diff] [blame] | 454 | } |
Mohammed Suhail Munshi | a2bb80e | 2023-06-19 14:57:57 +0100 | [diff] [blame] | 455 | if(!constant_bias && !remove_bias) |
Giorgio Arena | 63e0beb | 2021-09-24 14:04:27 +0100 | [diff] [blame] | 456 | { |
| 457 | fill(AccessorType(_bias), randomizer_offset + 2); |
| 458 | } |
Georgios Pinitas | 72ee9b4 | 2021-05-10 22:26:37 +0100 | [diff] [blame] | 459 | |
| 460 | fc.run(); |
| 461 | } |
| 462 | |
| 463 | // Run reference and compare |
| 464 | { |
Georgios Pinitas | 72ee9b4 | 2021-05-10 22:26:37 +0100 | [diff] [blame] | 465 | // Fill reference |
| 466 | fill(src, randomizer_offset); |
Giorgio Arena | 63e0beb | 2021-09-24 14:04:27 +0100 | [diff] [blame] | 467 | if(!constant_weights) |
| 468 | { |
| 469 | fill(weights, randomizer_offset + 1); |
| 470 | } |
Mohammed Suhail Munshi | a2bb80e | 2023-06-19 14:57:57 +0100 | [diff] [blame] | 471 | if(!constant_bias && !remove_bias) |
Giorgio Arena | 63e0beb | 2021-09-24 14:04:27 +0100 | [diff] [blame] | 472 | { |
| 473 | fill(bias, randomizer_offset + 2); |
| 474 | } |
Georgios Pinitas | 72ee9b4 | 2021-05-10 22:26:37 +0100 | [diff] [blame] | 475 | |
Giorgio Arena | 63e0beb | 2021-09-24 14:04:27 +0100 | [diff] [blame] | 476 | auto dst = reference::activation_layer(reference::fully_connected_layer<T>(src, weights, bias, dst_shape, dst_qinfo), activation_info, dst_qinfo); |
Georgios Pinitas | 72ee9b4 | 2021-05-10 22:26:37 +0100 | [diff] [blame] | 477 | |
| 478 | // Validate |
| 479 | validate_with_tolerance(_dst, dst); |
| 480 | } |
| 481 | |
| 482 | randomizer_offset += 100; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | private: |
| 487 | TensorType _src{}, _weights{}, _bias{}, _dst{}; |
| 488 | DataType _data_type{ DataType::UNKNOWN }; |
| 489 | }; |
Giorgio Arena | 63e0beb | 2021-09-24 14:04:27 +0100 | [diff] [blame] | 490 | |
| 491 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 492 | class FullyConnectedWithDynamicWeightsFixture : public FullyConnectedWithDynamicTensorsFixture<TensorType, AccessorType, FunctionType, T> |
| 493 | { |
| 494 | public: |
Giorgio Arena | 63e0beb | 2021-09-24 14:04:27 +0100 | [diff] [blame] | 495 | void setup(TensorShape src_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape dst_shape, |
Viet-Hoa Do | a3e57c2 | 2023-03-13 16:20:04 +0000 | [diff] [blame] | 496 | DataType data_type, ActivationLayerInfo activation_info, bool weights_reshaped) |
Giorgio Arena | 63e0beb | 2021-09-24 14:04:27 +0100 | [diff] [blame] | 497 | { |
| 498 | FullyConnectedWithDynamicTensorsFixture<TensorType, AccessorType, FunctionType, T>::setup(src_shape, weights_shape, bias_shape, |
Mohammed Suhail Munshi | a2bb80e | 2023-06-19 14:57:57 +0100 | [diff] [blame] | 499 | dst_shape, data_type, activation_info, false, true, weights_reshaped, false); |
| 500 | } |
| 501 | }; |
| 502 | |
| 503 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 504 | class FullyConnectedDynamicNoBiasFixture : public FullyConnectedWithDynamicTensorsFixture<TensorType, AccessorType, FunctionType, T> |
| 505 | { |
| 506 | public: |
Mohammed Suhail Munshi | a2bb80e | 2023-06-19 14:57:57 +0100 | [diff] [blame] | 507 | void setup(TensorShape src_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape dst_shape, |
| 508 | DataType data_type, ActivationLayerInfo activation_info, bool weights_reshaped) |
| 509 | { |
| 510 | FullyConnectedWithDynamicTensorsFixture<TensorType, AccessorType, FunctionType, T>::setup(src_shape, weights_shape, bias_shape, |
| 511 | dst_shape, data_type, activation_info, false, true, weights_reshaped, true); |
Giorgio Arena | 63e0beb | 2021-09-24 14:04:27 +0100 | [diff] [blame] | 512 | } |
| 513 | }; |
| 514 | |
| 515 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 516 | class FullyConnectedWithDynamicBiasFixture : public FullyConnectedWithDynamicTensorsFixture<TensorType, AccessorType, FunctionType, T> |
| 517 | { |
| 518 | public: |
Giorgio Arena | 63e0beb | 2021-09-24 14:04:27 +0100 | [diff] [blame] | 519 | void setup(TensorShape src_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape dst_shape, |
| 520 | DataType data_type, ActivationLayerInfo activation_info) |
| 521 | { |
| 522 | FullyConnectedWithDynamicTensorsFixture<TensorType, AccessorType, FunctionType, T>::setup(src_shape, weights_shape, bias_shape, |
Mohammed Suhail Munshi | a2bb80e | 2023-06-19 14:57:57 +0100 | [diff] [blame] | 523 | dst_shape, data_type, activation_info, true, false, false, false /* weights_reshaped (not used) */); |
Giorgio Arena | 63e0beb | 2021-09-24 14:04:27 +0100 | [diff] [blame] | 524 | } |
| 525 | }; |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 526 | } // namespace validation |
| 527 | } // namespace test |
| 528 | } // namespace arm_compute |
| 529 | #endif /* ARM_COMPUTE_TEST_FULLY_CONNECTED_LAYER_FIXTURE */ |