Skip to content

Commit 23c1605

Browse files
gggekovStrycekSimon
authored andcommitted
Arm backend: Fix bug in memory allocation
Differential Revision: D82220111 Pull Request resolved: pytorch#14210
1 parent 49d9845 commit 23c1605

File tree

3 files changed

+8
-20
lines changed

3 files changed

+8
-20
lines changed

examples/arm/executor_runner/arm_executor_runner.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -763,13 +763,7 @@ void log_mem_status(const RunnerContext& ctx) {
763763
ET_LOG(Info, "method_allocator_executor: %zu bytes", executor_memsize);
764764
}
765765
if (ctx.temp_allocator->size() > 0) {
766-
ET_LOG(
767-
Info,
768-
"peak_temp_allocator: %zu / %zu free: %zu ( used: %zu %% ) ",
769-
ctx.temp_allocator->peak_used(),
770-
ctx.temp_allocator->size(),
771-
ctx.temp_allocator->free_size(),
772-
100 * ctx.temp_allocator->peak_used() / ctx.temp_allocator->size());
766+
ET_LOG(Info, "temp_allocator: %zu", ctx.temp_allocator->size());
773767
}
774768
}
775769

@@ -927,15 +921,20 @@ void verify_result(RunnerContext& ctx, const void* model_pte) {
927921
void run_model(RunnerContext& ctx, const void* model_pte) {
928922
Error status;
929923
ET_LOG(Info, "Starting running %d inferences...", num_inferences);
930-
931924
int n = 0;
932925
StartMeasurements();
933926
for (n = 0; n < num_inferences; n++) {
927+
ET_LOG(Debug, "Running inference number %d", n);
934928
// Run the model.
935929
status = ctx.method.value()->execute();
936930
if (status != Error::Ok) {
937931
break;
938932
}
933+
// Reset the temporary allocator holding the scratch buffer between
934+
// inferences. We want to reuse the temp_allocator between inferences of the
935+
// same Ethos-U custom delegate, not allocate memory with every new
936+
// inference.
937+
ctx.temp_allocator.reset(temp_allocation_pool_size, temp_allocation_pool);
939938
}
940939
StopMeasurements(n);
941940

examples/arm/executor_runner/arm_memory_allocator.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "arm_memory_allocator.h"
88

99
ArmMemoryAllocator::ArmMemoryAllocator(uint32_t size, uint8_t* base_address)
10-
: MemoryAllocator(size, base_address), used_(0), peak_used_(0) {}
10+
: MemoryAllocator(size, base_address), used_(0) {}
1111

1212
void* ArmMemoryAllocator::allocate(size_t size, size_t alignment) {
1313
void* ret = executorch::runtime::MemoryAllocator::allocate(size, alignment);
@@ -22,8 +22,6 @@ void* ArmMemoryAllocator::allocate(size_t size, size_t alignment) {
2222
} else {
2323
used_ = (used_ | (alignment - 1)) + 1 + size;
2424
}
25-
if (used_ > peak_used_)
26-
peak_used_ = used_;
2725
}
2826
return ret;
2927
}
@@ -32,10 +30,6 @@ size_t ArmMemoryAllocator::used_size() const {
3230
return used_;
3331
}
3432

35-
size_t ArmMemoryAllocator::peak_used() const {
36-
return peak_used_;
37-
}
38-
3933
size_t ArmMemoryAllocator::free_size() const {
4034
return executorch::runtime::MemoryAllocator::size() - used_;
4135
}

examples/arm/executor_runner/arm_memory_allocator.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,10 @@ class ArmMemoryAllocator : public executorch::runtime::MemoryAllocator {
2121
// Returns the used size of the allocator's memory buffer.
2222
size_t used_size() const;
2323

24-
// Returns the peak memory usage of the allocator's memory buffer
25-
// Peak usage is useful when doing multiple allocations & resets
26-
size_t peak_used() const;
27-
2824
// Returns the free size of the allocator's memory buffer.
2925
size_t free_size() const;
3026
void reset();
3127

3228
private:
3329
size_t used_;
34-
size_t peak_used_;
3530
};

0 commit comments

Comments
 (0)