Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory Pool Improvement For Variadic Sized Inputs #4190

Merged
merged 34 commits into from
Oct 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
f5783b5
A LayerNorm_x86 class mocking LayerNorm for tests;
LinHeLurking Jul 18, 2022
7a94b1a
All SIMD optimizations success wihout support_packing; Maybe there's …
LinHeLurking Jul 20, 2022
b126e0f
Located error about packed layout.
LinHeLurking Jul 21, 2022
1982605
All test passed; Now it supports packing layout
LinHeLurking Jul 21, 2022
0fa8689
Fix runtime cpu dispatch;
LinHeLurking Jul 21, 2022
6a683d3
Use fmadd wrapper in x86_usability.h;
LinHeLurking Jul 22, 2022
bf95312
Merge packed & unpacked code.
LinHeLurking Jul 22, 2022
af97b05
Func rename.
LinHeLurking Jul 22, 2022
a9be63a
Simplify and merge more branches about packed layout;
LinHeLurking Jul 22, 2022
976692a
Code format
LinHeLurking Jul 24, 2022
d7007c3
Replace some member functions with static inline functions.
LinHeLurking Jul 25, 2022
508d143
Add copyright header
LinHeLurking Jul 25, 2022
cf015d8
apply code-format changes
LinHeLurking Jul 25, 2022
5084955
Add more tests with 16 packed for AVX512
LinHeLurking Jul 25, 2022
48fb4ea
Code format
LinHeLurking Jul 25, 2022
8c4ed97
Merge branch 'master' of https://github.com/Tencent/ncnn
LinHeLurking Jul 25, 2022
3c2c1c8
Merge branch 'master' of https://github.com/LinHeLurking/ncnn
LinHeLurking Jul 25, 2022
487568d
Copyright statement year fixed
LinHeLurking Jul 26, 2022
23db5ab
Fix accidentally added corelation of mean/var and SIMD ISA
LinHeLurking Jul 26, 2022
72777b4
Fix accidentally added corelation of fmadd/affine_fmadd and SIMD ISA
LinHeLurking Jul 26, 2022
b20d298
Fix a wrong test param
LinHeLurking Jul 26, 2022
4fddf9e
Fix runtime dispatch
LinHeLurking Jul 26, 2022
2555b3e
apply code-format changes
LinHeLurking Jul 26, 2022
1b118f7
no store duplicates
nihui Jul 29, 2022
649a63b
Merge branch 'Tencent:master' into master
LinHeLurking Aug 1, 2022
49a5b7f
Simple miss count for better space efficiency
LinHeLurking Aug 11, 2022
2ecda04
Change miss count position;
LinHeLurking Aug 16, 2022
cee9d22
Simple double ended greedy;
LinHeLurking Sep 7, 2022
3f2731f
Merge branch 'Tencent:master' into master
LinHeLurking Sep 7, 2022
577d593
Merge branch 'master' into mem-pool
LinHeLurking Sep 7, 2022
ee7ca30
Fix santinizer bug;
LinHeLurking Sep 26, 2022
d4b6698
Change it_max/it_min initial value;
LinHeLurking Oct 6, 2022
4a0a1a9
Add size drop threshold setter;
LinHeLurking Oct 6, 2022
4066294
set workspace allocator cr to zero as we had some sort of recylcing c…
nihui Oct 8, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions benchmark/benchncnn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ int main(int argc, char** argv)

g_loop_count = loop_count;

g_blob_pool_allocator.set_size_compare_ratio(0.0f);
g_workspace_pool_allocator.set_size_compare_ratio(0.5f);
g_blob_pool_allocator.set_size_compare_ratio(0.f);
g_workspace_pool_allocator.set_size_compare_ratio(0.f);

#if NCNN_VULKAN
if (use_vulkan_compute)
Expand Down
74 changes: 70 additions & 4 deletions src/allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ class PoolAllocatorPrivate
Mutex budgets_lock;
Mutex payouts_lock;
unsigned int size_compare_ratio; // 0~256
size_t size_drop_threshold;
std::list<std::pair<size_t, void*> > budgets;
std::list<std::pair<size_t, void*> > payouts;
};

PoolAllocator::PoolAllocator()
: Allocator(), d(new PoolAllocatorPrivate)
{
d->size_compare_ratio = 192; // 0.75f * 256
d->size_compare_ratio = 0;
d->size_drop_threshold = 10;
}

PoolAllocator::~PoolAllocator()
Expand Down Expand Up @@ -99,12 +101,17 @@ void PoolAllocator::set_size_compare_ratio(float scr)
d->size_compare_ratio = (unsigned int)(scr * 256);
}

