fetch_op_handle.cc 2.6 KB
Newer Older
Y
Yu Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//   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/fetch_op_handle.h"

17 18 19
#include <string>
#include <vector>

Y
Yu Yang 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
namespace paddle {
namespace framework {
namespace details {

FetchOpHandle::FetchOpHandle(FeedFetchList *data, size_t offset,
                             std::vector<Scope *> *local_scopes)
    : data_(data), offset_(offset), local_scopes_(local_scopes) {}

FetchOpHandle::~FetchOpHandle() {
  for (auto *input_var : inputs_) {
    input_var->pending_ops_.erase(this);
  }
}

void FetchOpHandle::Wait(platform::DeviceContext *waited_dev) {
  PADDLE_THROW("Nobody should wait FetchOp. Unexpceted Error");
}

void FetchOpHandle::WaitAndMergeCPUTensors() const {
  std::vector<const LoDTensor *> tensors_ptr;
  tensors_ptr.reserve(tensors_.size());
  for (auto &t : tensors_) {
    tensors_ptr.emplace_back(&t);
  }
  data_->at(offset_).MergeLoDTensor(tensors_ptr, platform::CPUPlace());
}

void FetchOpHandle::RunImpl() {
Y
Yu Yang 已提交
48 49
  auto cpu_ctx =
      platform::DeviceContextPool::Instance().Get(platform::CPUPlace());
Y
Yu Yang 已提交
50 51
  for (auto *input : inputs_) {
    auto *var = static_cast<VarHandle *>(input);
Y
Yu Yang 已提交
52
    var->generated_op_->Wait(cpu_ctx);
Y
Yu Yang 已提交
53 54
  }
  tensors_.resize(inputs_.size());
Y
Yu Yang 已提交
55 56
  auto *var_handle = static_cast<VarHandle *>(inputs_[0]);
  auto &var_name = var_handle->name_;
Y
Yu Yang 已提交
57 58 59 60 61
  platform::CPUPlace cpu;
  auto &scopes = *local_scopes_;

  for (size_t i = 0; i < scopes.size(); ++i) {
    auto &scope = scopes[i];
Y
Yu Yang 已提交
62 63 64 65 66 67
    auto *var =
        scope->FindVar(kLocalExecScopeName)->Get<Scope *>()->FindVar(var_name);
    PADDLE_ENFORCE_NOT_NULL(var, "Cannot find variable %s in execution scope",
                            var_name);
    auto &t = var->Get<framework::LoDTensor>();
    if (platform::is_gpu_place(t.place())) {
Y
Yu Yang 已提交
68
#ifdef PADDLE_WITH_CUDA
F
fengjiayi 已提交
69
      TensorCopy(t, cpu, *dev_ctxes_[t.place()], &tensors_[i], true);
Y
Yu Yang 已提交
70 71 72 73 74 75
#endif
    } else {
      tensors_[i].ShareDataWith(t);
      tensors_[i].set_lod(t.lod());
    }
  }
Y
Yu Yang 已提交
76 77

  this->WaitAndMergeCPUTensors();
Y
Yu Yang 已提交
78 79
}

Y
Yu Yang 已提交
80 81
std::string FetchOpHandle::Name() const { return "Fetch"; }

Y
Yu Yang 已提交
82 83 84
}  // namespace details
}  // namespace framework
}  // namespace paddle