xref: /aosp_15_r20/external/ComputeLibrary/src/runtime/TensorAllocator.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2016-2020 Arm Limited.
3*c217d954SCole Faust  *
4*c217d954SCole Faust  * SPDX-License-Identifier: MIT
5*c217d954SCole Faust  *
6*c217d954SCole Faust  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust  * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust  * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust  * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust  * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust  *
13*c217d954SCole Faust  * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust  * copies or substantial portions of the Software.
15*c217d954SCole Faust  *
16*c217d954SCole Faust  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust  * SOFTWARE.
23*c217d954SCole Faust  */
24*c217d954SCole Faust #include "arm_compute/runtime/TensorAllocator.h"
25*c217d954SCole Faust 
26*c217d954SCole Faust #include "arm_compute/core/Coordinates.h"
27*c217d954SCole Faust #include "arm_compute/core/Error.h"
28*c217d954SCole Faust #include "arm_compute/core/TensorInfo.h"
29*c217d954SCole Faust #include "arm_compute/runtime/MemoryGroup.h"
30*c217d954SCole Faust #include "arm_compute/runtime/MemoryRegion.h"
31*c217d954SCole Faust 
32*c217d954SCole Faust #include <cstddef>
33*c217d954SCole Faust 
34*c217d954SCole Faust using namespace arm_compute;
35*c217d954SCole Faust 
36*c217d954SCole Faust namespace
37*c217d954SCole Faust {
validate_subtensor_shape(const TensorInfo & parent_info,const TensorInfo & child_info,const Coordinates & coords)38*c217d954SCole Faust bool validate_subtensor_shape(const TensorInfo &parent_info, const TensorInfo &child_info, const Coordinates &coords)
39*c217d954SCole Faust {
40*c217d954SCole Faust     bool               is_valid     = true;
41*c217d954SCole Faust     const TensorShape &parent_shape = parent_info.tensor_shape();
42*c217d954SCole Faust     const TensorShape &child_shape  = child_info.tensor_shape();
43*c217d954SCole Faust     const size_t       parent_dims  = parent_info.num_dimensions();
44*c217d954SCole Faust     const size_t       child_dims   = child_info.num_dimensions();
45*c217d954SCole Faust 
46*c217d954SCole Faust     if(child_dims <= parent_dims)
47*c217d954SCole Faust     {
48*c217d954SCole Faust         for(size_t num_dimensions = child_dims; num_dimensions > 0; --num_dimensions)
49*c217d954SCole Faust         {
50*c217d954SCole Faust             const size_t child_dim_size = coords[num_dimensions - 1] + child_shape[num_dimensions - 1];
51*c217d954SCole Faust 
52*c217d954SCole Faust             if((coords[num_dimensions - 1] < 0) || (child_dim_size > parent_shape[num_dimensions - 1]))
53*c217d954SCole Faust             {
54*c217d954SCole Faust                 is_valid = false;
55*c217d954SCole Faust                 break;
56*c217d954SCole Faust             }
57*c217d954SCole Faust         }
58*c217d954SCole Faust     }
59*c217d954SCole Faust     else
60*c217d954SCole Faust     {
61*c217d954SCole Faust         is_valid = false;
62*c217d954SCole Faust     }
63*c217d954SCole Faust 
64*c217d954SCole Faust     return is_valid;
65*c217d954SCole Faust }
66*c217d954SCole Faust } // namespace
67*c217d954SCole Faust 
TensorAllocator(IMemoryManageable * owner)68*c217d954SCole Faust TensorAllocator::TensorAllocator(IMemoryManageable *owner)
69*c217d954SCole Faust     : _owner(owner), _associated_memory_group(nullptr), _memory()
70*c217d954SCole Faust {
71*c217d954SCole Faust }
72*c217d954SCole Faust 
~TensorAllocator()73*c217d954SCole Faust TensorAllocator::~TensorAllocator()
74*c217d954SCole Faust {
75*c217d954SCole Faust     info().set_is_resizable(true);
76*c217d954SCole Faust }
77*c217d954SCole Faust 
TensorAllocator(TensorAllocator && o)78*c217d954SCole Faust TensorAllocator::TensorAllocator(TensorAllocator &&o) noexcept
79*c217d954SCole Faust     : ITensorAllocator(std::move(o)),
80*c217d954SCole Faust       _owner(o._owner),
81*c217d954SCole Faust       _associated_memory_group(o._associated_memory_group),
82*c217d954SCole Faust       _memory(std::move(o._memory))
83*c217d954SCole Faust {
84*c217d954SCole Faust     o._owner                   = nullptr;
85*c217d954SCole Faust     o._associated_memory_group = nullptr;
86*c217d954SCole Faust     o._memory                  = Memory();
87*c217d954SCole Faust }
88*c217d954SCole Faust 
operator =(TensorAllocator && o)89*c217d954SCole Faust TensorAllocator &TensorAllocator::operator=(TensorAllocator &&o) noexcept
90*c217d954SCole Faust {
91*c217d954SCole Faust     if(&o != this)
92*c217d954SCole Faust     {
93*c217d954SCole Faust         _owner   = o._owner;
94*c217d954SCole Faust         o._owner = nullptr;
95*c217d954SCole Faust 
96*c217d954SCole Faust         _associated_memory_group   = o._associated_memory_group;
97*c217d954SCole Faust         o._associated_memory_group = nullptr;
98*c217d954SCole Faust 
99*c217d954SCole Faust         _memory   = std::move(o._memory);
100*c217d954SCole Faust         o._memory = Memory();
101*c217d954SCole Faust 
102*c217d954SCole Faust         ITensorAllocator::operator=(std::move(o));
103*c217d954SCole Faust     }
104*c217d954SCole Faust     return *this;
105*c217d954SCole Faust }
106*c217d954SCole Faust 
init(const TensorAllocator & allocator,const Coordinates & coords,TensorInfo & sub_info)107*c217d954SCole Faust void TensorAllocator::init(const TensorAllocator &allocator, const Coordinates &coords, TensorInfo &sub_info)
108*c217d954SCole Faust {
109*c217d954SCole Faust     // Get parent info
110*c217d954SCole Faust     const TensorInfo parent_info = allocator.info();
111*c217d954SCole Faust 
112*c217d954SCole Faust     // Check if coordinates and new shape are within the parent tensor
113*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON(!validate_subtensor_shape(parent_info, sub_info, coords));
114*c217d954SCole Faust     ARM_COMPUTE_UNUSED(validate_subtensor_shape);
115*c217d954SCole Faust 
116*c217d954SCole Faust     // Copy pointer to buffer
117*c217d954SCole Faust     _memory = Memory(allocator._memory.region());
118*c217d954SCole Faust 
119*c217d954SCole Faust     // Init tensor info with new dimensions
120*c217d954SCole Faust     size_t total_size = parent_info.offset_element_in_bytes(coords) + sub_info.total_size() - sub_info.offset_first_element_in_bytes();
121*c217d954SCole Faust     sub_info.init(sub_info.tensor_shape(), sub_info.format(), parent_info.strides_in_bytes(), parent_info.offset_element_in_bytes(coords), total_size);
122*c217d954SCole Faust 
123*c217d954SCole Faust     // Set TensorInfo
124*c217d954SCole Faust     init(sub_info);
125*c217d954SCole Faust }
126*c217d954SCole Faust 
data() const127*c217d954SCole Faust uint8_t *TensorAllocator::data() const
128*c217d954SCole Faust {
129*c217d954SCole Faust     return (_memory.region() == nullptr) ? nullptr : reinterpret_cast<uint8_t *>(_memory.region()->buffer());
130*c217d954SCole Faust }
131*c217d954SCole Faust 
allocate()132*c217d954SCole Faust void TensorAllocator::allocate()
133*c217d954SCole Faust {
134*c217d954SCole Faust     // Align to 64-byte boundaries by default if alignment is not specified
135*c217d954SCole Faust     const size_t alignment_to_use = (alignment() != 0) ? alignment() : 64;
136*c217d954SCole Faust     if(_associated_memory_group == nullptr)
137*c217d954SCole Faust     {
138*c217d954SCole Faust         _memory.set_owned_region(std::make_unique<MemoryRegion>(info().total_size(), alignment_to_use));
139*c217d954SCole Faust     }
140*c217d954SCole Faust     else
141*c217d954SCole Faust     {
142*c217d954SCole Faust         _associated_memory_group->finalize_memory(_owner, _memory, info().total_size(), alignment_to_use);
143*c217d954SCole Faust     }
144*c217d954SCole Faust     info().set_is_resizable(false);
145*c217d954SCole Faust }
146*c217d954SCole Faust 
free()147*c217d954SCole Faust void TensorAllocator::free()
148*c217d954SCole Faust {
149*c217d954SCole Faust     _memory.set_region(nullptr);
150*c217d954SCole Faust     info().set_is_resizable(true);
151*c217d954SCole Faust }
152*c217d954SCole Faust 
import_memory(void * memory)153*c217d954SCole Faust Status TensorAllocator::import_memory(void *memory)
154*c217d954SCole Faust {
155*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON(memory == nullptr);
156*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON(_associated_memory_group != nullptr);
157*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON(alignment() != 0 && !arm_compute::utility::check_aligned(memory, alignment()));
158*c217d954SCole Faust 
159*c217d954SCole Faust     _memory.set_owned_region(std::make_unique<MemoryRegion>(memory, info().total_size()));
160*c217d954SCole Faust     info().set_is_resizable(false);
161*c217d954SCole Faust 
162*c217d954SCole Faust     return Status{};
163*c217d954SCole Faust }
164*c217d954SCole Faust 
set_associated_memory_group(IMemoryGroup * associated_memory_group)165*c217d954SCole Faust void TensorAllocator::set_associated_memory_group(IMemoryGroup *associated_memory_group)
166*c217d954SCole Faust {
167*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON(associated_memory_group == nullptr);
168*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON(_associated_memory_group != nullptr && _associated_memory_group != associated_memory_group);
169*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON(_memory.region() != nullptr && _memory.region()->buffer() != nullptr);
170*c217d954SCole Faust 
171*c217d954SCole Faust     _associated_memory_group = associated_memory_group;
172*c217d954SCole Faust }
173*c217d954SCole Faust 
lock()174*c217d954SCole Faust uint8_t *TensorAllocator::lock()
175*c217d954SCole Faust {
176*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
177*c217d954SCole Faust     return reinterpret_cast<uint8_t *>(_memory.region()->buffer());
178*c217d954SCole Faust }
179*c217d954SCole Faust 
unlock()180*c217d954SCole Faust void TensorAllocator::unlock()
181*c217d954SCole Faust {
182*c217d954SCole Faust }
183