Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 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, |
| 62 | DataLayout data_layout, ActivationLayerInfo act_info) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 63 | { |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 64 | const DataType 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] | 65 | |
| 66 | TensorShape weights_shape(kernel_size.width, kernel_size.height); |
| 67 | |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 68 | const TensorInfo in_info(in_shape, 1, input_data_type); |
| 69 | const TensorInfo we_info(weights_shape, 1, weights_data_type); |
Manuel Bottini | a788c2f | 2019-04-08 13:18:00 +0100 | [diff] [blame] | 70 | const TensorShape out_shape = compute_depthwise_convolution_shape(in_info, we_info, pad_stride_info, depth_multiplier, dilation); |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 71 | |
| 72 | weights_shape.set(2, out_shape.z()); |
Pablo Tello | 941cd70 | 2017-12-12 14:35:00 +0000 | [diff] [blame] | 73 | const TensorShape biases_shape(weights_shape[2]); |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 74 | |
Pablo Tello | a28aebc | 2019-06-03 14:59:48 +0100 | [diff] [blame] | 75 | _target = compute_target(in_shape, weights_shape, biases_shape, out_shape, pad_stride_info, dilation, depth_multiplier, |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 76 | input_data_type, weights_data_type, bias_data_type, input_quantization_info, weights_quantization_info, output_quantization_info, data_layout, act_info); |
Pablo Tello | a28aebc | 2019-06-03 14:59:48 +0100 | [diff] [blame] | 77 | _reference = compute_reference(in_shape, weights_shape, biases_shape, out_shape, pad_stride_info, dilation, depth_multiplier, |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 78 | input_data_type, weights_data_type, bias_data_type, input_quantization_info, weights_quantization_info, output_quantization_info, act_info); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | protected: |
| 82 | template <typename U> |
| 83 | void fill(U &&tensor, int i) |
| 84 | { |
| 85 | switch(tensor.data_type()) |
| 86 | { |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 87 | case DataType::QASYMM8: |
| 88 | { |
| 89 | std::uniform_int_distribution<uint8_t> distribution(0, 10); |
| 90 | library->fill(tensor, distribution, i); |
| 91 | break; |
| 92 | } |
Michele Di Giorgio | 4cd4cde | 2020-01-06 14:07:44 +0000 | [diff] [blame] | 93 | case DataType::QASYMM8_SIGNED: |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 94 | case DataType::QSYMM8_PER_CHANNEL: |
| 95 | { |
| 96 | std::uniform_int_distribution<int8_t> distribution(-10, 10); |
| 97 | library->fill(tensor, distribution, i); |
| 98 | break; |
| 99 | } |
Frank Lei | 8cdfdb8 | 2018-01-02 16:49:33 +0800 | [diff] [blame] | 100 | case DataType::F16: |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 101 | { |
Giorgio Arena | 6aeb217 | 2020-12-15 15:45:43 +0000 | [diff] [blame^] | 102 | arm_compute::utils::uniform_real_distribution_fp16 distribution{ half(-1.0f), half(1.0f) }; |
| 103 | library->fill(tensor, distribution, i); |
| 104 | break; |
| 105 | } |
| 106 | case DataType::F32: |
| 107 | { |
| 108 | std::uniform_real_distribution<float> distribution(-1.0f, 1.0f); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 109 | library->fill(tensor, distribution, i); |
| 110 | break; |
| 111 | } |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 112 | case DataType::S32: |
| 113 | { |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 114 | std::uniform_int_distribution<int32_t> distribution(-100, 100); |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 115 | library->fill(tensor, distribution, i); |
| 116 | break; |
| 117 | } |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 118 | default: |
| 119 | library->fill_tensor_uniform(tensor, i); |
| 120 | } |
| 121 | } |
| 122 | |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 123 | TensorType compute_target(TensorShape input_shape, TensorShape weights_shape, TensorShape biases_shape, TensorShape output_shape, PadStrideInfo &pad_stride_info, Size2D dilation, |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 124 | unsigned int depth_multiplier, const DataType input_data_type, const DataType weights_data_type, const DataType bias_data_type, |
| 125 | const QuantizationInfo &input_quantization_info, const QuantizationInfo &weights_quantization_info, const QuantizationInfo &output_quantization_info, |
Georgios Pinitas | ddec4d6 | 2019-07-10 19:23:02 +0100 | [diff] [blame] | 126 | const DataLayout data_layout, const ActivationLayerInfo &act_info) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 127 | { |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 128 | if(data_layout == DataLayout::NHWC) |
| 129 | { |
| 130 | permute(input_shape, PermutationVector(2U, 0U, 1U)); |
| 131 | permute(weights_shape, PermutationVector(2U, 0U, 1U)); |
| 132 | permute(output_shape, PermutationVector(2U, 0U, 1U)); |
| 133 | } |
| 134 | |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 135 | // Create tensors |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 136 | TensorType src = create_tensor<TensorType>(input_shape, input_data_type, 1, input_quantization_info, data_layout); |
| 137 | TensorType weights = create_tensor<TensorType>(weights_shape, weights_data_type, 1, weights_quantization_info, data_layout); |
Pablo Tello | a28aebc | 2019-06-03 14:59:48 +0100 | [diff] [blame] | 138 | TensorType biases = create_tensor<TensorType>(biases_shape, bias_data_type, 1, input_quantization_info, data_layout); |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 139 | TensorType dst = create_tensor<TensorType>(output_shape, input_data_type, 1, output_quantization_info, data_layout); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 140 | |
| 141 | // Create Depthwise Convolution configure function |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 142 | FunctionType dwc; |
Manuel Bottini | a788c2f | 2019-04-08 13:18:00 +0100 | [diff] [blame] | 143 | dwc.configure(&src, &weights, &biases, &dst, pad_stride_info, depth_multiplier, act_info, dilation); |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 144 | |
| 145 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 146 | ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 147 | ARM_COMPUTE_EXPECT(biases.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 148 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 149 | |
| 150 | // Allocate tensors |
| 151 | src.allocator()->allocate(); |
| 152 | weights.allocator()->allocate(); |
Georgios Pinitas | 81a26ad | 2017-10-23 20:29:30 +0100 | [diff] [blame] | 153 | biases.allocator()->allocate(); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 154 | dst.allocator()->allocate(); |
| 155 | |
| 156 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 157 | ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
Georgios Pinitas | 81a26ad | 2017-10-23 20:29:30 +0100 | [diff] [blame] | 158 | ARM_COMPUTE_EXPECT(!biases.info()->is_resizable(), framework::LogLevel::ERRORS); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 159 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 160 | |
| 161 | // Fill tensors |
| 162 | fill(AccessorType(src), 0); |
| 163 | fill(AccessorType(weights), 1); |
Georgios Pinitas | 81a26ad | 2017-10-23 20:29:30 +0100 | [diff] [blame] | 164 | fill(AccessorType(biases), 2); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 165 | |
| 166 | // Compute function |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 167 | dwc.run(); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 168 | |
| 169 | return dst; |
| 170 | } |
| 171 | |
Pablo Tello | a28aebc | 2019-06-03 14:59:48 +0100 | [diff] [blame] | 172 | SimpleTensor<T> compute_reference(const TensorShape &in_shape, const TensorShape &weights_shape, const TensorShape &biases_shape, const TensorShape &out_shape, |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 173 | const PadStrideInfo &pad_stride_info, const Size2D &dilation, unsigned int depth_multiplier, |
| 174 | const DataType input_data_type, const DataType weights_data_type, const DataType bias_data_type, |
| 175 | const QuantizationInfo &input_quantization_info, const QuantizationInfo &weights_quantization_info, const QuantizationInfo &output_quantization_info, |
Georgios Pinitas | ddec4d6 | 2019-07-10 19:23:02 +0100 | [diff] [blame] | 176 | const ActivationLayerInfo &act_info) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 177 | { |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 178 | SimpleTensor<T> src{ in_shape, input_data_type, 1, input_quantization_info }; |
| 179 | SimpleTensor<TW> weights{ weights_shape, weights_data_type, 1, weights_quantization_info }; |
Pablo Tello | a28aebc | 2019-06-03 14:59:48 +0100 | [diff] [blame] | 180 | SimpleTensor<TBias> biases{ biases_shape, bias_data_type, 1, input_quantization_info }; |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 181 | |
| 182 | fill(src, 0); |
| 183 | fill(weights, 1); |
Georgios Pinitas | 81a26ad | 2017-10-23 20:29:30 +0100 | [diff] [blame] | 184 | fill(biases, 2); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 185 | |
Pablo Tello | a28aebc | 2019-06-03 14:59:48 +0100 | [diff] [blame] | 186 | SimpleTensor<T> depth_out = reference::depthwise_convolution(src, weights, biases, out_shape, pad_stride_info, depth_multiplier, dilation, output_quantization_info); |
Manuel Bottini | a788c2f | 2019-04-08 13:18:00 +0100 | [diff] [blame] | 187 | return (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] | 188 | } |
| 189 | |
Georgios Pinitas | ddec4d6 | 2019-07-10 19:23:02 +0100 | [diff] [blame] | 190 | TensorType _target{}; |
| 191 | SimpleTensor<T> _reference{}; |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 195 | class DepthwiseConvolutionLayerValidationFixture : public DepthwiseConvolutionLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T, T> |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 196 | { |
| 197 | public: |
| 198 | template <typename...> |
Manuel Bottini | a788c2f | 2019-04-08 13:18:00 +0100 | [diff] [blame] | 199 | void setup(TensorShape in_shape, Size2D kernel_size, PadStrideInfo pad_stride_info, Size2D dilation, unsigned int depth_multiplier, DataType data_type, DataLayout data_layout, |
| 200 | ActivationLayerInfo act_info) |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 201 | { |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 202 | DepthwiseConvolutionLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T, T>::setup(in_shape, kernel_size, pad_stride_info, dilation, depth_multiplier, |
| 203 | data_type, data_type, QuantizationInfo(), QuantizationInfo(), QuantizationInfo(), |
| 204 | data_layout, act_info); |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 205 | } |
| 206 | }; |
| 207 | |
| 208 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 209 | class DepthwiseConvolutionLayerNativeValidationFixture : public DepthwiseConvolutionLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T, T> |
Giorgio Arena | 44f5572 | 2019-07-12 14:49:49 +0100 | [diff] [blame] | 210 | { |
| 211 | public: |
| 212 | template <typename...> |
| 213 | 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, |
| 214 | DataLayout data_layout) |
| 215 | { |
| 216 | const TensorShape src_shape(width, height, channel, batch); |
| 217 | const TensorShape weights_shape(kernel_size.width, kernel_size.height, channel * depth_multiplier); |
| 218 | const TensorShape biases_shape(weights_shape.z()); |
| 219 | |
| 220 | PadStrideInfo conv_info; |
| 221 | if(padding_valid) |
| 222 | { |
| 223 | conv_info = PadStrideInfo(); |
| 224 | } |
| 225 | else |
| 226 | { |
| 227 | conv_info = calculate_same_pad(src_shape, weights_shape, PadStrideInfo(stride.width, stride.height), DataLayout::NCHW, dilation); |
| 228 | } |
| 229 | |
| 230 | _target = compute_target(src_shape, weights_shape, biases_shape, conv_info, dilation, depth_multiplier, data_type, data_layout); |
| 231 | _reference = compute_reference(src_shape, weights_shape, biases_shape, conv_info, dilation, depth_multiplier, data_type); |
| 232 | } |
| 233 | |
| 234 | protected: |
| 235 | template <typename U> |
| 236 | void fill(U &&tensor, int i) |
| 237 | { |
| 238 | switch(tensor.data_type()) |
| 239 | { |
| 240 | case DataType::F32: |
| 241 | { |
| 242 | std::uniform_real_distribution<> distribution(-1.0f, 1.0f); |
| 243 | library->fill(tensor, distribution, i); |
| 244 | break; |
| 245 | } |
| 246 | default: |
| 247 | library->fill_tensor_uniform(tensor, i); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | TensorType compute_target(TensorShape input_shape, TensorShape weights_shape, TensorShape biases_shape, PadStrideInfo &conv_info, Size2D dilation, |
| 252 | unsigned int depth_multiplier, const DataType data_type, const DataLayout data_layout) |
| 253 | { |
| 254 | if(data_layout == DataLayout::NHWC) |
| 255 | { |
| 256 | permute(input_shape, PermutationVector(2U, 0U, 1U)); |
| 257 | permute(weights_shape, PermutationVector(2U, 0U, 1U)); |
| 258 | } |
| 259 | |
| 260 | // Create tensors |
| 261 | TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, QuantizationInfo(), data_layout); |
| 262 | TensorType weights = create_tensor<TensorType>(weights_shape, data_type, 1, QuantizationInfo(), data_layout); |
| 263 | TensorType biases = create_tensor<TensorType>(biases_shape, data_type, 1, QuantizationInfo(), data_layout); |
| 264 | TensorType dst = create_tensor<TensorType>(TensorShape(), data_type, 1, QuantizationInfo(), data_layout); |
| 265 | |
| 266 | // Create Depthwise Convolution configure function |
| 267 | FunctionType dwc; |
| 268 | dwc.configure(&src, &weights, &biases, &dst, conv_info, depth_multiplier, dilation); |
| 269 | |
| 270 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 271 | ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 272 | ARM_COMPUTE_EXPECT(biases.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 273 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 274 | |
| 275 | // Allocate tensors |
| 276 | src.allocator()->allocate(); |
| 277 | weights.allocator()->allocate(); |
| 278 | biases.allocator()->allocate(); |
| 279 | dst.allocator()->allocate(); |
| 280 | |
| 281 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 282 | ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 283 | ARM_COMPUTE_EXPECT(!biases.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 284 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 285 | |
| 286 | // Fill tensors |
| 287 | fill(AccessorType(src), 0); |
| 288 | fill(AccessorType(weights), 1); |
| 289 | fill(AccessorType(biases), 2); |
| 290 | |
| 291 | // Compute function |
| 292 | dwc.run(); |
| 293 | |
| 294 | return dst; |
| 295 | } |
| 296 | |
| 297 | SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &biases_shape, const PadStrideInfo &conv_info, |
| 298 | const Size2D &dilation, unsigned int depth_multiplier, const DataType data_type) |
| 299 | { |
| 300 | SimpleTensor<T> src{ input_shape, data_type }; |
| 301 | SimpleTensor<T> weights{ weights_shape, data_type }; |
| 302 | SimpleTensor<T> biases{ biases_shape, data_type }; |
| 303 | |
| 304 | fill(src, 0); |
| 305 | fill(weights, 1); |
| 306 | fill(biases, 2); |
| 307 | |
| 308 | const TensorShape dst_shape = compute_depthwise_convolution_shape(TensorInfo(input_shape, 1, data_type), TensorInfo(weights_shape, 1, data_type), conv_info, |
| 309 | depth_multiplier, dilation); |
| 310 | return reference::depthwise_convolution(src, weights, biases, dst_shape, conv_info, depth_multiplier, dilation); |
| 311 | } |
| 312 | |
| 313 | TensorType _target{}; |
| 314 | SimpleTensor<T> _reference{}; |
| 315 | }; |
| 316 | |
| 317 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 318 | class DepthwiseConvolutionLayerNativeConfigurableValidationFixture : public DepthwiseConvolutionLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T, T> |
Gian Marco Iodice | 9285adb | 2019-09-05 16:10:27 +0100 | [diff] [blame] | 319 | { |
| 320 | public: |
| 321 | template <typename...> |
| 322 | 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, |
| 323 | DataLayout data_layout, const ActivationLayerInfo &act_info, unsigned int n0) |
| 324 | { |
| 325 | const TensorShape src_shape(width, height, channel, batch); |
| 326 | const TensorShape weights_shape(kernel_size.width, kernel_size.height, channel * depth_multiplier); |
| 327 | const TensorShape biases_shape(weights_shape.z()); |
| 328 | |
| 329 | PadStrideInfo conv_info; |
| 330 | if(padding_valid) |
| 331 | { |
| 332 | conv_info = PadStrideInfo(); |
| 333 | } |
| 334 | else |
| 335 | { |
| 336 | conv_info = calculate_same_pad(src_shape, weights_shape, PadStrideInfo(stride.width, stride.height), DataLayout::NCHW, dilation); |
| 337 | } |
| 338 | |
| 339 | _target = compute_target(src_shape, weights_shape, biases_shape, conv_info, dilation, depth_multiplier, data_type, data_layout, act_info, n0); |
| 340 | _reference = compute_reference(src_shape, weights_shape, biases_shape, conv_info, dilation, depth_multiplier, data_type, act_info); |
| 341 | } |
| 342 | |
| 343 | protected: |
| 344 | template <typename U> |
| 345 | void fill(U &&tensor, int i) |
| 346 | { |
| 347 | switch(tensor.data_type()) |
| 348 | { |
| 349 | case DataType::F32: |
| 350 | { |
Giorgio Arena | 6aeb217 | 2020-12-15 15:45:43 +0000 | [diff] [blame^] | 351 | std::uniform_real_distribution<float> distribution(-1.0f, 1.0f); |
Gian Marco Iodice | 9285adb | 2019-09-05 16:10:27 +0100 | [diff] [blame] | 352 | library->fill(tensor, distribution, i); |
| 353 | break; |
| 354 | } |
| 355 | case DataType::F16: |
| 356 | { |
Giorgio Arena | 6aeb217 | 2020-12-15 15:45:43 +0000 | [diff] [blame^] | 357 | arm_compute::utils::uniform_real_distribution_fp16 distribution{ half(-1.0f), half(1.0f) }; |
Gian Marco Iodice | 9285adb | 2019-09-05 16:10:27 +0100 | [diff] [blame] | 358 | library->fill(tensor, distribution, i); |
| 359 | break; |
| 360 | } |
| 361 | default: |
| 362 | library->fill_tensor_uniform(tensor, i); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | TensorType compute_target(TensorShape input_shape, TensorShape weights_shape, TensorShape biases_shape, PadStrideInfo &conv_info, Size2D dilation, |
| 367 | unsigned int depth_multiplier, const DataType data_type, const DataLayout data_layout, const ActivationLayerInfo &act_info, unsigned int n0) |
| 368 | { |
| 369 | if(data_layout == DataLayout::NHWC) |
| 370 | { |
| 371 | permute(input_shape, PermutationVector(2U, 0U, 1U)); |
| 372 | permute(weights_shape, PermutationVector(2U, 0U, 1U)); |
| 373 | } |
| 374 | |
| 375 | // Create tensors |
| 376 | TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, QuantizationInfo(), data_layout); |
| 377 | TensorType weights = create_tensor<TensorType>(weights_shape, data_type, 1, QuantizationInfo(), data_layout); |
| 378 | TensorType biases = create_tensor<TensorType>(biases_shape, data_type, 1, QuantizationInfo(), data_layout); |
| 379 | TensorType dst = create_tensor<TensorType>(TensorShape(), data_type, 1, QuantizationInfo(), data_layout); |
| 380 | |
| 381 | DWCWeightsKernelInfo dwc_weights_info; |
| 382 | dwc_weights_info.n0 = n0; |
| 383 | |
| 384 | DWCKernelInfo dwc_info; |
| 385 | dwc_info.activation_info = act_info; |
| 386 | |
| 387 | // Create Depthwise Convolution configure function |
| 388 | FunctionType dwc; |
| 389 | dwc.configure(&src, &weights, &biases, &dst, dwc_weights_info, dwc_info, conv_info, depth_multiplier, dilation); |
| 390 | |
| 391 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 392 | ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 393 | ARM_COMPUTE_EXPECT(biases.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 394 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 395 | |
| 396 | // Allocate tensors |
| 397 | src.allocator()->allocate(); |
| 398 | weights.allocator()->allocate(); |
| 399 | biases.allocator()->allocate(); |
| 400 | dst.allocator()->allocate(); |
| 401 | |
| 402 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 403 | ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 404 | ARM_COMPUTE_EXPECT(!biases.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 405 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 406 | |
| 407 | // Fill tensors |
| 408 | fill(AccessorType(src), 0); |
| 409 | fill(AccessorType(weights), 1); |
| 410 | fill(AccessorType(biases), 2); |
| 411 | |
| 412 | // Compute function |
| 413 | dwc.run(); |
| 414 | |
| 415 | return dst; |
| 416 | } |
| 417 | |
| 418 | SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &biases_shape, const PadStrideInfo &conv_info, |
| 419 | const Size2D &dilation, unsigned int depth_multiplier, const DataType data_type, const ActivationLayerInfo &act_info) |
| 420 | { |
| 421 | SimpleTensor<T> src{ input_shape, data_type }; |
| 422 | SimpleTensor<T> weights{ weights_shape, data_type }; |
| 423 | SimpleTensor<T> biases{ biases_shape, data_type }; |
| 424 | |
| 425 | fill(src, 0); |
| 426 | fill(weights, 1); |
| 427 | fill(biases, 2); |
| 428 | |
| 429 | const TensorShape dst_shape = compute_depthwise_convolution_shape(TensorInfo(input_shape, 1, data_type), TensorInfo(weights_shape, 1, data_type), conv_info, |
| 430 | depth_multiplier, dilation); |
| 431 | return reference::activation_layer(reference::depthwise_convolution(src, weights, biases, dst_shape, conv_info, depth_multiplier, dilation), act_info); |
| 432 | } |
| 433 | |
| 434 | TensorType _target{}; |
| 435 | SimpleTensor<T> _reference{}; |
| 436 | }; |
| 437 | |
| 438 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 439 | class DepthwiseConvolutionLayerValidationQuantizedFixture : public DepthwiseConvolutionLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T, T> |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 440 | { |
| 441 | public: |
| 442 | template <typename...> |
Pablo Tello | a28aebc | 2019-06-03 14:59:48 +0100 | [diff] [blame] | 443 | void setup(TensorShape in_shape, Size2D kernel_size, PadStrideInfo pad_stride_info, Size2D dilation, unsigned int depth_multiplier, DataType data_type, |
| 444 | 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] | 445 | { |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 446 | DepthwiseConvolutionLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T, T>::setup(in_shape, kernel_size, pad_stride_info, dilation, depth_multiplier, data_type, |
| 447 | data_type, input_quantization_info, input_quantization_info, output_quantization_info, |
| 448 | data_layout, act_info); |
| 449 | } |
| 450 | }; |
| 451 | |
| 452 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, typename TW> |
| 453 | class DepthwiseConvolutionLayerValidationQuantizedPerChannelFixture : public DepthwiseConvolutionLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T, TW> |
| 454 | { |
| 455 | public: |
| 456 | template <typename...> |
| 457 | 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] | 458 | 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] | 459 | { |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 460 | const float out_scale = output_quantization_info.uniform().scale; |
| 461 | const float in_scale = input_quantization_info.uniform().scale; |
| 462 | |
| 463 | std::vector<float> weights_scales{}; |
| 464 | std::mt19937 gen(library->seed()); |
| 465 | std::uniform_real_distribution<> dis(0.01f, out_scale / in_scale); |
| 466 | for(size_t i = 0; i < in_shape.z() * depth_multiplier; ++i) |
| 467 | { |
| 468 | weights_scales.push_back(dis(gen)); |
| 469 | } |
| 470 | |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 471 | DepthwiseConvolutionLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T, TW>::setup(in_shape, kernel_size, pad_stride_info, dilation, depth_multiplier, |
| 472 | input_data_type, weights_data_type, |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 473 | input_quantization_info, QuantizationInfo(weights_scales), output_quantization_info, |
Michele Di Giorgio | 633d30b | 2019-10-08 17:17:18 +0100 | [diff] [blame] | 474 | data_layout, act_info); |
Dmitry Savenko | d7295b7 | 2017-11-20 22:00:08 +0700 | [diff] [blame] | 475 | } |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 476 | }; |
| 477 | } // namespace validation |
| 478 | } // namespace test |
| 479 | } // namespace arm_compute |
| 480 | #endif /* ARM_COMPUTE_TEST_DEPTHWISE_CONVOLUTION_FIXTURE */ |