Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | 9c70037 | 2020-01-08 11:33:44 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 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" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 37 | #include "tests/validation/reference/FullyConnectedLayer.h" |
| 38 | #include "tests/validation/reference/Utils.h" |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 39 | |
| 40 | #include <random> |
| 41 | |
| 42 | namespace arm_compute |
| 43 | { |
| 44 | namespace test |
| 45 | { |
| 46 | namespace validation |
| 47 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 48 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 49 | class FullyConnectedLayerValidationGenericFixture : public framework::Fixture |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 50 | { |
| 51 | public: |
Sang-Hoon Park | b66aa3b | 2020-01-10 14:44:13 +0000 | [diff] [blame] | 52 | using TDecay = typename std::decay<T>::type; |
| 53 | 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] | 54 | |
| 55 | public: |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 56 | template <typename...> |
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, |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 58 | DataType data_type, QuantizationInfo quantization_info) |
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 | |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 63 | _data_type = data_type; |
| 64 | _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] | 65 | _quantization_info = quantization_info; |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 66 | |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 67 | _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] | 68 | _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | protected: |
| 72 | template <typename U> |
| 73 | void fill(U &&tensor, int i) |
| 74 | { |
Michele Di Giorgio | 9c70037 | 2020-01-08 11:33:44 +0000 | [diff] [blame] | 75 | if(_data_type == DataType::QASYMM8) |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 76 | { |
| 77 | std::uniform_int_distribution<uint8_t> distribution(0, 30); |
| 78 | library->fill(tensor, distribution, i); |
| 79 | } |
Michele Di Giorgio | 9c70037 | 2020-01-08 11:33:44 +0000 | [diff] [blame] | 80 | else if(_data_type == DataType::QASYMM8_SIGNED) |
| 81 | { |
| 82 | std::uniform_int_distribution<int8_t> distribution(-15, 15); |
| 83 | library->fill(tensor, distribution, i); |
| 84 | } |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 85 | else if(_data_type == DataType::S32) |
| 86 | { |
| 87 | std::uniform_int_distribution<int32_t> distribution(-50, 50); |
| 88 | library->fill(tensor, distribution, i); |
| 89 | } |
| 90 | else if(is_data_type_float(_data_type)) |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 91 | { |
Gian Marco Iodice | 35aea37 | 2018-08-24 14:30:36 +0100 | [diff] [blame] | 92 | std::uniform_real_distribution<> distribution(-1.0f, 1.0f); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 93 | library->fill(tensor, distribution, i); |
| 94 | } |
| 95 | else |
| 96 | { |
| 97 | library->fill_tensor_uniform(tensor, i); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | 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] | 102 | bool reshape_weights) |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 103 | { |
| 104 | TensorShape reshaped_weights_shape(weights_shape); |
| 105 | |
| 106 | // Test actions depending on the target settings |
| 107 | // |
| 108 | // | reshape | !reshape |
| 109 | // -----------+-----------+--------------------------- |
| 110 | // transpose | | *** |
| 111 | // -----------+-----------+--------------------------- |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 112 | // !transpose | transpose | transpose |
| 113 | // | | |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 114 | // |
| 115 | // ***: That combination is invalid. But we can ignore the transpose flag and handle all !reshape the same |
| 116 | if(!reshape_weights || !transpose_weights) |
| 117 | { |
| 118 | const size_t shape_x = reshaped_weights_shape.x(); |
| 119 | reshaped_weights_shape.set(0, reshaped_weights_shape.y()); |
| 120 | reshaped_weights_shape.set(1, shape_x); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | // Create tensors |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 124 | TensorType src = create_tensor<TensorType>(input_shape, _data_type, 1, _quantization_info); |
| 125 | TensorType weights = create_tensor<TensorType>(reshaped_weights_shape, _data_type, 1, _quantization_info); |
| 126 | TensorType bias = create_tensor<TensorType>(bias_shape, _bias_data_type, 1, _quantization_info); |
| 127 | TensorType dst = create_tensor<TensorType>(output_shape, _data_type, 1, _quantization_info); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 128 | |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 129 | // Create Fully Connected layer info |
| 130 | FullyConnectedLayerInfo fc_info; |
| 131 | fc_info.transpose_weights = transpose_weights; |
| 132 | fc_info.are_weights_reshaped = !reshape_weights; |
| 133 | |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 134 | // Create and configure function. |
| 135 | FunctionType fc; |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 136 | fc.configure(&src, &weights, &bias, &dst, fc_info); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 137 | |
| 138 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 139 | ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 140 | ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 141 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 142 | |
| 143 | // Allocate tensors |
| 144 | src.allocator()->allocate(); |
| 145 | weights.allocator()->allocate(); |
| 146 | bias.allocator()->allocate(); |
| 147 | dst.allocator()->allocate(); |
| 148 | |
| 149 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 150 | ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 151 | ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 152 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 153 | |
| 154 | // Fill tensors |
| 155 | fill(AccessorType(src), 0); |
| 156 | fill(AccessorType(bias), 2); |
| 157 | |
| 158 | if(!reshape_weights || !transpose_weights) |
| 159 | { |
| 160 | TensorShape tmp_shape(weights_shape); |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 161 | RawTensor tmp(tmp_shape, _data_type, 1); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 162 | |
| 163 | // Fill with original shape |
| 164 | fill(tmp, 1); |
| 165 | |
| 166 | // Transpose elementwise |
| 167 | tmp = transpose(tmp); |
| 168 | |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 169 | AccessorType weights_accessor(weights); |
| 170 | |
| 171 | for(int i = 0; i < tmp.num_elements(); ++i) |
| 172 | { |
| 173 | Coordinates coord = index2coord(tmp.shape(), i); |
| 174 | std::copy_n(static_cast<const RawTensor::value_type *>(tmp(coord)), |
| 175 | tmp.element_size(), |
| 176 | static_cast<RawTensor::value_type *>(weights_accessor(coord))); |
| 177 | } |
| 178 | } |
| 179 | else |
| 180 | { |
| 181 | fill(AccessorType(weights), 1); |
| 182 | } |
| 183 | |
| 184 | // Compute NEFullyConnectedLayer function |
| 185 | fc.run(); |
| 186 | |
| 187 | return dst; |
| 188 | } |
| 189 | |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 190 | 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] | 191 | { |
| 192 | // Create reference |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 193 | SimpleTensor<T> src{ input_shape, _data_type, 1, _quantization_info }; |
| 194 | SimpleTensor<T> weights{ weights_shape, _data_type, 1, _quantization_info }; |
| 195 | SimpleTensor<TBias> bias{ bias_shape, _bias_data_type, 1, _quantization_info }; |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 196 | |
| 197 | // Fill reference |
| 198 | fill(src, 0); |
| 199 | fill(weights, 1); |
| 200 | fill(bias, 2); |
| 201 | |
| 202 | return reference::fully_connected_layer<T>(src, weights, bias, output_shape); |
| 203 | } |
| 204 | |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 205 | TensorType _target{}; |
| 206 | SimpleTensor<T> _reference{}; |
| 207 | DataType _data_type{}; |
| 208 | DataType _bias_data_type{}; |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 209 | QuantizationInfo _quantization_info{}; |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 210 | }; |
| 211 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 212 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 213 | class FullyConnectedLayerValidationFixture : public FullyConnectedLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 214 | { |
| 215 | public: |
| 216 | template <typename...> |
| 217 | void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, bool transpose_weights, bool reshape_weights, DataType data_type) |
| 218 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 219 | FullyConnectedLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, transpose_weights, |
| 220 | reshape_weights, data_type, |
| 221 | QuantizationInfo()); |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 222 | } |
| 223 | }; |
| 224 | |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 225 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 226 | class FullyConnectedLayerValidationQuantizedFixture : public FullyConnectedLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 227 | { |
| 228 | public: |
| 229 | template <typename...> |
| 230 | void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, bool transpose_weights, bool reshape_weights, DataType data_type, |
| 231 | QuantizationInfo quantization_info) |
| 232 | { |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 233 | FullyConnectedLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, transpose_weights, |
| 234 | reshape_weights, data_type, |
| 235 | quantization_info); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 236 | } |
| 237 | }; |
| 238 | } // namespace validation |
| 239 | } // namespace test |
| 240 | } // namespace arm_compute |
| 241 | #endif /* ARM_COMPUTE_TEST_FULLY_CONNECTED_LAYER_FIXTURE */ |