garbage_collector.h 8.0 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
#include "paddle/fluid/platform/device_context.h"
F
fwenguang 已提交
25 26 27
#ifdef PADDLE_WITH_MLU
#include "paddle/fluid/platform/device/mlu/device_context.h"
#endif
L
Leo Chen 已提交
28
#include "paddle/fluid/platform/stream_callback_manager.h"
S
sneaxiy 已提交
29 30 31 32 33 34

namespace paddle {
namespace framework {

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

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

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

S
sneaxiy 已提交
41
  virtual void Wait() const {}
S
sneaxiy 已提交
42 43

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

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

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

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

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

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

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

71 72 73 74 75 76 77 78 79 80
#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

J
jianghaicheng 已提交
81 82 83 84 85 86 87 88 89 90
#ifdef PADDLE_WITH_IPU
class IPUGarbageCollector : public GarbageCollector {
 public:
  IPUGarbageCollector(const platform::IPUPlace &place, size_t max_memory_size);

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

91
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
S
sneaxiy 已提交
92
class UnsafeFastGPUGarbageCollector : public GarbageCollector {
S
fix bug  
sneaxiy 已提交
93 94
 public:
  UnsafeFastGPUGarbageCollector(const platform::CUDAPlace &place,
S
sneaxiy 已提交
95
                                size_t max_memory_size);
S
fix bug  
sneaxiy 已提交
96 97

 protected:
S
sneaxiy 已提交
98
  void ClearCallback(const std::function<void()> &callback) override;
S
fix bug  
sneaxiy 已提交
99 100
};

S
sneaxiy 已提交
101
class DefaultStreamGarbageCollector : public GarbageCollector {
S
sneaxiy 已提交
102 103
 public:
  DefaultStreamGarbageCollector(const platform::CUDAPlace &place,
S
sneaxiy 已提交
104
                                size_t max_memory_size);
S
sneaxiy 已提交
105

S
sneaxiy 已提交
106
  void Wait() const override;
S
sneaxiy 已提交
107 108

 protected:
S
sneaxiy 已提交
109
  void ClearCallback(const std::function<void()> &callback) override;
S
sneaxiy 已提交
110 111
};

S
sneaxiy 已提交
112
class StreamGarbageCollector : public GarbageCollector {
S
sneaxiy 已提交
113 114
 public:
  StreamGarbageCollector(const platform::CUDAPlace &place,
S
sneaxiy 已提交
115
                         size_t max_memory_size);
S
sneaxiy 已提交
116

S
sneaxiy 已提交
117
  ~StreamGarbageCollector();
S
sneaxiy 已提交
118

S
sneaxiy 已提交
119
  void Wait() const override;
S
sneaxiy 已提交
120

121
  gpuStream_t stream() const;
S
sneaxiy 已提交
122 123

 protected:
S
sneaxiy 已提交
124
  void ClearCallback(const std::function<void()> &callback) override;
S
sneaxiy 已提交
125 126

 private:
127
  gpuStream_t stream_;
128 129
  std::unique_ptr<platform::StreamCallbackManager<gpuStream_t>>
      callback_manager_;
S
sneaxiy 已提交
130
};
131 132 133 134 135 136 137 138 139

class CUDAPinnedGarbageCollector : public GarbageCollector {
 public:
  CUDAPinnedGarbageCollector(const platform::CUDAPinnedPlace &place,
                             size_t max_memory_size);

 protected:
  void ClearCallback(const std::function<void()> &callback) override;
};
S
sneaxiy 已提交
140 141
#endif

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
#ifdef PADDLE_WITH_ASCEND_CL
class NPUDefaultStreamGarbageCollector : public GarbageCollector {
 public:
  NPUDefaultStreamGarbageCollector(const platform::NPUPlace &place,
                                   size_t max_memory_size);

  void Wait() const override;

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

class NPUUnsafeFastGarbageCollector : public GarbageCollector {
 public:
  NPUUnsafeFastGarbageCollector(const platform::NPUPlace &place,
                                size_t max_memory_size);

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

F
fwenguang 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
#ifdef PADDLE_WITH_MLU
class MLUDefaultStreamGarbageCollector : public GarbageCollector {
 public:
  MLUDefaultStreamGarbageCollector(const platform::MLUPlace &place,
                                   size_t max_memory_size);

  void Wait() const override;

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

class MLUUnsafeFastGarbageCollector : public GarbageCollector {
 public:
  MLUUnsafeFastGarbageCollector(const platform::MLUPlace &place,
                                size_t max_memory_size);

 protected:
  void ClearCallback(const std::function<void()> &callback) override;
};
class MLUStreamGarbageCollector : public GarbageCollector {
 public:
  MLUStreamGarbageCollector(const platform::MLUPlace &place,
                            size_t max_memory_size);

  ~MLUStreamGarbageCollector();

  void Wait() const override;

  mluStream stream() const;

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

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

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
#ifdef PADDLE_WITH_CUSTOM_DEVICE
class CustomDefaultStreamGarbageCollector : public GarbageCollector {
 public:
  CustomDefaultStreamGarbageCollector(const platform::CustomPlace &place,
                                      size_t max_memory_size);

  void Wait() const override;

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

class CustomDeviceUnsafeFastGarbageCollector : public GarbageCollector {
 public:
  CustomDeviceUnsafeFastGarbageCollector(const platform::CustomPlace &place,
                                         size_t max_memory_size);

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

class CustomStreamGarbageCollector : public GarbageCollector {
 public:
  CustomStreamGarbageCollector(const platform::CustomPlace &place,
                               size_t max_memory_size);

  ~CustomStreamGarbageCollector();

  void Wait() const override;

234
  phi::stream::Stream *stream() const;
235 236 237 238 239

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

 private:
240 241
  std::unique_ptr<phi::stream::Stream> stream_;
  std::unique_ptr<phi::CallbackManager> callback_manager_;
242 243 244
};
#endif

S
sneaxiy 已提交
245 246 247 248 249 250 251
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 已提交
252 253 254 255 256 257 258 259 260
  // 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 已提交
261 262
  GarbageQueue *garbage_queue = nullptr;
  {
Z
Zeng Jinle 已提交
263
    std::lock_guard<std::mutex> guard(*mutex_);
S
sneaxiy 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    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 已提交
282 283 284
int64_t GetEagerDeletionThreshold();
bool IsFastEagerDeletionModeEnabled();

S
sneaxiy 已提交
285 286 287
void SetEagerDeletionMode(double threshold, double fraction, bool fast_mode);

double GetEagerDeletionMemoryFraction();
S
sneaxiy 已提交
288

S
sneaxiy 已提交
289 290
}  // namespace framework
}  // namespace paddle