fetch_op_handle.cc 3.0 KB
Newer Older
Y
Yu Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//   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"
16 17
#include <string>
#include <vector>
18
#include "paddle/fluid/platform/profiler.h"
19

Y
Yu Yang 已提交
20 21 22 23
namespace paddle {
namespace framework {
namespace details {

X
Xin Pan 已提交
24
FetchOpHandle::FetchOpHandle(ir::Node *node, FeedFetchList *data, size_t offset,
Y
Yu Yang 已提交
25
                             std::vector<Scope *> *local_scopes)
X
Xin Pan 已提交
26 27 28 29
    : OpHandleBase(node),
      data_(data),
      offset_(offset),
      local_scopes_(local_scopes) {}
Y
Yu Yang 已提交
30

X
Xin Pan 已提交
31
FetchOpHandle::~FetchOpHandle() {}
Y
Yu Yang 已提交
32

C
chengduoZH 已提交
33
void FetchOpHandle::RecordWaitEventOnCtx(platform::DeviceContext *waited_ctx) {
Y
Yu Yang 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46
  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() {
47
  platform::RecordEvent record_event(Name());
C
chengduoZH 已提交
48 49
  WaitInputVarGenerated(platform::CPUPlace());

Y
Yu Yang 已提交
50 51 52 53
  tensors_.resize(inputs_.size());
  platform::CPUPlace cpu;
  auto &scopes = *local_scopes_;

C
chengduoZH 已提交
54 55
  for (size_t i = 0; i < inputs_.size(); ++i) {
    auto *var_handle = static_cast<VarHandle *>(inputs_[i]);
G
gongweibao 已提交
56
    auto &scope = scopes.at(var_handle->scope_idx());
C
chengduoZH 已提交
57 58
    auto *var = scope->FindVar(kLocalExecScopeName)
                    ->Get<Scope *>()
G
gongweibao 已提交
59
                    ->FindVar(var_handle->name());
Y
Yu Yang 已提交
60
    PADDLE_ENFORCE_NOT_NULL(var, "Cannot find variable %s in execution scope",
G
gongweibao 已提交
61
                            var_handle->name());
C
chengduoZH 已提交
62

Y
Yu Yang 已提交
63 64
    auto &t = var->Get<framework::LoDTensor>();
    if (platform::is_gpu_place(t.place())) {
Y
Yu Yang 已提交
65
#ifdef PADDLE_WITH_CUDA
66 67
      TensorCopy(t, cpu, *dev_ctxes_.at(t.place()), &tensors_[i]);
      dev_ctxes_.at(t.place())->Wait();
Y
Yu Yang 已提交
68 69 70 71
#endif
    } else {
      tensors_[i].ShareDataWith(t);
    }
F
fengjiayi 已提交
72
    tensors_[i].set_lod(t.lod());
Y
Yu Yang 已提交
73
  }
Y
Yu Yang 已提交
74 75

  this->WaitAndMergeCPUTensors();
Y
Yu Yang 已提交
76 77
}

C
chengduoZH 已提交
78 79 80
void FetchOpHandle::WaitInputVarGenerated(const platform::Place &place) {
  auto cpu_ctx = platform::DeviceContextPool::Instance().Get(place);
  for (auto *input : inputs_) {
X
Xin Pan 已提交
81 82
    if (input->GeneratedOp()) {
      input->GeneratedOp()->RecordWaitEventOnCtx(cpu_ctx);
C
chengduoZH 已提交
83 84 85 86
    }
  }
}

87 88
bool FetchOpHandle::IsMultiDeviceTransfer() { return true; }

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

Y
Yu Yang 已提交
91 92 93
}  // namespace details
}  // namespace framework
}  // namespace paddle