blob: db09957c092cae03112b840a651452d8bef34ce3 [file] [log] [blame]
Michalis Spyrou55b3d122018-05-09 09:59:23 +01001/*
Pablo Tello54e98d92019-02-05 16:16:19 +00002 * Copyright (c) 2018-2019 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{
46template <typename TensorType, typename ITensorType, typename AccessorType, typename FunctionType, typename T>
Pablo Tello3dd5b682019-03-04 14:14:02 +000047class ConcatenateLayerValidationFixture : public framework::Fixture
Michalis Spyrou55b3d122018-05-09 09:59:23 +010048{
49public:
50 template <typename...>
Pablo Tello3dd5b682019-03-04 14:14:02 +000051 void setup(TensorShape shape, DataType data_type, unsigned int axis)
Michalis Spyrou55b3d122018-05-09 09:59:23 +010052 {
53 // Create input shapes
54 std::mt19937 gen(library->seed());
Michele Di Giorgio27400b92018-11-01 13:44:05 +000055 std::uniform_int_distribution<> num_dis(2, 8);
Pablo Tello54e98d92019-02-05 16:16:19 +000056 std::uniform_int_distribution<> offset_dis(0, 20);
Michalis Spyrou55b3d122018-05-09 09:59:23 +010057
Pablo Tello54e98d92019-02-05 16:16:19 +000058 const int num_tensors = num_dis(gen);
59
60 std::vector<TensorShape> shapes(num_tensors, shape);
61
62 // vector holding the quantization info:
63 // the last element is the output quantization info
64 // all other elements are the quantization info for the input tensors
65 std::vector<QuantizationInfo> qinfo(num_tensors + 1, QuantizationInfo());
66 for(auto &qi : qinfo)
67 {
68 qi = QuantizationInfo(1.f / 255.f, offset_dis(gen));
69 }
Michalis Spyrou55b3d122018-05-09 09:59:23 +010070 std::bernoulli_distribution mutate_dis(0.5f);
71 std::uniform_real_distribution<> change_dis(-0.25f, 0.f);
72
73 // Generate more shapes based on the input
74 for(auto &s : shapes)
75 {
76 // Randomly change the first dimension
77 if(mutate_dis(gen))
78 {
79 // Decrease the dimension by a small percentage. Don't increase
80 // as that could make tensor too large.
Pablo Tello3dd5b682019-03-04 14:14:02 +000081 s.set(axis, s[axis] + 2 * static_cast<int>(s[axis] * change_dis(gen)));
Michalis Spyrou55b3d122018-05-09 09:59:23 +010082 }
83 }
84
Pablo Tello3dd5b682019-03-04 14:14:02 +000085 _target = compute_target(shapes, qinfo, data_type, axis);
86 _reference = compute_reference(shapes, qinfo, data_type, axis);
Michalis Spyrou55b3d122018-05-09 09:59:23 +010087 }
88
89protected:
90 template <typename U>
91 void fill(U &&tensor, int i)
92 {
93 library->fill_tensor_uniform(tensor, i);
94 }
95
Pablo Tello3dd5b682019-03-04 14:14:02 +000096 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 +010097 {
98 std::vector<TensorType> srcs;
99 std::vector<ITensorType *> src_ptrs;
100
101 // Create tensors
102 srcs.reserve(shapes.size());
103
Pablo Tello54e98d92019-02-05 16:16:19 +0000104 for(size_t j = 0; j < shapes.size(); ++j)
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100105 {
Pablo Tello54e98d92019-02-05 16:16:19 +0000106 srcs.emplace_back(create_tensor<TensorType>(shapes[j], data_type, 1, qinfo[j]));
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100107 src_ptrs.emplace_back(&srcs.back());
108 }
109
Pablo Tello3dd5b682019-03-04 14:14:02 +0000110 const TensorShape dst_shape = misc::shape_calculator::calculate_concatenate_shape(src_ptrs, axis);
111 TensorType dst = create_tensor<TensorType>(dst_shape, data_type, 1, qinfo[shapes.size()]);
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100112
113 // Create and configure function
Pablo Tello3dd5b682019-03-04 14:14:02 +0000114 FunctionType concat;
115 switch(axis)
116 {
117 case 0:
118 concat.configure(src_ptrs, &dst, DataLayoutDimension::WIDTH);
119 break;
120 case 1:
121 concat.configure(src_ptrs, &dst, DataLayoutDimension::HEIGHT);
122 break;
123 case 2:
124 concat.configure(src_ptrs, &dst, DataLayoutDimension::CHANNEL);
125 break;
126 default:
127 ARM_COMPUTE_ERROR("Not supported");
128 break;
129 }
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100130
131 for(auto &src : srcs)
132 {
133 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
134 }
135
136 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
137
138 // Allocate tensors
139 for(auto &src : srcs)
140 {
141 src.allocator()->allocate();
142 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
143 }
144
145 dst.allocator()->allocate();
146 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
147
148 // Fill tensors
149 int i = 0;
150 for(auto &src : srcs)
151 {
152 fill(AccessorType(src), i++);
153 }
154
155 // Compute function
Pablo Tello3dd5b682019-03-04 14:14:02 +0000156 concat.run();
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100157
158 return dst;
159 }
160
Pablo Tello3dd5b682019-03-04 14:14:02 +0000161 SimpleTensor<T> compute_reference(const std::vector<TensorShape> &shapes, const std::vector<QuantizationInfo> &qinfo, DataType data_type, unsigned int axis)
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100162 {
163 std::vector<SimpleTensor<T>> srcs;
164
165 // Create and fill tensors
Pablo Tello54e98d92019-02-05 16:16:19 +0000166 for(size_t j = 0; j < shapes.size(); ++j)
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100167 {
Pablo Tello54e98d92019-02-05 16:16:19 +0000168 srcs.emplace_back(shapes[j], data_type, 1, qinfo[j]);
169 fill(srcs.back(), j);
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100170 }
171
Pablo Tello3dd5b682019-03-04 14:14:02 +0000172 const TensorShape dst_shape = calculate_concatenate_shape(shapes, axis);
Pablo Tello54e98d92019-02-05 16:16:19 +0000173 SimpleTensor<T> dst{ dst_shape, data_type, 1, qinfo[shapes.size()] };
Pablo Tello3dd5b682019-03-04 14:14:02 +0000174 return reference::concatenate_layer<T>(srcs, dst, axis);
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100175 }
176
177 TensorType _target{};
178 SimpleTensor<T> _reference{};
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100179};
180} // namespace validation
181} // namespace test
182} // namespace arm_compute
183#endif /* ARM_COMPUTE_TEST_WIDTHCONCATENATE_LAYER_FIXTURE */