[Feature] Add Temporary Allocator (#14875)
* Add Temporal Allocator * add Temporay Allocator to DeviceContext test=develop * code refine test=develop * fix mean_iou test=develop * Add DeviceTemporaryAllocator test=develop * fix conv_op bug test=develop * small fix test=develop * code refine test=develop * log refine test=develop * fix unit test test=develop * move double check * refine concat_and_split test=develop * add limit_of_temporary_allocation test=develop * fix name test=developrevert-15207-remove_op_handle_lock_and_fix_var
parent
484c24b756
commit
79bd6dfa18
@ -0,0 +1,42 @@
|
||||
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#pragma once
|
||||
#include "paddle/fluid/framework/tensor.h"
|
||||
#include "paddle/fluid/platform/temporary_allocator.h"
|
||||
namespace paddle {
|
||||
namespace platform {
|
||||
|
||||
template <typename T>
|
||||
paddle::framework::Tensor GetTensor(
|
||||
memory::allocation::AllocationPtr temp_allocation_ptr,
|
||||
const framework::DDim &dim) {
|
||||
auto &deleter = temp_allocation_ptr.get_deleter();
|
||||
auto *allocation_ptr = temp_allocation_ptr.release();
|
||||
auto shared_allocation =
|
||||
std::shared_ptr<memory::allocation::Allocation>(allocation_ptr, deleter);
|
||||
|
||||
PADDLE_ENFORCE(dynamic_cast<TemporaryAllocation *>(allocation_ptr) != nullptr,
|
||||
"The AllocationPtr must be TemporaryAllocation.");
|
||||
PADDLE_ENFORCE_EQ(allocation_ptr->size(),
|
||||
framework::product(dim) * sizeof(T));
|
||||
|
||||
paddle::framework::Tensor temp_tensor(std::type_index(typeid(T)));
|
||||
temp_tensor.Resize(dim);
|
||||
temp_tensor.ResetHolder(std::move(shared_allocation));
|
||||
return temp_tensor;
|
||||
}
|
||||
|
||||
} // namespace platform
|
||||
} // namespace paddle
|
@ -0,0 +1,95 @@
|
||||
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "paddle/fluid/platform/temporary_allocator.h"
|
||||
#include "paddle/fluid/memory/allocation/allocator_facade.h"
|
||||
|
||||
DEFINE_double(limit_of_temporary_allocation, -1,
|
||||
"The up limit of temporary_allocation size.");
|
||||
|
||||
namespace paddle {
|
||||
namespace platform {
|
||||
namespace alloc = memory::allocation;
|
||||
|
||||
TemporaryAllocation::TemporaryAllocation(
|
||||
alloc::AllocationPtr &&underlying_allocation)
|
||||
: Allocation(underlying_allocation->ptr(), underlying_allocation->size(),
|
||||
underlying_allocation->place()),
|
||||
underlying_allocation_(std::move(underlying_allocation)) {}
|
||||
|
||||
TemporaryAllocator::TemporaryAllocator(platform::Place place) : place_(place) {
|
||||
temp_mem_queue_.reset(new std::deque<TemporaryAllocation *>());
|
||||
}
|
||||
|
||||
bool TemporaryAllocator::IsAllocThreadSafe() const { return true; }
|
||||
|
||||
void TemporaryAllocator::Release(const std::function<void()> &callback) {
|
||||
std::shared_ptr<std::deque<TemporaryAllocation *>> t_allocations;
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mtx_);
|
||||
callback();
|
||||
t_allocations = temp_mem_queue_;
|
||||
temp_mem_queue_.reset(new std::deque<TemporaryAllocation *>());
|
||||
wait_delete_mem_ = 0;
|
||||
}
|
||||
for (auto tmp : *t_allocations) {
|
||||
VLOG(10) << "Delete temporary allocation " << tmp->ptr()
|
||||
<< " size: " << tmp->size();
|
||||
delete tmp;
|
||||
}
|
||||
}
|
||||
|
||||
void TemporaryAllocator::Free(alloc::Allocation *allocation) {
|
||||
auto *temp_allocation = dynamic_cast<TemporaryAllocation *>(allocation);
|
||||
PADDLE_ENFORCE_NOT_NULL(temp_allocation);
|
||||
if (platform::is_gpu_place(temp_allocation->place())) {
|
||||
size_t wait_delete_mem = 0;
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mtx_);
|
||||
temp_mem_queue_->emplace_back(temp_allocation);
|
||||
wait_delete_mem_ += temp_allocation->size();
|
||||
wait_delete_mem = wait_delete_mem_;
|
||||
VLOG(10) << "Move temporary allocation: " << temp_allocation->ptr()
|
||||
<< " to delete queue: " << temp_allocation->size() << "; "
|
||||
<< "wait_delete_mem: " << wait_delete_mem_;
|
||||
}
|
||||
if (FLAGS_limit_of_temporary_allocation > 0 &&
|
||||
wait_delete_mem > FLAGS_limit_of_temporary_allocation) {
|
||||
Release(callback_);
|
||||
}
|
||||
return;
|
||||
}
|
||||
delete temp_allocation;
|
||||
}
|
||||
|
||||
size_t TemporaryAllocator::TemporaryAllocationQueueSize() {
|
||||
std::unique_lock<std::mutex> lock(mtx_);
|
||||
return temp_mem_queue_ ? temp_mem_queue_->size() : 0;
|
||||
}
|
||||
|
||||
void TemporaryAllocator::SetCallback(const std::function<void()> &callback) {
|
||||
callback_ = callback;
|
||||
}
|
||||
|
||||
alloc::Allocation *TemporaryAllocator::AllocateImpl(
|
||||
size_t size, alloc::Allocator::Attr attr) {
|
||||
auto raw_allocation =
|
||||
alloc::AllocatorFacade::Instance().Alloc(place_, size, attr);
|
||||
auto temp_mem = new TemporaryAllocation(std::move(raw_allocation));
|
||||
VLOG(10) << "Alloc temporary allocation: " << temp_mem->ptr() << ": " << size;
|
||||
return temp_mem;
|
||||
}
|
||||
|
||||
} // namespace platform
|
||||
} // namespace paddle
|
@ -0,0 +1,63 @@
|
||||
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#pragma once
|
||||
#include <condition_variable> // NOLINT
|
||||
#include <deque>
|
||||
#include <mutex> // NOLINT
|
||||
#include "paddle/fluid/memory/allocation/allocator.h"
|
||||
#include "paddle/fluid/platform/lock_guard_ptr.h"
|
||||
namespace paddle {
|
||||
namespace platform {
|
||||
|
||||
class TemporaryAllocation : public memory::allocation::Allocation {
|
||||
public:
|
||||
explicit TemporaryAllocation(
|
||||
memory::allocation::AllocationPtr &&underlying_allocation);
|
||||
|
||||
memory::allocation::AllocationPtr underlying_allocation_;
|
||||
};
|
||||
|
||||
class TemporaryAllocator : public memory::allocation::Allocator {
|
||||
public:
|
||||
explicit TemporaryAllocator(platform::Place place);
|
||||
|
||||
void Release(const std::function<void()> &callback);
|
||||
|
||||
size_t TemporaryAllocationQueueSize();
|
||||
|
||||
bool IsAllocThreadSafe() const override;
|
||||
|
||||
void SetCallback(const std::function<void()> &callback);
|
||||
|
||||
protected:
|
||||
void Free(memory::allocation::Allocation *allocation) override;
|
||||
|
||||
memory::allocation::Allocation *AllocateImpl(
|
||||
size_t size, memory::allocation::Allocator::Attr attr) override;
|
||||
|
||||
private:
|
||||
platform::Place place_;
|
||||
|
||||
// When the allocation is not held by any variable, it should be placed
|
||||
// to temp_mem_queue immediately.
|
||||
std::shared_ptr<std::deque<TemporaryAllocation *>> temp_mem_queue_{nullptr};
|
||||
|
||||
std::mutex mtx_;
|
||||
size_t wait_delete_mem_{0};
|
||||
std::function<void()> callback_;
|
||||
};
|
||||
|
||||
} // namespace platform
|
||||
} // namespace paddle
|
@ -0,0 +1,165 @@
|
||||
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "paddle/fluid/platform/temporary_allocator.h"
|
||||
#include <gtest/gtest.h>
|
||||
#include "paddle/fluid/framework/tensor.h"
|
||||
#include "paddle/fluid/platform/create_tensor_with_allocationptr.h"
|
||||
DECLARE_double(limit_of_temporary_allocation);
|
||||
|
||||
namespace paddle {
|
||||
namespace platform {
|
||||
|
||||
TEST(temporary_allocator, temporary_allocator) {
|
||||
platform::CPUPlace cpu_place;
|
||||
TemporaryAllocator alloc(cpu_place);
|
||||
alloc.Allocate(100);
|
||||
|
||||
#ifdef PADDLE_WITH_CUDA
|
||||
platform::CUDAPlace gpu_place(0);
|
||||
TemporaryAllocator gpu_alloc(gpu_place);
|
||||
|
||||
auto allocation = gpu_alloc.Allocate(101);
|
||||
PADDLE_ENFORCE_EQ(gpu_alloc.TemporaryAllocationQueueSize(), 0);
|
||||
gpu_alloc.Release([]() {});
|
||||
PADDLE_ENFORCE_EQ(gpu_alloc.TemporaryAllocationQueueSize(), 0);
|
||||
|
||||
{
|
||||
auto allocation = gpu_alloc.Allocate(102);
|
||||
PADDLE_ENFORCE_EQ(gpu_alloc.TemporaryAllocationQueueSize(), 0);
|
||||
}
|
||||
PADDLE_ENFORCE_EQ(gpu_alloc.TemporaryAllocationQueueSize(), 1);
|
||||
gpu_alloc.Release([]() {});
|
||||
PADDLE_ENFORCE_EQ(gpu_alloc.TemporaryAllocationQueueSize(), 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(temporary_allocator, add_callback) {
|
||||
#ifdef PADDLE_WITH_CUDA
|
||||
FLAGS_limit_of_temporary_allocation = 10;
|
||||
platform::CUDAPlace gpu_place(0);
|
||||
TemporaryAllocator gpu_alloc(gpu_place);
|
||||
|
||||
platform::DeviceContextPool& pool = platform::DeviceContextPool::Instance();
|
||||
auto* dev_ctx =
|
||||
static_cast<platform::CUDADeviceContext*>(pool.Get(gpu_place));
|
||||
auto stream = dev_ctx->stream();
|
||||
bool deleted = false;
|
||||
gpu_alloc.SetCallback([stream, &deleted]() {
|
||||
PADDLE_ENFORCE(cudaStreamSynchronize(stream));
|
||||
PADDLE_ENFORCE(cudaGetLastError());
|
||||
deleted = true;
|
||||
});
|
||||
{ gpu_alloc.Allocate(100); }
|
||||
PADDLE_ENFORCE(deleted);
|
||||
FLAGS_limit_of_temporary_allocation = -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(temporary_allocator, create_tensor_with_allocationptr) {
|
||||
platform::CPUPlace cpu_place;
|
||||
TemporaryAllocator cpu_alloc(cpu_place);
|
||||
{
|
||||
size_t memory_size = 200;
|
||||
auto allocation = cpu_alloc.Allocate(memory_size);
|
||||
void* address = allocation->ptr();
|
||||
int numel = memory_size / sizeof(float);
|
||||
framework::Tensor tensor =
|
||||
GetTensor<float>(std::move(allocation), framework::make_ddim({numel}));
|
||||
PADDLE_ENFORCE_EQ(address, tensor.data<float>());
|
||||
PADDLE_ENFORCE_EQ(tensor.numel(), numel);
|
||||
}
|
||||
|
||||
#ifdef PADDLE_WITH_CUDA
|
||||
platform::CUDAPlace gpu_place(0);
|
||||
TemporaryAllocator gpu_alloc(gpu_place);
|
||||
|
||||
{
|
||||
size_t memory_size = 300;
|
||||
auto allocation = gpu_alloc.Allocate(memory_size);
|
||||
void* address = allocation->ptr();
|
||||
int numel = memory_size / sizeof(float);
|
||||
framework::Tensor tensor =
|
||||
GetTensor<float>(std::move(allocation), framework::make_ddim({numel}));
|
||||
PADDLE_ENFORCE_EQ(address, tensor.data<float>());
|
||||
PADDLE_ENFORCE_EQ(tensor.numel(), numel);
|
||||
}
|
||||
|
||||
// The allocation is not holded now, it should be placed to
|
||||
// TemporaryAllocationQueue.
|
||||
PADDLE_ENFORCE_EQ(gpu_alloc.TemporaryAllocationQueueSize(), 1);
|
||||
gpu_alloc.Release([]() {});
|
||||
PADDLE_ENFORCE_EQ(gpu_alloc.TemporaryAllocationQueueSize(), 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(temporary_allocator, create_tensor_with_allocationptr2) {
|
||||
platform::CPUPlace cpu_place;
|
||||
TemporaryAllocator cpu_alloc(cpu_place);
|
||||
{
|
||||
size_t memory_size = 400;
|
||||
int numel = memory_size / sizeof(float);
|
||||
|
||||
framework::Tensor out_side_tensor;
|
||||
void* address;
|
||||
{
|
||||
auto allocation = cpu_alloc.Allocate(memory_size);
|
||||
address = allocation->ptr();
|
||||
framework::Tensor tensor = GetTensor<float>(
|
||||
std::move(allocation), framework::make_ddim({numel}));
|
||||
PADDLE_ENFORCE_EQ(address, tensor.data<float>());
|
||||
PADDLE_ENFORCE_EQ(tensor.numel(), numel);
|
||||
|
||||
out_side_tensor.ShareDataWith(tensor);
|
||||
}
|
||||
PADDLE_ENFORCE_EQ(address, out_side_tensor.data<float>());
|
||||
PADDLE_ENFORCE_EQ(out_side_tensor.numel(), numel);
|
||||
}
|
||||
|
||||
#ifdef PADDLE_WITH_CUDA
|
||||
platform::CUDAPlace gpu_place(0);
|
||||
TemporaryAllocator gpu_alloc(gpu_place);
|
||||
{
|
||||
void* address;
|
||||
size_t memory_size = 500;
|
||||
int numel = memory_size / sizeof(float);
|
||||
framework::Tensor out_side_tensor;
|
||||
{
|
||||
auto allocation = gpu_alloc.Allocate(memory_size);
|
||||
address = allocation->ptr();
|
||||
framework::Tensor tensor = GetTensor<float>(
|
||||
std::move(allocation), framework::make_ddim({numel}));
|
||||
PADDLE_ENFORCE_EQ(address, tensor.data<float>());
|
||||
PADDLE_ENFORCE_EQ(tensor.numel(), numel);
|
||||
|
||||
out_side_tensor.ShareDataWith(tensor);
|
||||
}
|
||||
PADDLE_ENFORCE_EQ(address, out_side_tensor.data<float>());
|
||||
PADDLE_ENFORCE_EQ(out_side_tensor.numel(), numel);
|
||||
// The allocation is holded by out_side_tensor.
|
||||
PADDLE_ENFORCE_EQ(gpu_alloc.TemporaryAllocationQueueSize(), 0);
|
||||
gpu_alloc.Release([]() {});
|
||||
PADDLE_ENFORCE_EQ(gpu_alloc.TemporaryAllocationQueueSize(), 0);
|
||||
}
|
||||
|
||||
// The allocation is not holded now, it should be placed to
|
||||
// TemporaryAllocationQueue.
|
||||
PADDLE_ENFORCE_EQ(gpu_alloc.TemporaryAllocationQueueSize(), 1);
|
||||
gpu_alloc.Release([]() {});
|
||||
PADDLE_ENFORCE_EQ(gpu_alloc.TemporaryAllocationQueueSize(), 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace platform
|
||||
} // namespace paddle
|
Loading…
Reference in new issue