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