blob: 7f7ed8b20b322807fb8addd2cdb701ca06934d86 [file] [log] [blame]
Ioan-Cristian Szabo2ded0af2017-12-15 10:05:52 +00001/*
Anthony Barbier72856ab2018-01-11 10:45:24 +00002 * Copyright (c) 2017-2018 ARM Limited.
Ioan-Cristian Szabo2ded0af2017-12-15 10:05:52 +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_DEPTHCONCATENATELAYERFIXTURE
25#define ARM_COMPUTE_TEST_DEPTHCONCATENATELAYERFIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
29#include "arm_compute/core/Utils.h"
Georgios Pinitase29acf12018-07-16 14:40:09 +010030#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Ioan-Cristian Szabo2ded0af2017-12-15 10:05:52 +000031#include "tests/AssetsLibrary.h"
32#include "tests/Globals.h"
33#include "tests/Utils.h"
34#include "tests/framework/Fixture.h"
35
36#include <random>
37
38namespace arm_compute
39{
40namespace test
41{
42namespace benchmark
43{
44/** Fixture that can be used for NE/CL/GC */
45template <typename TensorType, typename ITensorType, typename Function, typename AccessorType>
46class DepthConcatenateLayerFixture : public framework::Fixture
47{
48public:
49 inline std::vector<TensorShape> generate_input_shapes(TensorShape shape)
50 {
51 // Create input shapes
52 std::mt19937 gen(library->seed());
53 std::uniform_int_distribution<> num_dis(2, 6);
54 const int num_tensors = num_dis(gen);
55
56 std::vector<TensorShape> shapes(num_tensors, shape);
57 std::uniform_int_distribution<> depth_dis(1, 7);
58 std::bernoulli_distribution mutate_dis(0.25f);
59 std::uniform_real_distribution<> change_dis(-0.25f, 0.f);
60
61 // Generate more shapes based on the input
62 for(auto &s : shapes)
63 {
64 // Set the depth of the tensor
65 s.set(2, depth_dis(gen));
66
67 // Randomly change the first dimension
68 if(mutate_dis(gen))
69 {
70 // Decrease the dimension by a small percentage. Don't increase
71 // as that could make tensor too large. Also the change must be
72 // an even number. Otherwise out depth concatenate fails.
73 s.set(0, s[0] + 2 * static_cast<int>(s[0] * change_dis(gen)));
74 }
75
76 // Repeat the same as above for the second dimension
77 if(mutate_dis(gen))
78 {
79 s.set(1, s[1] + 2 * static_cast<int>(s[1] * change_dis(gen)));
80 }
81 }
82
83 return shapes;
84 }
85
86 template <typename...>
87 void setup(TensorShape shape, DataType data_type)
88 {
89 // Generate input shapes
90 std::vector<TensorShape> src_shapes = generate_input_shapes(shape);
91
92 // Create tensors
93 _srcs.reserve(src_shapes.size());
94
95 std::vector<ITensorType *> src_ptrs;
96
97 for(const auto &shape : src_shapes)
98 {
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010099 _srcs.emplace_back(create_tensor<TensorType>(shape, data_type, 1));
Ioan-Cristian Szabo2ded0af2017-12-15 10:05:52 +0000100 src_ptrs.emplace_back(&_srcs.back());
101 }
102
Georgios Pinitase29acf12018-07-16 14:40:09 +0100103 TensorShape dst_shape = misc::shape_calculator::calculate_depth_concatenate_shape(src_ptrs);
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100104 _dst = create_tensor<TensorType>(dst_shape, data_type, 1);
Ioan-Cristian Szabo2ded0af2017-12-15 10:05:52 +0000105
106 _depth_concat.configure(src_ptrs, &_dst);
107
108 for(auto &src : _srcs)
109 {
110 src.allocator()->allocate();
111 }
112
113 _dst.allocator()->allocate();
Ioan-Cristian Szabo2ded0af2017-12-15 10:05:52 +0000114 }
115
116 void run()
117 {
118 _depth_concat.run();
119 }
120
Joel Liang1c5ffd62017-12-28 10:09:51 +0800121 void sync()
122 {
123 sync_if_necessary<TensorType>();
124 sync_tensor_if_necessary<TensorType>(_dst);
125 }
126
Ioan-Cristian Szabo2ded0af2017-12-15 10:05:52 +0000127 void teardown()
128 {
129 for(auto &src : _srcs)
130 {
131 src.allocator()->free();
132 }
133
134 _srcs.clear();
135
136 _dst.allocator()->free();
137 }
138
139private:
140 std::vector<TensorType> _srcs{};
141 TensorType _dst{};
142 Function _depth_concat{};
Ioan-Cristian Szabo2ded0af2017-12-15 10:05:52 +0000143};
144} // namespace benchmark
145} // namespace test
146} // namespace arm_compute
147#endif /* ARM_COMPUTE_TEST_DEPTHCONCATENATELAYERFIXTURE */