blob: 9d047a006714e61837ecac522193f8a9357071a9 [file] [log] [blame]
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +00001/*
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +01002 * Copyright (c) 2018-2021 Arm Limited.
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +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_REVERSE_FIXTURE
25#define ARM_COMPUTE_TEST_REVERSE_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/runtime/Tensor.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/reference/Reverse.h"
37
38namespace arm_compute
39{
40namespace test
41{
42namespace validation
43{
44template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
45class ReverseValidationFixture : public framework::Fixture
46{
47public:
48 template <typename...>
49 void setup(TensorShape shape, TensorShape axis_shape, DataType data_type)
50 {
51 _target = compute_target(shape, axis_shape, data_type);
52 _reference = compute_reference(shape, axis_shape, data_type);
53 }
54
55protected:
56 template <typename U>
57 void fill(U &&tensor)
58 {
59 library->fill_tensor_uniform(tensor, 0);
60 }
Michalis Spyrou110b9202018-12-28 16:32:49 +000061 std::vector<int> generate_random_axis()
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +000062 {
Michalis Spyrou110b9202018-12-28 16:32:49 +000063 std::vector<int> axis_v = { 0, 1, 2, 3 };
64 std::mt19937 g(0);
65 std::shuffle(axis_v.begin(), axis_v.end(), g);
66
67 return axis_v;
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +000068 }
69
70 TensorType compute_target(const TensorShape &shape, const TensorShape &axis_shape, DataType data_type)
71 {
72 // Create tensors
73 TensorType src = create_tensor<TensorType>(shape, data_type, 1);
74 TensorType axis = create_tensor<TensorType>(axis_shape, DataType::U32, 1);
75 TensorType dst;
76
77 // Create and configure function
78 FunctionType reverse_func;
79 reverse_func.configure(&src, &dst, &axis);
80
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010081 ARM_COMPUTE_ASSERT(src.info()->is_resizable());
82 ARM_COMPUTE_ASSERT(axis.info()->is_resizable());
83 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +000084
85 // Allocate tensors
86 src.allocator()->allocate();
87 axis.allocator()->allocate();
88 dst.allocator()->allocate();
89
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010090 ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
91 ARM_COMPUTE_ASSERT(!axis.info()->is_resizable());
92 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +000093
94 // Fill tensors
95 fill(AccessorType(src));
Michalis Spyrou110b9202018-12-28 16:32:49 +000096 {
97 auto axis_data = AccessorType(axis);
98 auto axis_v = generate_random_axis();
99 std::copy(axis_v.begin(), axis_v.begin() + axis_shape.x(), static_cast<int32_t *>(axis_data.data()));
100 }
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +0000101
102 // Compute function
103 reverse_func.run();
104
105 return dst;
106 }
107
108 SimpleTensor<T> compute_reference(const TensorShape &shape, const TensorShape &axis_shape, DataType data_type)
109 {
110 // Create reference
111 SimpleTensor<T> src{ shape, data_type };
112 SimpleTensor<uint32_t> axis{ axis_shape, DataType::U32 };
113
114 // Fill reference
115 fill(src);
Michalis Spyrou110b9202018-12-28 16:32:49 +0000116 auto axis_v = generate_random_axis();
117 std::copy(axis_v.begin(), axis_v.begin() + axis_shape.x(), axis.data());
Michele Di Giorgio5daeffd2018-11-26 10:01:15 +0000118
119 return reference::reverse<T>(src, axis);
120 }
121
122 TensorType _target{};
123 SimpleTensor<T> _reference{};
124};
125} // namespace validation
126} // namespace test
127} // namespace arm_compute
128#endif /* ARM_COMPUTE_TEST_REVERSE_FIXTURE */