Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 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 | { |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 48 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool run_interleave> |
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: |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 52 | using TBias = typename std::conditional<std::is_same<typename std::decay<T>::type, uint8_t>::value, int32_t, T>::type; |
| 53 | |
| 54 | public: |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 55 | template <typename...> |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 56 | void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, bool transpose_weights, bool reshape_weights, |
| 57 | DataType data_type, int fractional_bits, QuantizationInfo quantization_info) |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 58 | { |
| 59 | ARM_COMPUTE_UNUSED(weights_shape); |
| 60 | ARM_COMPUTE_UNUSED(bias_shape); |
| 61 | |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 62 | _data_type = data_type; |
| 63 | _bias_data_type = is_data_type_quantized_asymmetric(data_type) ? DataType::S32 : data_type; |
| 64 | _fractional_bits = fractional_bits; |
| 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); |
| 68 | _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, transpose_weights, reshape_weights); |
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 | { |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 75 | if(is_data_type_quantized_asymmetric(_data_type)) |
| 76 | { |
| 77 | std::uniform_int_distribution<uint8_t> distribution(0, 30); |
| 78 | library->fill(tensor, distribution, i); |
| 79 | } |
| 80 | else if(_data_type == DataType::S32) |
| 81 | { |
| 82 | std::uniform_int_distribution<int32_t> distribution(-50, 50); |
| 83 | library->fill(tensor, distribution, i); |
| 84 | } |
| 85 | else if(is_data_type_float(_data_type)) |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 86 | { |
| 87 | std::uniform_real_distribution<> distribution(0.5f, 1.f); |
| 88 | library->fill(tensor, distribution, i); |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | library->fill_tensor_uniform(tensor, i); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | 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] | 97 | bool reshape_weights) |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 98 | { |
| 99 | TensorShape reshaped_weights_shape(weights_shape); |
| 100 | |
| 101 | // Test actions depending on the target settings |
| 102 | // |
| 103 | // | reshape | !reshape |
| 104 | // -----------+-----------+--------------------------- |
| 105 | // transpose | | *** |
| 106 | // -----------+-----------+--------------------------- |
| 107 | // !transpose | transpose | transpose & |
| 108 | // | | transpose1xW (if required) |
| 109 | // |
| 110 | // ***: That combination is invalid. But we can ignore the transpose flag and handle all !reshape the same |
| 111 | if(!reshape_weights || !transpose_weights) |
| 112 | { |
| 113 | const size_t shape_x = reshaped_weights_shape.x(); |
| 114 | reshaped_weights_shape.set(0, reshaped_weights_shape.y()); |
| 115 | reshaped_weights_shape.set(1, shape_x); |
| 116 | |
| 117 | // Weights have to be passed reshaped |
| 118 | // Transpose 1xW for batched version |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 119 | if(!reshape_weights && output_shape.y() > 1 && run_interleave) |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 120 | { |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 121 | const int transpose_width = 16 / data_size_from_type(_data_type); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 122 | const float shape_x = reshaped_weights_shape.x(); |
| 123 | reshaped_weights_shape.set(0, reshaped_weights_shape.y() * transpose_width); |
| 124 | reshaped_weights_shape.set(1, static_cast<unsigned int>(std::ceil(shape_x / transpose_width))); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // Create tensors |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 129 | TensorType src = create_tensor<TensorType>(input_shape, _data_type, 1, _fractional_bits, _quantization_info); |
| 130 | TensorType weights = create_tensor<TensorType>(reshaped_weights_shape, _data_type, 1, _fractional_bits, _quantization_info); |
| 131 | TensorType bias = create_tensor<TensorType>(bias_shape, _bias_data_type, 1, _fractional_bits, _quantization_info); |
| 132 | TensorType dst = create_tensor<TensorType>(output_shape, _data_type, 1, _fractional_bits, _quantization_info); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 133 | |
| 134 | // Create and configure function. |
| 135 | FunctionType fc; |
| 136 | fc.configure(&src, &weights, &bias, &dst, transpose_weights, !reshape_weights); |
| 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); |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 161 | RawTensor tmp(tmp_shape, _data_type, 1, _fractional_bits); |
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 | |
| 169 | // Reshape weights for batched runs |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 170 | if(!reshape_weights && output_shape.y() > 1 && run_interleave) |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 171 | { |
| 172 | // Transpose with interleave |
| 173 | const int interleave_size = 16 / tmp.element_size(); |
| 174 | tmp = transpose(tmp, interleave_size); |
| 175 | } |
| 176 | |
| 177 | AccessorType weights_accessor(weights); |
| 178 | |
| 179 | for(int i = 0; i < tmp.num_elements(); ++i) |
| 180 | { |
| 181 | Coordinates coord = index2coord(tmp.shape(), i); |
| 182 | std::copy_n(static_cast<const RawTensor::value_type *>(tmp(coord)), |
| 183 | tmp.element_size(), |
| 184 | static_cast<RawTensor::value_type *>(weights_accessor(coord))); |
| 185 | } |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | fill(AccessorType(weights), 1); |
| 190 | } |
| 191 | |
| 192 | // Compute NEFullyConnectedLayer function |
| 193 | fc.run(); |
| 194 | |
| 195 | return dst; |
| 196 | } |
| 197 | |
| 198 | SimpleTensor<T> compute_reference(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] | 199 | bool reshape_weights) |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 200 | { |
| 201 | // Create reference |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 202 | SimpleTensor<T> src{ input_shape, _data_type, 1, _fractional_bits, _quantization_info }; |
| 203 | SimpleTensor<T> weights{ weights_shape, _data_type, 1, _fractional_bits, _quantization_info }; |
| 204 | SimpleTensor<TBias> bias{ bias_shape, _bias_data_type, 1, _fractional_bits, _quantization_info }; |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 205 | |
| 206 | // Fill reference |
| 207 | fill(src, 0); |
| 208 | fill(weights, 1); |
| 209 | fill(bias, 2); |
| 210 | |
| 211 | return reference::fully_connected_layer<T>(src, weights, bias, output_shape); |
| 212 | } |
| 213 | |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 214 | TensorType _target{}; |
| 215 | SimpleTensor<T> _reference{}; |
| 216 | DataType _data_type{}; |
| 217 | DataType _bias_data_type{}; |
| 218 | int _fractional_bits{}; |
| 219 | QuantizationInfo _quantization_info{}; |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 220 | }; |
| 221 | |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 222 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool run_interleave> |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 223 | class FullyConnectedLayerValidationFixture : public FullyConnectedLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T, run_interleave> |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 224 | { |
| 225 | public: |
| 226 | template <typename...> |
| 227 | void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, bool transpose_weights, bool reshape_weights, DataType data_type) |
| 228 | { |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 229 | FullyConnectedLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T, run_interleave>::setup(input_shape, weights_shape, bias_shape, output_shape, transpose_weights, |
| 230 | reshape_weights, data_type, |
| 231 | 0, QuantizationInfo()); |
| 232 | } |
| 233 | }; |
| 234 | |
| 235 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool run_interleave> |
| 236 | class FullyConnectedLayerValidationFixedPointFixture : public FullyConnectedLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T, run_interleave> |
| 237 | { |
| 238 | public: |
| 239 | template <typename...> |
| 240 | void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, bool transpose_weights, bool reshape_weights, DataType data_type, int fractional_bits) |
| 241 | { |
| 242 | FullyConnectedLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T, run_interleave>::setup(input_shape, weights_shape, bias_shape, output_shape, transpose_weights, |
| 243 | reshape_weights, data_type, |
| 244 | fractional_bits, QuantizationInfo()); |
| 245 | } |
| 246 | }; |
| 247 | |
| 248 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool run_interleave> |
| 249 | class FullyConnectedLayerValidationQuantizedFixture : public FullyConnectedLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T, run_interleave> |
| 250 | { |
| 251 | public: |
| 252 | template <typename...> |
| 253 | void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, bool transpose_weights, bool reshape_weights, DataType data_type, |
| 254 | QuantizationInfo quantization_info) |
| 255 | { |
| 256 | FullyConnectedLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T, run_interleave>::setup(input_shape, weights_shape, bias_shape, output_shape, transpose_weights, |
| 257 | reshape_weights, data_type, |
| 258 | 0, quantization_info); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 259 | } |
| 260 | }; |
| 261 | } // namespace validation |
| 262 | } // namespace test |
| 263 | } // namespace arm_compute |
| 264 | #endif /* ARM_COMPUTE_TEST_FULLY_CONNECTED_LAYER_FIXTURE */ |