eager_deletion_op_handle.cc 4.3 KB
Newer Older
S
sneaxiy 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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.

S
sneaxiy 已提交
15 16 17 18
#include <memory>
#include <unordered_set>
#include <utility>

S
sneaxiy 已提交
19 20 21 22
#include "paddle/fluid/framework/details/eager_deletion_op_handle.h"
#include "paddle/fluid/framework/lod_tensor_array.h"
#include "paddle/fluid/framework/scope.h"
#include "paddle/fluid/framework/selected_rows.h"
23
#include "paddle/fluid/platform/profiler.h"
S
sneaxiy 已提交
24
#ifdef PADDLE_WITH_CUDA
S
fix bug  
sneaxiy 已提交
25
#include "paddle/fluid/platform/cuda_device_guard.h"
S
sneaxiy 已提交
26
#endif
S
sneaxiy 已提交
27 28 29 30 31 32 33

namespace paddle {
namespace framework {
namespace details {

EagerDeletionOpHandle::EagerDeletionOpHandle(
    ir::Node *node, const Scope *scope, const platform::Place &place,
S
sneaxiy 已提交
34
    const std::unordered_set<std::string> &var_names, GarbageCollector *gc,
35
    ir::AtomicReferenceCountMap *ref_cnts)
S
fix bug  
sneaxiy 已提交
36 37
    : OpHandleBase(node),
      scope_(scope),
Z
Zeng Jinle 已提交
38
      var_names_(var_names.begin(), var_names.end()),
S
fix bug  
sneaxiy 已提交
39 40
      gc_(gc),
      ref_cnts_(ref_cnts) {
S
sneaxiy 已提交
41 42
#ifdef PADDLE_WITH_CUDA
  if (platform::is_gpu_place(place)) {
S
sneaxiy 已提交
43
    dev_ctx_ = reinterpret_cast<platform::CUDADeviceContext *>(
S
sneaxiy 已提交
44
        platform::DeviceContextPool::Instance().Get(place));
S
sneaxiy 已提交
45
    if (dynamic_cast<StreamGarbageCollector *>(gc_)) {
S
fix bug  
sneaxiy 已提交
46 47
      platform::CUDADeviceGuard guard(
          boost::get<platform::CUDAPlace>(place).device);
S
sneaxiy 已提交
48
      PADDLE_ENFORCE(cudaEventCreateWithFlags(&event_, cudaEventDisableTiming));
S
fix bug  
sneaxiy 已提交
49
      PADDLE_ENFORCE_NOT_NULL(event_);
S
sneaxiy 已提交
50 51 52
    }
  }
#endif
S
sneaxiy 已提交
53
  PADDLE_ENFORCE(!var_names_.empty(), "Var names cannot be empty");
S
sneaxiy 已提交
54 55 56 57 58 59
}

EagerDeletionOpHandle::~EagerDeletionOpHandle() {
#ifdef PADDLE_WITH_CUDA
  if (event_) {
    auto gpu_place = boost::get<platform::CUDAPlace>(dev_ctx_->GetPlace());
S
fix bug  
sneaxiy 已提交
60
    platform::CUDADeviceGuard guard(gpu_place.device);
S
sneaxiy 已提交
61 62 63 64 65
    PADDLE_ENFORCE(cudaEventDestroy(event_));
  }
#endif
}

S
sneaxiy 已提交
66
std::string EagerDeletionOpHandle::Name() const { return "eager_deletion"; }
S
sneaxiy 已提交
67 68

void EagerDeletionOpHandle::RunImpl() {
69
  platform::RecordEvent record_event(Name());
S
sneaxiy 已提交
70
  Scope *exec_scope = nullptr;
S
sneaxiy 已提交
71
  std::deque<std::shared_ptr<memory::Allocation>> garbages;
S
sneaxiy 已提交
72 73
  for (auto &name : var_names_) {
    auto it = ref_cnts_->find(name);
S
sneaxiy 已提交
74
    // Reference count has not decreased to 0
S
sneaxiy 已提交
75
    if (it == ref_cnts_->end() || it->second.fetch_sub(1) != 1) {
S
sneaxiy 已提交
76 77 78
      continue;
    }

S
sneaxiy 已提交
79 80 81 82
    if (!exec_scope) {
      exec_scope = scope_->FindVar(kLocalExecScopeName)->Get<Scope *>();
    }

S
sneaxiy 已提交
83
    // Var not found
S
sneaxiy 已提交
84 85 86 87 88
    auto *var = exec_scope->FindVar(name);
    if (var == nullptr) {
      continue;
    }

S
sneaxiy 已提交
89 90
    VLOG(2) << "Erase variable " << name;

S
sneaxiy 已提交
91
    if (var->IsType<LoDTensor>()) {
S
sneaxiy 已提交
92
      garbages.emplace_back(var->GetMutable<LoDTensor>()->MoveMemoryHolder());
S
sneaxiy 已提交
93
    } else if (var->IsType<SelectedRows>()) {
S
sneaxiy 已提交
94
      garbages.emplace_back(
S
sneaxiy 已提交
95
          var->GetMutable<SelectedRows>()->mutable_value()->MoveMemoryHolder());
S
sneaxiy 已提交
96
    } else if (var->IsType<LoDTensorArray>()) {
S
sneaxiy 已提交
97 98
      auto *tensor_arr = var->GetMutable<LoDTensorArray>();
      for (auto &t : *tensor_arr) {
S
sneaxiy 已提交
99
        garbages.emplace_back(t.MoveMemoryHolder());
S
sneaxiy 已提交
100
      }
S
sneaxiy 已提交
101 102
    } else {
      PADDLE_THROW("Type %s of %s is not supported eager deletion",
S
sneaxiy 已提交
103
                   framework::ToTypeName(var->Type()), name);
S
sneaxiy 已提交
104 105 106
    }
  }

S
sneaxiy 已提交
107 108
  if (!garbages.empty()) {
    ClearGarbages(&garbages);
S
sneaxiy 已提交
109 110 111
  }
}

S
sneaxiy 已提交
112 113
void EagerDeletionOpHandle::ClearGarbages(
    std::deque<std::shared_ptr<memory::Allocation>> *garbages) {
S
sneaxiy 已提交
114 115 116 117
#ifdef PADDLE_WITH_CUDA
  if (event_) {
    auto compute_stream = dev_ctx_->stream();
    auto callback_stream =
S
sneaxiy 已提交
118
        reinterpret_cast<StreamGarbageCollector *>(gc_)->stream();
S
sneaxiy 已提交
119 120 121 122
    auto callback_func = [=]() {
      PADDLE_ENFORCE(cudaEventRecord(event_, compute_stream));
      PADDLE_ENFORCE(cudaStreamWaitEvent(callback_stream, event_, 0));
    };
S
sneaxiy 已提交
123
    gc_->Add(std::move(*garbages), callback_func);
S
sneaxiy 已提交
124 125
  } else {
#endif
S
sneaxiy 已提交
126
    gc_->Add(std::move(*garbages));
S
sneaxiy 已提交
127 128 129 130 131 132 133 134
#ifdef PADDLE_WITH_CUDA
  }
#endif
}

}  // namespace details
}  // namespace framework
}  // namespace paddle