1 /* 2 * Copyright (c) 2018-2021 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_PADLAYER_FIXTURE 25 #define ARM_COMPUTE_TEST_PADLAYER_FIXTURE 26 27 #include "arm_compute/core/TensorShape.h" 28 #include "arm_compute/core/Types.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" 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...> setup(TensorShape shape,DataType data_type,const PaddingList & padding,const PaddingMode mode)48 void setup(TensorShape shape, DataType data_type, const PaddingList &padding, const PaddingMode mode) 49 { 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); 70 } 71 72 protected: 73 template <typename U> fill(U && tensor,int i)74 void fill(U &&tensor, int i) 75 { 76 library->fill_tensor_uniform(tensor, i); 77 } 78 compute_target(const TensorShape & shape,DataType data_type,const PaddingList & paddings,const PaddingMode mode)79 TensorType compute_target(const TensorShape &shape, 80 DataType data_type, 81 const PaddingList &paddings, 82 const PaddingMode mode) 83 { 84 // Create tensors 85 TensorType src = create_tensor<TensorType>(shape, data_type); 86 TensorType dst; 87 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 93 // Create and configure function 94 FunctionType padding; 95 padding.configure(&src, &dst, paddings, const_value, mode); 96 97 ARM_COMPUTE_ASSERT(src.info()->is_resizable()); 98 ARM_COMPUTE_ASSERT(dst.info()->is_resizable()); 99 100 // Allocate tensors 101 src.allocator()->allocate(); 102 dst.allocator()->allocate(); 103 104 ARM_COMPUTE_ASSERT(!src.info()->is_resizable()); 105 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); 106 107 // Fill tensors 108 fill(AccessorType(src), 0); 109 110 // Compute function 111 padding.run(); 112 return dst; 113 } 114 compute_reference(const TensorShape & shape,DataType data_type,const PaddingList & paddings,const PaddingMode mode)115 SimpleTensor<T> compute_reference(const TensorShape &shape, DataType data_type, 116 const PaddingList &paddings, const PaddingMode mode) 117 { 118 // Create reference tensor 119 SimpleTensor<T> src{ shape, data_type }; 120 SimpleTensor<T> const_val{ TensorShape(1), data_type }; 121 122 // Fill reference tensor 123 fill(src, 0); 124 fill(const_val, 1); 125 126 return reference::pad_layer(src, paddings, const_val[0], mode); 127 } 128 129 TensorType _target{}; 130 SimpleTensor<T> _reference{}; 131 }; 132 133 } // namespace validation 134 } // namespace test 135 } // namespace arm_compute 136 #endif /* ARM_COMPUTE_TEST_PADLAYER_FIXTURE */ 137