blob: 7320a032bdfb3f22b0285f506074d2bae3578d96 [file] [log] [blame]
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +00001/*
Matthew Bentham945b8da2023-07-12 11:54:59 +00002 * Copyright (c) 2018-2021, 2023 Arm Limited.
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +00003 *
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_STACK_LAYER_FIXTURE
25#define ARM_COMPUTE_TEST_STACK_LAYER_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 "tests/AssetsLibrary.h"
32#include "tests/Globals.h"
33#include "tests/IAccessor.h"
34#include "tests/framework/Asserts.h"
35#include "tests/framework/Fixture.h"
36#include "tests/validation/Helpers.h"
37#include "tests/validation/reference/StackLayer.h"
38#include "tests/validation/reference/Utils.h"
39
40#include <random>
41#include <vector>
42
43namespace arm_compute
44{
45namespace test
46{
47namespace validation
48{
49using namespace arm_compute::misc::shape_calculator;
50
51template <typename TensorType, typename AbstractTensorType, typename AccessorType, typename FunctionType, typename T>
52class StackLayerValidationFixture : public framework::Fixture
53{
54public:
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000055 void setup(TensorShape shape_src, int axis, DataType data_type, int num_tensors)
56 {
57 _target = compute_target(shape_src, axis, data_type, num_tensors);
58 _reference = compute_reference(shape_src, axis, data_type, num_tensors);
59 }
60
61protected:
62 template <typename U>
63 void fill(U &&tensor, unsigned int i)
64 {
65 library->fill_tensor_uniform(tensor, i);
66 }
67
68 TensorType compute_target(TensorShape shape_src, int axis, DataType data_type, int num_tensors)
69 {
70 std::vector<TensorType> tensors(num_tensors);
71 std::vector<AbstractTensorType *> src(num_tensors);
72
73 // Create vector of input tensors
74 for(int i = 0; i < num_tensors; ++i)
75 {
76 tensors[i] = create_tensor<TensorType>(shape_src, data_type);
77 src[i] = &(tensors[i]);
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010078 ARM_COMPUTE_ASSERT(tensors[i].info()->is_resizable());
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000079 }
80
81 // Create tensors
Isabella Gottardiee7c15d2018-12-17 16:15:34 +000082 TensorType dst;
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000083
84 // The output tensor will be auto-initialized within the function
85
86 // Create and configure function
87 FunctionType stack;
88 stack.configure(src, axis, &dst);
89
90 // Allocate and fill the input tensors
91 for(int i = 0; i < num_tensors; ++i)
92 {
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010093 ARM_COMPUTE_ASSERT(tensors[i].info()->is_resizable());
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000094 tensors[i].allocator()->allocate();
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010095 ARM_COMPUTE_ASSERT(!tensors[i].info()->is_resizable());
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +000096
97 // Fill input tensor
98 fill(AccessorType(tensors[i]), i);
99 }
100
101 // Allocate output tensor
102 dst.allocator()->allocate();
103
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100104 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +0000105
106 // Compute stack function
107 stack.run();
108
109 return dst;
110 }
111
112 SimpleTensor<T> compute_reference(const TensorShape &shape_src, int axis, DataType data_type, int num_tensors)
113 {
114 std::vector<SimpleTensor<T>> src;
115
116 for(int i = 0; i < num_tensors; ++i)
117 {
118 src.emplace_back(std::move(SimpleTensor<T>(shape_src, data_type, 1)));
119
120 fill(src[i], i);
121 }
122
123 // Wrap around negative values
124 const unsigned int axis_u = wrap_around(axis, static_cast<int>(shape_src.num_dimensions() + 1));
125
126 const TensorShape shape_dst = compute_stack_shape(TensorInfo(shape_src, 1, data_type), axis_u, num_tensors);
127
128 return reference::stack_layer<T>(src, shape_dst, data_type, axis_u);
129 }
130
131 TensorType _target{};
132 SimpleTensor<T> _reference{};
133};
134} // namespace validation
135} // namespace test
136} // namespace arm_compute
137#endif /* ARM_COMPUTE_TEST_STACK_LAYER_FIXTURE */