Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 1 | /* |
Pablo Tello | 29cab36 | 2022-03-10 17:05:34 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2022 Arm Limited. |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +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_CONVOLUTION_LAYER_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_CONVOLUTION_LAYER_FIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
Gunes Bayir | cc171f9 | 2021-09-13 13:38:29 +0100 | [diff] [blame] | 29 | #include "arm_compute/graph/Utils.h" |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 30 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
Francesco Petrogalli | 553f695 | 2022-06-30 10:22:01 +0000 | [diff] [blame] | 31 | #include "src/core/NEON/kernels/arm_gemm/utils.hpp" |
Gunes Bayir | cc171f9 | 2021-09-13 13:38:29 +0100 | [diff] [blame] | 32 | #include "src/graph/mutators/MutatorUtils.h" |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 33 | #include "tests/AssetsLibrary.h" |
| 34 | #include "tests/Globals.h" |
| 35 | #include "tests/IAccessor.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 36 | #include "tests/framework/Asserts.h" |
| 37 | #include "tests/framework/Fixture.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 38 | #include "tests/validation/Helpers.h" |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 39 | #include "tests/validation/reference/ActivationLayer.h" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 40 | #include "tests/validation/reference/ConvolutionLayer.h" |
Gunes Bayir | cc171f9 | 2021-09-13 13:38:29 +0100 | [diff] [blame] | 41 | #include "tests/validation/reference/PadLayer.h" |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 42 | #include "tests/validation/reference/Permute.h" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 43 | #include "tests/validation/reference/Utils.h" |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 44 | |
| 45 | #include <random> |
| 46 | |
| 47 | namespace arm_compute |
| 48 | { |
| 49 | namespace test |
| 50 | { |
| 51 | namespace validation |
| 52 | { |
Georgios Pinitas | c0b6f76 | 2020-11-02 01:37:17 +0000 | [diff] [blame] | 53 | namespace detail |
| 54 | { |
| 55 | template <typename ConvolutionFunction, typename TensorType> |
| 56 | void configure_conv_function(ConvolutionFunction &func, |
| 57 | TensorType *src, const TensorType *weights, const TensorType *bias, TensorType *dst, |
| 58 | const PadStrideInfo &info, const WeightsInfo &weights_info, |
| 59 | const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups) |
| 60 | { |
| 61 | func.configure(src, weights, bias, dst, info, weights_info, dilation, act_info, num_groups); |
| 62 | } |
| 63 | } // namespace detail |
| 64 | |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 65 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, typename TW> |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 66 | class ConvolutionValidationGenericFixture : public framework::Fixture |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 67 | { |
| 68 | public: |
Georgios Pinitas | 6e1791b | 2019-12-02 19:01:25 +0000 | [diff] [blame] | 69 | using TBias = typename std::conditional < std::is_same<typename std::decay<T>::type, uint8_t>::value |
| 70 | || std::is_same<typename std::decay<T>::type, int8_t>::value, |
| 71 | int32_t, T >::type; |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 72 | |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 73 | public: |
| 74 | template <typename...> |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 75 | void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, Size2D dilation, bool reshape_weights, |
Giorgio Arena | 63825e8 | 2021-03-25 14:54:50 +0000 | [diff] [blame] | 76 | DataType data_type, DataType weights_data_type, DataLayout data_layout, QuantizationInfo quantization_info, QuantizationInfo weight_quantization_info, ActivationLayerInfo act_info, |
Gunes Bayir | cc171f9 | 2021-09-13 13:38:29 +0100 | [diff] [blame] | 77 | bool mixed_layout = false, PaddingList pre_pad_layer = PaddingList({})) |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 78 | { |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 79 | _mixed_layout = mixed_layout; |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 80 | _data_type = data_type; |
| 81 | _weights_data_type = weights_data_type; |
| 82 | _is_quantized = is_data_type_quantized_asymmetric(data_type); |
Georgios Pinitas | c7b183a | 2020-03-06 18:12:09 +0000 | [diff] [blame] | 83 | _is_bfloat16 = data_type == DataType::BFLOAT16; |
| 84 | _bias_data_type = _is_quantized ? DataType::S32 : (_is_bfloat16 ? DataType::F32 : data_type); |
| 85 | _output_data_type = _is_bfloat16 ? DataType::F32 : data_type; |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 86 | _quantization_info = quantization_info; |
| 87 | _weight_quantization_info = weight_quantization_info; |
| 88 | _data_layout = data_layout; |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 89 | |
Gunes Bayir | cc171f9 | 2021-09-13 13:38:29 +0100 | [diff] [blame] | 90 | _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, reshape_weights, dilation, act_info, pre_pad_layer); |
| 91 | _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, dilation, act_info, pre_pad_layer); |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | protected: |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 95 | void mix_layout(FunctionType &layer, TensorType &src, TensorType &dst) |
| 96 | { |
| 97 | // Test Multi DataLayout graph cases, when the data layout changes after configure |
| 98 | src.info()->set_data_layout(_data_layout == DataLayout::NCHW ? DataLayout::NHWC : DataLayout::NCHW); |
| 99 | dst.info()->set_data_layout(_data_layout == DataLayout::NCHW ? DataLayout::NHWC : DataLayout::NCHW); |
| 100 | |
| 101 | // Compute Convolution function |
| 102 | layer.run(); |
| 103 | |
| 104 | // Reinstating original data layout for the test suite to properly check the values |
| 105 | src.info()->set_data_layout(_data_layout); |
| 106 | dst.info()->set_data_layout(_data_layout); |
| 107 | } |
| 108 | |
Georgios Pinitas | c7b183a | 2020-03-06 18:12:09 +0000 | [diff] [blame] | 109 | void regularize_values(void *values, size_t size) |
| 110 | { |
| 111 | float *fvalues = static_cast<float *>(values); |
| 112 | for(size_t i = 0; i < size; ++i) |
| 113 | { |
| 114 | fvalues[i] = float(bfloat16(fvalues[i])); |
| 115 | } |
| 116 | } |
| 117 | |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 118 | template <typename U> |
| 119 | void fill(U &&tensor, int i) |
| 120 | { |
| 121 | switch(tensor.data_type()) |
| 122 | { |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 123 | case DataType::QASYMM8: |
| 124 | { |
Francesco Petrogalli | 553f695 | 2022-06-30 10:22:01 +0000 | [diff] [blame] | 125 | std::pair<int, int> bounds = get_quantized_bounds(tensor.quantization_info(), -1.0f, 1.0f); |
Pablo Tello | 29cab36 | 2022-03-10 17:05:34 +0000 | [diff] [blame] | 126 | std::uniform_int_distribution<uint32_t> distribution(bounds.first, bounds.second); |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 127 | library->fill(tensor, distribution, i); |
| 128 | break; |
| 129 | } |
Georgios Pinitas | 6e1791b | 2019-12-02 19:01:25 +0000 | [diff] [blame] | 130 | case DataType::QASYMM8_SIGNED: |
| 131 | { |
Francesco Petrogalli | 553f695 | 2022-06-30 10:22:01 +0000 | [diff] [blame] | 132 | std::pair<int, int> bounds = get_quantized_qasymm8_signed_bounds(tensor.quantization_info(), -1.0f, 1.0f); |
Pablo Tello | 29cab36 | 2022-03-10 17:05:34 +0000 | [diff] [blame] | 133 | std::uniform_int_distribution<int32_t> distribution(bounds.first, bounds.second); |
Georgios Pinitas | 6e1791b | 2019-12-02 19:01:25 +0000 | [diff] [blame] | 134 | library->fill(tensor, distribution, i); |
| 135 | break; |
| 136 | } |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 137 | case DataType::QSYMM8_PER_CHANNEL: |
| 138 | { |
| 139 | int min_bound = 128; |
| 140 | int max_bound = -127; |
| 141 | for(size_t i = 0; i < _weight_quantization_info.scale().size(); i++) |
| 142 | { |
| 143 | std::pair<int, int> bounds = get_symm_quantized_per_channel_bounds(tensor.quantization_info(), -1.0f, 1.0f, i); |
| 144 | if(bounds.first < min_bound) |
| 145 | { |
| 146 | min_bound = bounds.first; |
| 147 | } |
| 148 | if(bounds.second > max_bound) |
| 149 | { |
| 150 | max_bound = bounds.second; |
| 151 | } |
| 152 | } |
Pablo Tello | 29cab36 | 2022-03-10 17:05:34 +0000 | [diff] [blame] | 153 | std::uniform_int_distribution<int32_t> distribution(min_bound, max_bound); |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 154 | library->fill(tensor, distribution, i); |
| 155 | break; |
| 156 | } |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 157 | case DataType::S32: |
| 158 | { |
| 159 | std::uniform_int_distribution<int32_t> distribution(-100, 100); |
| 160 | library->fill(tensor, distribution, i); |
| 161 | break; |
| 162 | } |
Georgios Pinitas | c7b183a | 2020-03-06 18:12:09 +0000 | [diff] [blame] | 163 | case DataType::BFLOAT16: |
Giorgio Arena | a8e2aeb | 2021-01-06 11:34:57 +0000 | [diff] [blame] | 164 | { |
| 165 | arm_compute::utils::uniform_real_distribution_16bit<bfloat16> distribution{ -1.0f, 1.0f }; |
| 166 | library->fill(tensor, distribution, i); |
| 167 | break; |
| 168 | } |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 169 | case DataType::F16: |
Giorgio Arena | 6aeb217 | 2020-12-15 15:45:43 +0000 | [diff] [blame] | 170 | { |
Giorgio Arena | a8e2aeb | 2021-01-06 11:34:57 +0000 | [diff] [blame] | 171 | 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] | 172 | library->fill(tensor, distribution, i); |
| 173 | break; |
| 174 | } |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 175 | case DataType::F32: |
| 176 | { |
Giorgio Arena | 6aeb217 | 2020-12-15 15:45:43 +0000 | [diff] [blame] | 177 | std::uniform_real_distribution<float> distribution(-1.0f, 1.0f); |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 178 | library->fill(tensor, distribution, i); |
| 179 | break; |
| 180 | } |
| 181 | default: |
| 182 | library->fill_tensor_uniform(tensor, i); |
| 183 | } |
| 184 | } |
| 185 | |
Gunes Bayir | cc171f9 | 2021-09-13 13:38:29 +0100 | [diff] [blame] | 186 | // given input is IN nchw format |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 187 | TensorType compute_target(TensorShape input_shape, TensorShape weights_shape, const TensorShape &bias_shape, TensorShape output_shape, const PadStrideInfo &info, |
Gunes Bayir | cc171f9 | 2021-09-13 13:38:29 +0100 | [diff] [blame] | 188 | bool reshape_weights, const Size2D &dilation, const ActivationLayerInfo act_info, PaddingList pre_pad_layer = PaddingList({})) |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 189 | { |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 190 | ARM_COMPUTE_ERROR_ON((input_shape[2] % weights_shape[2]) != 0); |
| 191 | |
| 192 | const unsigned int num_groups = input_shape[2] / weights_shape[2]; |
| 193 | |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 194 | if(_data_layout == DataLayout::NHWC) |
Moritz Pflanzer | cde1e8a | 2017-09-08 09:53:14 +0100 | [diff] [blame] | 195 | { |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 196 | permute(input_shape, PermutationVector(2U, 0U, 1U)); |
| 197 | permute(weights_shape, PermutationVector(2U, 0U, 1U)); |
| 198 | permute(output_shape, PermutationVector(2U, 0U, 1U)); |
Gunes Bayir | cc171f9 | 2021-09-13 13:38:29 +0100 | [diff] [blame] | 199 | |
| 200 | if(pre_pad_layer.size() > 0) |
| 201 | { |
| 202 | // make sure paddings exist for each c,h,w dimensions |
| 203 | for(unsigned int i = 0; i < 3 - pre_pad_layer.size(); ++i) |
| 204 | { |
| 205 | pre_pad_layer.push_back({ 0, 0 }); |
| 206 | } |
| 207 | |
| 208 | // rotate padding info from nchw to nhwc |
| 209 | std::rotate(pre_pad_layer.begin(), pre_pad_layer.begin() + 2, pre_pad_layer.begin() + 3); |
| 210 | } |
Moritz Pflanzer | cde1e8a | 2017-09-08 09:53:14 +0100 | [diff] [blame] | 211 | } |
| 212 | |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 213 | const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH); |
| 214 | const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT); |
| 215 | |
| 216 | WeightsInfo weights_info(!reshape_weights, weights_shape[idx_width], weights_shape[idx_height], weights_shape[3]); |
| 217 | TensorShape reshaped_weights_shape(weights_shape); |
| 218 | |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 219 | // Create tensors |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 220 | TensorType src = create_tensor<TensorType>(input_shape, _data_type, 1, _quantization_info, _data_layout); |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 221 | TensorType weights = create_tensor<TensorType>(reshaped_weights_shape, _weights_data_type, 1, _weight_quantization_info, _data_layout); |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 222 | TensorType bias = create_tensor<TensorType>(bias_shape, _bias_data_type, 1, _quantization_info, _data_layout); |
Georgios Pinitas | c7b183a | 2020-03-06 18:12:09 +0000 | [diff] [blame] | 223 | TensorType dst = create_tensor<TensorType>(output_shape, _output_data_type, 1, _quantization_info, _data_layout); |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 224 | |
| 225 | // Create and configure function |
| 226 | FunctionType conv; |
Gunes Bayir | cc171f9 | 2021-09-13 13:38:29 +0100 | [diff] [blame] | 227 | |
| 228 | const unsigned int height_index = arm_compute::graph::get_dimension_idx(_data_layout, DataLayoutDimension::HEIGHT); |
| 229 | const unsigned int width_index = arm_compute::graph::get_dimension_idx(_data_layout, DataLayoutDimension::WIDTH); |
| 230 | |
| 231 | const PaddingInfo pad_w = width_index < pre_pad_layer.size() ? pre_pad_layer[width_index] : PaddingInfo(0, 0); |
| 232 | const PaddingInfo pad_h = height_index < pre_pad_layer.size() ? pre_pad_layer[height_index] : PaddingInfo(0, 0); |
| 233 | |
| 234 | if(pre_pad_layer.size() > 0 && arm_compute::graph::is_padding_in_height_or_width(_data_layout, pre_pad_layer)) |
| 235 | { |
| 236 | // this is the logic implemented in NodeFusionMutator -> fuse_pad_with_convolution |
| 237 | const PadStrideInfo new_conv_info( |
| 238 | info.stride().first, |
| 239 | info.stride().second, |
| 240 | info.pad_left() + pad_w.first, |
| 241 | info.pad_right() + pad_w.second, |
| 242 | info.pad_top() + pad_h.first, |
| 243 | info.pad_bottom() + pad_h.second, |
| 244 | info.round()); |
| 245 | detail::configure_conv_function(conv, &src, &weights, &bias, &dst, new_conv_info, weights_info, dilation, act_info, num_groups); |
| 246 | } |
| 247 | else |
| 248 | { |
| 249 | detail::configure_conv_function(conv, &src, &weights, &bias, &dst, info, weights_info, dilation, act_info, num_groups); |
| 250 | } |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 251 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 252 | ARM_COMPUTE_ASSERT(src.info()->is_resizable()); |
| 253 | ARM_COMPUTE_ASSERT(weights.info()->is_resizable()); |
| 254 | ARM_COMPUTE_ASSERT(bias.info()->is_resizable()); |
| 255 | ARM_COMPUTE_ASSERT(dst.info()->is_resizable()); |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 256 | |
Giorgio Arena | 63825e8 | 2021-03-25 14:54:50 +0000 | [diff] [blame] | 257 | add_padding_x({ &src, &weights, &bias, &dst }, _data_layout); |
| 258 | |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 259 | // Allocate tensors |
| 260 | src.allocator()->allocate(); |
| 261 | weights.allocator()->allocate(); |
| 262 | bias.allocator()->allocate(); |
| 263 | dst.allocator()->allocate(); |
| 264 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 265 | ARM_COMPUTE_ASSERT(!src.info()->is_resizable()); |
| 266 | ARM_COMPUTE_ASSERT(!weights.info()->is_resizable()); |
| 267 | ARM_COMPUTE_ASSERT(!bias.info()->is_resizable()); |
| 268 | ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 269 | |
| 270 | // Fill tensors |
| 271 | fill(AccessorType(src), 0); |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 272 | fill(AccessorType(weights), 1); |
| 273 | fill(AccessorType(bias), 2); |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 274 | |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 275 | if(_mixed_layout) |
| 276 | { |
| 277 | mix_layout(conv, src, dst); |
| 278 | } |
| 279 | else |
| 280 | { |
| 281 | // Compute Convolution function |
| 282 | conv.run(); |
| 283 | } |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 284 | |
| 285 | return dst; |
| 286 | } |
| 287 | |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 288 | SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info, |
Gunes Bayir | cc171f9 | 2021-09-13 13:38:29 +0100 | [diff] [blame] | 289 | const Size2D &dilation, const ActivationLayerInfo act_info, PaddingList pre_pad_layer = PaddingList({})) |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 290 | { |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 291 | ARM_COMPUTE_ERROR_ON((input_shape[2] % weights_shape[2]) != 0); |
| 292 | |
| 293 | const unsigned int num_groups = input_shape[2] / weights_shape[2]; |
| 294 | |
Georgios Pinitas | c7b183a | 2020-03-06 18:12:09 +0000 | [diff] [blame] | 295 | // Setup reference data types |
| 296 | const DataType src_dt = _is_bfloat16 ? DataType::F32 : _data_type; |
| 297 | const DataType weights_dt = _is_bfloat16 ? DataType::F32 : _weights_data_type; |
| 298 | const DataType bias_dt = _is_bfloat16 ? DataType::F32 : _bias_data_type; |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 299 | |
Georgios Pinitas | c7b183a | 2020-03-06 18:12:09 +0000 | [diff] [blame] | 300 | // Create reference |
| 301 | SimpleTensor<T> src{ input_shape, src_dt, 1, _quantization_info }; |
| 302 | SimpleTensor<TW> weights{ weights_shape, weights_dt, 1, _weight_quantization_info }; |
| 303 | SimpleTensor<TBias> bias{ bias_shape, bias_dt, 1, _quantization_info }; |
| 304 | |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 305 | fill(src, 0); |
| 306 | fill(weights, 1); |
| 307 | fill(bias, 2); |
| 308 | |
Georgios Pinitas | c7b183a | 2020-03-06 18:12:09 +0000 | [diff] [blame] | 309 | // Fill with bfloat16 to perform the conversion and reduce the mismatches in the output |
| 310 | if(_is_bfloat16) |
| 311 | { |
| 312 | regularize_values(static_cast<void *>(src.data()), src.num_elements()); |
| 313 | regularize_values(static_cast<void *>(weights.data()), weights.num_elements()); |
| 314 | } |
| 315 | |
Gunes Bayir | cc171f9 | 2021-09-13 13:38:29 +0100 | [diff] [blame] | 316 | if(pre_pad_layer.size() > 0) |
| 317 | { |
| 318 | src = reference::pad_layer<T>(src, pre_pad_layer, PixelValue(0), PaddingMode::CONSTANT); |
| 319 | } |
| 320 | |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 321 | return (act_info.enabled()) ? reference::activation_layer<T>(reference::convolution_layer<T>(src, weights, bias, output_shape, info, dilation, num_groups), |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 322 | act_info) : |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 323 | reference::convolution_layer<T>(src, weights, bias, output_shape, info, dilation, num_groups); |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 324 | } |
| 325 | |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 326 | TensorType _target{}; |
| 327 | SimpleTensor<T> _reference{}; |
| 328 | DataType _data_type{}; |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 329 | DataType _weights_data_type{}; |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 330 | DataType _bias_data_type{}; |
Georgios Pinitas | c7b183a | 2020-03-06 18:12:09 +0000 | [diff] [blame] | 331 | DataType _output_data_type{}; |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 332 | DataLayout _data_layout{}; |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 333 | QuantizationInfo _quantization_info{}; |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 334 | QuantizationInfo _weight_quantization_info{}; |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 335 | bool _is_quantized = false; |
Georgios Pinitas | c7b183a | 2020-03-06 18:12:09 +0000 | [diff] [blame] | 336 | bool _is_bfloat16 = false; |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 337 | bool _mixed_layout = false; |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 338 | }; |
| 339 | |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 340 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool mixed_layout = false> |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 341 | class ConvolutionValidationFixture : public ConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T, T> |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 342 | { |
| 343 | public: |
| 344 | template <typename...> |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 345 | void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, Size2D dilation, bool reshape_weights, DataType data_type, |
Michalis Spyrou | e250389 | 2018-04-23 15:17:31 +0100 | [diff] [blame] | 346 | DataLayout data_layout, ActivationLayerInfo act_info) |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 347 | { |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 348 | ConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, dilation, reshape_weights, |
| 349 | data_type, data_type, data_layout, |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 350 | QuantizationInfo(), QuantizationInfo(), act_info, mixed_layout); |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 351 | } |
| 352 | }; |
| 353 | |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 354 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool mixed_layout = false> |
Gunes Bayir | cc171f9 | 2021-09-13 13:38:29 +0100 | [diff] [blame] | 355 | class ConvolutionValidationWithPaddingFixture : public ConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T, T> |
| 356 | { |
| 357 | public: |
| 358 | template <typename...> |
| 359 | void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, Size2D dilation, bool reshape_weights, DataType data_type, |
| 360 | DataLayout data_layout, ActivationLayerInfo act_info, PaddingList pre_pad_layer = PaddingList({})) |
| 361 | { |
| 362 | ConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, dilation, reshape_weights, |
| 363 | data_type, data_type, data_layout, |
| 364 | QuantizationInfo(), QuantizationInfo(), act_info, mixed_layout, pre_pad_layer); |
| 365 | } |
| 366 | }; |
| 367 | |
| 368 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool mixed_layout = false> |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 369 | class ConvolutionValidationQuantizedFixture : public ConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T, T> |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 370 | { |
| 371 | public: |
| 372 | template <typename...> |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 373 | void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, Size2D dilation, bool reshape_weights, DataType data_type, |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 374 | DataLayout data_layout, QuantizationInfo quantization_info, ActivationLayerInfo act_info) |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 375 | { |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 376 | ConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, dilation, reshape_weights, |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 377 | data_type, data_type, data_layout, quantization_info, quantization_info, act_info, mixed_layout); |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 378 | } |
| 379 | }; |
| 380 | |
| 381 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, typename TW> |
| 382 | class ConvolutionValidationQuantizedPerChannelFixture : public ConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T, TW> |
| 383 | { |
| 384 | public: |
| 385 | template <typename...> |
| 386 | void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, Size2D dilation, bool reshape_weights, DataType data_type, |
| 387 | DataLayout data_layout, QuantizationInfo quantization_info, ActivationLayerInfo act_info, DataType weights_data_type) |
| 388 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 389 | std::vector<float> weights_scales{}; |
| 390 | std::mt19937 gen(library->seed()); |
| 391 | std::uniform_real_distribution<float> dis(0.01f, 1.f); |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 392 | for(size_t i = 0; i < output_shape[2]; ++i) |
| 393 | { |
| 394 | weights_scales.push_back(dis(gen)); |
| 395 | } |
| 396 | ConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T, TW>::setup(input_shape, weights_shape, bias_shape, output_shape, info, dilation, |
| 397 | reshape_weights, data_type, weights_data_type, data_layout, |
| 398 | quantization_info, QuantizationInfo(weights_scales), act_info); |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 399 | } |
| 400 | }; |
Francesco Petrogalli | 553f695 | 2022-06-30 10:22:01 +0000 | [diff] [blame] | 401 | |
| 402 | #ifdef ARM_COMPUTE_ENABLE_FIXED_FORMAT_KERNELS |
| 403 | inline TensorInfo prepare_weights(const TensorInfo tensor_info, const arm_gemm::WeightFormat weight_format) |
| 404 | { |
| 405 | const DataLayout data_layout = tensor_info.data_layout(); |
| 406 | ARM_COMPUTE_EXPECT(data_layout == DataLayout::NHWC, framework::LogLevel::ERRORS); |
| 407 | const DataType data_type = tensor_info.data_type(); |
| 408 | const TensorShape tensor_shape = tensor_info.tensor_shape(); |
| 409 | const int N = tensor_shape[get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES)]; // N=O |
| 410 | const int H = tensor_shape[get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT)]; |
| 411 | const int W = tensor_shape[get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH)]; |
| 412 | const int C = tensor_shape[get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL)]; // C=I |
| 413 | |
| 414 | const int interleave_by = arm_gemm::interleave_by(weight_format); |
| 415 | const int block_by = arm_gemm::block_by(weight_format); |
| 416 | const int Ip = arm_gemm::roundup<unsigned int>(C, block_by); // C'=I' |
| 417 | const int Op = arm_gemm::roundup<unsigned int>(N, interleave_by); // O'=N' |
| 418 | |
| 419 | const TensorShape TS(Ip, W, H, Op); |
| 420 | return TensorInfo(TS, 1 /*num_channels*/, data_type, data_layout); |
| 421 | } |
| 422 | |
| 423 | template <typename ScalarType, typename AccessorType> |
| 424 | inline void rearrange_data(const AccessorType src, AccessorType dst, const arm_gemm::WeightFormat weight_format) |
| 425 | { |
| 426 | ARM_COMPUTE_EXPECT(arm_gemm::is_fixed_format(weight_format), framework::LogLevel::ERRORS); |
| 427 | // Data Layout: OHWIo<interleave_by>i<block_by> |
| 428 | const int interleave_by = arm_gemm::interleave_by(weight_format); |
| 429 | const int block_by = arm_gemm::block_by(weight_format); |
| 430 | const TensorShape src_tensor_shape = src.shape(); |
| 431 | const DataLayout data_layout = src.data_layout(); |
| 432 | ARM_COMPUTE_EXPECT(data_layout == DataLayout::NHWC, framework::LogLevel::ERRORS); |
| 433 | const unsigned int O = src_tensor_shape[get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES)]; // N=O |
| 434 | const unsigned int H = src_tensor_shape[get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT)]; |
| 435 | const unsigned int W = src_tensor_shape[get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH)]; |
| 436 | const unsigned int I = src_tensor_shape[get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL)]; // C=I |
| 437 | const unsigned int Ip = arm_gemm::roundup<unsigned int>(I, block_by); // C'=I' |
| 438 | const unsigned int Op = arm_gemm::roundup<unsigned int>(O, interleave_by); // N'=O' |
| 439 | |
| 440 | ARM_COMPUTE_EXPECT_EQUAL(Op * H * W * Ip, (unsigned)dst.num_elements(), framework::LogLevel::ERRORS); |
| 441 | ARM_COMPUTE_EXPECT(src.num_elements() <= dst.num_elements(), framework::LogLevel::ERRORS); |
| 442 | |
| 443 | const ScalarType *src_ptr = reinterpret_cast<const ScalarType *>(src.data()); |
| 444 | ScalarType *dst_ptr = reinterpret_cast<ScalarType *>(dst.data()); |
| 445 | for(unsigned i = 0; i < I; ++i) |
| 446 | for(unsigned w = 0; w < W; ++w) |
| 447 | for(unsigned h = 0; h < H; ++h) |
| 448 | for(unsigned o = 0; o < O; ++o) |
| 449 | { |
| 450 | ScalarType src_element; |
| 451 | switch(data_layout) |
| 452 | { |
| 453 | case DataLayout::NHWC: |
| 454 | { |
| 455 | src_element = src_ptr[o * H * W * I + h * W * I + w * I + i]; |
| 456 | } |
| 457 | break; |
| 458 | default: |
| 459 | { |
| 460 | ARM_COMPUTE_ERROR("Unsupported memory layout."); |
| 461 | } |
| 462 | } |
| 463 | const int x5 = std::floor(((float)o) / interleave_by); |
| 464 | const int x4 = h; |
| 465 | const int x3 = w; |
| 466 | const int x2 = std::floor((float)i / block_by); |
| 467 | const int x1 = o % interleave_by; |
| 468 | const int x0 = i % block_by; |
| 469 | unsigned dst_idx = x5 * H * W * Ip * interleave_by |
| 470 | + x4 * W * Ip * interleave_by |
| 471 | + x3 * Ip * interleave_by |
| 472 | + x2 * interleave_by * block_by |
| 473 | + x1 * block_by |
| 474 | + x0; |
| 475 | dst_ptr[dst_idx] = src_element; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | template <typename ConvolutionFunction, typename TensorClass, typename AccessorType, typename ScalarType> |
| 480 | class VariableWeightsFixtureBaseClass : public framework::Fixture |
| 481 | { |
| 482 | public: |
| 483 | template <typename...> |
| 484 | void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, Size2D dilation, DataLayout data_layout, |
| 485 | const DataType data_type) |
| 486 | { |
| 487 | conv = std::make_unique<ConvolutionFunction>(); |
| 488 | // prepare data |
| 489 | _data_layout = data_layout; |
| 490 | // Fixed format kernels for variable weights can work only with NHWC format. |
| 491 | ARM_COMPUTE_EXPECT_EQUAL(_data_layout, DataLayout::NHWC, framework::LogLevel::ERRORS); |
| 492 | _data_type = data_type; |
| 493 | // run the code |
| 494 | compute_target(input_shape, weights_shape, bias_shape, output_shape, info, dilation); |
| 495 | compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, dilation); |
| 496 | } |
| 497 | void teardown() |
| 498 | { |
| 499 | _target.allocator()->free(); |
| 500 | } |
| 501 | |
| 502 | protected: |
| 503 | template <typename U> |
| 504 | void fill(U &&tensor, int i) |
| 505 | { |
| 506 | switch(tensor.data_type()) |
| 507 | { |
| 508 | case DataType::F16: |
| 509 | { |
| 510 | arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -1.0f, 1.0f }; |
| 511 | library->fill(tensor, distribution, i); |
| 512 | break; |
| 513 | } |
| 514 | case DataType::F32: |
| 515 | { |
| 516 | std::uniform_real_distribution<float> distribution(-1.0f, 1.0f); |
| 517 | library->fill(tensor, distribution, i); |
| 518 | break; |
| 519 | } |
| 520 | default: |
| 521 | library->fill_tensor_uniform(tensor, i); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | private: |
| 526 | virtual void configure_and_execute_kernel(TensorInfo src_tensor_info, TensorInfo weight_tensor_info, TensorInfo bias_tensor_info, TensorInfo dst_tensor_info, const WeightsInfo weights_info, |
| 527 | const PadStrideInfo &conv_info, |
| 528 | const Size2D &dilation) = 0; |
| 529 | |
| 530 | void compute_target(TensorShape input_shape, TensorShape weights_shape, const TensorShape &bias_shape, TensorShape output_shape, const PadStrideInfo &conv_info, |
| 531 | const Size2D &dilation) |
| 532 | { |
| 533 | // The dataset is always in NCHW format - we need to make C the |
| 534 | // innermost dimension because the fixed-format kernel work only |
| 535 | // with NHWC layout. |
| 536 | permute(input_shape, PermutationVector(2U, 0U, 1U)); |
| 537 | permute(weights_shape, PermutationVector(2U, 0U, 1U)); |
| 538 | permute(output_shape, PermutationVector(2U, 0U, 1U)); |
| 539 | const auto src_tensor_info = TensorInfo(input_shape, 1, _data_type, _data_layout); |
| 540 | const auto weight_tensor_info = TensorInfo(weights_shape, 1, _data_type, _data_layout); |
| 541 | const auto bias_tensor_info = TensorInfo(bias_shape, 1, _data_type, _data_layout); |
| 542 | auto dst_tensor_info = TensorInfo(output_shape, 1, _data_type, _data_layout); |
| 543 | |
| 544 | const int kernel_height = weights_shape[get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT)]; |
| 545 | const int kernel_width = weights_shape[get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH)]; |
| 546 | const int num_kernels = weights_shape[get_data_layout_dimension_index(_data_layout, DataLayoutDimension::BATCHES)]; |
| 547 | |
| 548 | const WeightsInfo query_weights_info(/*reshape_weights*/ false, kernel_width, kernel_height, num_kernels, false, arm_gemm::WeightFormat::ANY); |
| 549 | const bool kernel_found = bool(ConvolutionFunction::has_opt_impl(_computed_weight_format, &src_tensor_info, &weight_tensor_info, |
| 550 | &bias_tensor_info, &dst_tensor_info, conv_info, query_weights_info)); |
| 551 | // Make surethat the setup founds a fixed-format kernel as requested by the test case. |
| 552 | ARM_COMPUTE_EXPECT(kernel_found, framework::LogLevel::ERRORS); |
| 553 | ARM_COMPUTE_EXPECT(arm_gemm::is_fixed_format(_computed_weight_format), framework::LogLevel::ERRORS); |
| 554 | |
| 555 | const WeightsInfo weights_info(/*reshape_weights*/ false, kernel_width, kernel_height, num_kernels, false, _computed_weight_format); |
| 556 | configure_and_execute_kernel(src_tensor_info, weight_tensor_info, bias_tensor_info, dst_tensor_info, weights_info, conv_info, |
| 557 | dilation); |
| 558 | } |
| 559 | void compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info, |
| 560 | const Size2D &dilation) |
| 561 | { |
| 562 | ARM_COMPUTE_UNUSED(input_shape, weights_shape, bias_shape, output_shape, info, |
| 563 | dilation); |
| 564 | |
| 565 | // Create reference |
| 566 | SimpleTensor<ScalarType> src{ input_shape, _data_type }; |
| 567 | SimpleTensor<ScalarType> weights{ weights_shape, _data_type }; |
| 568 | SimpleTensor<ScalarType> bias{ bias_shape, _data_type }; |
| 569 | fill(src, 0); |
| 570 | fill(bias, 1); |
| 571 | fill(weights, 3); |
| 572 | _reference = reference::convolution_layer<ScalarType>(src, weights, bias, output_shape, info, dilation, 1 /*num_groups*/); |
| 573 | } |
| 574 | DataLayout _data_layout{}; |
| 575 | DataType _data_type{}; |
| 576 | |
| 577 | protected: |
| 578 | std::unique_ptr<ConvolutionFunction> conv{}; |
| 579 | arm_gemm::WeightFormat _computed_weight_format{ arm_gemm::WeightFormat::UNSPECIFIED }; |
| 580 | TensorClass _target{}; |
| 581 | SimpleTensor<ScalarType> _reference{}; |
| 582 | }; |
| 583 | |
| 584 | template <typename ConvolutionFunction, typename TensorClass, typename AccessorType, typename ScalarType> |
| 585 | class VariableWeightsFixture : public VariableWeightsFixtureBaseClass<ConvolutionFunction, TensorClass, AccessorType, ScalarType> |
| 586 | { |
| 587 | void configure_and_execute_kernel(TensorInfo src_tensor_info, TensorInfo weight_tensor_info, TensorInfo bias_tensor_info, TensorInfo dst_tensor_info, const WeightsInfo weights_info, |
| 588 | const PadStrideInfo &conv_info, |
| 589 | const Size2D &dilation) |
| 590 | { |
| 591 | this->conv->configure(&src_tensor_info, &weight_tensor_info, &bias_tensor_info, &dst_tensor_info, conv_info, weights_info, dilation); |
| 592 | |
| 593 | // Allocate input tensors |
| 594 | auto src = create_tensor<TensorClass>(src_tensor_info); |
| 595 | auto weights_original = create_tensor<TensorClass>(weight_tensor_info); |
| 596 | const TensorInfo new_tensor_info = prepare_weights(weight_tensor_info, this->_computed_weight_format); |
| 597 | auto weights_transformed = create_tensor<TensorClass>(new_tensor_info); |
| 598 | auto bias = create_tensor<TensorClass>(bias_tensor_info); |
| 599 | src.allocator()->allocate(); |
| 600 | weights_original.allocator()->allocate(); |
| 601 | weights_transformed.allocator()->allocate(); |
| 602 | bias.allocator()->allocate(); |
| 603 | // Allocate destination tensor |
| 604 | this->_target = create_tensor<TensorClass>(dst_tensor_info); |
| 605 | this->_target.allocator()->allocate(); |
| 606 | |
| 607 | // Prepare source and biases that are left unchanged. |
| 608 | this->fill(AccessorType(src), 0); |
| 609 | this->fill(AccessorType(bias), 1); |
| 610 | |
| 611 | // First run |
| 612 | this->fill(AccessorType(weights_original), 2); |
| 613 | rearrange_data<ScalarType, AccessorType>(AccessorType(weights_original), AccessorType(weights_transformed), this->_computed_weight_format); |
| 614 | ITensorPack run_pack{ { TensorType::ACL_SRC_0, &src }, { TensorType::ACL_SRC_1, &weights_transformed }, { TensorType::ACL_SRC_2, &bias }, { TensorType::ACL_DST, &(this->_target) } }; |
| 615 | this->conv->run(run_pack); |
| 616 | // Second run, with new weights |
| 617 | this->fill(AccessorType(weights_original), 3); |
| 618 | rearrange_data<ScalarType, AccessorType>(AccessorType(weights_original), AccessorType(weights_transformed), this->_computed_weight_format); |
| 619 | this->conv->run(run_pack); |
| 620 | src.allocator()->free(); |
| 621 | weights_original.allocator()->free(); |
| 622 | weights_transformed.allocator()->free(); |
| 623 | bias.allocator()->free(); |
| 624 | } |
| 625 | }; |
| 626 | |
| 627 | template <typename ConvolutionFunction, typename TensorClass, typename AccessorType, typename ScalarType> |
| 628 | class VariableWeightsFixtureNEInterface : public VariableWeightsFixtureBaseClass<ConvolutionFunction, TensorClass, AccessorType, ScalarType> |
| 629 | { |
| 630 | void configure_and_execute_kernel(TensorInfo src_tensor_info, TensorInfo weight_tensor_info, TensorInfo bias_tensor_info, TensorInfo dst_tensor_info, const WeightsInfo weights_info, |
| 631 | const PadStrideInfo &conv_info, |
| 632 | const Size2D &dilation) |
| 633 | { |
| 634 | // Allocate input tensors |
| 635 | auto src = create_tensor<TensorClass>(src_tensor_info); |
| 636 | auto weights_original = create_tensor<TensorClass>(weight_tensor_info); |
| 637 | const TensorInfo new_tensor_info = prepare_weights(weight_tensor_info, this->_computed_weight_format); |
| 638 | auto weights_transformed = create_tensor<TensorClass>(new_tensor_info); |
| 639 | auto bias = create_tensor<TensorClass>(bias_tensor_info); |
| 640 | src.allocator()->allocate(); |
| 641 | weights_original.allocator()->allocate(); |
| 642 | weights_transformed.allocator()->allocate(); |
| 643 | bias.allocator()->allocate(); |
| 644 | // Allocate destination tensor |
| 645 | this->_target = create_tensor<TensorClass>(dst_tensor_info); |
| 646 | this->_target.allocator()->allocate(); |
| 647 | this->conv->configure(&src, &weights_transformed, &bias, &(this->_target), conv_info, weights_info, dilation); |
| 648 | // Prepare source and biases that are left unchanged. |
| 649 | this->fill(AccessorType(src), 0); |
| 650 | this->fill(AccessorType(bias), 1); |
| 651 | |
| 652 | // First run |
| 653 | this->fill(AccessorType(weights_original), 2); |
| 654 | rearrange_data<ScalarType, AccessorType>(AccessorType(weights_original), AccessorType(weights_transformed), this->_computed_weight_format); |
| 655 | this->conv->run(); |
| 656 | // Second run, with new weights |
| 657 | this->fill(AccessorType(weights_original), 3); |
| 658 | rearrange_data<ScalarType, AccessorType>(AccessorType(weights_original), AccessorType(weights_transformed), this->_computed_weight_format); |
| 659 | this->conv->run(); |
| 660 | src.allocator()->free(); |
| 661 | weights_original.allocator()->free(); |
| 662 | weights_transformed.allocator()->free(); |
| 663 | bias.allocator()->free(); |
| 664 | } |
| 665 | }; |
| 666 | |
| 667 | template <typename ConvolutionClass> |
| 668 | class HasOptImplFixture : public framework::Fixture |
| 669 | { |
| 670 | public: |
| 671 | template <typename...> |
| 672 | void setup(DataType data_type, arm_gemm::WeightFormat query_weight_format) |
| 673 | { |
| 674 | auto conv = std::make_unique<ConvolutionClass>(); |
| 675 | const auto src_info = TensorInfo(TensorShape(1U, 5U, 2U), 1, data_type, DataLayout::NHWC); |
| 676 | const auto weight_info = TensorInfo(TensorShape(1U, 3U, 2U, 3U), 1, data_type, DataLayout::NHWC); |
| 677 | const auto bias_info = TensorInfo(TensorShape(3U), 1, data_type, DataLayout::NHWC); |
| 678 | auto dst_info = TensorInfo(TensorShape(1U, 7U, 3U), 1, data_type, DataLayout::NHWC); |
| 679 | const auto conv_info = PadStrideInfo(1, 1, 0, 0, 2, 2, DimensionRoundingType::FLOOR); |
| 680 | const WeightsInfo weights_info(false, 3U, 3U, 1U, false, query_weight_format); |
| 681 | _kernel_found = bool(ConvolutionClass::has_opt_impl(_computed_weight_format, &src_info, &weight_info, |
| 682 | &bias_info, &dst_info, conv_info, weights_info)); |
| 683 | } |
| 684 | |
| 685 | protected: |
| 686 | bool _kernel_found{ false }; |
| 687 | arm_gemm::WeightFormat _computed_weight_format{ arm_gemm::WeightFormat::UNSPECIFIED }; |
| 688 | }; |
| 689 | #endif // ARM_COMPUTE_ENABLE_FIXED_FORMAT_KERNELS |
| 690 | |
Moritz Pflanzer | b3d2579 | 2017-07-26 11:49:37 +0100 | [diff] [blame] | 691 | } // namespace validation |
| 692 | } // namespace test |
| 693 | } // namespace arm_compute |
| 694 | #endif /* ARM_COMPUTE_TEST_CONVOLUTION_LAYER_FIXTURE */ |