Skip to content

Commit ce9c22d

Browse files
authored
Reset Temp Allocator after each use
Differential Revision: D80191057 Pull Request resolved: #13384
1 parent 31e13b0 commit ce9c22d

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

runtime/executor/method.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,9 @@ Error Method::resolve_operator(
713713
}
714714
TensorMeta* meta = allocator->allocateList<TensorMeta>(n_args);
715715
if (meta == nullptr) {
716+
if (allocator == memory_manager_->temp_allocator()) {
717+
memory_manager_->temp_allocator()->reset();
718+
}
716719
return Error::MemoryAllocationFailed;
717720
}
718721

@@ -726,6 +729,9 @@ Error Method::resolve_operator(
726729
executorch::aten::DimOrderType* dim_order_ptr =
727730
allocator->allocateList<executorch::aten::DimOrderType>(tensor.dim());
728731
if (dim_order_ptr == nullptr) {
732+
if (allocator == memory_manager_->temp_allocator()) {
733+
memory_manager_->temp_allocator()->reset();
734+
}
729735
return Error::MemoryAllocationFailed;
730736
}
731737
size_t size = tensor.dim();
@@ -751,9 +757,18 @@ Error Method::resolve_operator(
751757
"Missing operator: [%" ET_PRIssize_t "] %s",
752758
static_cast<ssize_t>(op_index),
753759
operator_name);
760+
if (allocator == memory_manager_->temp_allocator()) {
761+
memory_manager_->temp_allocator()->reset();
762+
}
754763
return op_function.error();
755764
}
756765
kernels[kernel_index] = op_function.get();
766+
767+
// If we used the temp allocator here, reset it.
768+
if (allocator == memory_manager_->temp_allocator()) {
769+
memory_manager_->temp_allocator()->reset();
770+
}
771+
757772
return Error::Ok;
758773
}
759774

@@ -1547,6 +1562,9 @@ Error Method::execute() {
15471562
i);
15481563
}
15491564
ET_LOG(Debug, "Executing method: %s.", method_meta().name());
1565+
if (temp_allocator_ != nullptr) {
1566+
temp_allocator_->reset();
1567+
}
15501568

15511569
// Chains are executed sequentially today, but future async designs may
15521570
// branch and run many in parallel or out of order.

runtime/executor/test/kernel_integration_test.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,9 @@ TEST_F(KernelTempMemoryAllocatorIntegrationTest, UsingTempMemoryAllocator) {
367367
EXPECT_EQ(control_->total_allocated_size, 4);
368368
EXPECT_EQ(temp_allocator_->number_of_allocations, 1);
369369
EXPECT_EQ(temp_allocator_->total_allocated_size, 4);
370-
// The temp allocator should have been reset after the execution.
371-
EXPECT_EQ(temp_allocator_->number_of_resets, 1);
370+
// The temp allocator should have been reset after the execution and before
371+
// method execution.
372+
EXPECT_EQ(temp_allocator_->number_of_resets, 2);
372373
EXPECT_EQ(temp_allocator_->currently_allocated_size, 0);
373374

374375
control_->temp_memory_size = 8;
@@ -379,6 +380,6 @@ TEST_F(KernelTempMemoryAllocatorIntegrationTest, UsingTempMemoryAllocator) {
379380
EXPECT_EQ(temp_allocator_->number_of_allocations, 2);
380381
EXPECT_EQ(temp_allocator_->total_allocated_size, 12);
381382
// The temp allocator should have been reset after the execution.
382-
EXPECT_EQ(temp_allocator_->number_of_resets, 2);
383+
EXPECT_EQ(temp_allocator_->number_of_resets, 4);
383384
EXPECT_EQ(temp_allocator_->currently_allocated_size, 0);
384385
}

0 commit comments

Comments
 (0)