Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017-2018 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_IM2COL_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_IM2COL_FIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/TensorShape.h" |
| 29 | #include "arm_compute/core/Types.h" |
| 30 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 31 | #include "arm_compute/runtime/Tensor.h" |
| 32 | #include "tests/AssetsLibrary.h" |
| 33 | #include "tests/Globals.h" |
| 34 | #include "tests/IAccessor.h" |
| 35 | #include "tests/framework/Asserts.h" |
| 36 | #include "tests/framework/Fixture.h" |
| 37 | #include "tests/validation/reference/Im2Col.h" |
| 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | namespace test |
| 42 | { |
| 43 | namespace validation |
| 44 | { |
| 45 | using namespace arm_compute::misc::shape_calculator; |
| 46 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 47 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool batch_size_on_z> |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 48 | class Im2ColValidationFixture : public framework::Fixture |
| 49 | { |
| 50 | public: |
| 51 | template <typename...> |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 52 | void setup(TensorShape input_shape, DataType data_type, const Size2D &kernel_dims, const PadStrideInfo &conv_info, const QuantizationInfo &quant_info, const DataLayout &data_layout, |
| 53 | bool channels_first_output_nhwc) |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 54 | { |
| 55 | _kernel_dims = kernel_dims; |
| 56 | _conv_info = conv_info; |
| 57 | _quant_info = quant_info; |
| 58 | _data_layout = data_layout; |
| 59 | _has_bias = data_type != DataType::QASYMM8; |
| 60 | |
| 61 | if(_data_layout == DataLayout::NHWC) |
| 62 | { |
Pablo Tello | 764b1af | 2018-04-23 16:11:45 +0100 | [diff] [blame] | 63 | permute(input_shape, PermutationVector(2U, 0U, 1U)); |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Pablo Tello | 764b1af | 2018-04-23 16:11:45 +0100 | [diff] [blame] | 66 | TensorInfo input_info(input_shape, 1, data_type); |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 67 | input_info.set_data_layout(_data_layout); |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 68 | |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 69 | const TensorShape output_shape = compute_im2col_conv_shape(&input_info, _kernel_dims, _conv_info, _has_bias, Size2D(1U, 1U), batch_size_on_z); |
Pablo Tello | 4a626a7 | 2018-04-04 10:01:14 +0100 | [diff] [blame] | 70 | _target = compute_target(input_shape, output_shape, data_type); |
Pablo Tello | 764b1af | 2018-04-23 16:11:45 +0100 | [diff] [blame] | 71 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 72 | compute_reference(input_shape, output_shape, data_type, channels_first_output_nhwc); |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | protected: |
| 76 | template <typename U> |
| 77 | void fill(U &&tensor) |
| 78 | { |
| 79 | library->fill_tensor_uniform(tensor, 0); |
| 80 | } |
| 81 | |
Pablo Tello | 764b1af | 2018-04-23 16:11:45 +0100 | [diff] [blame] | 82 | TensorType compute_target(const TensorShape &input_shape, const TensorShape &output_shape, DataType data_type) |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 83 | { |
| 84 | // Create tensors |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 85 | TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, _quant_info, _data_layout); |
| 86 | TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, _quant_info); |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 87 | |
| 88 | // Create and configure function |
| 89 | FunctionType im2col_func; |
| 90 | im2col_func.configure(&src, &dst, _kernel_dims, _conv_info, _has_bias); |
| 91 | |
| 92 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 93 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 94 | |
| 95 | // Allocate tensors |
| 96 | src.allocator()->allocate(); |
| 97 | dst.allocator()->allocate(); |
| 98 | |
| 99 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 100 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 101 | |
| 102 | // Fill tensors |
| 103 | fill(AccessorType(src)); |
| 104 | |
| 105 | // Compute function |
| 106 | im2col_func.run(); |
| 107 | |
| 108 | return dst; |
| 109 | } |
| 110 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 111 | void compute_reference(const TensorShape &input_shape, const TensorShape &output_shape, DataType data_type, bool channels_first_output_nhwc) |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 112 | { |
| 113 | // Create reference |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 114 | SimpleTensor<T> src{ input_shape, data_type, 1, _quant_info, _data_layout }; |
| 115 | _reference = SimpleTensor<T>(output_shape, data_type, 1, _quant_info, DataLayout::NCHW); |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 116 | |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 117 | // Fill reference |
| 118 | fill(src); |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 119 | |
| 120 | reference::im2col<T>(src, _reference, _kernel_dims, _conv_info, _has_bias, channels_first_output_nhwc); |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 121 | } |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 122 | TensorType _target{}; |
| 123 | SimpleTensor<T> _reference{}; |
| 124 | Size2D _kernel_dims{}; |
| 125 | PadStrideInfo _conv_info{}; |
| 126 | DataLayout _data_layout{}; |
| 127 | QuantizationInfo _quant_info{}; |
| 128 | bool _has_bias{}; |
| 129 | }; |
| 130 | } // namespace validation |
| 131 | } // namespace test |
| 132 | } // namespace arm_compute |
| 133 | #endif /* ARM_COMPUTE_TEST_IM2COL_FIXTURE */ |