garbage_collector.h 4.5 KB
Newer Older
S
sneaxiy 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// 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 <deque>
#include <functional>
#include <memory>
#include <mutex>  // NOLINT
S
sneaxiy 已提交
21
#include <utility>
W
wanghuancoder 已提交
22

S
sneaxiy 已提交
23
#include "gflags/gflags.h"
S
sneaxiy 已提交
24 25
#include "paddle/fluid/platform/device_context.h"

W
wanghuancoder 已提交
26 27 28 29 30 31
namespace paddle {
namespace platform {
class DeviceContext;
}  // namespace platform
}  // namespace paddle

S
sneaxiy 已提交
32 33 34 35 36
namespace paddle {
namespace framework {

class GarbageCollector {
 public:
S
sneaxiy 已提交
37
  using GarbageQueue = std::deque<std::shared_ptr<memory::Allocation>>;
S
sneaxiy 已提交
38

S
sneaxiy 已提交
39
  GarbageCollector(const platform::Place &place, size_t max_memory_size);
S
sneaxiy 已提交
40

Z
Zeng Jinle 已提交
41
  virtual ~GarbageCollector() PADDLE_MAY_THROW {}
S
fix bug  
sneaxiy 已提交
42

S
sneaxiy 已提交
43
  virtual void Wait() const {}
S
sneaxiy 已提交
44 45

  template <typename Container>
S
sneaxiy 已提交
46
  void Add(Container &&objs);
S
sneaxiy 已提交
47 48

  template <typename Container, typename Callback>
S
sneaxiy 已提交
49
  void Add(Container &&objs, Callback &&callback);
S
sneaxiy 已提交
50

L
Leo Chen 已提交
51 52 53 54
  void DirectClearCallback(const std::function<void()> &callback) {
    ClearCallback(callback);
  }

S
sneaxiy 已提交
55 56 57 58
 protected:
  virtual void ClearCallback(const std::function<void()> &callback) = 0;

  platform::DeviceContext *dev_ctx_;
S
sneaxiy 已提交
59
  std::unique_ptr<GarbageQueue> garbages_;
Z
Zeng Jinle 已提交
60
  mutable std::unique_ptr<std::mutex> mutex_;
S
sneaxiy 已提交
61
  const size_t max_memory_size_;
S
sneaxiy 已提交
62
  size_t cur_memory_size_{0};
S
sneaxiy 已提交
63 64
};

S
sneaxiy 已提交
65
class CPUGarbageCollector : public GarbageCollector {
S
sneaxiy 已提交
66
 public:
S
sneaxiy 已提交
67
  CPUGarbageCollector(const platform::CPUPlace &place, size_t max_memory_size);
S
sneaxiy 已提交
68 69

 protected:
S
sneaxiy 已提交
70
  void ClearCallback(const std::function<void()> &callback) override;
S
sneaxiy 已提交
71 72
};

73 74 75 76 77 78 79 80 81 82
#ifdef PADDLE_WITH_XPU
class XPUGarbageCollector : public GarbageCollector {
 public:
  XPUGarbageCollector(const platform::XPUPlace &place, size_t max_memory_size);

 protected:
  void ClearCallback(const std::function<void()> &callback) override;
};
#endif

S
sneaxiy 已提交
83
#ifdef PADDLE_WITH_CUDA
S
sneaxiy 已提交
84
class UnsafeFastGPUGarbageCollector : public GarbageCollector {
S
fix bug  
sneaxiy 已提交
85 86
 public:
  UnsafeFastGPUGarbageCollector(const platform::CUDAPlace &place,
S
sneaxiy 已提交
87
                                size_t max_memory_size);
S
fix bug  
sneaxiy 已提交
88 89

 protected:
S
sneaxiy 已提交
90
  void ClearCallback(const std::function<void()> &callback) override;
S
fix bug  
sneaxiy 已提交
91 92
};

S
sneaxiy 已提交
93
class DefaultStreamGarbageCollector : public GarbageCollector {
S
sneaxiy 已提交
94 95
 public:
  DefaultStreamGarbageCollector(const platform::CUDAPlace &place,
S
sneaxiy 已提交
96
                                size_t max_memory_size);
S
sneaxiy 已提交
97

S
sneaxiy 已提交
98
  void Wait() const override;
S
sneaxiy 已提交
99 100

 protected:
S
sneaxiy 已提交
101
  void ClearCallback(const std::function<void()> &callback) override;
S
sneaxiy 已提交
102 103
};

S
sneaxiy 已提交
104
class StreamGarbageCollector : public GarbageCollector {
S
sneaxiy 已提交
105 106
 public:
  StreamGarbageCollector(const platform::CUDAPlace &place,
S
sneaxiy 已提交
107
                         size_t max_memory_size);
S
sneaxiy 已提交
108

S
sneaxiy 已提交
109
  ~StreamGarbageCollector();
S
sneaxiy 已提交
110

S
sneaxiy 已提交
111
  void Wait() const override;
S
sneaxiy 已提交
112

S
sneaxiy 已提交
113
  cudaStream_t stream() const;
S
sneaxiy 已提交
114 115

 protected:
S
sneaxiy 已提交
116
  void ClearCallback(const std::function<void()> &callback) override;
S
sneaxiy 已提交
117 118 119 120 121 122 123

 private:
  cudaStream_t stream_;
  std::unique_ptr<platform::StreamCallbackManager> callback_manager_;
};
#endif

S
sneaxiy 已提交
124 125 126 127 128 129 130
template <typename Container>
void GarbageCollector::Add(Container &&objs) {
  Add(std::forward<Container>(objs), []() {});
}

template <typename Container, typename Callback>
void GarbageCollector::Add(Container &&objs, Callback &&callback) {
Z
Zeng Jinle 已提交
131 132 133 134 135 136 137 138 139
  // Special case when FLAGS_eager_delete_tensor_gb=0.0
  // It speeds up GC about 2~3%.
  if (max_memory_size_ <= 1) {
    callback();
    auto *container = new Container(std::move(objs));
    ClearCallback([container] { delete container; });
    return;
  }

S
sneaxiy 已提交
140 141
  GarbageQueue *garbage_queue = nullptr;
  {
Z
Zeng Jinle 已提交
142
    std::lock_guard<std::mutex> guard(*mutex_);
S
sneaxiy 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    for (auto &obj : objs) {
      if (!obj) continue;
      cur_memory_size_ += obj->size();
      garbages_->push_back(std::move(obj));
    }
    if (cur_memory_size_ >= max_memory_size_) {
      cur_memory_size_ = 0;
      garbage_queue = garbages_.release();
      garbages_.reset(new GarbageQueue());
    }
  }

  if (garbage_queue) {
    callback();
    ClearCallback([garbage_queue]() { delete garbage_queue; });
  }
}

S
sneaxiy 已提交
161 162 163
int64_t GetEagerDeletionThreshold();
bool IsFastEagerDeletionModeEnabled();

S
sneaxiy 已提交
164 165 166
void SetEagerDeletionMode(double threshold, double fraction, bool fast_mode);

double GetEagerDeletionMemoryFraction();
S
sneaxiy 已提交
167

S
sneaxiy 已提交
168 169
}  // namespace framework
}  // namespace paddle