Skip to content

Commit 953214a

Browse files
committed
add more unittest
modify allocator strategy remove changes of legacy buddy_allocator test=develop
1 parent fd23262 commit 953214a

34 files changed

+615
-306
lines changed

paddle/fluid/framework/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ cc_test(cow_ptr_tests SRCS details/cow_ptr_test.cc)
202202

203203
cc_test(tuple_test SRCS tuple_test.cc )
204204

205+
cc_test(inlined_vector_test SRCS inlined_vector_test.cc)
206+
205207
if (NOT WIN32)
206208
cc_test(rw_lock_test SRCS rw_lock_test.cc)
207209
endif (NOT WIN32)

paddle/fluid/framework/inlined_stack.h renamed to paddle/fluid/framework/inlined_vector.h

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@
1414

1515
#pragma once
1616

17-
#include <deque>
17+
#include <vector>
1818
#include "paddle/fluid/platform/enforce.h"
1919

2020
namespace paddle {
2121
namespace framework {
2222

2323
template <typename T, size_t N>
24-
class InlinedStack {
24+
class InlinedVector {
2525
static_assert(N > 0, "N must be larger than 0");
2626

2727
public:
28-
inline void push(const T& item) {
28+
inline void push_back(const T& item) {
2929
if (size_ < N) {
3030
head_[size_] = item;
3131
} else {
@@ -34,21 +34,21 @@ class InlinedStack {
3434
++size_;
3535
}
3636

37-
inline void pop() {
38-
PADDLE_ENFORCE(!empty(), "Try to pop element from empty stack.");
37+
inline void pop_back() {
38+
PADDLE_ENFORCE(!empty(), "Try to pop back element from empty vector.");
3939
if (size_ > N) {
4040
tail_.pop_back();
4141
}
4242
--size_;
4343
}
4444

45-
inline const T& top() const {
46-
PADDLE_ENFORCE(!empty(), "Try to get top element of empty stack.");
45+
inline const T& back() const {
46+
PADDLE_ENFORCE(!empty(), "Try to get back element of empty vector.");
4747
return size_ <= N ? head_[size_ - 1] : tail_.back();
4848
}
4949

50-
inline T& top() {
51-
PADDLE_ENFORCE(!empty(), "Try to get top element of empty stack.");
50+
inline T& back() {
51+
PADDLE_ENFORCE(!empty(), "Try to get back element of empty vector.");
5252
return size_ <= N ? head_[size_ - 1] : tail_.back();
5353
}
5454

@@ -63,10 +63,19 @@ class InlinedStack {
6363
return i < N ? head_[i] : tail_[i - N];
6464
}
6565

66+
operator std::vector<T>() const {
67+
std::vector<T> ret;
68+
ret.reserve(size_);
69+
for (size_t i = 0; i < size_; ++i) {
70+
ret.emplace_back((*this)[i]);
71+
}
72+
return ret;
73+
}
74+
6675
private:
6776
T head_[N];
6877
size_t size_{0};
69-
std::deque<T> tail_;
78+
std::vector<T> tail_;
7079
};
7180

7281
} // namespace framework
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "paddle/fluid/framework/inlined_vector.h"
16+
#include <vector>
17+
#include "gtest/gtest.h"
18+
19+
namespace paddle {
20+
namespace framework {
21+
22+
TEST(inlined_stack, inlined_stack) {
23+
size_t max_num = 10;
24+
25+
InlinedVector<size_t, 5> stack;
26+
27+
for (size_t i = 0; i < max_num; ++i) {
28+
ASSERT_EQ(stack.size(), i);
29+
stack.push_back(i);
30+
ASSERT_EQ(stack.size(), i + 1);
31+
}
32+
33+
std::vector<size_t> vec = stack;
34+
35+
ASSERT_EQ(stack.size(), vec.size());
36+
37+
for (size_t i = 0; i < vec.size(); ++i) {
38+
ASSERT_EQ(stack[i], vec[i]);
39+
}
40+
41+
for (size_t i = 0; i < max_num; ++i) {
42+
ASSERT_EQ(stack[i], i);
43+
}
44+
45+
for (size_t i = 0; i < max_num; ++i) {
46+
ASSERT_EQ(stack.back(), max_num - 1 - i);
47+
stack.pop_back();
48+
ASSERT_EQ(stack.size(), max_num - 1 - i);
49+
}
50+
}
51+
52+
} // namespace framework
53+
} // namespace paddle

paddle/fluid/memory/allocation/CMakeLists.txt

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ cc_library(cpu_allocator SRCS cpu_allocator.cc DEPS allocator)
33
cc_library(best_fit_allocator SRCS best_fit_allocator.cc DEPS allocator)
44
cc_library(locked_allocator SRCS locked_allocator.cc DEPS allocator)
55
cc_library(buffered_allocator SRCS buffered_allocator.cc DEPS allocator)
6-
cc_library(multi_bin_buffered_allocator SRCS multi_bin_buffered_allocator.cc DEPS allocator)
6+
cc_library(multi_bin_buffered_allocator SRCS multi_bin_buffered_allocator.cc DEPS allocator gflags)
77
cc_library(legacy_allocator SRCS legacy_allocator.cc DEPS allocator buddy_allocator profiler)
8+
cc_library(zero_size_allocator SRCS zero_size_allocator.cc DEPS allocator)
89
cc_test(buffered_allocator_test SRCS buffered_allocator_test.cc DEPS best_fit_allocator locked_allocator buffered_allocator cpu_allocator)
910
cc_test(multi_bin_buffered_allocator_test SRCS multi_bin_buffered_allocator_test.cc DEPS best_fit_allocator locked_allocator multi_bin_buffered_allocator cpu_allocator)
1011

11-
cc_library(auto_increment_best_fit_allocator SRCS auto_increment_best_fit_allocator.cc DEPS allocator)
12-
cc_test(auto_increment_best_fit_allocator_test SRCS auto_increment_best_fit_allocator_test.cc DEPS cpu_allocator auto_increment_best_fit_allocator)
12+
cc_library(auto_growth_best_fit_allocator SRCS auto_growth_best_fit_allocator.cc DEPS allocator)
13+
cc_test(auto_growth_best_fit_allocator_test SRCS auto_growth_best_fit_allocator_test.cc DEPS cpu_allocator auto_growth_best_fit_allocator)
14+
15+
if (NOT WIN32)
16+
cc_test(test_multi_bin_buffered_allocator_division_plan SRCS test_multi_bin_buffered_allocator_division_plan.cc DEPS multi_bin_buffered_allocator)
17+
endif()
1318

1419
if (WITH_GPU)
1520
nv_library(cuda_allocator SRCS cuda_allocator.cc DEPS allocator cuda_device_guard)
@@ -42,30 +47,20 @@ else ()
4247
set(AllocatorFacadeDeps)
4348
endif()
4449

50+
list(APPEND AllocatorFacadeDeps cpu_allocator locked_allocator best_fit_allocator aligned_allocator auto_increment_allocator conditional_allocator retry_allocator buffered_allocator multi_bin_buffered_allocator auto_growth_best_fit_allocator legacy_allocator zero_size_allocator)
51+
4552
cc_library(aligned_allocator SRCS aligned_allocator.cc DEPS allocator)
4653
cc_library(auto_increment_allocator SRCS auto_increment_allocator.cc DEPS allocator)
47-
cc_library(zero_size_allocator SRCS zero_size_allocator.cc DEPS allocator)
4854
cc_library(conditional_allocator SRCS conditional_allocator.cc DEPS allocator)
49-
cc_library(allocator_strategy SRCS allocator_strategy.cc DEPS gflags)
50-
cc_library(allocator_facade SRCS allocator_facade.cc DEPS
51-
${AllocatorFacadeDeps}
52-
cpu_allocator
53-
locked_allocator
54-
best_fit_allocator
55-
aligned_allocator
56-
auto_increment_allocator
57-
zero_size_allocator
58-
conditional_allocator
59-
retry_allocator
60-
buffered_allocator
61-
multi_bin_buffered_allocator
62-
auto_increment_best_fit_allocator
63-
allocator_strategy
64-
legacy_allocator
65-
)
55+
cc_library(allocator_strategy SRCS allocator_strategy.cc DEPS gflags ${AllocatorFacadeDeps})
56+
cc_library(allocator_facade SRCS allocator_facade.cc DEPS allocator_strategy)
6657

6758
nv_test(allocation_and_eigen_test SRCS allocation_and_eigen_test.cu DEPS allocator_facade)
6859

6960
cc_test(retry_allocator_test SRCS retry_allocator_test.cc DEPS retry_allocator best_fit_allocator locked_allocator cpu_allocator)
7061

7162
cc_test(allocator_facade_test SRCS allocator_facade_test.cc DEPS allocator_facade)
63+
64+
cc_test(naive_best_fit_allocator_facade_test SRCS naive_best_fit_allocator_facade_test.cc DEPS allocator_facade)
65+
66+
cc_test(auto_growth_best_fit_allocator_facade_test SRCS auto_growth_best_fit_allocator_facade_test.cc DEPS allocator_facade)

paddle/fluid/memory/allocation/aligned_allocator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#pragma once
1616
#include <memory>
17+
#include <utility>
1718
#include "paddle/fluid/memory/allocation/allocator.h"
1819

1920
namespace paddle {

paddle/fluid/memory/allocation/allocator.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,24 @@ bool Allocator::IsAllocThreadSafe() const { return false; }
2727

2828
AllocationPtr Allocator::Allocate(size_t size, Allocator::Attr attr) {
2929
auto ptr = AllocateImpl(size, attr);
30-
ptr->RegisterAllocatorChain(this);
30+
ptr->RegisterDecoratedAllocator(this);
3131
return AllocationPtr(ptr);
3232
}
3333

3434
void Allocator::FreeImpl(Allocation* allocation) {
35-
Allocator* allocator = allocation->TopAllocator();
35+
Allocator* allocator = allocation->TopDecoratedAllocator();
3636
allocator->Free(allocation);
3737
}
3838

3939
void Allocator::Free(Allocation* allocation) {
40-
allocation->PopAllocator();
40+
allocation->PopDecoratedAllocator();
4141
FreeImpl(allocation);
4242
}
4343

4444
const char* BadAlloc::what() const noexcept { return msg_.c_str(); }
4545

4646
void AllocationDeleter::operator()(Allocation* allocation) const {
47-
Allocator* allocator = allocation->TopAllocator();
47+
Allocator* allocator = allocation->TopDecoratedAllocator();
4848
allocator->Free(allocation);
4949
}
5050

paddle/fluid/memory/allocation/allocator.h

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
#pragma once
1616
#include <memory>
1717
#include <string>
18+
#include <utility>
1819
#include <vector>
19-
#include "paddle/fluid/framework/inlined_stack.h"
20+
#include "paddle/fluid/framework/inlined_vector.h"
2021
#include "paddle/fluid/platform/place.h"
2122

2223
namespace paddle {
@@ -78,29 +79,26 @@ class Allocation {
7879

7980
virtual ~Allocation();
8081

81-
// This function should only be used in unittest
82-
std::vector<Allocator*> GetAllocatorChain() const {
83-
std::vector<Allocator*> allocators;
84-
for (size_t i = 0; i < allocator_chain_.size(); ++i) {
85-
allocators.push_back(allocator_chain_[i]);
86-
}
87-
return allocators;
82+
private:
83+
std::vector<Allocator*> DecoratedAllocators() const {
84+
return static_cast<std::vector<Allocator*>>(decorated_allocators_);
8885
}
8986

90-
private:
91-
inline void RegisterAllocatorChain(Allocator* allocator) {
92-
allocator_chain_.push(allocator);
87+
inline void RegisterDecoratedAllocator(Allocator* allocator) {
88+
decorated_allocators_.push_back(allocator);
9389
}
9490

95-
inline void PopAllocator() { allocator_chain_.pop(); }
91+
inline void PopDecoratedAllocator() { decorated_allocators_.pop_back(); }
9692

97-
inline Allocator* TopAllocator() { return allocator_chain_.top(); }
93+
inline Allocator* TopDecoratedAllocator() {
94+
return decorated_allocators_.back();
95+
}
9896

9997
private:
10098
void* ptr_;
10199
size_t size_;
102100
platform::Place place_;
103-
framework::InlinedStack<Allocator*, 8> allocator_chain_;
101+
framework::InlinedVector<Allocator*, 8> decorated_allocators_;
104102

105103
friend class Allocator;
106104
friend class AllocationDeleter;

0 commit comments

Comments
 (0)