blob: f37fc779fe9b1fea4198c60f40e88173ae2ac9f1 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +01002 * Copyright (c) 2016-2020 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"
Pablo Tellodb8485a2019-09-24 11:03:47 +010028#include "arm_compute/runtime/CL/CLRuntimeContext.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/runtime/CL/CLScheduler.h"
30
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010031#include "support/MemorySupport.h"
32
Georgios Pinitasdf310362018-11-14 13:16:56 +000033namespace arm_compute
34{
35const cl::Buffer CLTensorAllocator::_empty_buffer = cl::Buffer();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036
Georgios Pinitas99d40952018-04-23 16:26:46 +010037namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038{
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010039/** Helper function used to allocate the backing memory of a tensor
40 *
41 * @param[in] context OpenCL context to use
42 * @param[in] size Size of the allocation
43 * @param[in] alignment Alignment of the allocation
44 *
45 * @return A wrapped memory region
46 */
Pablo Tellodb8485a2019-09-24 11:03:47 +010047std::unique_ptr<ICLMemoryRegion> allocate_region(CLCoreRuntimeContext *ctx, size_t size, cl_uint alignment)
Georgios Pinitas99d40952018-04-23 16:26:46 +010048{
49 // Try fine-grain SVM
Pablo Tellodb8485a2019-09-24 11:03:47 +010050 std::unique_ptr<ICLMemoryRegion> region = support::cpp14::make_unique<CLFineSVMMemoryRegion>(ctx,
Georgios Pinitasdf310362018-11-14 13:16:56 +000051 CL_MEM_READ_WRITE | CL_MEM_SVM_FINE_GRAIN_BUFFER,
52 size,
53 alignment);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054
Georgios Pinitas99d40952018-04-23 16:26:46 +010055 // Try coarse-grain SVM in case of failure
56 if(region != nullptr && region->ptr() == nullptr)
57 {
Pablo Tellodb8485a2019-09-24 11:03:47 +010058 region = support::cpp14::make_unique<CLCoarseSVMMemoryRegion>(ctx, CL_MEM_READ_WRITE, size, alignment);
Georgios Pinitas99d40952018-04-23 16:26:46 +010059 }
60 // Try legacy buffer memory in case of failure
61 if(region != nullptr && region->ptr() == nullptr)
62 {
Pablo Tellodb8485a2019-09-24 11:03:47 +010063 region = support::cpp14::make_unique<CLBufferMemoryRegion>(ctx, CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, size);
Georgios Pinitas99d40952018-04-23 16:26:46 +010064 }
65 return region;
66}
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010067/** Clears quantization arrays
68 *
69 * @param[in, out] scale Quantization scale array
70 * @param[in, out] offset Quantization offset array
71 */
72void clear_quantization_arrays(CLFloatArray &scale, CLInt32Array &offset)
73{
74 // Clear arrays
75 scale = CLFloatArray();
76 offset = CLInt32Array();
77}
78/** Helper function used to create quantization data arrays
79 *
80 * @param[in, out] scale Quantization scale array
81 * @param[in, out] offset Quantization offset array
82 * @param[in] qinfo Quantization info
83 * @param[in] pad_size Pad size to use in case array needs to be padded for computation purposes
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010084 */
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());
Michalis Spyrou3f632f32019-08-22 16:52:00 +010096
97 if(!qinfo.offset().empty())
98 {
99 // Create offset array
100 const std::vector<int32_t> &qoffset = qinfo.offset();
101 const size_t offset_element_size = sizeof(std::remove_reference<decltype(qoffset)>::type::value_type);
102 offset = CLInt32Array(num_elements + pad_size);
103 offset.resize(num_elements);
104 CLScheduler::get().queue().enqueueWriteBuffer(offset.cl_buffer(), CL_TRUE, 0, num_elements * offset_element_size, qinfo.offset().data());
105 }
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100106}
Georgios Pinitas99d40952018-04-23 16:26:46 +0100107} // namespace
108
Pablo Tellodb8485a2019-09-24 11:03:47 +0100109CLTensorAllocator::CLTensorAllocator(IMemoryManageable *owner, CLRuntimeContext *ctx)
110 : _ctx(ctx), _owner(owner), _associated_memory_group(nullptr), _memory(), _mapping(nullptr), _scale(), _offset()
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100111{
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100112}
113
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100114CLQuantization CLTensorAllocator::quantization() const
115{
116 return { &_scale, &_offset };
117}
118
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119uint8_t *CLTensorAllocator::data()
120{
Georgios Pinitasdf310362018-11-14 13:16:56 +0000121 return _mapping;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122}
123
124const cl::Buffer &CLTensorAllocator::cl_data() const
125{
Georgios Pinitasdf310362018-11-14 13:16:56 +0000126 return _memory.region() == nullptr ? _empty_buffer : _memory.cl_region()->cl_data();
Pablo Telloe86a09f2018-01-11 15:44:48 +0000127}
128
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129void CLTensorAllocator::allocate()
130{
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100131 // Allocate tensor backing memory
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100132 if(_associated_memory_group == nullptr)
133 {
Georgios Pinitasb785dd42019-09-19 12:09:32 +0100134 // Perform memory allocation
Pablo Tellodb8485a2019-09-24 11:03:47 +0100135 if(_ctx == nullptr)
136 {
137 auto legacy_ctx = CLCoreRuntimeContext(nullptr, CLScheduler::get().context(), CLScheduler::get().queue());
138 _memory.set_owned_region(allocate_region(&legacy_ctx, info().total_size(), 0));
139 }
140 else
141 {
142 _memory.set_owned_region(allocate_region(_ctx->core_runtime_context(), info().total_size(), 0));
143 }
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100144 }
145 else
146 {
Georgios Pinitas26014cf2019-09-09 19:00:57 +0100147 _associated_memory_group->finalize_memory(_owner, _memory, info().total_size(), alignment());
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100148 }
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100149
150 // Allocate and fill the quantization parameter arrays
Michalis Spyrouc8530212019-08-22 11:44:04 +0100151 if(is_data_type_quantized_per_channel(info().data_type()))
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100152 {
153 const size_t pad_size = 0;
154 populate_quantization_info(_scale, _offset, info().quantization_info(), pad_size);
155 }
156
157 // Lock allocator
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100158 info().set_is_resizable(false);
159}
160
161void CLTensorAllocator::free()
162{
Georgios Pinitasdf310362018-11-14 13:16:56 +0000163 _mapping = nullptr;
164 _memory.set_region(nullptr);
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100165 clear_quantization_arrays(_scale, _offset);
Georgios Pinitasdf310362018-11-14 13:16:56 +0000166 info().set_is_resizable(true);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100167}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100168
Georgios Pinitas4d0351c2019-04-03 15:11:16 +0100169Status CLTensorAllocator::import_memory(cl::Buffer buffer)
Georgios Pinitas99d40952018-04-23 16:26:46 +0100170{
Georgios Pinitasdf310362018-11-14 13:16:56 +0000171 ARM_COMPUTE_RETURN_ERROR_ON(buffer.get() == nullptr);
Georgios Pinitas4d0351c2019-04-03 15:11:16 +0100172 ARM_COMPUTE_RETURN_ERROR_ON(buffer.getInfo<CL_MEM_SIZE>() < info().total_size());
Georgios Pinitasdf310362018-11-14 13:16:56 +0000173 ARM_COMPUTE_RETURN_ERROR_ON(buffer.getInfo<CL_MEM_CONTEXT>().get() != CLScheduler::get().context().get());
Georgios Pinitas99d40952018-04-23 16:26:46 +0100174 ARM_COMPUTE_RETURN_ERROR_ON(_associated_memory_group != nullptr);
Georgios Pinitasdf310362018-11-14 13:16:56 +0000175
Pablo Tellodb8485a2019-09-24 11:03:47 +0100176 if(_ctx == nullptr)
177 {
178 auto legacy_ctx = CLCoreRuntimeContext(nullptr, CLScheduler::get().context(), CLScheduler::get().queue());
179 _memory.set_owned_region(support::cpp14::make_unique<CLBufferMemoryRegion>(buffer, &legacy_ctx));
180 }
181 else
182 {
183 _memory.set_owned_region(support::cpp14::make_unique<CLBufferMemoryRegion>(buffer, _ctx->core_runtime_context()));
184 }
Georgios Pinitas99d40952018-04-23 16:26:46 +0100185
Pablo Tellodb8485a2019-09-24 11:03:47 +0100186 info().set_is_resizable(false);
Georgios Pinitas99d40952018-04-23 16:26:46 +0100187 return Status{};
188}
189
Georgios Pinitas26014cf2019-09-09 19:00:57 +0100190void CLTensorAllocator::set_associated_memory_group(IMemoryGroup *associated_memory_group)
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100191{
192 ARM_COMPUTE_ERROR_ON(associated_memory_group == nullptr);
Michalis Spyroucaa7dee2019-09-09 19:23:39 +0100193 ARM_COMPUTE_ERROR_ON(_associated_memory_group != nullptr && _associated_memory_group != associated_memory_group);
Georgios Pinitasdf310362018-11-14 13:16:56 +0000194 ARM_COMPUTE_ERROR_ON(_memory.region() != nullptr && _memory.cl_region()->cl_data().get() != nullptr);
195
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100196 _associated_memory_group = associated_memory_group;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100197}
198
199uint8_t *CLTensorAllocator::lock()
200{
Pablo Tellodb8485a2019-09-24 11:03:47 +0100201 if(_ctx)
202 {
203 return map(_ctx->gpu_scheduler()->queue(), true);
204 }
205 else
206 {
207 return map(CLScheduler::get().queue(), true);
208 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100209}
210
211void CLTensorAllocator::unlock()
212{
Georgios Pinitas99d40952018-04-23 16:26:46 +0100213 ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
Pablo Tellodb8485a2019-09-24 11:03:47 +0100214 if(_ctx)
215 {
216 unmap(_ctx->gpu_scheduler()->queue(), reinterpret_cast<uint8_t *>(_memory.region()->buffer()));
217 }
218 else
219 {
220 //Legacy singleton api
221 unmap(CLScheduler::get().queue(), reinterpret_cast<uint8_t *>(_memory.region()->buffer()));
222 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100223}
224
225uint8_t *CLTensorAllocator::map(cl::CommandQueue &q, bool blocking)
226{
Georgios Pinitasdf310362018-11-14 13:16:56 +0000227 ARM_COMPUTE_ERROR_ON(_mapping != nullptr);
Georgios Pinitas99d40952018-04-23 16:26:46 +0100228 ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
229 ARM_COMPUTE_ERROR_ON(_memory.region()->buffer() != nullptr);
Georgios Pinitasdf310362018-11-14 13:16:56 +0000230
231 _mapping = reinterpret_cast<uint8_t *>(_memory.cl_region()->map(q, blocking));
232 return _mapping;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100233}
234
235void CLTensorAllocator::unmap(cl::CommandQueue &q, uint8_t *mapping)
236{
Georgios Pinitasdf310362018-11-14 13:16:56 +0000237 ARM_COMPUTE_ERROR_ON(_mapping == nullptr);
238 ARM_COMPUTE_ERROR_ON(_mapping != mapping);
Georgios Pinitas99d40952018-04-23 16:26:46 +0100239 ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
240 ARM_COMPUTE_ERROR_ON(_memory.region()->buffer() == nullptr);
Georgios Pinitasdf310362018-11-14 13:16:56 +0000241 ARM_COMPUTE_UNUSED(mapping);
242
243 _memory.cl_region()->unmap(q);
244 _mapping = nullptr;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100245}
Georgios Pinitasdf310362018-11-14 13:16:56 +0000246} // namespace arm_compute