Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | dea2d2d | 2018-12-19 16:23:17 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +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_PADLAYER_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_PADLAYER_FIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 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" |
| 34 | #include "tests/validation/Helpers.h" |
| 35 | #include "tests/validation/reference/PadLayer.h" |
| 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace test |
| 40 | { |
| 41 | namespace validation |
| 42 | { |
| 43 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 44 | class PaddingFixture : public framework::Fixture |
| 45 | { |
| 46 | public: |
| 47 | template <typename...> |
Usama Arif | 8cf8c11 | 2019-03-14 15:36:54 +0000 | [diff] [blame] | 48 | void setup(TensorShape shape, DataType data_type, const PaddingList &padding, const PaddingMode mode) |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 49 | { |
Usama Arif | 8cf8c11 | 2019-03-14 15:36:54 +0000 | [diff] [blame] | 50 | PaddingList clamped_padding = padding; |
| 51 | if(mode != PaddingMode::CONSTANT) |
| 52 | { |
| 53 | // Clamp padding to prevent applying more than is possible. |
| 54 | for(uint32_t i = 0; i < padding.size(); ++i) |
| 55 | { |
| 56 | if(mode == PaddingMode::REFLECT) |
| 57 | { |
| 58 | clamped_padding[i].first = std::min(static_cast<uint64_t>(padding[i].first), static_cast<uint64_t>(shape[i] - 1)); |
| 59 | clamped_padding[i].second = std::min(static_cast<uint64_t>(padding[i].second), static_cast<uint64_t>(shape[i] - 1)); |
| 60 | } |
| 61 | else |
| 62 | { |
| 63 | clamped_padding[i].first = std::min(static_cast<uint64_t>(padding[i].first), static_cast<uint64_t>(shape[i])); |
| 64 | clamped_padding[i].second = std::min(static_cast<uint64_t>(padding[i].second), static_cast<uint64_t>(shape[i])); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | _target = compute_target(shape, data_type, clamped_padding, mode); |
| 69 | _reference = compute_reference(shape, data_type, clamped_padding, mode); |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | protected: |
| 73 | template <typename U> |
Usama Arif | 8cf8c11 | 2019-03-14 15:36:54 +0000 | [diff] [blame] | 74 | void fill(U &&tensor, int i) |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 75 | { |
Usama Arif | 8cf8c11 | 2019-03-14 15:36:54 +0000 | [diff] [blame] | 76 | library->fill_tensor_uniform(tensor, i); |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | TensorType compute_target(const TensorShape &shape, |
| 80 | DataType data_type, |
Usama Arif | 8cf8c11 | 2019-03-14 15:36:54 +0000 | [diff] [blame] | 81 | const PaddingList &paddings, |
| 82 | const PaddingMode mode) |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 83 | { |
| 84 | // Create tensors |
| 85 | TensorType src = create_tensor<TensorType>(shape, data_type); |
| 86 | TensorType dst; |
| 87 | |
Usama Arif | 8cf8c11 | 2019-03-14 15:36:54 +0000 | [diff] [blame] | 88 | TensorType const_val = create_tensor<TensorType>(TensorShape(1), data_type); |
| 89 | const_val.allocator()->allocate(); |
| 90 | fill(AccessorType(const_val), 1); |
| 91 | T const_value = *static_cast<T *>(AccessorType(const_val)(Coordinates(0))); |
| 92 | |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 93 | // Create and configure function |
| 94 | FunctionType padding; |
Usama Arif | 8cf8c11 | 2019-03-14 15:36:54 +0000 | [diff] [blame] | 95 | padding.configure(&src, &dst, paddings, const_value, mode); |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 96 | |
| 97 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 98 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 99 | |
| 100 | // Allocate tensors |
| 101 | src.allocator()->allocate(); |
| 102 | dst.allocator()->allocate(); |
| 103 | |
| 104 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 105 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 106 | |
| 107 | // Fill tensors |
Usama Arif | 8cf8c11 | 2019-03-14 15:36:54 +0000 | [diff] [blame] | 108 | fill(AccessorType(src), 0); |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 109 | |
| 110 | // Compute function |
| 111 | padding.run(); |
| 112 | |
| 113 | return dst; |
| 114 | } |
| 115 | |
| 116 | SimpleTensor<T> compute_reference(const TensorShape &shape, DataType data_type, |
Usama Arif | 8cf8c11 | 2019-03-14 15:36:54 +0000 | [diff] [blame] | 117 | const PaddingList &paddings, const PaddingMode mode) |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 118 | { |
| 119 | // Create reference tensor |
| 120 | SimpleTensor<T> src{ shape, data_type }; |
Usama Arif | 8cf8c11 | 2019-03-14 15:36:54 +0000 | [diff] [blame] | 121 | SimpleTensor<T> const_val{ TensorShape(1), data_type }; |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 122 | |
| 123 | // Fill reference tensor |
Usama Arif | 8cf8c11 | 2019-03-14 15:36:54 +0000 | [diff] [blame] | 124 | fill(src, 0); |
| 125 | fill(const_val, 1); |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 126 | |
Usama Arif | 8cf8c11 | 2019-03-14 15:36:54 +0000 | [diff] [blame] | 127 | return reference::pad_layer(src, paddings, const_val[0], mode); |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | TensorType _target{}; |
| 131 | SimpleTensor<T> _reference{}; |
| 132 | }; |
| 133 | |
| 134 | } // namespace validation |
| 135 | } // namespace test |
| 136 | } // namespace arm_compute |
| 137 | #endif /* ARM_COMPUTE_TEST_PADLAYER_FIXTURE */ |