blob: cf9b12eab6115f2109cc80c491a4e6f78e40269e [file] [log] [blame]
Michalis Spyrou55b3d122018-05-09 09:59:23 +01001/*
2 * Copyright (c) 2018 ARM Limited.
3 *
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"
36#include "tests/validation/reference/WidthConcatenateLayer.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 WidthConcatenateLayerValidationFixture : public framework::Fixture
48{
49public:
50 template <typename...>
51 void setup(TensorShape shape, DataType data_type)
52 {
53 // Create input shapes
54 std::mt19937 gen(library->seed());
55 std::uniform_int_distribution<> num_dis(2, 4);
56 const int num_tensors = num_dis(gen);
57
58 std::vector<TensorShape> shapes(num_tensors, shape);
59 std::bernoulli_distribution mutate_dis(0.5f);
60 std::uniform_real_distribution<> change_dis(-0.25f, 0.f);
61
62 // Generate more shapes based on the input
63 for(auto &s : shapes)
64 {
65 // Randomly change the first dimension
66 if(mutate_dis(gen))
67 {
68 // Decrease the dimension by a small percentage. Don't increase
69 // as that could make tensor too large.
70 s.set(0, s[0] + 2 * static_cast<int>(s[0] * change_dis(gen)));
71 }
72 }
73
74 _target = compute_target(shapes, data_type);
75 _reference = compute_reference(shapes, data_type);
76 }
77
78protected:
79 template <typename U>
80 void fill(U &&tensor, int i)
81 {
82 library->fill_tensor_uniform(tensor, i);
83 }
84
85 TensorType compute_target(std::vector<TensorShape> shapes, DataType data_type)
86 {
87 std::vector<TensorType> srcs;
88 std::vector<ITensorType *> src_ptrs;
89
90 // Create tensors
91 srcs.reserve(shapes.size());
92
93 for(const auto &shape : shapes)
94 {
95 srcs.emplace_back(create_tensor<TensorType>(shape, data_type, 1, _fractional_bits));
96 src_ptrs.emplace_back(&srcs.back());
97 }
98
99 TensorShape dst_shape = misc::shape_calculator::calculate_width_concatenate_shape(src_ptrs);
100 TensorType dst = create_tensor<TensorType>(dst_shape, data_type, 1, _fractional_bits);
101
102 // Create and configure function
103 FunctionType width_concat;
104 width_concat.configure(src_ptrs, &dst);
105
106 for(auto &src : srcs)
107 {
108 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
109 }
110
111 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
112
113 // Allocate tensors
114 for(auto &src : srcs)
115 {
116 src.allocator()->allocate();
117 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
118 }
119
120 dst.allocator()->allocate();
121 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
122
123 // Fill tensors
124 int i = 0;
125 for(auto &src : srcs)
126 {
127 fill(AccessorType(src), i++);
128 }
129
130 // Compute function
131 width_concat.run();
132
133 return dst;
134 }
135
136 SimpleTensor<T> compute_reference(std::vector<TensorShape> shapes, DataType data_type)
137 {
138 std::vector<SimpleTensor<T>> srcs;
139
140 // Create and fill tensors
141 int i = 0;
142 for(const auto &shape : shapes)
143 {
144 srcs.emplace_back(shape, data_type, 1, _fractional_bits);
145 fill(srcs.back(), i++);
146 }
147
148 return reference::widthconcatenate_layer<T>(srcs);
149 }
150
151 TensorType _target{};
152 SimpleTensor<T> _reference{};
153
154private:
155 int _fractional_bits{ 1 };
156};
157} // namespace validation
158} // namespace test
159} // namespace arm_compute
160#endif /* ARM_COMPUTE_TEST_WIDTHCONCATENATE_LAYER_FIXTURE */