blob: 028a764fc26174a7131d2aac03ba13e53fecf4ef [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitas4d0351c2019-04-03 15:11:16 +01002 * Copyright (c) 2016-2019 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +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
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/TensorInfo.h"
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010028#include "arm_compute/runtime/CL/CLMemoryGroup.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/runtime/CL/CLScheduler.h"
30
Georgios Pinitasdf310362018-11-14 13:16:56 +000031namespace arm_compute
32{
33const cl::Buffer CLTensorAllocator::_empty_buffer = cl::Buffer();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034
Georgios Pinitas99d40952018-04-23 16:26:46 +010035namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036{
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010037/** Helper function used to allocate the backing memory of a tensor
38 *
39 * @param[in] context OpenCL context to use
40 * @param[in] size Size of the allocation
41 * @param[in] alignment Alignment of the allocation
42 *
43 * @return A wrapped memory region
44 */
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010045std::unique_ptr<ICLMemoryRegion> allocate_region(const cl::Context &context, size_t size, cl_uint alignment)
Georgios Pinitas99d40952018-04-23 16:26:46 +010046{
47 // Try fine-grain SVM
Georgios Pinitasdf310362018-11-14 13:16:56 +000048 std::unique_ptr<ICLMemoryRegion> region = support::cpp14::make_unique<CLFineSVMMemoryRegion>(context,
49 CL_MEM_READ_WRITE | CL_MEM_SVM_FINE_GRAIN_BUFFER,
50 size,
51 alignment);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010052
Georgios Pinitas99d40952018-04-23 16:26:46 +010053 // Try coarse-grain SVM in case of failure
54 if(region != nullptr && region->ptr() == nullptr)
55 {
Georgios Pinitasdf310362018-11-14 13:16:56 +000056 region = support::cpp14::make_unique<CLCoarseSVMMemoryRegion>(context, CL_MEM_READ_WRITE, size, alignment);
Georgios Pinitas99d40952018-04-23 16:26:46 +010057 }
58 // Try legacy buffer memory in case of failure
59 if(region != nullptr && region->ptr() == nullptr)
60 {
Georgios Pinitasdf310362018-11-14 13:16:56 +000061 region = support::cpp14::make_unique<CLBufferMemoryRegion>(context, CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, size);
Georgios Pinitas99d40952018-04-23 16:26:46 +010062 }
63 return region;
64}
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010065/** Clears quantization arrays
66 *
67 * @param[in, out] scale Quantization scale array
68 * @param[in, out] offset Quantization offset array
69 */
70void clear_quantization_arrays(CLFloatArray &scale, CLInt32Array &offset)
71{
72 // Clear arrays
73 scale = CLFloatArray();
74 offset = CLInt32Array();
75}
76/** Helper function used to create quantization data arrays
77 *
78 * @param[in, out] scale Quantization scale array
79 * @param[in, out] offset Quantization offset array
80 * @param[in] qinfo Quantization info
81 * @param[in] pad_size Pad size to use in case array needs to be padded for computation purposes
82 *
83 * @return A pair (scale, offset) containing the respective allocated and filled arrays
84 */
85void populate_quantization_info(CLFloatArray &scale, CLInt32Array &offset, const QuantizationInfo &qinfo, size_t pad_size)
86{
87 clear_quantization_arrays(scale, offset);
88
89 // Create scale array
Georgios Pinitas3d13af82019-06-04 13:04:16 +010090 const std::vector<float> &qscale = qinfo.scale();
91 const size_t num_elements = qscale.size();
92 const size_t element_size = sizeof(std::remove_reference<decltype(qscale)>::type::value_type);
93 scale = CLFloatArray(num_elements + pad_size);
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010094 scale.resize(num_elements);
Georgios Pinitas3d13af82019-06-04 13:04:16 +010095 CLScheduler::get().queue().enqueueWriteBuffer(scale.cl_buffer(), CL_TRUE, 0, num_elements * element_size, qinfo.scale().data());
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010096}
Georgios Pinitas99d40952018-04-23 16:26:46 +010097} // namespace
98
99CLTensorAllocator::CLTensorAllocator(CLTensor *owner)
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100100 : _associated_memory_group(nullptr), _memory(), _mapping(nullptr), _owner(owner), _scale(), _offset()
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100101{
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100102}
103
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100104CLQuantization CLTensorAllocator::quantization() const
105{
106 return { &_scale, &_offset };
107}
108
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109uint8_t *CLTensorAllocator::data()
110{
Georgios Pinitasdf310362018-11-14 13:16:56 +0000111 return _mapping;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112}
113
114const cl::Buffer &CLTensorAllocator::cl_data() const
115{
Georgios Pinitasdf310362018-11-14 13:16:56 +0000116 return _memory.region() == nullptr ? _empty_buffer : _memory.cl_region()->cl_data();
Pablo Telloe86a09f2018-01-11 15:44:48 +0000117}
118
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119void CLTensorAllocator::allocate()
120{
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100121 // Allocate tensor backing memory
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100122 if(_associated_memory_group == nullptr)
123 {
Georgios Pinitasdf310362018-11-14 13:16:56 +0000124 if(_memory.region() != nullptr && _memory.cl_region()->cl_data().get() != nullptr)
Michele Di Giorgioaaab6022018-05-14 11:58:24 +0100125 {
126 // Memory is already allocated. Reuse it if big enough, otherwise fire an assertion
Georgios Pinitasdf310362018-11-14 13:16:56 +0000127 ARM_COMPUTE_ERROR_ON_MSG(info().total_size() > _memory.region()->size(),
128 "Reallocation of a bigger memory region is not allowed!");
Michele Di Giorgioaaab6022018-05-14 11:58:24 +0100129 }
130 else
131 {
132 // Perform memory allocation
Georgios Pinitasdf310362018-11-14 13:16:56 +0000133 _memory.set_owned_region(allocate_region(CLScheduler::get().context(), info().total_size(), 0));
Michele Di Giorgioaaab6022018-05-14 11:58:24 +0100134 }
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100135 }
136 else
137 {
Georgios Pinitasdf310362018-11-14 13:16:56 +0000138 _associated_memory_group->finalize_memory(_owner, _memory, info().total_size());
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100139 }
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100140
141 // Allocate and fill the quantization parameter arrays
Michalis Spyrouc8530212019-08-22 11:44:04 +0100142 if(is_data_type_quantized_per_channel(info().data_type()))
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100143 {
144 const size_t pad_size = 0;
145 populate_quantization_info(_scale, _offset, info().quantization_info(), pad_size);
146 }
147
148 // Lock allocator
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149 info().set_is_resizable(false);
150}
151
152void CLTensorAllocator::free()
153{
Georgios Pinitasdf310362018-11-14 13:16:56 +0000154 _mapping = nullptr;
155 _memory.set_region(nullptr);
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100156 clear_quantization_arrays(_scale, _offset);
Georgios Pinitasdf310362018-11-14 13:16:56 +0000157 info().set_is_resizable(true);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100158}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100159
Georgios Pinitas4d0351c2019-04-03 15:11:16 +0100160Status CLTensorAllocator::import_memory(cl::Buffer buffer)
Georgios Pinitas99d40952018-04-23 16:26:46 +0100161{
Georgios Pinitasdf310362018-11-14 13:16:56 +0000162 ARM_COMPUTE_RETURN_ERROR_ON(buffer.get() == nullptr);
Georgios Pinitas4d0351c2019-04-03 15:11:16 +0100163 ARM_COMPUTE_RETURN_ERROR_ON(buffer.getInfo<CL_MEM_SIZE>() < info().total_size());
Georgios Pinitasdf310362018-11-14 13:16:56 +0000164 ARM_COMPUTE_RETURN_ERROR_ON(buffer.getInfo<CL_MEM_CONTEXT>().get() != CLScheduler::get().context().get());
Georgios Pinitas99d40952018-04-23 16:26:46 +0100165 ARM_COMPUTE_RETURN_ERROR_ON(_associated_memory_group != nullptr);
Georgios Pinitasdf310362018-11-14 13:16:56 +0000166
167 _memory.set_owned_region(support::cpp14::make_unique<CLBufferMemoryRegion>(buffer));
Georgios Pinitas99d40952018-04-23 16:26:46 +0100168 info().set_is_resizable(false);
169
170 return Status{};
171}
172
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100173void CLTensorAllocator::set_associated_memory_group(CLMemoryGroup *associated_memory_group)
174{
175 ARM_COMPUTE_ERROR_ON(associated_memory_group == nullptr);
176 ARM_COMPUTE_ERROR_ON(_associated_memory_group != nullptr);
Georgios Pinitasdf310362018-11-14 13:16:56 +0000177 ARM_COMPUTE_ERROR_ON(_memory.region() != nullptr && _memory.cl_region()->cl_data().get() != nullptr);
178
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100179 _associated_memory_group = associated_memory_group;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100180}
181
182uint8_t *CLTensorAllocator::lock()
183{
Georgios Pinitas99d40952018-04-23 16:26:46 +0100184 return map(CLScheduler::get().queue(), true);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100185}
186
187void CLTensorAllocator::unlock()
188{
Georgios Pinitas99d40952018-04-23 16:26:46 +0100189 ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
190 unmap(CLScheduler::get().queue(), reinterpret_cast<uint8_t *>(_memory.region()->buffer()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100191}
192
193uint8_t *CLTensorAllocator::map(cl::CommandQueue &q, bool blocking)
194{
Georgios Pinitasdf310362018-11-14 13:16:56 +0000195 ARM_COMPUTE_ERROR_ON(_mapping != nullptr);
Georgios Pinitas99d40952018-04-23 16:26:46 +0100196 ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
197 ARM_COMPUTE_ERROR_ON(_memory.region()->buffer() != nullptr);
Georgios Pinitasdf310362018-11-14 13:16:56 +0000198
199 _mapping = reinterpret_cast<uint8_t *>(_memory.cl_region()->map(q, blocking));
200 return _mapping;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100201}
202
203void CLTensorAllocator::unmap(cl::CommandQueue &q, uint8_t *mapping)
204{
Georgios Pinitasdf310362018-11-14 13:16:56 +0000205 ARM_COMPUTE_ERROR_ON(_mapping == nullptr);
206 ARM_COMPUTE_ERROR_ON(_mapping != mapping);
Georgios Pinitas99d40952018-04-23 16:26:46 +0100207 ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
208 ARM_COMPUTE_ERROR_ON(_memory.region()->buffer() == nullptr);
Georgios Pinitasdf310362018-11-14 13:16:56 +0000209 ARM_COMPUTE_UNUSED(mapping);
210
211 _memory.cl_region()->unmap(q);
212 _mapping = nullptr;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100213}
Georgios Pinitasdf310362018-11-14 13:16:56 +0000214} // namespace arm_compute