Skip to content

Commit eaa165b

Browse files
committed
[ET-VK] Allocate memory for weight and activation tensors lazily
Summary: * Allocate memory for weight tensors right before the prepacking shader is dispatched, rather than while building the graph * Move allocation of shared objects (i.e. memory for intermediate tensors) to occur after prepacking ## Motivation Prevent screen blackout (Llama 3.2 1B) / device crash (Llama 3.2 3B) when running Llama 3.2 models on Samsung Galaxy S24. This behaviour is related to high peak memory usage when loading the model. ## Full Context During model loading, Vulkan delegate needs to store 3 copies of constant data in memory at various points: * source data obtained from loading the model * staging buffer * GPU texture/buffer The general rationale of this change is to allocate memory for each copy only when necessary to minimize the "overlap" when all 3 exist at once. ### Current Order of operations Legend: * `W` represents total weight nbytes * `w` represents weight nbytes for one tensor * `A` represents total activations nbytes * `M` represents approximation of total memory footprint First, model file is loaded Then, when building compute graph, for each weight tensor: 1. Weight data is loaded from NamedDataMap (`M = W`) 2. GPU texture/buffer for weight is initialized + memory allocated (`M = 2W`) 3. After building the graph, `graph->prepare()` is called which currently allocates memory for the activation tensors as well (`M = 2W + A`) Then, during the prepacking stage for each weight tensor, each weight tensor is copied individually: 1. Staging buffer initialized (`M = 2W + A + w`) 2. Copy CPU weight data to staging + CPU Weight data is freed (`M = 2W + A`) 3. Compute shader dispatch to copy staging to GPU texture/buffer + free staging buffer (`M = 2W + A - w`) The peak usage in mainline will be `M = 2W + A + w` ### Revised order of operations This change revises the order of operations: 1. Weight data is loaded from NamedDataMap (`M = W`) 2. GPU texture/buffer for weight is initialized, but **memory is not allocated** (`M = W`) Then, during the prepacking stage for each weight tensor, each weight tensor is copied individually: 1. Staging buffer initialized (`M = W + w`) 2. **Memory allocated for GPU texture/buffer** (`M = W + 2w`) 3. Copy CPU weight data to staging + CPU Weight data is freed (`M = W + w`) 4. Compute shader dispatch to copy staging to GPU texture/buffer + free staging buffer (`M = W`) **Then, after all prepacking operations complete, only then is Activation memory allocated** (`M = W + A`) Under this scheme, peak memory is reduced to `M = W + A` (or alternatively `M = W + 2w` if `2w > A`) which is (or at least very close to) the theoretical minimum. Test Plan: ## Logging Memory Usage Using ``` uint64_t getVmRssInKB() { std::ifstream statusFile("/proc/self/status"); std::string l, num; while (std::getline(statusFile, l)) { if (l.substr(0, 5) == "VmRSS") { size_t pos = l.find_first_of("0123456789"); num = l.substr(pos); break; } } uint64_t vmRssInKB = std::stoi(num); return vmRssInKB; } uint64_t getVmaStatsInKB() { auto stats = vkcompute::api::context()->adapter_ptr()->vma().get_memory_statistics(); uint64_t vmaBlockInKB = stats.total.statistics.blockBytes >> 10; return vmaBlockInKB; } ``` to log memory footprint at various points of inference when running the llama_runner binary with Llama 3.2 1B, we can compare the memory footprint with and without these changes. With changes: P1908051860 (Meta only) ``` Memory usage before model compilation: 1115760 KB (VmRSS), 0 KB (VMA) Memory usage after graph building: 1924832 KB (VmRSS), 17920 KB (VMA) Memory usage after graph preparation: 1935312 KB (VmRSS), 17920 KB (VMA) Memory usage prepack start: 1935312 KB, VMA Block: 17920 KB Memory usage after prepack operations: 1372376 KB (VmRSS), 2330528 KB (VMA) Memory usage before execute: 1372804 KB (VmRSS), 2330528 KB (VMA) Memory usage at end of execute: 1376916 KB (VmRSS), 2330528 KB (VMA) ``` WIthout changes: P1908054759 (Meta only) ``` Memory usage before model compilation: 1114784 KB (VmRSS), 0 KB (VMA) Memory usage after graph building: 1924432 KB (VmRSS), 962464 KB (VMA) Memory usage after graph preparation: 1922916 KB (VmRSS), 2326432 KB (VMA) Memory usage prepack start: 1922916 KB, VMA Block: 2326432 KB Memory usage after prepack operations: 1359180 KB (VmRSS), 2330528 KB (VMA) Memory usage before execute: 1359492 KB (VmRSS), 2330528 KB (VMA) Memory usage at end of execute: 1363636 KB (VmRSS), 2330528 KB (VMA) ``` It is evident how peak memory can be reduced with these changes, as VMA footprint gradually increases while loading the model while VmRss gradually decreases. Without these changes, VMA footprint will reach its peak after initializing the graph. Visually, it can also be verified that Samsung Galaxy S24's screen no longer blacks out while loading the model. [ghstack-poisoned]
1 parent a1a612b commit eaa165b

