1 /*
2 * Copyright (c) 2018-2019 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 #include "arm_compute/runtime/CL/CLTensorAllocator.h"
25
26 #include "arm_compute/core/utils/misc/MMappedFile.h"
27 #include "arm_compute/runtime/CL/CLScheduler.h"
28 #include "arm_compute/runtime/CL/functions/CLActivationLayer.h"
29 #include "arm_compute/runtime/MemoryGroup.h"
30 #include "tests/CL/CLAccessor.h"
31 #include "tests/Globals.h"
32 #include "tests/framework/Asserts.h"
33 #include "tests/framework/Macros.h"
34 #include "tests/validation/Validation.h"
35 #include "tests/validation/reference/ActivationLayer.h"
36
37 #include <memory>
38 #include <random>
39
40 namespace arm_compute
41 {
42 namespace test
43 {
44 namespace validation
45 {
46 namespace
47 {
import_malloc_memory_helper(void * ptr,size_t size)48 cl_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);
59 ARM_COMPUTE_ASSERT(err == CL_SUCCESS);
60
61 return buf;
62 }
63 } // namespace
64
65 TEST_SUITE(CL)
TEST_SUITE(UNIT)66 TEST_SUITE(UNIT)
67 TEST_SUITE(TensorAllocator)
68
69 /** Validates import memory interface when importing cl buffer objects */
70 TEST_CASE(ImportMemoryBuffer, framework::DatasetMode::ALL)
71 {
72 // Init tensor info
73 const TensorInfo info(TensorShape(24U, 16U, 3U), 1, DataType::F32);
74
75 // 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);
78
79 // Negative case : Import nullptr
80 CLTensor t1;
81 t1.allocator()->init(info);
82 ARM_COMPUTE_EXPECT(!bool(t1.allocator()->import_memory(cl::Buffer())), framework::LogLevel::ERRORS);
83 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 MemoryGroup mg;
88 t2.allocator()->set_associated_memory_group(&mg);
89 ARM_COMPUTE_EXPECT(!bool(t2.allocator()->import_memory(buf)), framework::LogLevel::ERRORS);
90 ARM_COMPUTE_EXPECT(t2.info()->is_resizable(), framework::LogLevel::ERRORS);
91
92 // 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);
97 ARM_COMPUTE_EXPECT(t3.info()->is_resizable(), framework::LogLevel::ERRORS);
98
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);
108 }
109
110 /** Validates import memory interface when importing malloced memory */
TEST_CASE(ImportMemoryMalloc,framework::DatasetMode::ALL)111 TEST_CASE(ImportMemoryMalloc, framework::DatasetMode::ALL)
112 {
113 // Check if import extension is supported
114 if(!device_supports_extension(CLKernelLibrary::get().get_device(), "cl_arm_import_memory_host"))
115 {
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
172 #if !defined(BARE_METAL)
173 /** Validates import memory interface when importing memory mapped objects */
TEST_CASE(ImportMemoryMappedFile,framework::DatasetMode::ALL)174 TEST_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
241 /** Validates symmetric per channel quantization */
TEST_CASE(Symm8PerChannelQuantizationInfo,framework::DatasetMode::ALL)242 TEST_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);
252 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);
255
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
280 TEST_SUITE_END() // TensorAllocator
281 TEST_SUITE_END() // UNIT
282 TEST_SUITE_END() // CL
283 } // namespace validation
284 } // namespace test
285 } // namespace arm_compute
286