Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019 ARM Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #ifndef ARM_COMPUTE_TEST_FFT_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_FFT_FIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/Types.h" |
| 28 | #include "arm_compute/runtime/FunctionDescriptors.h" |
| 29 | #include "tests/AssetsLibrary.h" |
| 30 | #include "tests/Globals.h" |
| 31 | #include "tests/IAccessor.h" |
| 32 | #include "tests/framework/Asserts.h" |
| 33 | #include "tests/framework/Fixture.h" |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 34 | #include "tests/validation/reference/ActivationLayer.h" |
| 35 | #include "tests/validation/reference/ConvolutionLayer.h" |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 36 | #include "tests/validation/reference/DFT.h" |
| 37 | |
| 38 | #include <random> |
| 39 | |
| 40 | namespace arm_compute |
| 41 | { |
| 42 | namespace test |
| 43 | { |
| 44 | namespace validation |
| 45 | { |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 46 | template <typename TensorType, typename AccessorType, typename FunctionType, typename InfoType, typename T> |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 47 | class FFTValidationFixture : public framework::Fixture |
| 48 | { |
| 49 | public: |
| 50 | template <typename...> |
| 51 | void setup(TensorShape shape, DataType data_type) |
| 52 | { |
| 53 | _target = compute_target(shape, data_type); |
| 54 | _reference = compute_reference(shape, data_type); |
| 55 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(_target.info()->tensor_shape(), _reference.shape()); |
| 56 | } |
| 57 | |
| 58 | protected: |
| 59 | template <typename U> |
| 60 | void fill(U &&tensor) |
| 61 | { |
| 62 | std::uniform_real_distribution<float> distribution(-5.f, 5.f); |
| 63 | library->fill(tensor, distribution, 0); |
| 64 | } |
| 65 | |
| 66 | TensorType compute_target(const TensorShape &shape, DataType data_type) |
| 67 | { |
| 68 | // Create tensors |
| 69 | TensorType src = create_tensor<TensorType>(shape, data_type, 2); |
| 70 | TensorType dst = create_tensor<TensorType>(shape, data_type, 2); |
| 71 | |
| 72 | // Create and configure function |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 73 | FunctionType fft; |
| 74 | fft.configure(&src, &dst, InfoType()); |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 75 | |
| 76 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 77 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 78 | |
| 79 | // Allocate tensors |
| 80 | src.allocator()->allocate(); |
| 81 | dst.allocator()->allocate(); |
| 82 | |
| 83 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 84 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 85 | |
| 86 | // Fill tensors |
| 87 | fill(AccessorType(src)); |
| 88 | |
| 89 | // Compute function |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 90 | fft.run(); |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 91 | |
| 92 | return dst; |
| 93 | } |
| 94 | |
| 95 | SimpleTensor<T> compute_reference(const TensorShape &shape, DataType data_type) |
| 96 | { |
| 97 | // Create reference |
| 98 | SimpleTensor<T> src{ shape, data_type, 2 }; |
| 99 | |
| 100 | // Fill reference |
| 101 | fill(src); |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 102 | if(std::is_same<InfoType, FFT1DInfo>::value) |
| 103 | { |
| 104 | return reference::dft_1d(src, reference::FFTDirection::Forward); |
| 105 | } |
| 106 | else |
| 107 | { |
| 108 | return reference::dft_2d(src, reference::FFTDirection::Forward); |
| 109 | } |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | TensorType _target{}; |
| 113 | SimpleTensor<T> _reference{}; |
| 114 | }; |
Georgios Pinitas | 8be9148 | 2019-03-26 17:23:28 +0000 | [diff] [blame] | 115 | |
| 116 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 117 | class FFTConvolutionValidationGenericFixture : public framework::Fixture |
| 118 | { |
| 119 | public: |
| 120 | template <typename...> |
| 121 | void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, Size2D dilation, |
| 122 | DataType data_type, DataLayout data_layout, ActivationLayerInfo act_info) |
| 123 | { |
| 124 | _data_type = data_type; |
| 125 | _data_layout = data_layout; |
| 126 | |
| 127 | _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, dilation, act_info); |
| 128 | _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, dilation, act_info); |
| 129 | } |
| 130 | |
| 131 | protected: |
| 132 | template <typename U> |
| 133 | void fill(U &&tensor, int i) |
| 134 | { |
| 135 | switch(tensor.data_type()) |
| 136 | { |
| 137 | case DataType::F32: |
| 138 | { |
| 139 | std::uniform_real_distribution<> distribution(-1.0f, 1.0f); |
| 140 | library->fill(tensor, distribution, i); |
| 141 | break; |
| 142 | } |
| 143 | default: |
| 144 | library->fill_tensor_uniform(tensor, i); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | TensorType compute_target(TensorShape input_shape, TensorShape weights_shape, const TensorShape &bias_shape, TensorShape output_shape, const PadStrideInfo &info, |
| 149 | const Size2D &dilation, const ActivationLayerInfo act_info) |
| 150 | { |
| 151 | ARM_COMPUTE_UNUSED(dilation); |
| 152 | ARM_COMPUTE_ERROR_ON((input_shape[2] % weights_shape[2]) != 0); |
| 153 | |
| 154 | if(_data_layout == DataLayout::NHWC) |
| 155 | { |
| 156 | permute(input_shape, PermutationVector(2U, 0U, 1U)); |
| 157 | permute(weights_shape, PermutationVector(2U, 0U, 1U)); |
| 158 | permute(output_shape, PermutationVector(2U, 0U, 1U)); |
| 159 | } |
| 160 | |
| 161 | // Create tensors |
| 162 | TensorType src = create_tensor<TensorType>(input_shape, _data_type, 1, QuantizationInfo(), _data_layout); |
| 163 | TensorType weights = create_tensor<TensorType>(weights_shape, _data_type, 1, QuantizationInfo(), _data_layout); |
| 164 | TensorType bias = create_tensor<TensorType>(bias_shape, _data_type, 1, QuantizationInfo(), _data_layout); |
| 165 | TensorType dst = create_tensor<TensorType>(output_shape, _data_type, 1, QuantizationInfo(), _data_layout); |
| 166 | |
| 167 | // Create and configure function |
| 168 | FunctionType conv; |
| 169 | conv.configure(&src, &weights, &bias, &dst, info, act_info); |
| 170 | |
| 171 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 172 | ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 173 | ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 174 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 175 | |
| 176 | // Allocate tensors |
| 177 | src.allocator()->allocate(); |
| 178 | weights.allocator()->allocate(); |
| 179 | bias.allocator()->allocate(); |
| 180 | dst.allocator()->allocate(); |
| 181 | |
| 182 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 183 | ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 184 | ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 185 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 186 | |
| 187 | // Fill tensors |
| 188 | fill(AccessorType(src), 0); |
| 189 | fill(AccessorType(weights), 1); |
| 190 | fill(AccessorType(bias), 2); |
| 191 | |
| 192 | // Compute convolution function |
| 193 | conv.run(); |
| 194 | |
| 195 | return dst; |
| 196 | } |
| 197 | |
| 198 | SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info, |
| 199 | const Size2D &dilation, const ActivationLayerInfo act_info) |
| 200 | { |
| 201 | ARM_COMPUTE_ERROR_ON((input_shape[2] % weights_shape[2]) != 0); |
| 202 | |
| 203 | // Create reference |
| 204 | SimpleTensor<T> src{ input_shape, _data_type, 1 }; |
| 205 | SimpleTensor<T> weights{ weights_shape, _data_type, 1 }; |
| 206 | SimpleTensor<T> bias{ bias_shape, _data_type, 1 }; |
| 207 | |
| 208 | // Fill reference |
| 209 | fill(src, 0); |
| 210 | fill(weights, 1); |
| 211 | fill(bias, 2); |
| 212 | |
| 213 | return (act_info.enabled()) ? reference::activation_layer<T>(reference::convolution_layer<T>(src, weights, bias, output_shape, info, dilation), act_info) : reference::convolution_layer<T>(src, |
| 214 | weights, bias, output_shape, info, dilation); |
| 215 | } |
| 216 | |
| 217 | TensorType _target{}; |
| 218 | SimpleTensor<T> _reference{}; |
| 219 | DataType _data_type{}; |
| 220 | DataLayout _data_layout{}; |
| 221 | }; |
| 222 | |
| 223 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 224 | class FFTConvolutionValidationFixture : public FFTConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 225 | { |
| 226 | public: |
| 227 | template <typename...> |
| 228 | void setup(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info, Size2D dilation, |
| 229 | DataType data_type, DataLayout data_layout, ActivationLayerInfo act_info) |
| 230 | { |
| 231 | FFTConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, weights_shape, bias_shape, output_shape, info, dilation, |
| 232 | data_type, data_layout, act_info); |
| 233 | } |
| 234 | }; |
Georgios Pinitas | 0bc7849 | 2019-03-18 20:07:37 +0000 | [diff] [blame] | 235 | } // namespace validation |
| 236 | } // namespace test |
| 237 | } // namespace arm_compute |
| 238 | #endif /* ARM_COMPUTE_TEST_FFT_FIXTURE */ |