blob: d91f4dd022f72c97681e3bed5f84e802b408eb0f [file] [log] [blame]
Georgios Pinitas99d40952018-04-23 16:26:46 +01001/*
Georgios Pinitas4d0351c2019-04-03 15:11:16 +01002 * Copyright (c) 2018-2019 ARM Limited.
Georgios Pinitas99d40952018-04-23 16:26:46 +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#include "arm_compute/runtime/CL/CLTensorAllocator.h"
25
Georgios Pinitasb8d5b952019-05-16 14:13:03 +010026#include "arm_compute/core/utils/misc/MMappedFile.h"
Georgios Pinitas99d40952018-04-23 16:26:46 +010027#include "arm_compute/runtime/CL/CLMemoryGroup.h"
28#include "arm_compute/runtime/CL/CLScheduler.h"
Georgios Pinitas4d0351c2019-04-03 15:11:16 +010029#include "arm_compute/runtime/CL/functions/CLActivationLayer.h"
30#include "tests/CL/CLAccessor.h"
31#include "tests/Globals.h"
Georgios Pinitas99d40952018-04-23 16:26:46 +010032#include "tests/framework/Asserts.h"
33#include "tests/framework/Macros.h"
Georgios Pinitas4d0351c2019-04-03 15:11:16 +010034#include "tests/validation/Validation.h"
35#include "tests/validation/reference/ActivationLayer.h"
Georgios Pinitas99d40952018-04-23 16:26:46 +010036
37#include <memory>
Georgios Pinitas4d0351c2019-04-03 15:11:16 +010038#include <random>
Georgios Pinitas99d40952018-04-23 16:26:46 +010039
40namespace arm_compute
41{
42namespace test
43{
44namespace validation
45{
Georgios Pinitas4d0351c2019-04-03 15:11:16 +010046namespace
47{
48cl_mem import_malloc_memory_helper(void *ptr, size_t size)
49{
50 const cl_import_properties_arm import_properties[] =
51 {
52 CL_IMPORT_TYPE_ARM,
53 CL_IMPORT_TYPE_HOST_ARM,
54 0
55 };
56
57 cl_int err = CL_SUCCESS;
58 cl_mem buf = clImportMemoryARM(CLKernelLibrary::get().context().get(), CL_MEM_READ_WRITE, import_properties, ptr, size, &err);
Georgios Pinitas17dae872019-04-12 17:08:40 +010059 ARM_COMPUTE_ASSERT(err == CL_SUCCESS);
Georgios Pinitas4d0351c2019-04-03 15:11:16 +010060
61 return buf;
62}
63} // namespace
64
Georgios Pinitas99d40952018-04-23 16:26:46 +010065TEST_SUITE(CL)
66TEST_SUITE(UNIT)
67TEST_SUITE(TensorAllocator)
68
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010069/** Validates import memory interface when importing cl buffer objects */
Georgios Pinitas4d0351c2019-04-03 15:11:16 +010070TEST_CASE(ImportMemoryBuffer, framework::DatasetMode::ALL)
Georgios Pinitas99d40952018-04-23 16:26:46 +010071{
72 // Init tensor info
Georgios Pinitas4d0351c2019-04-03 15:11:16 +010073 const TensorInfo info(TensorShape(24U, 16U, 3U), 1, DataType::F32);
Georgios Pinitas99d40952018-04-23 16:26:46 +010074
Georgios Pinitasdf310362018-11-14 13:16:56 +000075 // Allocate memory buffer
76 const size_t total_size = info.total_size();
77 auto buf = cl::Buffer(CLScheduler::get().context(), CL_MEM_READ_WRITE, total_size);
Georgios Pinitas99d40952018-04-23 16:26:46 +010078
Georgios Pinitasdf310362018-11-14 13:16:56 +000079 // Negative case : Import nullptr
Georgios Pinitas99d40952018-04-23 16:26:46 +010080 CLTensor t1;
81 t1.allocator()->init(info);
Georgios Pinitasdf310362018-11-14 13:16:56 +000082 ARM_COMPUTE_EXPECT(!bool(t1.allocator()->import_memory(cl::Buffer())), framework::LogLevel::ERRORS);
Georgios Pinitas99d40952018-04-23 16:26:46 +010083 ARM_COMPUTE_EXPECT(t1.info()->is_resizable(), framework::LogLevel::ERRORS);
84
85 // Negative case : Import memory to a tensor that is memory managed
86 CLTensor t2;
87 CLMemoryGroup mg;
88 t2.allocator()->set_associated_memory_group(&mg);
Georgios Pinitasdf310362018-11-14 13:16:56 +000089 ARM_COMPUTE_EXPECT(!bool(t2.allocator()->import_memory(buf)), framework::LogLevel::ERRORS);
Georgios Pinitas99d40952018-04-23 16:26:46 +010090 ARM_COMPUTE_EXPECT(t2.info()->is_resizable(), framework::LogLevel::ERRORS);
91
Georgios Pinitas4d0351c2019-04-03 15:11:16 +010092 // Negative case : Invalid buffer size
93 CLTensor t3;
94 const TensorInfo info_neg(TensorShape(32U, 16U, 3U), 1, DataType::F32);
95 t3.allocator()->init(info_neg);
96 ARM_COMPUTE_EXPECT(!bool(t3.allocator()->import_memory(buf)), framework::LogLevel::ERRORS);
Georgios Pinitas99d40952018-04-23 16:26:46 +010097 ARM_COMPUTE_EXPECT(t3.info()->is_resizable(), framework::LogLevel::ERRORS);
Georgios Pinitas4d0351c2019-04-03 15:11:16 +010098
99 // Positive case : Set raw pointer
100 CLTensor t4;
101 t4.allocator()->init(info);
102 ARM_COMPUTE_EXPECT(bool(t4.allocator()->import_memory(buf)), framework::LogLevel::ERRORS);
103 ARM_COMPUTE_EXPECT(!t4.info()->is_resizable(), framework::LogLevel::ERRORS);
104 ARM_COMPUTE_EXPECT(t4.cl_buffer().get() == buf.get(), framework::LogLevel::ERRORS);
105 t4.allocator()->free();
106 ARM_COMPUTE_EXPECT(t4.info()->is_resizable(), framework::LogLevel::ERRORS);
107 ARM_COMPUTE_EXPECT(t4.cl_buffer().get() != buf.get(), framework::LogLevel::ERRORS);
Georgios Pinitas99d40952018-04-23 16:26:46 +0100108}
109
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100110/** Validates import memory interface when importing malloced memory */
Georgios Pinitas4d0351c2019-04-03 15:11:16 +0100111TEST_CASE(ImportMemoryMalloc, framework::DatasetMode::ALL)
112{
113 // Check if import extension is supported
Georgios Pinitas17dae872019-04-12 17:08:40 +0100114 if(!device_supports_extension(CLKernelLibrary::get().get_device(), "cl_arm_import_memory_host"))
Georgios Pinitas4d0351c2019-04-03 15:11:16 +0100115 {
116 return;
117 }
118 else
119 {
120 const ActivationLayerInfo act_info(ActivationLayerInfo::ActivationFunction::RELU);
121 const TensorShape shape = TensorShape(24U, 16U, 3U);
122 const DataType data_type = DataType::F32;
123
124 // Create tensor
125 const TensorInfo info(shape, 1, data_type);
126 CLTensor tensor;
127 tensor.allocator()->init(info);
128
129 // Create and configure activation function
130 CLActivationLayer act_func;
131 act_func.configure(&tensor, nullptr, act_info);
132
133 // Allocate and import tensor
134 const size_t total_size_in_elems = tensor.info()->tensor_shape().total_size();
135 const size_t total_size_in_bytes = tensor.info()->total_size();
136 const size_t alignment = CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE>();
137 size_t space = total_size_in_bytes + alignment;
138 auto raw_data = support::cpp14::make_unique<uint8_t[]>(space);
139
140 void *aligned_ptr = raw_data.get();
141 support::cpp11::align(alignment, total_size_in_bytes, aligned_ptr, space);
142
143 cl::Buffer wrapped_buffer(import_malloc_memory_helper(aligned_ptr, total_size_in_bytes));
144 ARM_COMPUTE_EXPECT(bool(tensor.allocator()->import_memory(wrapped_buffer)), framework::LogLevel::ERRORS);
145 ARM_COMPUTE_EXPECT(!tensor.info()->is_resizable(), framework::LogLevel::ERRORS);
146
147 // Fill tensor
148 std::uniform_real_distribution<float> distribution(-5.f, 5.f);
149 std::mt19937 gen(library->seed());
150 auto *typed_ptr = reinterpret_cast<float *>(aligned_ptr);
151 for(unsigned int i = 0; i < total_size_in_elems; ++i)
152 {
153 typed_ptr[i] = distribution(gen);
154 }
155
156 // Execute function and sync
157 act_func.run();
158 CLScheduler::get().sync();
159
160 // Validate result by checking that the input has no negative values
161 for(unsigned int i = 0; i < total_size_in_elems; ++i)
162 {
163 ARM_COMPUTE_EXPECT(typed_ptr[i] >= 0, framework::LogLevel::ERRORS);
164 }
165
166 // Release resources
167 tensor.allocator()->free();
168 ARM_COMPUTE_EXPECT(tensor.info()->is_resizable(), framework::LogLevel::ERRORS);
169 }
170}
171
Georgios Pinitasb8d5b952019-05-16 14:13:03 +0100172#if !defined(BARE_METAL)
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100173/** Validates import memory interface when importing memory mapped objects */
Georgios Pinitasb8d5b952019-05-16 14:13:03 +0100174TEST_CASE(ImportMemoryMappedFile, framework::DatasetMode::ALL)
175{
176 // Check if import extension is supported
177 if(!device_supports_extension(CLKernelLibrary::get().get_device(), "cl_arm_import_memory_host"))
178 {
179 return;
180 }
181 else
182 {
183 const ActivationLayerInfo act_info(ActivationLayerInfo::ActivationFunction::RELU);
184 const TensorShape shape = TensorShape(24U, 16U, 3U);
185 const DataType data_type = DataType::F32;
186
187 // Create tensor
188 const TensorInfo info(shape, 1, data_type);
189 CLTensor tensor;
190 tensor.allocator()->init(info);
191
192 // Create and configure activation function
193 CLActivationLayer act_func;
194 act_func.configure(&tensor, nullptr, act_info);
195
196 // Get number of elements
197 const size_t total_size_in_elems = tensor.info()->tensor_shape().total_size();
198 const size_t total_size_in_bytes = tensor.info()->total_size();
199
200 // Create file
201 std::ofstream output_file("test_mmap_import.bin", std::ios::binary | std::ios::out);
202 output_file.seekp(total_size_in_bytes - 1);
203 output_file.write("", 1);
204 output_file.close();
205
206 // Map file
207 utils::mmap_io::MMappedFile mmapped_file("test_mmap_import.bin", 0 /** Whole file */, 0);
208 ARM_COMPUTE_EXPECT(mmapped_file.is_mapped(), framework::LogLevel::ERRORS);
209 unsigned char *data = mmapped_file.data();
210
211 cl::Buffer wrapped_buffer(import_malloc_memory_helper(data, total_size_in_bytes));
212 ARM_COMPUTE_EXPECT(bool(tensor.allocator()->import_memory(wrapped_buffer)), framework::LogLevel::ERRORS);
213 ARM_COMPUTE_EXPECT(!tensor.info()->is_resizable(), framework::LogLevel::ERRORS);
214
215 // Fill tensor
216 std::uniform_real_distribution<float> distribution(-5.f, 5.f);
217 std::mt19937 gen(library->seed());
218 auto *typed_ptr = reinterpret_cast<float *>(data);
219 for(unsigned int i = 0; i < total_size_in_elems; ++i)
220 {
221 typed_ptr[i] = distribution(gen);
222 }
223
224 // Execute function and sync
225 act_func.run();
226 CLScheduler::get().sync();
227
228 // Validate result by checking that the input has no negative values
229 for(unsigned int i = 0; i < total_size_in_elems; ++i)
230 {
231 ARM_COMPUTE_EXPECT(typed_ptr[i] >= 0, framework::LogLevel::ERRORS);
232 }
233
234 // Release resources
235 tensor.allocator()->free();
236 ARM_COMPUTE_EXPECT(tensor.info()->is_resizable(), framework::LogLevel::ERRORS);
237 }
238}
239#endif // !defined(BARE_METAL)
240
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100241/** Validates symmetric per channel quantization */
242TEST_CASE(Symm8PerChannelQuantizationInfo, framework::DatasetMode::ALL)
243{
244 // Create tensor
245 CLTensor tensor;
246 const std::vector<float> scale = { 0.25f, 1.4f, 3.2f, 2.3f, 4.7f };
247 const TensorInfo info(TensorShape(32U, 16U), 1, DataType::QSYMM8_PER_CHANNEL, QuantizationInfo(scale));
248 tensor.allocator()->init(info);
249
250 // Check quantization information
251 ARM_COMPUTE_EXPECT(!tensor.info()->quantization_info().empty(), framework::LogLevel::ERRORS);
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100252 ARM_COMPUTE_EXPECT(!tensor.info()->quantization_info().scale().empty(), framework::LogLevel::ERRORS);
253 ARM_COMPUTE_EXPECT(tensor.info()->quantization_info().scale().size() == scale.size(), framework::LogLevel::ERRORS);
254 ARM_COMPUTE_EXPECT(tensor.info()->quantization_info().offset().empty(), framework::LogLevel::ERRORS);
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100255
256 CLQuantization quantization = tensor.quantization();
257 ARM_COMPUTE_ASSERT(quantization.scale != nullptr);
258 ARM_COMPUTE_ASSERT(quantization.offset != nullptr);
259
260 // Check OpenCL quantization arrays before allocating
261 ARM_COMPUTE_EXPECT(quantization.scale->max_num_values() == 0, framework::LogLevel::ERRORS);
262 ARM_COMPUTE_EXPECT(quantization.offset->max_num_values() == 0, framework::LogLevel::ERRORS);
263
264 // Check OpenCL quantization arrays after allocating
265 tensor.allocator()->allocate();
266 ARM_COMPUTE_EXPECT(quantization.scale->max_num_values() == scale.size(), framework::LogLevel::ERRORS);
267 ARM_COMPUTE_EXPECT(quantization.offset->max_num_values() == 0, framework::LogLevel::ERRORS);
268
269 // Validate that the scale values are the same
270 auto cl_scale_buffer = quantization.scale->cl_buffer();
271 void *mapped_ptr = CLScheduler::get().queue().enqueueMapBuffer(cl_scale_buffer, CL_TRUE, CL_MAP_READ, 0, scale.size());
272 auto cl_scale_ptr = static_cast<float *>(mapped_ptr);
273 for(unsigned int i = 0; i < scale.size(); ++i)
274 {
275 ARM_COMPUTE_EXPECT(cl_scale_ptr[i] == scale[i], framework::LogLevel::ERRORS);
276 }
277 CLScheduler::get().queue().enqueueUnmapMemObject(cl_scale_buffer, mapped_ptr);
278}
279
Georgios Pinitas4d0351c2019-04-03 15:11:16 +0100280TEST_SUITE_END() // TensorAllocator
281TEST_SUITE_END() // UNIT
282TEST_SUITE_END() // CL
Georgios Pinitas99d40952018-04-23 16:26:46 +0100283} // namespace validation
284} // namespace test
285} // namespace arm_compute