File tree

9 files changed

+134
-31
lines changed

9 files changed

+134
-31
lines changed

backends/vulkan/runtime/api/containers/Tensor.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,16 @@ VkMemoryRequirements vTensor::get_memory_requirements() const {
897897
return {};
898898
}
899899

900+
bool vTensor::memory_is_bound() const {
901+
switch (storage_type()) {
902+
case utils::kBuffer:
903+
return storage_->buffer_.has_memory();
904+
case utils::kTexture2D:
905+
case utils::kTexture3D:
906+
return storage_->image_.has_memory();
907+
}
908+
}
909+
900910
void vTensor::bind_allocation(const vkapi::Allocation& allocation) {
901911
switch (storage_type()) {
902912
case utils::kBuffer:
@@ -909,6 +919,18 @@ void vTensor::bind_allocation(const vkapi::Allocation& allocation) {
909919
}
910920
}
911921

922+
void vTensor::acquire_allocation(vkapi::Allocation&& allocation) {
923+
switch (storage_type()) {
924+
case utils::kBuffer:
925+
storage_->buffer_.acquire_allocation(std::move(allocation));
926+
break;
927+
case utils::kTexture2D:
928+
case utils::kTexture3D:
929+
storage_->image_.acquire_allocation(std::move(allocation));
930+
break;
931+
}
932+
}
933+
912934
void vTensor::update_metadata() {
913935
numel_ = utils::multiply_integers(sizes_);
914936
strides_ = calculate_strides(sizes_, dim_order_);

backends/vulkan/runtime/api/containers/Tensor.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,12 @@ class vTensor final {
560560
*/
561561
VmaAllocationCreateInfo get_allocation_create_info() const;
562562

563+
/*
564+
* Checks if the tensor's underlying buffer or image resource is bound to a
565+
* memory allocation.
566+
*/
567+
bool memory_is_bound() const;
568+
563569
/*
564570
* Return the VkMemoryRequirements of the underlying resource
565571
*/
@@ -570,6 +576,11 @@ class vTensor final {
570576
*/
571577
void bind_allocation(const vkapi::Allocation& allocation);
572578

579+
/*
580+
* Binds and acquires a rvalue memory allocation
581+
*/
582+
void acquire_allocation(vkapi::Allocation&& allocation);
583+
573584
private:
574585
/*
575586
* Assuming sizes, dim order, or axis mapping was modified, recompute all

backends/vulkan/runtime/graph/ComputeGraph.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,6 @@ ValueRef ComputeGraph::add_tensor(
356356
const utils::GPUMemoryLayout memory_layout,
357357
const int64_t shared_object_idx,
358358
const utils::AxisMapLayout axis_map_layout) {
359-
bool allocate_memory = shared_object_idx < 0;
360-
361359
ValueRef idx(static_cast<int>(values_.size()));
362360
check_no_active_value_ptrs();
363361
values_.emplace_back(api::vTensor(
@@ -366,10 +364,10 @@ ValueRef ComputeGraph::add_tensor(
366364
dtype,
367365
storage_type,
368366
memory_layout,
369-
allocate_memory,
367+
false,
370368
axis_map_layout));
371369

372-
if (!allocate_memory) {
370+
if (shared_object_idx >= 0) {
373371
get_shared_object(shared_object_idx).add_user(this, idx);
374372
}
375373
return idx;
@@ -626,6 +624,17 @@ SharedObject& ComputeGraph::get_shared_object(const int64_t idx) {
626624
return shared_objects_.at(idx);
627625
}
628626

627+
void ComputeGraph::create_dedicated_allocation_for(const ValueRef idx) {
628+
vTensorPtr tensor = get_tensor(idx);
629+
if (!tensor->memory_is_bound()) {
630+
VmaAllocationCreateInfo alloc_create_info =
631+
context()->adapter_ptr()->vma().gpuonly_resource_create_info();
632+
tensor->acquire_allocation(
633+
context()->adapter_ptr()->vma().create_allocation(
634+
tensor->get_memory_requirements(), alloc_create_info));
635+
}
636+
}
637+
629638
void ComputeGraph::update_descriptor_counts(
630639
const vkapi::ShaderInfo& shader_info,
631640
bool execute) {
@@ -823,11 +832,6 @@ void ComputeGraph::prepare() {
823832
if (config_.enable_querypool) {
824833
context_->initialize_querypool();
825834
}
826-
827-
for (SharedObject& shared_object : shared_objects_) {
828-
shared_object.allocate(this);
829-
shared_object.bind_users(this);
830-
}
831835
}
832836

833837
void ComputeGraph::prepare_pipelines() {
@@ -950,6 +954,12 @@ void ComputeGraph::prepack() {
950954
submit_current_cmd_and_wait(/*final_use=*/true);
951955
context_->flush();
952956
staging_nbytes_in_cmd_ = 0;
957+
958+
// Initialize allocations for intermediate tensors
959+
for (SharedObject& shared_object : shared_objects_) {
960+
shared_object.allocate(this);
961+
shared_object.bind_users(this);
962+
}
953963
}
954964

955965
void ComputeGraph::execute() {

backends/vulkan/runtime/graph/ComputeGraph.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,13 @@ class ComputeGraph final {
827827

828828
SharedObject& get_shared_object(const int64_t idx);
829829

830+
/*
831+
* Creates a dedicated memory allocation for a vTensor value, and have the
832+
* tensor acquire the allocation object. If the tensor is already bound to a
833+
* memory allocation, this function will be a no-op.
834+
*/
835+
void create_dedicated_allocation_for(const ValueRef idx);
836+
830837
//
831838
// Graph Preparation
832839
//

backends/vulkan/runtime/graph/ops/PrepackNode.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ void PrepackNode::encode(ComputeGraph* graph) {
9797
}
9898

9999
{
100+
// If the vTensor is not yet bound to a memory allocation, create a new one
101+
// and aquire it.
102+
graph->create_dedicated_allocation_for(packed_);
103+
100104
vkapi::PipelineBarrier pipeline_barrier{};
101105
vkapi::DescriptorSet descriptor_set = context->get_descriptor_set(
102106
shader_, local_workgroup_size_, spec_vars_, push_constants_offset);

backends/vulkan/runtime/vk_api/memory/Buffer.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,23 @@ VmaAllocationInfo VulkanBuffer::allocation_info() const {
136136
return info;
137137
}
138138

139+
void VulkanBuffer::bind_allocation_impl(const Allocation& memory) {
140+
VK_CHECK_COND(!memory_, "Cannot bind an already bound allocation!");
141+
if (!is_copy_) {
142+
VK_CHECK(vmaBindBufferMemory(allocator_, memory.allocation, handle_));
143+
}
144+
}
145+
146+
void VulkanBuffer::bind_allocation(const Allocation& memory) {
147+
bind_allocation_impl(memory);
148+
memory_.allocation = memory.allocation;
149+
}
150+
151+
void VulkanBuffer::acquire_allocation(Allocation&& memory) {
152+
bind_allocation_impl(memory);
153+
memory_ = std::move(memory);
154+
}
155+
139156
VkMemoryRequirements VulkanBuffer::get_memory_requirements() const {
140157
VkMemoryRequirements memory_requirements;
141158
vkGetBufferMemoryRequirements(this->device(), handle_, &memory_requirements);

backends/vulkan/runtime/vk_api/memory/Buffer.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,21 @@ class VulkanBuffer final {
162162
return (handle_ == other.handle_) && is_copy_;
163163
}
164164

165-
inline void bind_allocation(const Allocation& memory) {
166-
VK_CHECK_COND(!memory_, "Cannot bind an already bound allocation!");
167-
if (!is_copy_) {
168-
VK_CHECK(vmaBindBufferMemory(allocator_, memory.allocation, handle_));
169-
}
170-
memory_.allocation = memory.allocation;
171-
}
165+
private:
166+
void bind_allocation_impl(const Allocation& memory);
167+
168+
public:
169+
/*
170+
* Given a memory allocation, bind it to the underlying VkImage. The lifetime
171+
* of the memory allocation is assumed to be managed externally.
172+
*/
173+
void bind_allocation(const Allocation& memory);
174+
175+
/*
176+
* Given a rvalue memory allocation, bind it to the underlying VkImage and
177+
* also acquire ownership of the memory allocation.
178+
*/
179+
void acquire_allocation(Allocation&& memory);
172180

173181
VkMemoryRequirements get_memory_requirements() const;
174182

backends/vulkan/runtime/vk_api/memory/Image.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,30 @@ void VulkanImage::create_image_view() {
319319
&(handles_.image_view)));
320320
}
321321

322+
void VulkanImage::bind_allocation_impl(const Allocation& memory) {
323+
VK_CHECK_COND(!memory_, "Cannot bind an already bound allocation!");
324+
// To prevent multiple instances of binding the same VkImage to a memory
325+
// block, do not actually bind memory if this VulkanImage is a copy. Assume
326+
// that the original VulkanImage is responsible for binding the image.
327+
if (!is_copy_) {
328+
VK_CHECK(vmaBindImageMemory(allocator_, memory.allocation, handles_.image));
329+
}
330+
331+
// Only create the image view if the image has been bound to memory
332+
owns_view_ = true;
333+
create_image_view();
334+
}
335+
336+
void VulkanImage::bind_allocation(const Allocation& memory) {
337+
bind_allocation_impl(memory);
338+
memory_.allocation = memory.allocation;
339+
}
340+
341+
void VulkanImage::acquire_allocation(Allocation&& memory) {
342+
bind_allocation_impl(memory);
343+
memory_ = std::move(memory);
344+
}
345+
322346
VkMemoryRequirements VulkanImage::get_memory_requirements() const {
323347
VkMemoryRequirements memory_requirements;
324348
vkGetImageMemoryRequirements(

backends/vulkan/runtime/vk_api/memory/Image.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -242,21 +242,21 @@ class VulkanImage final {
242242
return (handles_.image == other.handles_.image) && is_copy_;
243243
}
244244

245-
inline void bind_allocation(const Allocation& memory) {
246-
VK_CHECK_COND(!memory_, "Cannot bind an already bound allocation!");
247-
// To prevent multiple instances of binding the same VkImage to a memory
248-
// block, do not actually bind memory if this VulkanImage is a copy. Assume
249-
// that the original VulkanImage is responsible for binding the image.
250-
if (!is_copy_) {
251-
VK_CHECK(
252-
vmaBindImageMemory(allocator_, memory.allocation, handles_.image));
253-
}
254-
memory_.allocation = memory.allocation;
255-
256-
// Only create the image view if the image has been bound to memory
257-
owns_view_ = true;
258-
create_image_view();
259-
}
245+
private:
246+
void bind_allocation_impl(const Allocation& memory);
247+
248+
public:
249+
/*
250+
* Given a memory allocation, bind it to the underlying VkImage. The lifetime
251+
* of the memory allocation is assumed to be managed externally.
252+
*/
253+
void bind_allocation(const Allocation& memory);
254+
255+
/*
256+
* Given a rvalue memory allocation, bind it to the underlying VkImage and
257+
* also acquire ownership of the memory allocation.
258+
*/
259+
void acquire_allocation(Allocation&& memory);
260260

261261
VkMemoryRequirements get_memory_requirements() const;
262262

0 commit comments

Comments
 (0)