blob: 3a021661aca4faf3e1b63b20f61b3c357330b95b [file] [log] [blame]
Michalis Spyrou55b3d122018-05-09 09:59:23 +01001/*
Matthew Bentham945b8da2023-07-12 11:54:59 +00002 * Copyright (c) 2018-2021, 2023 Arm Limited.
Michalis Spyrou55b3d122018-05-09 09:59:23 +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_WIDTHCONCATENATE_LAYER_FIXTURE
25#define ARM_COMPUTE_TEST_WIDTHCONCATENATE_LAYER_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"
Pablo Tello3dd5b682019-03-04 14:14:02 +000036#include "tests/validation/reference/ConcatenateLayer.h"
Michalis Spyrou55b3d122018-05-09 09:59:23 +010037
38#include <random>
39
40namespace arm_compute
41{
42namespace test
43{
44namespace validation
45{
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +010046template <typename TensorType, typename ITensorType, typename AccessorType, typename FunctionType, typename T, bool CI = true>
Pablo Tello3dd5b682019-03-04 14:14:02 +000047class ConcatenateLayerValidationFixture : public framework::Fixture
Michalis Spyrou55b3d122018-05-09 09:59:23 +010048{
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010049private:
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +010050 using SrcITensorType = typename std::conditional<CI, const ITensorType, ITensorType>::type;
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010051
Michalis Spyrou55b3d122018-05-09 09:59:23 +010052public:
Pablo Tello3dd5b682019-03-04 14:14:02 +000053 void setup(TensorShape shape, DataType data_type, unsigned int axis)
Michalis Spyrou55b3d122018-05-09 09:59:23 +010054 {
55 // Create input shapes
56 std::mt19937 gen(library->seed());
Michele Di Giorgio27400b92018-11-01 13:44:05 +000057 std::uniform_int_distribution<> num_dis(2, 8);
Pablo Tello54e98d92019-02-05 16:16:19 +000058 std::uniform_int_distribution<> offset_dis(0, 20);
Michalis Spyrou55b3d122018-05-09 09:59:23 +010059
Pablo Tello54e98d92019-02-05 16:16:19 +000060 const int num_tensors = num_dis(gen);
61
62 std::vector<TensorShape> shapes(num_tensors, shape);
63
64 // vector holding the quantization info:
65 // the last element is the output quantization info
66 // all other elements are the quantization info for the input tensors
67 std::vector<QuantizationInfo> qinfo(num_tensors + 1, QuantizationInfo());
68 for(auto &qi : qinfo)
69 {
70 qi = QuantizationInfo(1.f / 255.f, offset_dis(gen));
71 }
Giorgio Arena4bdd1772020-12-17 16:47:07 +000072 std::bernoulli_distribution mutate_dis(0.5f);
73 std::uniform_real_distribution<float> change_dis(-0.25f, 0.f);
Michalis Spyrou55b3d122018-05-09 09:59:23 +010074
75 // Generate more shapes based on the input
76 for(auto &s : shapes)
77 {
Michalis Spyroua9c44722019-04-05 17:18:36 +010078 // Randomly change the dimension
Michalis Spyrou55b3d122018-05-09 09:59:23 +010079 if(mutate_dis(gen))
80 {
81 // Decrease the dimension by a small percentage. Don't increase
82 // as that could make tensor too large.
Pablo Tello3dd5b682019-03-04 14:14:02 +000083 s.set(axis, s[axis] + 2 * static_cast<int>(s[axis] * change_dis(gen)));
Michalis Spyrou55b3d122018-05-09 09:59:23 +010084 }
85 }
86
Pablo Tello3dd5b682019-03-04 14:14:02 +000087 _target = compute_target(shapes, qinfo, data_type, axis);
88 _reference = compute_reference(shapes, qinfo, data_type, axis);
Michalis Spyrou55b3d122018-05-09 09:59:23 +010089 }
90
91protected:
92 template <typename U>
93 void fill(U &&tensor, int i)
94 {
95 library->fill_tensor_uniform(tensor, i);
96 }
97
Pablo Tello3dd5b682019-03-04 14:14:02 +000098 TensorType compute_target(const std::vector<TensorShape> &shapes, const std::vector<QuantizationInfo> &qinfo, DataType data_type, unsigned int axis)
Michalis Spyrou55b3d122018-05-09 09:59:23 +010099 {
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100100 std::vector<TensorType> srcs;
101 std::vector<SrcITensorType *> src_ptrs;
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100102
103 // Create tensors
104 srcs.reserve(shapes.size());
105
Pablo Tello54e98d92019-02-05 16:16:19 +0000106 for(size_t j = 0; j < shapes.size(); ++j)
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100107 {
Pablo Tello54e98d92019-02-05 16:16:19 +0000108 srcs.emplace_back(create_tensor<TensorType>(shapes[j], data_type, 1, qinfo[j]));
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100109 src_ptrs.emplace_back(&srcs.back());
110 }
111
Pablo Tello3dd5b682019-03-04 14:14:02 +0000112 const TensorShape dst_shape = misc::shape_calculator::calculate_concatenate_shape(src_ptrs, axis);
113 TensorType dst = create_tensor<TensorType>(dst_shape, data_type, 1, qinfo[shapes.size()]);
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100114
115 // Create and configure function
Pablo Tello3dd5b682019-03-04 14:14:02 +0000116 FunctionType concat;
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100117 concat.configure(src_ptrs, &dst, axis);
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100118
119 for(auto &src : srcs)
120 {
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100121 ARM_COMPUTE_ASSERT(src.info()->is_resizable());
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100122 }
123
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100124 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100125
126 // Allocate tensors
127 for(auto &src : srcs)
128 {
129 src.allocator()->allocate();
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100130 ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100131 }
132
133 dst.allocator()->allocate();
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100134 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100135
136 // Fill tensors
137 int i = 0;
138 for(auto &src : srcs)
139 {
140 fill(AccessorType(src), i++);
141 }
142
143 // Compute function
Pablo Tello3dd5b682019-03-04 14:14:02 +0000144 concat.run();
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100145
146 return dst;
147 }
148
Michalis Spyroua9c44722019-04-05 17:18:36 +0100149 SimpleTensor<T> compute_reference(std::vector<TensorShape> &shapes, const std::vector<QuantizationInfo> &qinfo, DataType data_type, unsigned int axis)
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100150 {
151 std::vector<SimpleTensor<T>> srcs;
Michalis Spyroua9c44722019-04-05 17:18:36 +0100152 std::vector<TensorShape *> src_ptrs;
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100153
154 // Create and fill tensors
Pablo Tello54e98d92019-02-05 16:16:19 +0000155 for(size_t j = 0; j < shapes.size(); ++j)
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100156 {
Pablo Tello54e98d92019-02-05 16:16:19 +0000157 srcs.emplace_back(shapes[j], data_type, 1, qinfo[j]);
158 fill(srcs.back(), j);
Michalis Spyroua9c44722019-04-05 17:18:36 +0100159 src_ptrs.emplace_back(&shapes[j]);
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100160 }
161
Michalis Spyroua9c44722019-04-05 17:18:36 +0100162 const TensorShape dst_shape = misc::shape_calculator::calculate_concatenate_shape(src_ptrs, axis);
Pablo Tello54e98d92019-02-05 16:16:19 +0000163 SimpleTensor<T> dst{ dst_shape, data_type, 1, qinfo[shapes.size()] };
Pablo Tello3dd5b682019-03-04 14:14:02 +0000164 return reference::concatenate_layer<T>(srcs, dst, axis);
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100165 }
166
167 TensorType _target{};
168 SimpleTensor<T> _reference{};
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100169};
170} // namespace validation
171} // namespace test
172} // namespace arm_compute
173#endif /* ARM_COMPUTE_TEST_WIDTHCONCATENATE_LAYER_FIXTURE */