void PoolAllocator::set_size_drop_threshold(size_t threshold)
{
d->size_drop_threshold = threshold;
}

void* PoolAllocator::fastMalloc(size_t size)
{
d->budgets_lock.lock();

// find free budget
std::list<std::pair<size_t, void*> >::iterator it = d->budgets.begin();
std::list<std::pair<size_t, void*> >::iterator it = d->budgets.begin(), it_max = d->budgets.begin(), it_min = d->budgets.begin();
for (; it != d->budgets.end(); ++it)
{
size_t bs = it->first;
Expand All @@ -126,6 +133,35 @@ void* PoolAllocator::fastMalloc(size_t size)

return ptr;
}

if (bs < it_min->first)
{
it_min = it;
}
if (bs > it_max->first)
{
it_max = it;
}
}

if (d->budgets.size() >= d->size_drop_threshold)
{
// All chunks in pool are not chosen. Then try to drop some outdated
// chunks and return them to OS.
if (it_max->first < size)
{
// Current query is asking for a chunk larger than any cached chunks.
// Then remove the smallest one.
ncnn::fastFree(it_min->second);
d->budgets.erase(it_min);
}
else if (it_min->first > size)
{
// Current query is asking for a chunk smaller than any cached chunks.
// Then remove the largest one.
ncnn::fastFree(it_max->second);
d->budgets.erase(it_max);
}
}

d->budgets_lock.unlock();
Expand Down Expand Up @@ -178,14 +214,16 @@ class UnlockedPoolAllocatorPrivate
{
public:
unsigned int size_compare_ratio; // 0~256
size_t size_drop_threshold;
std::list<std::pair<size_t, void*> > budgets;
std::list<std::pair<size_t, void*> > payouts;
};

UnlockedPoolAllocator::UnlockedPoolAllocator()
: Allocator(), d(new UnlockedPoolAllocatorPrivate)
{
d->size_compare_ratio = 192; // 0.75f * 256
d->size_compare_ratio = 0;
d->size_drop_threshold = 10;
}

UnlockedPoolAllocator::~UnlockedPoolAllocator()
Expand Down Expand Up @@ -240,10 +278,15 @@ void UnlockedPoolAllocator::set_size_compare_ratio(float scr)
d->size_compare_ratio = (unsigned int)(scr * 256);
}

void UnlockedPoolAllocator::set_size_drop_threshold(size_t threshold)
{
d->size_drop_threshold = threshold;
}

void* UnlockedPoolAllocator::fastMalloc(size_t size)
{
// find free budget
std::list<std::pair<size_t, void*> >::iterator it = d->budgets.begin();
std::list<std::pair<size_t, void*> >::iterator it = d->budgets.begin(), it_max = d->budgets.begin(), it_min = d->budgets.begin();
for (; it != d->budgets.end(); ++it)
{
size_t bs = it->first;
Expand All @@ -259,6 +302,29 @@ void* UnlockedPoolAllocator::fastMalloc(size_t size)

return ptr;
}

if (bs > it_max->first)
{
it_max = it;
}
if (bs < it_min->first)
{
it_min = it;
}
}

if (d->budgets.size() >= d->size_drop_threshold)
{
if (it_max->first < size)
{
ncnn::fastFree(it_min->second);
d->budgets.erase(it_min);
}
else if (it_min->first > size)
{
ncnn::fastFree(it_max->second);
d->budgets.erase(it_max);
}
}

// new
Expand Down
12 changes: 10 additions & 2 deletions src/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,13 @@ class NCNN_EXPORT PoolAllocator : public Allocator
~PoolAllocator();

// ratio range 0 ~ 1
// default cr = 0.75
// default cr = 0
void set_size_compare_ratio(float scr);

// budget drop threshold
// default threshold = 10
void set_size_drop_threshold(size_t);

// release all budgets immediately
void clear();

Expand All @@ -195,9 +199,13 @@ class NCNN_EXPORT UnlockedPoolAllocator : public Allocator
~UnlockedPoolAllocator();

// ratio range 0 ~ 1
// default cr = 0.75
// default cr = 0
void set_size_compare_ratio(float scr);

// budget drop threshold
// default threshold = 10
void set_size_drop_threshold(size_t);

// release all budgets immediately
void clear();

Expand Down
2 changes: 1 addition & 1 deletion src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1891,7 +1891,7 @@ int Net::load_model(const DataReader& dr)
if (!d->local_workspace_allocator)
{
d->local_workspace_allocator = new PoolAllocator;
d->local_workspace_allocator->set_size_compare_ratio(0.5f);
d->local_workspace_allocator->set_size_compare_ratio(0.f);
}
}
}
Expand Down