blob: 30b7dd55390c8666e62629f1f017cad0dc42bbe8 [file] [log] [blame]
Pablo Tello54303692018-11-22 16:14:36 +00001/*
Matthew Bentham945b8da2023-07-12 11:54:59 +00002 * Copyright (c) 2018-2021, 2023 Arm Limited.
Pablo Tello54303692018-11-22 16:14:36 +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_UNSTACK_FIXTURE
25#define ARM_COMPUTE_TEST_UNSTACK_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
29#include "arm_compute/core/utils/misc/ShapeCalculator.h"
30#include "tests/AssetsLibrary.h"
31#include "tests/Globals.h"
32#include "tests/IAccessor.h"
33#include "tests/framework/Asserts.h"
34#include "tests/framework/Fixture.h"
35#include "tests/validation/Helpers.h"
36#include "tests/validation/reference/Unstack.h"
37
38#include <random>
39
40namespace arm_compute
41{
42namespace test
43{
44namespace validation
45{
46template <typename TensorType, typename ITensorType, typename AccessorType, typename FunctionType, typename T>
47class UnstackValidationFixture : public framework::Fixture
48{
49public:
Pablo Tello54303692018-11-22 16:14:36 +000050 void setup(TensorShape input_shape, int axis, int num, DataType data_type)
51 {
52 _target = compute_target(input_shape, axis, num, data_type);
53 _reference = compute_reference(input_shape, axis, num, data_type);
54 }
55
56protected:
57 template <typename U>
58 void fill(U &&tensor, int i)
59 {
60 library->fill_tensor_uniform(tensor, i);
61 }
62
63 std::vector<TensorType> compute_target(TensorShape input_shape, int axis, unsigned int num, DataType data_type)
64 {
65 TensorType input_tensor = create_tensor<TensorType>(input_shape, data_type);
66 const unsigned int axis_u = wrap_around(axis, static_cast<int>(input_shape.num_dimensions()));
67 const unsigned int axis_size = input_shape[axis_u];
68 const unsigned int num_slices = std::min(num, axis_size);
69 std::vector<TensorType> output_slices(num_slices);
70 std::vector<ITensorType *> output_ptrs(num_slices);
71 for(size_t k = 0; k < num_slices; ++k)
72 {
73 output_ptrs[k] = &output_slices[k];
74 }
75 // Create and configure function
76 FunctionType unstack;
77 unstack.configure(&input_tensor, output_ptrs, axis);
78 // Allocate tensors
79 for(auto &out : output_slices)
80 {
81 out.allocator()->allocate();
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010082 ARM_COMPUTE_ASSERT(!out.info()->is_resizable());
Pablo Tello54303692018-11-22 16:14:36 +000083 }
84 input_tensor.allocator()->allocate();
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010085 ARM_COMPUTE_ASSERT(!input_tensor.info()->is_resizable());
Pablo Tello54303692018-11-22 16:14:36 +000086 fill(AccessorType(input_tensor), 0);
87 // Compute function
88 unstack.run();
89 return output_slices;
90 }
91
92 std::vector<SimpleTensor<T>> compute_reference(TensorShape input_shape, int axis, unsigned int num, DataType data_type)
93 {
94 const unsigned int axis_u = wrap_around(axis, static_cast<int>(input_shape.num_dimensions()));
95 const unsigned int axis_size = input_shape[axis_u];
96 const unsigned int num_output_tensors = (num == 0) ? axis_size : std::min(axis_size, num);
97 // create and fill input tensor
98 SimpleTensor<T> input_tensor{ input_shape, data_type };
99 fill(input_tensor, 0);
100 // create output tensors
101 const TensorShape slice_shape = arm_compute::misc::shape_calculator::calculate_unstack_shape(input_shape, axis_u);
102 std::vector<SimpleTensor<T>> output_tensors(num_output_tensors);
103 for(size_t k = 0; k < num_output_tensors; ++k)
104 {
105 output_tensors[k] = SimpleTensor<T>(slice_shape, data_type);
106 }
107
108 return reference::unstack<T>(input_tensor, output_tensors, axis);
109 }
110
111 std::vector<TensorType> _target{};
112 std::vector<SimpleTensor<T>> _reference{};
113};
114} // namespace validation
115} // namespace test
116} // namespace arm_compute
117#endif /* ARM_COMPUTE_TEST_UNSTACK_FIXTURE */