blob: 93b43616ff0bd71a969447769e6d5cc4b987adee [file] [log] [blame]
Giuseppe Rossinid7647d42018-07-17 18:13:13 +01001/*
Matthew Bentham945b8da2023-07-12 11:54:59 +00002 * Copyright (c) 2018-2021, 2023 Arm Limited.
Giuseppe Rossinid7647d42018-07-17 18:13:13 +01003 *
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 Rossinid7647d42018-07-17 18:13:13 +010029#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
37namespace arm_compute
38{
39namespace test
40{
41namespace validation
42{
43template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
44class PaddingFixture : public framework::Fixture
45{
46public:
Usama Arif8cf8c112019-03-14 15:36:54 +000047 void setup(TensorShape shape, DataType data_type, const PaddingList &padding, const PaddingMode mode)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010048 {
Usama Arif8cf8c112019-03-14 15:36:54 +000049 PaddingList clamped_padding = padding;
50 if(mode != PaddingMode::CONSTANT)
51 {
52 // Clamp padding to prevent applying more than is possible.
53 for(uint32_t i = 0; i < padding.size(); ++i)
54 {
55 if(mode == PaddingMode::REFLECT)
56 {
57 clamped_padding[i].first = std::min(static_cast<uint64_t>(padding[i].first), static_cast<uint64_t>(shape[i] - 1));
58 clamped_padding[i].second = std::min(static_cast<uint64_t>(padding[i].second), static_cast<uint64_t>(shape[i] - 1));
59 }
60 else
61 {
62 clamped_padding[i].first = std::min(static_cast<uint64_t>(padding[i].first), static_cast<uint64_t>(shape[i]));
63 clamped_padding[i].second = std::min(static_cast<uint64_t>(padding[i].second), static_cast<uint64_t>(shape[i]));
64 }
65 }
66 }
67 _target = compute_target(shape, data_type, clamped_padding, mode);
68 _reference = compute_reference(shape, data_type, clamped_padding, mode);
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010069 }
70
71protected:
72 template <typename U>
Usama Arif8cf8c112019-03-14 15:36:54 +000073 void fill(U &&tensor, int i)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010074 {
Usama Arif8cf8c112019-03-14 15:36:54 +000075 library->fill_tensor_uniform(tensor, i);
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010076 }
77
78 TensorType compute_target(const TensorShape &shape,
79 DataType data_type,
Usama Arif8cf8c112019-03-14 15:36:54 +000080 const PaddingList &paddings,
81 const PaddingMode mode)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010082 {
83 // Create tensors
84 TensorType src = create_tensor<TensorType>(shape, data_type);
85 TensorType dst;
86
Usama Arif8cf8c112019-03-14 15:36:54 +000087 TensorType const_val = create_tensor<TensorType>(TensorShape(1), data_type);
88 const_val.allocator()->allocate();
89 fill(AccessorType(const_val), 1);
90 T const_value = *static_cast<T *>(AccessorType(const_val)(Coordinates(0)));
91
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010092 // Create and configure function
93 FunctionType padding;
Usama Arif8cf8c112019-03-14 15:36:54 +000094 padding.configure(&src, &dst, paddings, const_value, mode);
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010095
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010096 ARM_COMPUTE_ASSERT(src.info()->is_resizable());
97 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010098
99 // Allocate tensors
100 src.allocator()->allocate();
101 dst.allocator()->allocate();
102
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100103 ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
104 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100105
106 // Fill tensors
Usama Arif8cf8c112019-03-14 15:36:54 +0000107 fill(AccessorType(src), 0);
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100108
109 // Compute function
110 padding.run();
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100111 return dst;
112 }
113
114 SimpleTensor<T> compute_reference(const TensorShape &shape, DataType data_type,
Usama Arif8cf8c112019-03-14 15:36:54 +0000115 const PaddingList &paddings, const PaddingMode mode)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100116 {
117 // Create reference tensor
118 SimpleTensor<T> src{ shape, data_type };
Usama Arif8cf8c112019-03-14 15:36:54 +0000119 SimpleTensor<T> const_val{ TensorShape(1), data_type };
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100120
121 // Fill reference tensor
Usama Arif8cf8c112019-03-14 15:36:54 +0000122 fill(src, 0);
123 fill(const_val, 1);
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100124
Usama Arif8cf8c112019-03-14 15:36:54 +0000125 return reference::pad_layer(src, paddings, const_val[0], mode);
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100126 }
127
128 TensorType _target{};
129 SimpleTensor<T> _reference{};
130};
131
132} // namespace validation
133} // namespace test
134} // namespace arm_compute
135#endif /* ARM_COMPUTE_TEST_PADLAYER_FIXTURE */