Skip to content

Commit 79bb135

Browse files
committed
Update on "[Executorch] Introduce caching cpu memory allocator"
Meant to use this for temp allocator for kernels. Specifically for sdpa, it seems that on iOS there is a significant overhead coming from allocations Differential Revision: [D85532079](https://our.internmc.facebook.com/intern/diff/D85532079/) [ghstack-poisoned]
2 parents 0c23c32 + 68d76d3 commit 79bb135

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

extension/memory_allocator/cpu_caching_malloc_allocator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace executorch::extension {
66

77
namespace {
88
size_t get_alignment_adjusted_size(size_t size, size_t alignment) {
9-
alignment = std::max(alignment, kDefaultAlignment);
9+
alignment = std::max(alignment, kCachingAllocatorDefaultAlignment);
1010
if (size % alignment != 0) {
1111
// Adjust size to the next multiple of alignment
1212
// This is needed for aligned_alloc to work
@@ -66,6 +66,7 @@ void CPUCachingAllocator::free_cached() {
6666
}
6767
}
6868
available_map_.clear();
69+
current_size_ = 0;
6970
}
7071

7172
void CPUCachingAllocator::reset() {
@@ -75,7 +76,6 @@ void CPUCachingAllocator::reset() {
7576
size_t alloc_size = it.second;
7677
// Cache the memory
7778
available_map_[alloc_size].push_back(ptr);
78-
current_size_ -= alloc_size;
7979
}
8080
allocation_map_.clear();
8181
}

extension/memory_allocator/cpu_caching_malloc_allocator.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ template <typename KeyType, typename ValueType>
4141
using FlatHashMap = std::unordered_map<KeyType, ValueType>;
4242
#endif
4343

44-
constexpr size_t kDefaultAlignment = 64;
44+
constexpr size_t kCachingAllocatorDefaultAlignment = 64;
4545
class CPUCachingAllocator : public executorch::runtime::MemoryAllocator {
4646
/*
4747
* What it does:
@@ -73,7 +73,7 @@ class CPUCachingAllocator : public executorch::runtime::MemoryAllocator {
7373
// Checks the cache to see if allocation of size bytes can be found.
7474
// If so return cached memory, else
7575
// allocates memory, records it for caching and returns.
76-
void* allocate(size_t size, size_t alignment = kDefaultAlignment) override;
76+
void* allocate(size_t size, size_t alignment = kCachingAllocatorDefaultAlignment) override;
7777
void reset() override;
7878
~CPUCachingAllocator();
7979
};

extension/memory_allocator/test/cpu_caching_malloc_allocator_test.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
using namespace ::testing;
1313
using executorch::extension::CPUCachingAllocator;
1414

15-
constexpr auto kDefaultAlignment = executorch::extension::kDefaultAlignment;
15+
constexpr auto kDefaultAlignment = executorch::extension::kCachingAllocatorDefaultAlignment;
1616

1717
class CPUCachingAllocatorTest : public ::testing::Test {
1818
protected:
@@ -25,7 +25,7 @@ class CPUCachingAllocatorTest : public ::testing::Test {
2525

2626
bool is_aligned(const void* ptr, size_t alignment) {
2727
uintptr_t addr = reinterpret_cast<uintptr_t>(ptr);
28-
return addr % alignment == 0;
28+
return (addr & (alignment - 1)) == 0;
2929
}
3030

3131
#define EXPECT_ALIGNED(ptr, alignment) \
@@ -297,5 +297,6 @@ TEST_F(CPUCachingAllocatorTest, ResetMultipleTimes) {
297297

298298
auto p2 = allocator.allocate(512);
299299
EXPECT_EQ(p, p2);
300+
allocator.reset();
300301
}
301302
}

0 commit comments

Comments
 (0)