|
10 | 10 |
|
11 | 11 | #include <executorch/runtime/core/memory_allocator.h>
|
12 | 12 | #include <executorch/runtime/core/result.h>
|
| 13 | +#include <executorch/runtime/core/span.h> |
13 | 14 | #include <executorch/runtime/platform/assert.h>
|
14 | 15 | #include <executorch/runtime/platform/compiler.h>
|
15 | 16 | #include <executorch/runtime/platform/log.h>
|
|
18 | 19 | namespace torch {
|
19 | 20 | namespace executor {
|
20 | 21 |
|
21 |
| -// A group of allocators that can be used to represent a device's memory |
22 |
| -// hierarchy. |
23 |
| -class HierarchicalAllocator { |
| 22 | +/** |
| 23 | + * A group of buffers that can be used to represent a device's memory hierarchy. |
| 24 | + */ |
| 25 | +class HierarchicalAllocator final { |
24 | 26 | public:
|
25 |
| - // Constructs a new hierarchycal allocator with the given array of allocators. |
26 |
| - // Memory IDs are assigned based on the index in the 'allocators' array. E.g. |
27 |
| - // the first allocator in the array will have a memory ID of 0. |
28 |
| - HierarchicalAllocator(uint32_t n_allocators, MemoryAllocator* allocators) |
29 |
| - : n_allocators_(n_allocators), allocators_(allocators) {} |
| 27 | + /** |
| 28 | + * Constructs a new hierarchical allocator with the given array of buffers. |
| 29 | + * |
| 30 | + * - Memory IDs are based on the index into `buffers`: `buffers[N]` will have |
| 31 | + * a memory ID of `N`. |
| 32 | + * - `buffers.size()` must be >= `MethodMeta::num_non_const_buffers()`. |
| 33 | + * - `buffers[N].size()` must be >= `MethodMeta::non_const_buffer_size(N)`. |
| 34 | + */ |
| 35 | + explicit HierarchicalAllocator(Span<Span<uint8_t>> buffers) |
| 36 | + : buffers_(buffers) {} |
| 37 | + |
| 38 | + /** |
| 39 | + * DEPRECATED: Use spans instead. |
| 40 | + */ |
| 41 | + __ET_DEPRECATED HierarchicalAllocator( |
| 42 | + uint32_t n_allocators, |
| 43 | + MemoryAllocator* allocators) |
| 44 | + : buffers_(to_spans(n_allocators, allocators)) {} |
30 | 45 |
|
31 | 46 | /**
|
32 | 47 | * Returns the address at the byte offset `offset_bytes` from the given
|
33 |
| - * allocator's base address, which should have at least `size_bytes` of memory |
34 |
| - * available inside the allocator's buffer. |
35 |
| - * |
36 |
| - * This is useful to point an object to this address when such information has |
37 |
| - * been predetermined. This method assumes that the given memory's allocator |
38 |
| - * has already reserved enough memory (i.e. there's no actual allocation call |
39 |
| - * to the underlying memory allocator). |
| 48 | + * buffer's base address, which points to at least `size_bytes` of memory. |
40 | 49 | *
|
41 |
| - * @param[in] memory_id The ID of the allocator in the hierarchy. |
42 |
| - * @param[in] offset_bytes The offset in bytes into the memory of the |
43 |
| - * specified allocator. |
| 50 | + * @param[in] memory_id The ID of the buffer in the hierarchy. |
| 51 | + * @param[in] offset_bytes The offset in bytes into the specified buffer. |
44 | 52 | * @param[in] size_bytes The amount of memory that should be available at
|
45 | 53 | * the offset.
|
46 | 54 | *
|
47 | 55 | * @returns On success, the address of the requested byte offset into the
|
48 |
| - * specified allocator. On failure, a non-Ok Error. |
| 56 | + * specified buffer. On failure, a non-Ok Error. |
49 | 57 | */
|
50 | 58 | __ET_NODISCARD Result<void*> get_offset_address(
|
51 | 59 | uint32_t memory_id,
|
52 | 60 | size_t offset_bytes,
|
53 | 61 | size_t size_bytes) {
|
54 |
| - Result<MemoryAllocator*> allocator_result = get_allocator(memory_id); |
55 |
| - if (!allocator_result.ok()) { |
56 |
| - return allocator_result.error(); |
57 |
| - } |
58 |
| - auto allocator = allocator_result.get(); |
59 | 62 | ET_CHECK_OR_RETURN_ERROR(
|
60 |
| - offset_bytes + size_bytes <= allocator->size(), |
| 63 | + memory_id < buffers_.size(), |
| 64 | + InvalidArgument, |
| 65 | + "id %" PRIu32 " >= %zu", |
| 66 | + memory_id, |
| 67 | + buffers_.size()); |
| 68 | + Span<uint8_t> buffer = buffers_[memory_id]; |
| 69 | + ET_CHECK_OR_RETURN_ERROR( |
| 70 | + offset_bytes + size_bytes <= buffer.size(), |
61 | 71 | MemoryAllocationFailed,
|
62 |
| - "offset_bytes (%zu) + size_bytes (%zu) >= allocator size (%" PRIu32 |
63 |
| - ") for memory_id %" PRIu32, |
| 72 | + "offset_bytes (%zu) + size_bytes (%zu) >= allocator size (%zu) " |
| 73 | + "for memory_id %" PRIu32, |
64 | 74 | offset_bytes,
|
65 | 75 | size_bytes,
|
66 |
| - allocator->size(), |
| 76 | + buffer.size(), |
67 | 77 | memory_id);
|
68 |
| - return allocator->base_address() + offset_bytes; |
| 78 | + return buffer.data() + offset_bytes; |
69 | 79 | }
|
70 | 80 |
|
71 |
| - virtual ~HierarchicalAllocator() {} |
72 |
| - |
73 | 81 | private:
|
74 |
| - /// Returns the memory allocator for the given 'memory_id' in the hierarchy. |
75 |
| - Result<MemoryAllocator*> get_allocator(uint32_t memory_id) const { |
76 |
| - ET_CHECK_OR_RETURN_ERROR( |
77 |
| - memory_id < n_allocators_, |
78 |
| - InvalidArgument, |
79 |
| - "Memory id %" PRIu32 " >= n_allocators_ %" PRIu32, |
80 |
| - memory_id, |
81 |
| - n_allocators_); |
82 |
| - return &allocators_[memory_id]; |
| 82 | + // TODO(T162089316): Remove the span array and to_spans once all users move to |
| 83 | + // spans. This array is necessary to hold the pointers and sizes that were |
| 84 | + // originally provided as MemoryAllocator instances. |
| 85 | + static constexpr size_t kSpanArraySize = 16; |
| 86 | + // NOTE: span_array_ must be declared before buffers_ so that it isn't |
| 87 | + // re-initialized to zeros after initializing buffers_. |
| 88 | + Span<uint8_t> span_array_[kSpanArraySize]; |
| 89 | + Span<Span<uint8_t>> to_spans( |
| 90 | + uint32_t n_allocators, |
| 91 | + MemoryAllocator* allocators) { |
| 92 | + ET_CHECK_MSG( |
| 93 | + n_allocators <= kSpanArraySize, |
| 94 | + "n_allocators %" PRIu32 " > %zu", |
| 95 | + n_allocators, |
| 96 | + kSpanArraySize); |
| 97 | + for (uint32_t i = 0; i < n_allocators; ++i) { |
| 98 | + span_array_[i] = |
| 99 | + Span<uint8_t>(allocators[i].base_address(), allocators[i].size()); |
| 100 | + } |
| 101 | + return {span_array_, n_allocators}; |
83 | 102 | }
|
84 | 103 |
|
85 |
| - // The HierarchicalAllocator holds n_allocators_ MemoryAllocators. |
86 |
| - uint32_t n_allocators_; |
87 |
| - |
88 |
| - // Memory allocators as an array, each ID corresponds to their index. |
89 |
| - MemoryAllocator* allocators_; |
| 104 | + /// The underlying buffers. |
| 105 | + Span<Span<uint8_t>> buffers_; |
90 | 106 | };
|
| 107 | + |
91 | 108 | } // namespace executor
|
92 | 109 | } // namespace torch
|
0 commit comments