Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 1 | /* |
Giorgio Arena | b309fc2 | 2021-01-05 09:46:16 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 Arm Limited. |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +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_DEPTHWISE_CONVOLUTION_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_DEPTHWISE_CONVOLUTION_FIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 30 | #include "tests/AssetsLibrary.h" |
| 31 | #include "tests/Globals.h" |
| 32 | #include "tests/IAccessor.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 33 | #include "tests/framework/Asserts.h" |
| 34 | #include "tests/framework/Fixture.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 35 | #include "tests/validation/Helpers.h" |
Manuel Bottini | a788c2f | 2019-04-08 13:18:00 +0100 | [diff] [blame] | 36 | #include "tests/validation/reference/ActivationLayer.h" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 37 | #include "tests/validation/reference/DepthwiseConvolutionLayer.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 38 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 39 | #include "utils/Utils.h" |
| 40 | |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 41 | #include <random> |
| 42 | |
| 43 | namespace arm_compute |
| 44 | { |
| 45 | namespace test |
| 46 | { |
| 47 | namespace validation |
| 48 | { |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 49 | using namespace arm_compute::misc::shape_calculator; |
| 50 | |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 51 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, typename TW> |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 52 | class DepthwiseConvolutionLayerValidationGenericFixture : public framework::Fixture |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 53 | { |
| 54 | public: |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 55 | using TBias = typename std::conditional < std::is_same<T, uint8_t>::value || std::is_same<T, int8_t>::value, int32_t, T >::type; |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 56 | |
| 57 | public: |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 58 | template <typename...> |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 59 | void setup(TensorShape in_shape, Size2D kernel_size, PadStrideInfo pad_stride_info, Size2D dilation, |
| 60 | unsigned int depth_multiplier, DataType input_data_type, DataType weights_data_type, |
| 61 | QuantizationInfo input_quantization_info, QuantizationInfo weights_quantization_info, QuantizationInfo output_quantization_info, |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 62 | DataLayout data_layout, ActivationLayerInfo act_info, bool mixed_layout = false) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 63 | { |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 64 | _mixed_layout = mixed_layout; |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 65 | _input_shape = in_shape; |
| 66 | _input_data_type = input_data_type; |
| 67 | _weights_data_type = weights_data_type; |
| 68 | _input_quantization_info = input_quantization_info; |
| 69 | _weights_quantization_info = weights_quantization_info; |
| 70 | _output_quantization_info = output_quantization_info; |
| 71 | _data_layout = data_layout; |
| 72 | _pad_stride_info = pad_stride_info; |
| 73 | _act_info = act_info; |
| 74 | _depth_multiplier = depth_multiplier; |
| 75 | _dilation = dilation; |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 76 | |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 77 | _bias_data_type = is_data_type_quantized(_input_data_type) ? DataType::S32 : _input_data_type; |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 78 | |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 79 | _weights_shape = TensorShape(kernel_size.width, kernel_size.height); |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 80 | |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 81 | const TensorInfo in_info(_input_shape, 1, _input_data_type); |
| 82 | const TensorInfo we_info(_weights_shape, 1, _weights_data_type); |
| 83 | const ConvolutionInfo info{ _pad_stride_info, _depth_multiplier, _act_info, _dilation }; |
| 84 | _output_shape = compute_depthwise_convolution_shape(in_info, we_info, info); |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 85 | |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 86 | _weights_shape.set(2, _output_shape.z()); |
| 87 | _biases_shape = TensorShape(_weights_shape[2]); |
| 88 | } |
| 89 | |
| 90 | void configure_target() |
| 91 | { |
| 92 | TensorShape input_shape = _input_shape; |
| 93 | TensorShape weights_shape = _weights_shape; |
| 94 | TensorShape output_shape = _output_shape; |
| 95 | |
| 96 | if(_data_layout == DataLayout::NHWC) |
| 97 | { |
| 98 | permute(input_shape, PermutationVector(2U, 0U, 1U)); |
| 99 | permute(weights_shape, PermutationVector(2U, 0U, 1U)); |
| 100 | permute(output_shape, PermutationVector(2U, 0U, 1U)); |
| 101 | } |
| 102 | |
| 103 | // Create tensors |
| 104 | _src = create_tensor<TensorType>(input_shape, _input_data_type, 1, _input_quantization_info, _data_layout); |
| 105 | _weights = create_tensor<TensorType>(weights_shape, _weights_data_type, 1, _weights_quantization_info, _data_layout); |
| 106 | _biases = create_tensor<TensorType>(_biases_shape, _bias_data_type, 1, _input_quantization_info, _data_layout); |
| 107 | _target = create_tensor<TensorType>(output_shape, _input_data_type, 1, _output_quantization_info, _data_layout); |
| 108 | |
| 109 | // Create Depthwise Convolution configure function |
| 110 | _dwc.configure(&_src, &_weights, &_biases, &_target, _pad_stride_info, _depth_multiplier, _act_info, _dilation); |
| 111 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 112 | ARM_COMPUTE_ASSERT(_src.info()->is_resizable()); |
| 113 | ARM_COMPUTE_ASSERT(_weights.info()->is_resizable()); |
| 114 | ARM_COMPUTE_ASSERT(_biases.info()->is_resizable()); |
| 115 | ARM_COMPUTE_ASSERT(_target.info()->is_resizable()); |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void allocate_and_run_target() |
| 119 | { |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 120 | add_padding_x({ &_src, &_weights, &_biases, &_target }, _data_layout); |
Giorgio Arena | 63825e8 | 2021-03-25 14:54:50 +0000 | [diff] [blame] | 121 | |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 122 | // Allocate tensors |
| 123 | _src.allocator()->allocate(); |
| 124 | _weights.allocator()->allocate(); |
| 125 | _biases.allocator()->allocate(); |
| 126 | _target.allocator()->allocate(); |
| 127 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 128 | ARM_COMPUTE_ASSERT(!_src.info()->is_resizable()); |
| 129 | ARM_COMPUTE_ASSERT(!_weights.info()->is_resizable()); |
| 130 | ARM_COMPUTE_ASSERT(!_biases.info()->is_resizable()); |
| 131 | ARM_COMPUTE_ASSERT(!_target.info()->is_resizable()); |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 132 | |
| 133 | // Fill tensors |
| 134 | fill(AccessorType(_src), 0); |
| 135 | fill(AccessorType(_weights), 1); |
| 136 | fill(AccessorType(_biases), 2); |
Giorgio Arena | 63825e8 | 2021-03-25 14:54:50 +0000 | [diff] [blame] | 137 | |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 138 | if(_mixed_layout) |
| 139 | { |
| 140 | mix_layout(_dwc, _src, _target); |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | // Compute function |
| 145 | _dwc.run(); |
| 146 | } |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void compute_reference() |
| 150 | { |
| 151 | SimpleTensor<T> src{ _input_shape, _input_data_type, 1, _input_quantization_info }; |
| 152 | SimpleTensor<TW> weights{ _weights_shape, _weights_data_type, 1, _weights_quantization_info }; |
| 153 | SimpleTensor<TBias> biases{ _biases_shape, _bias_data_type, 1, _input_quantization_info }; |
| 154 | |
| 155 | fill(src, 0); |
| 156 | fill(weights, 1); |
| 157 | fill(biases, 2); |
| 158 | |
| 159 | SimpleTensor<T> depth_out = reference::depthwise_convolution(src, weights, biases, _output_shape, _pad_stride_info, _depth_multiplier, _dilation, _output_quantization_info); |
| 160 | _reference = (_act_info.enabled()) ? reference::activation_layer<T>(depth_out, _act_info) : depth_out; |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | protected: |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 164 | void mix_layout(FunctionType &layer, TensorType &src, TensorType &dst) |
| 165 | { |
| 166 | // Test Multi DataLayout graph cases, when the data layout changes after configure |
| 167 | src.info()->set_data_layout(_data_layout == DataLayout::NCHW ? DataLayout::NHWC : DataLayout::NCHW); |
| 168 | dst.info()->set_data_layout(_data_layout == DataLayout::NCHW ? DataLayout::NHWC : DataLayout::NCHW); |
| 169 | |
| 170 | // Compute Convolution function |
| 171 | layer.run(); |
| 172 | |
| 173 | // Reinstating original data layout for the test suite to properly check the values |
| 174 | src.info()->set_data_layout(_data_layout); |
| 175 | dst.info()->set_data_layout(_data_layout); |
| 176 | } |
| 177 | |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 178 | template <typename U> |
| 179 | void fill(U &&tensor, int i) |
| 180 | { |
| 181 | switch(tensor.data_type()) |
| 182 | { |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 183 | case DataType::QASYMM8: |
| 184 | { |
| 185 | std::uniform_int_distribution<uint8_t> distribution(0, 10); |
| 186 | library->fill(tensor, distribution, i); |
| 187 | break; |
| 188 | } |
Michele Di Giorgio | 4cd4cde | 2020-01-06 14:07:44 +0000 | [diff] [blame] | 189 | case DataType::QASYMM8_SIGNED: |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 190 | case DataType::QSYMM8_PER_CHANNEL: |
| 191 | { |
| 192 | std::uniform_int_distribution<int8_t> distribution(-10, 10); |
| 193 | library->fill(tensor, distribution, i); |
| 194 | break; |
| 195 | } |
Frank Lei | 8cdfdb8 | 2018-01-02 16:49:33 +0800 | [diff] [blame] | 196 | case DataType::F16: |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 197 | { |
Giorgio Arena | a8e2aeb | 2021-01-06 11:34:57 +0000 | [diff] [blame] | 198 | arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -1.0f, 1.0f }; |
Giorgio Arena | 6aeb217 | 2020-12-15 15:45:43 +0000 | [diff] [blame] | 199 | library->fill(tensor, distribution, i); |
| 200 | break; |
| 201 | } |
| 202 | case DataType::F32: |
| 203 | { |
| 204 | std::uniform_real_distribution<float> distribution(-1.0f, 1.0f); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 205 | library->fill(tensor, distribution, i); |
| 206 | break; |
| 207 | } |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 208 | case DataType::S32: |
| 209 | { |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 210 | std::uniform_int_distribution<int32_t> distribution(-100, 100); |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 211 | library->fill(tensor, distribution, i); |
| 212 | break; |
| 213 | } |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 214 | default: |
| 215 | library->fill_tensor_uniform(tensor, i); |
| 216 | } |
| 217 | } |
| 218 | |
Georgios Pinitas | ddec4d6 | 2019-07-10 19:23:02 +0100 | [diff] [blame] | 219 | TensorType _target{}; |
| 220 | SimpleTensor<T> _reference{}; |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 221 | |
| 222 | TensorType _src{}; |
| 223 | TensorType _weights{}; |
| 224 | TensorType _biases{}; |
| 225 | FunctionType _dwc{}; |
| 226 | |
| 227 | TensorShape _input_shape{}; |
| 228 | TensorShape _weights_shape{}; |
| 229 | TensorShape _biases_shape{}; |
| 230 | TensorShape _output_shape{}; |
| 231 | DataType _input_data_type{}; |
| 232 | DataType _weights_data_type{}; |
| 233 | DataType _bias_data_type{}; |
| 234 | QuantizationInfo _input_quantization_info{}; |
| 235 | QuantizationInfo _weights_quantization_info{}; |
| 236 | QuantizationInfo _output_quantization_info{}; |
| 237 | DataLayout _data_layout{}; |
| 238 | PadStrideInfo _pad_stride_info{}; |
| 239 | ActivationLayerInfo _act_info{}; |
| 240 | unsigned int _depth_multiplier{}; |
| 241 | Size2D _dilation{}; |
Giorgio Arena | 63825e8 | 2021-03-25 14:54:50 +0000 | [diff] [blame] | 242 | bool _mixed_layout{ false }; |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 243 | }; |
| 244 | |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 245 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool mixed_layout = false> |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 246 | class DepthwiseConvolutionLayerValidationFixture : public DepthwiseConvolutionLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T, T> |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 247 | { |
| 248 | public: |
| 249 | template <typename...> |
Manuel Bottini | a788c2f | 2019-04-08 13:18:00 +0100 | [diff] [blame] | 250 | void setup(TensorShape in_shape, Size2D kernel_size, PadStrideInfo pad_stride_info, Size2D dilation, unsigned int depth_multiplier, DataType data_type, DataLayout data_layout, |
| 251 | ActivationLayerInfo act_info) |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 252 | { |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 253 | DepthwiseConvolutionLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T, T>::setup(in_shape, kernel_size, pad_stride_info, dilation, depth_multiplier, |
| 254 | data_type, data_type, QuantizationInfo(), QuantizationInfo(), QuantizationInfo(), |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 255 | data_layout, act_info, mixed_layout); |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 256 | } |
| 257 | }; |
| 258 | |
| 259 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 260 | class DepthwiseConvolutionLayerNativeValidationFixture : public DepthwiseConvolutionLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T, T> |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 261 | { |
| 262 | public: |
| 263 | template <typename...> |
| 264 | void setup(size_t width, size_t height, size_t channel, size_t batch, Size2D kernel_size, size_t depth_multiplier, Size2D dilation, Size2D stride, bool padding_valid, DataType data_type, |
| 265 | DataLayout data_layout) |
| 266 | { |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 267 | _dilation = dilation; |
| 268 | _depth_multiplier = depth_multiplier; |
| 269 | _data_type = data_type; |
| 270 | _data_layout = data_layout; |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 271 | |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 272 | _input_shape = TensorShape(width, height, channel, batch); |
| 273 | _weights_shape = TensorShape(kernel_size.width, kernel_size.height, channel * _depth_multiplier); |
| 274 | _biases_shape = TensorShape(_weights_shape.z()); |
| 275 | |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 276 | if(padding_valid) |
| 277 | { |
Gian Marco Iodice | c63b722 | 2021-06-30 08:39:44 +0000 | [diff] [blame^] | 278 | _conv_info = PadStrideInfo(); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 279 | } |
| 280 | else |
| 281 | { |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 282 | _conv_info = calculate_same_pad(_input_shape, _weights_shape, PadStrideInfo(stride.width, stride.height), DataLayout::NCHW, _dilation); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | void configure_target() |
| 287 | { |
| 288 | TensorShape input_shape = _input_shape; |
| 289 | TensorShape weights_shape = _weights_shape; |
| 290 | |
| 291 | if(_data_layout == DataLayout::NHWC) |
| 292 | { |
| 293 | permute(input_shape, PermutationVector(2U, 0U, 1U)); |
| 294 | permute(weights_shape, PermutationVector(2U, 0U, 1U)); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 295 | } |
| 296 | |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 297 | // Create tensors |
| 298 | _src = create_tensor<TensorType>(input_shape, _data_type, 1, QuantizationInfo(), _data_layout); |
| 299 | _weights = create_tensor<TensorType>(weights_shape, _data_type, 1, QuantizationInfo(), _data_layout); |
| 300 | _biases = create_tensor<TensorType>(_biases_shape, _data_type, 1, QuantizationInfo(), _data_layout); |
| 301 | _target = create_tensor<TensorType>(TensorShape(), _data_type, 1, QuantizationInfo(), _data_layout); |
| 302 | |
| 303 | // Create Depthwise Convolution configure function |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 304 | const ConvolutionInfo info |
| 305 | { |
| 306 | _conv_info, _depth_multiplier, ActivationLayerInfo(), _dilation |
| 307 | }; |
| 308 | _dwc.configure(_src.info(), _weights.info(), _biases.info(), _target.info(), info); |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 309 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 310 | ARM_COMPUTE_ASSERT(_src.info()->is_resizable()); |
| 311 | ARM_COMPUTE_ASSERT(_weights.info()->is_resizable()); |
| 312 | ARM_COMPUTE_ASSERT(_biases.info()->is_resizable()); |
| 313 | ARM_COMPUTE_ASSERT(_target.info()->is_resizable()); |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | void allocate_and_run_target() |
| 317 | { |
Giorgio Arena | 63825e8 | 2021-03-25 14:54:50 +0000 | [diff] [blame] | 318 | add_padding_x({ &_src, &_weights, &_biases, &_target }, _data_layout); |
| 319 | |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 320 | // Allocate tensors |
| 321 | _src.allocator()->allocate(); |
| 322 | _weights.allocator()->allocate(); |
| 323 | _biases.allocator()->allocate(); |
| 324 | _target.allocator()->allocate(); |
| 325 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 326 | ARM_COMPUTE_ASSERT(!_src.info()->is_resizable()); |
| 327 | ARM_COMPUTE_ASSERT(!_weights.info()->is_resizable()); |
| 328 | ARM_COMPUTE_ASSERT(!_biases.info()->is_resizable()); |
| 329 | ARM_COMPUTE_ASSERT(!_target.info()->is_resizable()); |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 330 | |
| 331 | // Fill tensors |
| 332 | fill(AccessorType(_src), 0); |
| 333 | fill(AccessorType(_weights), 1); |
| 334 | fill(AccessorType(_biases), 2); |
| 335 | |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 336 | arm_compute::ITensorPack pack; |
| 337 | pack.add_const_tensor(arm_compute::TensorType::ACL_SRC_0, &_src); |
| 338 | pack.add_const_tensor(arm_compute::TensorType::ACL_SRC_1, &_weights); |
| 339 | pack.add_const_tensor(arm_compute::TensorType::ACL_SRC_2, &_biases); |
| 340 | pack.add_tensor(arm_compute::TensorType::ACL_DST, &_target); |
| 341 | |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 342 | // Compute function |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 343 | _dwc.run(pack); |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | void compute_reference() |
| 347 | { |
| 348 | SimpleTensor<T> src{ _input_shape, _data_type }; |
| 349 | SimpleTensor<T> weights{ _weights_shape, _data_type }; |
| 350 | SimpleTensor<T> biases{ _biases_shape, _data_type }; |
| 351 | |
| 352 | fill(src, 0); |
| 353 | fill(weights, 1); |
| 354 | fill(biases, 2); |
| 355 | |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 356 | const ConvolutionInfo info{ _conv_info, _depth_multiplier, ActivationLayerInfo(), _dilation }; |
| 357 | const TensorShape dst_shape = compute_depthwise_convolution_shape(TensorInfo(_input_shape, 1, _data_type), TensorInfo(_weights_shape, 1, _data_type), info); |
| 358 | _reference = reference::depthwise_convolution(src, weights, biases, dst_shape, _conv_info, _depth_multiplier, _dilation); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | protected: |
| 362 | template <typename U> |
| 363 | void fill(U &&tensor, int i) |
| 364 | { |
| 365 | switch(tensor.data_type()) |
| 366 | { |
| 367 | case DataType::F32: |
| 368 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 369 | std::uniform_real_distribution<float> distribution(-1.0f, 1.0f); |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 370 | library->fill(tensor, distribution, i); |
| 371 | break; |
| 372 | } |
| 373 | default: |
| 374 | library->fill_tensor_uniform(tensor, i); |
| 375 | } |
| 376 | } |
| 377 | |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 378 | TensorType _target{}; |
| 379 | SimpleTensor<T> _reference{}; |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 380 | |
| 381 | TensorType _src{}; |
| 382 | TensorType _weights{}; |
| 383 | TensorType _biases{}; |
| 384 | FunctionType _dwc{}; |
| 385 | |
| 386 | TensorShape _input_shape{}; |
| 387 | TensorShape _weights_shape{}; |
| 388 | TensorShape _biases_shape{}; |
| 389 | DataType _data_type{}; |
| 390 | DataLayout _data_layout{}; |
| 391 | PadStrideInfo _conv_info{}; |
| 392 | Size2D _dilation{}; |
| 393 | unsigned int _depth_multiplier{}; |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 394 | }; |
| 395 | |
| 396 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 397 | class DepthwiseConvolutionLayerNativeConfigurableValidationFixture : public DepthwiseConvolutionLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T, T> |
Gian Marco Iodice | 9285adb | 2019-09-05 16:10:27 +0100 | [diff] [blame] | 398 | { |
| 399 | public: |
| 400 | template <typename...> |
| 401 | void setup(size_t width, size_t height, size_t channel, size_t batch, Size2D kernel_size, size_t depth_multiplier, Size2D dilation, Size2D stride, bool padding_valid, DataType data_type, |
Gian Marco Iodice | c63b722 | 2021-06-30 08:39:44 +0000 | [diff] [blame^] | 402 | DataLayout data_layout, const ActivationLayerInfo &act_info, unsigned int n0) |
Gian Marco Iodice | 9285adb | 2019-09-05 16:10:27 +0100 | [diff] [blame] | 403 | { |
Gian Marco Iodice | c63b722 | 2021-06-30 08:39:44 +0000 | [diff] [blame^] | 404 | _dilation = dilation; |
| 405 | _depth_multiplier = depth_multiplier; |
| 406 | _data_type = data_type; |
| 407 | _data_layout = data_layout; |
| 408 | _act_info = act_info; |
| 409 | _n0 = n0; |
Gian Marco Iodice | 9285adb | 2019-09-05 16:10:27 +0100 | [diff] [blame] | 410 | |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 411 | _input_shape = TensorShape(width, height, channel, batch); |
| 412 | _weights_shape = TensorShape(kernel_size.width, kernel_size.height, channel * _depth_multiplier); |
| 413 | _biases_shape = TensorShape(_weights_shape.z()); |
| 414 | |
Gian Marco Iodice | 9285adb | 2019-09-05 16:10:27 +0100 | [diff] [blame] | 415 | if(padding_valid) |
| 416 | { |
Gian Marco Iodice | c63b722 | 2021-06-30 08:39:44 +0000 | [diff] [blame^] | 417 | _conv_info = PadStrideInfo(); |
Gian Marco Iodice | 9285adb | 2019-09-05 16:10:27 +0100 | [diff] [blame] | 418 | } |
| 419 | else |
| 420 | { |
Gian Marco Iodice | c63b722 | 2021-06-30 08:39:44 +0000 | [diff] [blame^] | 421 | _conv_info = calculate_same_pad(_input_shape, _weights_shape, PadStrideInfo(stride.width, stride.height), DataLayout::NCHW, _dilation); |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
| 425 | void configure_target() |
| 426 | { |
| 427 | TensorShape input_shape = _input_shape; |
| 428 | TensorShape weights_shape = _weights_shape; |
| 429 | |
| 430 | if(_data_layout == DataLayout::NHWC) |
| 431 | { |
| 432 | permute(input_shape, PermutationVector(2U, 0U, 1U)); |
| 433 | permute(weights_shape, PermutationVector(2U, 0U, 1U)); |
Gian Marco Iodice | 9285adb | 2019-09-05 16:10:27 +0100 | [diff] [blame] | 434 | } |
| 435 | |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 436 | // Create tensors |
| 437 | _src = create_tensor<TensorType>(input_shape, _data_type, 1, QuantizationInfo(), _data_layout); |
| 438 | _weights = create_tensor<TensorType>(weights_shape, _data_type, 1, QuantizationInfo(), _data_layout); |
| 439 | _biases = create_tensor<TensorType>(_biases_shape, _data_type, 1, QuantizationInfo(), _data_layout); |
| 440 | _target = create_tensor<TensorType>(TensorShape(), _data_type, 1, QuantizationInfo(), _data_layout); |
| 441 | |
Gian Marco Iodice | c63b722 | 2021-06-30 08:39:44 +0000 | [diff] [blame^] | 442 | DWCWeightsKernelInfo dwc_weights_info; |
| 443 | dwc_weights_info.n0 = _n0; |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 444 | |
Gian Marco Iodice | c63b722 | 2021-06-30 08:39:44 +0000 | [diff] [blame^] | 445 | DWCKernelInfo dwc_info; |
| 446 | dwc_info.activation_info = _act_info; |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 447 | |
| 448 | // Create Depthwise Convolution configure function |
Gian Marco Iodice | c63b722 | 2021-06-30 08:39:44 +0000 | [diff] [blame^] | 449 | _dwc.configure(&_src, &_weights, &_biases, &_target, dwc_weights_info, dwc_info, _conv_info, _depth_multiplier, _dilation); |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 450 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 451 | ARM_COMPUTE_ASSERT(_src.info()->is_resizable()); |
| 452 | ARM_COMPUTE_ASSERT(_weights.info()->is_resizable()); |
| 453 | ARM_COMPUTE_ASSERT(_biases.info()->is_resizable()); |
| 454 | ARM_COMPUTE_ASSERT(_target.info()->is_resizable()); |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | void allocate_and_run_target() |
| 458 | { |
Gian Marco Iodice | c63b722 | 2021-06-30 08:39:44 +0000 | [diff] [blame^] | 459 | add_padding_x({ &_src, &_weights, &_biases, &_target }, _data_layout); |
Giorgio Arena | 63825e8 | 2021-03-25 14:54:50 +0000 | [diff] [blame] | 460 | |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 461 | // Allocate tensors |
| 462 | _src.allocator()->allocate(); |
| 463 | _weights.allocator()->allocate(); |
| 464 | _biases.allocator()->allocate(); |
| 465 | _target.allocator()->allocate(); |
| 466 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 467 | ARM_COMPUTE_ASSERT(!_src.info()->is_resizable()); |
| 468 | ARM_COMPUTE_ASSERT(!_weights.info()->is_resizable()); |
| 469 | ARM_COMPUTE_ASSERT(!_biases.info()->is_resizable()); |
| 470 | ARM_COMPUTE_ASSERT(!_target.info()->is_resizable()); |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 471 | |
| 472 | // Fill tensors |
| 473 | fill(AccessorType(_src), 0); |
| 474 | fill(AccessorType(_weights), 1); |
| 475 | fill(AccessorType(_biases), 2); |
| 476 | |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 477 | // Test Multi DataLayout graph cases, when the data layout changes after configure |
| 478 | _src.info()->set_data_layout(_data_layout == DataLayout::NCHW ? DataLayout::NHWC : DataLayout::NCHW); |
| 479 | _target.info()->set_data_layout(_data_layout == DataLayout::NCHW ? DataLayout::NHWC : DataLayout::NCHW); |
| 480 | |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 481 | // Compute function |
Gian Marco Iodice | c63b722 | 2021-06-30 08:39:44 +0000 | [diff] [blame^] | 482 | _dwc.run(); |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 483 | |
| 484 | // Reinstating original data layout for the test suite to properly check the values |
| 485 | _target.info()->set_data_layout(_data_layout); |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | void compute_reference() |
| 489 | { |
| 490 | SimpleTensor<T> src{ _input_shape, _data_type }; |
| 491 | SimpleTensor<T> weights{ _weights_shape, _data_type }; |
| 492 | SimpleTensor<T> biases{ _biases_shape, _data_type }; |
| 493 | |
| 494 | fill(src, 0); |
| 495 | fill(weights, 1); |
| 496 | fill(biases, 2); |
| 497 | |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 498 | const ConvolutionInfo info{ _conv_info, _depth_multiplier, _act_info, _dilation }; |
| 499 | const TensorShape dst_shape = compute_depthwise_convolution_shape(TensorInfo(_input_shape, 1, _data_type), TensorInfo(_weights_shape, 1, _data_type), info); |
Gian Marco Iodice | c63b722 | 2021-06-30 08:39:44 +0000 | [diff] [blame^] | 500 | _reference = reference::activation_layer(reference::depthwise_convolution(src, weights, biases, dst_shape, _conv_info, _depth_multiplier, _dilation), _act_info); |
Gian Marco Iodice | 9285adb | 2019-09-05 16:10:27 +0100 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | protected: |
| 504 | template <typename U> |
| 505 | void fill(U &&tensor, int i) |
| 506 | { |
| 507 | switch(tensor.data_type()) |
| 508 | { |
| 509 | case DataType::F32: |
| 510 | { |
Giorgio Arena | 6aeb217 | 2020-12-15 15:45:43 +0000 | [diff] [blame] | 511 | std::uniform_real_distribution<float> distribution(-1.0f, 1.0f); |
Gian Marco Iodice | 9285adb | 2019-09-05 16:10:27 +0100 | [diff] [blame] | 512 | library->fill(tensor, distribution, i); |
| 513 | break; |
| 514 | } |
| 515 | case DataType::F16: |
| 516 | { |
Giorgio Arena | a8e2aeb | 2021-01-06 11:34:57 +0000 | [diff] [blame] | 517 | arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -1.0f, 1.0f }; |
Gian Marco Iodice | 9285adb | 2019-09-05 16:10:27 +0100 | [diff] [blame] | 518 | library->fill(tensor, distribution, i); |
| 519 | break; |
| 520 | } |
| 521 | default: |
| 522 | library->fill_tensor_uniform(tensor, i); |
| 523 | } |
| 524 | } |
| 525 | |
Gian Marco Iodice | 9285adb | 2019-09-05 16:10:27 +0100 | [diff] [blame] | 526 | TensorType _target{}; |
| 527 | SimpleTensor<T> _reference{}; |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 528 | |
| 529 | TensorType _src{}; |
| 530 | TensorType _weights{}; |
| 531 | TensorType _biases{}; |
| 532 | FunctionType _dwc{}; |
| 533 | |
| 534 | TensorShape _input_shape{}; |
| 535 | TensorShape _weights_shape{}; |
| 536 | TensorShape _biases_shape{}; |
| 537 | DataType _data_type{}; |
| 538 | DataLayout _data_layout{}; |
| 539 | PadStrideInfo _conv_info{}; |
| 540 | ActivationLayerInfo _act_info{}; |
| 541 | Size2D _dilation{}; |
| 542 | unsigned int _depth_multiplier{}; |
| 543 | unsigned int _n0{}; |
Gian Marco Iodice | 9285adb | 2019-09-05 16:10:27 +0100 | [diff] [blame] | 544 | }; |
| 545 | |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 546 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool mixed_layout = false> |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 547 | class DepthwiseConvolutionLayerValidationQuantizedFixture : public DepthwiseConvolutionLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T, T> |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 548 | { |
| 549 | public: |
| 550 | template <typename...> |
Pablo Tello | a28aebc | 2019-06-03 14:59:48 +0100 | [diff] [blame] | 551 | void setup(TensorShape in_shape, Size2D kernel_size, PadStrideInfo pad_stride_info, Size2D dilation, unsigned int depth_multiplier, DataType data_type, |
| 552 | QuantizationInfo input_quantization_info, QuantizationInfo output_quantization_info, DataLayout data_layout, ActivationLayerInfo act_info) |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 553 | { |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 554 | DepthwiseConvolutionLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T, T>::setup(in_shape, kernel_size, pad_stride_info, dilation, depth_multiplier, data_type, |
| 555 | data_type, input_quantization_info, input_quantization_info, output_quantization_info, |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 556 | data_layout, act_info, mixed_layout); |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 557 | } |
| 558 | }; |
| 559 | |
| 560 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, typename TW> |
| 561 | class DepthwiseConvolutionLayerValidationQuantizedPerChannelFixture : public DepthwiseConvolutionLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T, TW> |
| 562 | { |
| 563 | public: |
| 564 | template <typename...> |
| 565 | void setup(TensorShape in_shape, Size2D kernel_size, PadStrideInfo pad_stride_info, Size2D dilation, unsigned int depth_multiplier, DataType input_data_type, DataType weights_data_type, |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 566 | QuantizationInfo input_quantization_info, QuantizationInfo output_quantization_info, DataLayout data_layout, ActivationLayerInfo act_info) |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 567 | { |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 568 | const float out_scale = output_quantization_info.uniform().scale; |
| 569 | const float in_scale = input_quantization_info.uniform().scale; |
| 570 | |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 571 | std::vector<float> weights_scales{}; |
| 572 | std::mt19937 gen(library->seed()); |
| 573 | std::uniform_real_distribution<float> dis(0.01f, out_scale / in_scale); |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 574 | for(size_t i = 0; i < in_shape.z() * depth_multiplier; ++i) |
| 575 | { |
| 576 | weights_scales.push_back(dis(gen)); |
| 577 | } |
| 578 | |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 579 | DepthwiseConvolutionLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T, TW>::setup(in_shape, kernel_size, pad_stride_info, dilation, depth_multiplier, |
| 580 | input_data_type, weights_data_type, |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 581 | input_quantization_info, QuantizationInfo(weights_scales), output_quantization_info, |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 582 | data_layout, act_info); |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 583 | } |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 584 | }; |
| 585 | } // namespace validation |
| 586 | } // namespace test |
| 587 | } // namespace arm_compute |
| 588 | #endif /* ARM_COMPUTE_TEST_DEPTHWISE_CONVOLUTION_FIXTURE */ |