eager_deletion_op_handle.cc 3.7 KB
Newer Older
S
sneaxiy 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// 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/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"
S
fix bug  
sneaxiy 已提交
19
#include "paddle/fluid/platform/cuda_device_guard.h"
S
sneaxiy 已提交
20 21 22 23 24 25 26

namespace paddle {
namespace framework {
namespace details {

EagerDeletionOpHandle::EagerDeletionOpHandle(
    ir::Node *node, const Scope *scope, const platform::Place &place,
S
fix bug  
sneaxiy 已提交
27 28 29 30 31 32 33
    const std::unordered_set<std::string> &var_names,
    GarbageCollector<Tensor> *gc, AtomicReferenceCountMap *ref_cnts)
    : OpHandleBase(node),
      scope_(scope),
      var_names_(var_names),
      gc_(gc),
      ref_cnts_(ref_cnts) {
S
sneaxiy 已提交
34 35 36 37 38
#ifdef PADDLE_WITH_CUDA
  if (platform::is_gpu_place(place)) {
    dev_ctx_ = static_cast<platform::CUDADeviceContext *>(
        platform::DeviceContextPool::Instance().Get(place));
    if (dynamic_cast<StreamGarbageCollector<Tensor> *>(gc_)) {
S
fix bug  
sneaxiy 已提交
39 40
      platform::CUDADeviceGuard guard(
          boost::get<platform::CUDAPlace>(place).device);
S
sneaxiy 已提交
41
      PADDLE_ENFORCE(cudaEventCreateWithFlags(&event_, cudaEventDisableTiming));
S
fix bug  
sneaxiy 已提交
42
      PADDLE_ENFORCE_NOT_NULL(event_);
S
sneaxiy 已提交
43 44 45 46 47 48 49 50 51
    }
  }
#endif
}

EagerDeletionOpHandle::~EagerDeletionOpHandle() {
#ifdef PADDLE_WITH_CUDA
  if (event_) {
    auto gpu_place = boost::get<platform::CUDAPlace>(dev_ctx_->GetPlace());
S
fix bug  
sneaxiy 已提交
52
    platform::CUDADeviceGuard guard(gpu_place.device);
S
sneaxiy 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    PADDLE_ENFORCE(cudaEventDestroy(event_));
  }
#endif
}

std::string EagerDeletionOpHandle::Name() const { return "eager_deletion"; }

void EagerDeletionOpHandle::RunImpl() {
  auto *exec_scope = scope_->FindVar(kLocalExecScopeName)->Get<Scope *>();
  std::vector<Tensor *> tensors;
  for (auto &name : var_names_) {
    auto it = ref_cnts_->find(name);
    if (it == ref_cnts_->end()) {
      continue;
    }

    auto *var = exec_scope->FindVar(name);
    if (var == nullptr) {
      continue;
    }

    if (var->IsType<LoDTensor>()) {
      if (it->second.fetch_sub(1) == 1) {
        tensors.emplace_back(var->GetMutable<LoDTensor>());
      }
    } else if (var->IsType<SelectedRows>()) {
      if (it->second.fetch_sub(1) == 1) {
        tensors.emplace_back(var->GetMutable<SelectedRows>()->mutable_value());
      }
    } else if (var->IsType<LoDTensorArray>()) {
      if (it->second.fetch_sub(1) == 1) {
        auto *tensor_arr = var->GetMutable<LoDTensorArray>();
        for (auto &t : *tensor_arr) {
          tensors.emplace_back(&t);
        }
      }
    }
  }

  if (!tensors.empty()) {
    ClearTensors(tensors);
  }
}

void EagerDeletionOpHandle::ClearTensors(const std::vector<Tensor *> &tensors) {
#ifdef PADDLE_WITH_CUDA
  if (event_) {
    auto compute_stream = dev_ctx_->stream();
    auto callback_stream =
        static_cast<StreamGarbageCollector<Tensor> *>(gc_)->stream();
    auto callback_func = [=]() {
      PADDLE_ENFORCE(cudaEventRecord(event_, compute_stream));
      PADDLE_ENFORCE(cudaStreamWaitEvent(callback_stream, event_, 0));
    };
    gc_->Add(tensors, callback_func);
  } else {
#endif
    gc_->Add(tensors);
#ifdef PADDLE_WITH_CUDA
  }
#endif
}

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