blob: ef19524d1c6491429da94701ccb3d71862e5188e [file] [log] [blame]
Georgios Pinitas84b51ad2017-11-07 13:24:57 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Georgios Pinitas84b51ad2017-11-07 13:24:57 +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#include "arm_compute/runtime/TensorAllocator.h"
25
Georgios Pinitasb8d5b952019-05-16 14:13:03 +010026#include "arm_compute/core/utils/misc/MMappedFile.h"
Georgios Pinitas17b12302018-06-18 18:13:51 +010027#include "arm_compute/core/utils/misc/Utility.h"
Georgios Pinitas84b51ad2017-11-07 13:24:57 +000028#include "arm_compute/runtime/MemoryGroup.h"
Georgios Pinitas99d40952018-04-23 16:26:46 +010029#include "arm_compute/runtime/MemoryRegion.h"
Georgios Pinitas4d0351c2019-04-03 15:11:16 +010030#include "arm_compute/runtime/NEON/functions/NEActivationLayer.h"
Georgios Pinitas17b12302018-06-18 18:13:51 +010031
Georgios Pinitas4d0351c2019-04-03 15:11:16 +010032#include "tests/Globals.h"
Georgios Pinitas84b51ad2017-11-07 13:24:57 +000033#include "tests/Utils.h"
34#include "tests/framework/Asserts.h"
35#include "tests/framework/Macros.h"
Georgios Pinitas4d0351c2019-04-03 15:11:16 +010036#include "tests/validation/Validation.h"
37#include "tests/validation/reference/ActivationLayer.h"
38
39#include <memory>
40#include <random>
Georgios Pinitas84b51ad2017-11-07 13:24:57 +000041
42namespace arm_compute
43{
44namespace test
45{
46namespace validation
47{
48TEST_SUITE(NEON)
49TEST_SUITE(UNIT)
50TEST_SUITE(TensorAllocator)
51
52TEST_CASE(ImportMemory, framework::DatasetMode::ALL)
53{
54 // Init tensor info
55 TensorInfo info(TensorShape(24U, 16U, 3U), 1, DataType::F32);
56
57 // Allocate memory buffer
Georgios Pinitasdf310362018-11-14 13:16:56 +000058 const size_t total_size = info.total_size();
Georgios Pinitas40f51a62020-11-21 03:04:18 +000059 auto data = std::make_unique<uint8_t[]>(total_size);
Georgios Pinitas84b51ad2017-11-07 13:24:57 +000060
Georgios Pinitas4d0351c2019-04-03 15:11:16 +010061 // Negative case : Import nullptr
Georgios Pinitas84b51ad2017-11-07 13:24:57 +000062 Tensor t1;
63 t1.allocator()->init(info);
Georgios Pinitas4d0351c2019-04-03 15:11:16 +010064 ARM_COMPUTE_EXPECT(!bool(t1.allocator()->import_memory(nullptr)), framework::LogLevel::ERRORS);
Georgios Pinitas84b51ad2017-11-07 13:24:57 +000065 ARM_COMPUTE_EXPECT(t1.info()->is_resizable(), framework::LogLevel::ERRORS);
66
Georgios Pinitas4d0351c2019-04-03 15:11:16 +010067 // Negative case : Import misaligned pointer
68 Tensor t2;
69 const size_t required_alignment = 339;
70 t2.allocator()->init(info, required_alignment);
71 ARM_COMPUTE_EXPECT(!bool(t2.allocator()->import_memory(data.get())), framework::LogLevel::ERRORS);
Georgios Pinitas84b51ad2017-11-07 13:24:57 +000072 ARM_COMPUTE_EXPECT(t2.info()->is_resizable(), framework::LogLevel::ERRORS);
73
Georgios Pinitasdf310362018-11-14 13:16:56 +000074 // Negative case : Import memory to a tensor that is memory managed
75 Tensor t3;
76 MemoryGroup mg;
77 t3.allocator()->set_associated_memory_group(&mg);
Georgios Pinitas4d0351c2019-04-03 15:11:16 +010078 ARM_COMPUTE_EXPECT(!bool(t3.allocator()->import_memory(data.get())), framework::LogLevel::ERRORS);
Georgios Pinitas84b51ad2017-11-07 13:24:57 +000079 ARM_COMPUTE_EXPECT(t3.info()->is_resizable(), framework::LogLevel::ERRORS);
Georgios Pinitas84b51ad2017-11-07 13:24:57 +000080
Georgios Pinitasdf310362018-11-14 13:16:56 +000081 // Positive case : Set raw pointer
Georgios Pinitas84b51ad2017-11-07 13:24:57 +000082 Tensor t4;
83 t4.allocator()->init(info);
Georgios Pinitas4d0351c2019-04-03 15:11:16 +010084 ARM_COMPUTE_EXPECT(bool(t4.allocator()->import_memory(data.get())), framework::LogLevel::ERRORS);
Georgios Pinitas84b51ad2017-11-07 13:24:57 +000085 ARM_COMPUTE_EXPECT(!t4.info()->is_resizable(), framework::LogLevel::ERRORS);
Georgios Pinitasdf310362018-11-14 13:16:56 +000086 ARM_COMPUTE_EXPECT(t4.buffer() == reinterpret_cast<uint8_t *>(data.get()), framework::LogLevel::ERRORS);
Georgios Pinitas84b51ad2017-11-07 13:24:57 +000087 t4.allocator()->free();
88 ARM_COMPUTE_EXPECT(t4.info()->is_resizable(), framework::LogLevel::ERRORS);
89 ARM_COMPUTE_EXPECT(t4.buffer() == nullptr, framework::LogLevel::ERRORS);
90}
91
Georgios Pinitas4d0351c2019-04-03 15:11:16 +010092TEST_CASE(ImportMemoryMalloc, framework::DatasetMode::ALL)
93{
94 const ActivationLayerInfo act_info(ActivationLayerInfo::ActivationFunction::RELU);
95 const TensorShape shape = TensorShape(24U, 16U, 3U);
96 const DataType data_type = DataType::F32;
97
98 // Create tensor
99 const TensorInfo info(shape, 1, data_type);
100 const size_t required_alignment = 64;
101 Tensor tensor;
102 tensor.allocator()->init(info, required_alignment);
103
104 // Create and configure activation function
105 NEActivationLayer act_func;
106 act_func.configure(&tensor, nullptr, act_info);
107
108 // Allocate and import tensor
109 const size_t total_size_in_elems = tensor.info()->tensor_shape().total_size();
110 const size_t total_size_in_bytes = tensor.info()->total_size();
111 size_t space = total_size_in_bytes + required_alignment;
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000112 auto raw_data = std::make_unique<uint8_t[]>(space);
Georgios Pinitas4d0351c2019-04-03 15:11:16 +0100113
114 void *aligned_ptr = raw_data.get();
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000115 std::align(required_alignment, total_size_in_bytes, aligned_ptr, space);
Georgios Pinitas4d0351c2019-04-03 15:11:16 +0100116
117 ARM_COMPUTE_EXPECT(bool(tensor.allocator()->import_memory(aligned_ptr)), framework::LogLevel::ERRORS);
118 ARM_COMPUTE_EXPECT(!tensor.info()->is_resizable(), framework::LogLevel::ERRORS);
119
120 // Fill tensor
121 std::uniform_real_distribution<float> distribution(-5.f, 5.f);
122 std::mt19937 gen(library->seed());
123 auto *typed_ptr = reinterpret_cast<float *>(aligned_ptr);
124 for(unsigned int i = 0; i < total_size_in_elems; ++i)
125 {
126 typed_ptr[i] = distribution(gen);
127 }
128
129 // Execute function and sync
130 act_func.run();
131
132 // Validate result by checking that the input has no negative values
133 for(unsigned int i = 0; i < total_size_in_elems; ++i)
134 {
135 ARM_COMPUTE_EXPECT(typed_ptr[i] >= 0, framework::LogLevel::ERRORS);
136 }
137
138 // Release resources
139 tensor.allocator()->free();
140 ARM_COMPUTE_EXPECT(tensor.info()->is_resizable(), framework::LogLevel::ERRORS);
141}
142
Georgios Pinitasdb09b372019-06-17 17:46:17 +0100143TEST_CASE(ImportMemoryMallocPadded, framework::DatasetMode::ALL)
144{
145 // Create tensor
146 Tensor tensor;
147 tensor.allocator()->init(TensorInfo(TensorShape(24U, 16U, 3U), 1, DataType::F32));
148
149 // Enforce tensor padding and validate that meta-data were updated
150 // Note: Padding might be updated after the function configuration in case of increased padding requirements
151 const PaddingSize enforced_padding(3U, 5U, 2U, 4U);
152 tensor.info()->extend_padding(enforced_padding);
153 validate(tensor.info()->padding(), enforced_padding);
154
155 // Create and configure activation function
156 NEActivationLayer act_func;
157 act_func.configure(&tensor, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
158
159 // Allocate and import tensor
160 const size_t total_size_in_bytes = tensor.info()->total_size();
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000161 auto raw_data = std::make_unique<uint8_t[]>(total_size_in_bytes);
Georgios Pinitasdb09b372019-06-17 17:46:17 +0100162
163 ARM_COMPUTE_EXPECT(bool(tensor.allocator()->import_memory(raw_data.get())), framework::LogLevel::ERRORS);
164 ARM_COMPUTE_EXPECT(!tensor.info()->is_resizable(), framework::LogLevel::ERRORS);
165
166 // Fill tensor while accounting padding
167 std::uniform_real_distribution<float> distribution(-5.f, 5.f);
168 std::mt19937 gen(library->seed());
169
170 Window tensor_window;
171 tensor_window.use_tensor_dimensions(tensor.info()->tensor_shape());
172 Iterator tensor_it(&tensor, tensor_window);
173
174 execute_window_loop(tensor_window, [&](const Coordinates &)
175 {
176 *reinterpret_cast<float *>(tensor_it.ptr()) = distribution(gen);
177 },
178 tensor_it);
179
180 // Execute function and sync
181 act_func.run();
182
183 // Validate result by checking that the input has no negative values
184 execute_window_loop(tensor_window, [&](const Coordinates &)
185 {
186 const float val = *reinterpret_cast<float *>(tensor_it.ptr());
187 ARM_COMPUTE_EXPECT(val >= 0, framework::LogLevel::ERRORS);
188 },
189 tensor_it);
190
191 // Release resources
192 tensor.allocator()->free();
193 ARM_COMPUTE_EXPECT(tensor.info()->is_resizable(), framework::LogLevel::ERRORS);
194}
195
Georgios Pinitasb8d5b952019-05-16 14:13:03 +0100196#if !defined(BARE_METAL)
197TEST_CASE(ImportMemoryMappedFile, framework::DatasetMode::ALL)
198{
199 const ActivationLayerInfo act_info(ActivationLayerInfo::ActivationFunction::RELU);
200 const TensorShape shape = TensorShape(24U, 16U, 3U);
201 const DataType data_type = DataType::F32;
202
203 // Create tensor
204 const TensorInfo info(shape, 1, data_type);
205 Tensor tensor;
206 tensor.allocator()->init(info);
207
208 // Create and configure activation function
209 NEActivationLayer act_func;
210 act_func.configure(&tensor, nullptr, act_info);
211
212 // Get number of elements
213 const size_t total_size_in_elems = tensor.info()->tensor_shape().total_size();
214 const size_t total_size_in_bytes = tensor.info()->total_size();
215
216 // Create file
217 std::ofstream output_file("test_mmap_import.bin", std::ios::binary | std::ios::out);
218 output_file.seekp(total_size_in_bytes - 1);
219 output_file.write("", 1);
220 output_file.close();
221
222 // Map file
223 utils::mmap_io::MMappedFile mmapped_file("test_mmap_import.bin", 0 /** Whole file */, 0);
224 ARM_COMPUTE_EXPECT(mmapped_file.is_mapped(), framework::LogLevel::ERRORS);
225 unsigned char *data = mmapped_file.data();
226
227 // Import memory mapped memory
228 ARM_COMPUTE_EXPECT(bool(tensor.allocator()->import_memory(data)), framework::LogLevel::ERRORS);
229 ARM_COMPUTE_EXPECT(!tensor.info()->is_resizable(), framework::LogLevel::ERRORS);
230
231 // Fill tensor
232 std::uniform_real_distribution<float> distribution(-5.f, 5.f);
233 std::mt19937 gen(library->seed());
234 auto *typed_ptr = reinterpret_cast<float *>(data);
235 for(unsigned int i = 0; i < total_size_in_elems; ++i)
236 {
237 typed_ptr[i] = distribution(gen);
238 }
239
240 // Execute function and sync
241 act_func.run();
242
243 // Validate result by checking that the input has no negative values
244 for(unsigned int i = 0; i < total_size_in_elems; ++i)
245 {
246 ARM_COMPUTE_EXPECT(typed_ptr[i] >= 0, framework::LogLevel::ERRORS);
247 }
248
249 // Release resources
250 tensor.allocator()->free();
251 ARM_COMPUTE_EXPECT(tensor.info()->is_resizable(), framework::LogLevel::ERRORS);
252}
253#endif // !defined(BARE_METAL)
254
Georgios Pinitas17b12302018-06-18 18:13:51 +0100255TEST_CASE(AlignedAlloc, framework::DatasetMode::ALL)
256{
257 // Init tensor info
258 TensorInfo info(TensorShape(24U, 16U, 3U), 1, DataType::F32);
259 const size_t requested_alignment = 1024;
260
261 Tensor t;
262 t.allocator()->init(info, requested_alignment);
263 t.allocator()->allocate();
264
265 ARM_COMPUTE_EXPECT(t.buffer() != nullptr, framework::LogLevel::ERRORS);
266 ARM_COMPUTE_EXPECT(t.allocator()->alignment() == requested_alignment, framework::LogLevel::ERRORS);
267 ARM_COMPUTE_EXPECT(arm_compute::utility::check_aligned(reinterpret_cast<void *>(t.buffer()), requested_alignment),
268 framework::LogLevel::ERRORS);
269}
270
Georgios Pinitas84b51ad2017-11-07 13:24:57 +0000271TEST_SUITE_END()
272TEST_SUITE_END()
273TEST_SUITE_END()
274} // namespace validation
275} // namespace test
276} // namespace arm_compute