blob: 1951813df82f16accf99ad64b4bcb6768e29c262 [file] [log] [blame]
Giuseppe Rossinid7647d42018-07-17 18:13:13 +01001/*
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +01002 * Copyright (c) 2018-2021 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:
47 template <typename...>
Usama Arif8cf8c112019-03-14 15:36:54 +000048 void setup(TensorShape shape, DataType data_type, const PaddingList &padding, const PaddingMode mode)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010049 {
Usama Arif8cf8c112019-03-14 15:36:54 +000050 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 Rossinid7647d42018-07-17 18:13:13 +010070 }
71
72protected:
73 template <typename U>
Usama Arif8cf8c112019-03-14 15:36:54 +000074 void fill(U &&tensor, int i)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010075 {
Usama Arif8cf8c112019-03-14 15:36:54 +000076 library->fill_tensor_uniform(tensor, i);
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010077 }
78
79 TensorType compute_target(const TensorShape &shape,
80 DataType data_type,
Usama Arif8cf8c112019-03-14 15:36:54 +000081 const PaddingList &paddings,
82 const PaddingMode mode)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010083 {
84 // Create tensors
85 TensorType src = create_tensor<TensorType>(shape, data_type);
86 TensorType dst;
87
Usama Arif8cf8c112019-03-14 15:36:54 +000088 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 Rossinid7647d42018-07-17 18:13:13 +010093 // Create and configure function
94 FunctionType padding;
Usama Arif8cf8c112019-03-14 15:36:54 +000095 padding.configure(&src, &dst, paddings, const_value, mode);
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010096
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010097 ARM_COMPUTE_ASSERT(src.info()->is_resizable());
98 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010099
100 // Allocate tensors
101 src.allocator()->allocate();
102 dst.allocator()->allocate();
103
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100104 ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
105 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100106
107 // Fill tensors
Usama Arif8cf8c112019-03-14 15:36:54 +0000108 fill(AccessorType(src), 0);
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100109
110 // Compute function
111 padding.run();
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100112 return dst;
113 }
114
115 SimpleTensor<T> compute_reference(const TensorShape &shape, DataType data_type,
Usama Arif8cf8c112019-03-14 15:36:54 +0000116 const PaddingList &paddings, const PaddingMode mode)
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100117 {
118 // Create reference tensor
119 SimpleTensor<T> src{ shape, data_type };
Usama Arif8cf8c112019-03-14 15:36:54 +0000120 SimpleTensor<T> const_val{ TensorShape(1), data_type };
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100121
122 // Fill reference tensor
Usama Arif8cf8c112019-03-14 15:36:54 +0000123 fill(src, 0);
124 fill(const_val, 1);
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100125
Usama Arif8cf8c112019-03-14 15:36:54 +0000126 return reference::pad_layer(src, paddings, const_val[0], mode);
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100127 }
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 */