Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 4d0351c | 2019-04-03 15:11:16 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2019 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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/Error.h" |
| 27 | #include "arm_compute/core/TensorInfo.h" |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 28 | #include "arm_compute/runtime/CL/CLMemoryGroup.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 30 | |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 31 | namespace arm_compute |
| 32 | { |
| 33 | const cl::Buffer CLTensorAllocator::_empty_buffer = cl::Buffer(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 35 | namespace |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | { |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 37 | /** 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 Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 45 | std::unique_ptr<ICLMemoryRegion> allocate_region(const cl::Context &context, size_t size, cl_uint alignment) |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 46 | { |
| 47 | // Try fine-grain SVM |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 48 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 52 | |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 53 | // Try coarse-grain SVM in case of failure |
| 54 | if(region != nullptr && region->ptr() == nullptr) |
| 55 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 56 | region = support::cpp14::make_unique<CLCoarseSVMMemoryRegion>(context, CL_MEM_READ_WRITE, size, alignment); |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 57 | } |
| 58 | // Try legacy buffer memory in case of failure |
| 59 | if(region != nullptr && region->ptr() == nullptr) |
| 60 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 61 | region = support::cpp14::make_unique<CLBufferMemoryRegion>(context, CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, size); |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 62 | } |
| 63 | return region; |
| 64 | } |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 65 | /** Clears quantization arrays |
| 66 | * |
| 67 | * @param[in, out] scale Quantization scale array |
| 68 | * @param[in, out] offset Quantization offset array |
| 69 | */ |
| 70 | void 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 | */ |
| 85 | void 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 Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 90 | 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 Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 94 | scale.resize(num_elements); |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 95 | CLScheduler::get().queue().enqueueWriteBuffer(scale.cl_buffer(), CL_TRUE, 0, num_elements * element_size, qinfo.scale().data()); |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 96 | } |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 97 | } // namespace |
| 98 | |
| 99 | CLTensorAllocator::CLTensorAllocator(CLTensor *owner) |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 100 | : _associated_memory_group(nullptr), _memory(), _mapping(nullptr), _owner(owner), _scale(), _offset() |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 101 | { |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 104 | CLQuantization CLTensorAllocator::quantization() const |
| 105 | { |
| 106 | return { &_scale, &_offset }; |
| 107 | } |
| 108 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 109 | uint8_t *CLTensorAllocator::data() |
| 110 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 111 | return _mapping; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | const cl::Buffer &CLTensorAllocator::cl_data() const |
| 115 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 116 | return _memory.region() == nullptr ? _empty_buffer : _memory.cl_region()->cl_data(); |
Pablo Tello | e86a09f | 2018-01-11 15:44:48 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 119 | void CLTensorAllocator::allocate() |
| 120 | { |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 121 | // Allocate tensor backing memory |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 122 | if(_associated_memory_group == nullptr) |
| 123 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 124 | if(_memory.region() != nullptr && _memory.cl_region()->cl_data().get() != nullptr) |
Michele Di Giorgio | aaab602 | 2018-05-14 11:58:24 +0100 | [diff] [blame] | 125 | { |
| 126 | // Memory is already allocated. Reuse it if big enough, otherwise fire an assertion |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 127 | ARM_COMPUTE_ERROR_ON_MSG(info().total_size() > _memory.region()->size(), |
| 128 | "Reallocation of a bigger memory region is not allowed!"); |
Michele Di Giorgio | aaab602 | 2018-05-14 11:58:24 +0100 | [diff] [blame] | 129 | } |
| 130 | else |
| 131 | { |
| 132 | // Perform memory allocation |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 133 | _memory.set_owned_region(allocate_region(CLScheduler::get().context(), info().total_size(), 0)); |
Michele Di Giorgio | aaab602 | 2018-05-14 11:58:24 +0100 | [diff] [blame] | 134 | } |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 135 | } |
| 136 | else |
| 137 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 138 | _associated_memory_group->finalize_memory(_owner, _memory, info().total_size()); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 139 | } |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 140 | |
| 141 | // Allocate and fill the quantization parameter arrays |
Michalis Spyrou | c853021 | 2019-08-22 11:44:04 +0100 | [diff] [blame] | 142 | if(is_data_type_quantized_per_channel(info().data_type())) |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 143 | { |
| 144 | const size_t pad_size = 0; |
| 145 | populate_quantization_info(_scale, _offset, info().quantization_info(), pad_size); |
| 146 | } |
| 147 | |
| 148 | // Lock allocator |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 149 | info().set_is_resizable(false); |
| 150 | } |
| 151 | |
| 152 | void CLTensorAllocator::free() |
| 153 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 154 | _mapping = nullptr; |
| 155 | _memory.set_region(nullptr); |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 156 | clear_quantization_arrays(_scale, _offset); |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 157 | info().set_is_resizable(true); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 158 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 159 | |
Georgios Pinitas | 4d0351c | 2019-04-03 15:11:16 +0100 | [diff] [blame] | 160 | Status CLTensorAllocator::import_memory(cl::Buffer buffer) |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 161 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 162 | ARM_COMPUTE_RETURN_ERROR_ON(buffer.get() == nullptr); |
Georgios Pinitas | 4d0351c | 2019-04-03 15:11:16 +0100 | [diff] [blame] | 163 | ARM_COMPUTE_RETURN_ERROR_ON(buffer.getInfo<CL_MEM_SIZE>() < info().total_size()); |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 164 | ARM_COMPUTE_RETURN_ERROR_ON(buffer.getInfo<CL_MEM_CONTEXT>().get() != CLScheduler::get().context().get()); |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 165 | ARM_COMPUTE_RETURN_ERROR_ON(_associated_memory_group != nullptr); |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 166 | |
| 167 | _memory.set_owned_region(support::cpp14::make_unique<CLBufferMemoryRegion>(buffer)); |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 168 | info().set_is_resizable(false); |
| 169 | |
| 170 | return Status{}; |
| 171 | } |
| 172 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 173 | void 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 Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 177 | ARM_COMPUTE_ERROR_ON(_memory.region() != nullptr && _memory.cl_region()->cl_data().get() != nullptr); |
| 178 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 179 | _associated_memory_group = associated_memory_group; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | uint8_t *CLTensorAllocator::lock() |
| 183 | { |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 184 | return map(CLScheduler::get().queue(), true); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | void CLTensorAllocator::unlock() |
| 188 | { |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 189 | ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr); |
| 190 | unmap(CLScheduler::get().queue(), reinterpret_cast<uint8_t *>(_memory.region()->buffer())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | uint8_t *CLTensorAllocator::map(cl::CommandQueue &q, bool blocking) |
| 194 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 195 | ARM_COMPUTE_ERROR_ON(_mapping != nullptr); |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 196 | ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr); |
| 197 | ARM_COMPUTE_ERROR_ON(_memory.region()->buffer() != nullptr); |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 198 | |
| 199 | _mapping = reinterpret_cast<uint8_t *>(_memory.cl_region()->map(q, blocking)); |
| 200 | return _mapping; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | void CLTensorAllocator::unmap(cl::CommandQueue &q, uint8_t *mapping) |
| 204 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 205 | ARM_COMPUTE_ERROR_ON(_mapping == nullptr); |
| 206 | ARM_COMPUTE_ERROR_ON(_mapping != mapping); |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 207 | ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr); |
| 208 | ARM_COMPUTE_ERROR_ON(_memory.region()->buffer() == nullptr); |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 209 | ARM_COMPUTE_UNUSED(mapping); |
| 210 | |
| 211 | _memory.cl_region()->unmap(q); |
| 212 | _mapping = nullptr; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 213 | } |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 214 | } // namespace arm_compute |