fetch_op_handle.cc 4.7 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
#include <string>
Z
Zhen Wang 已提交
17
#include <utility>
18
#include <vector>
19
#include "paddle/fluid/platform/profiler.h"
20

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

Z
Zhen Wang 已提交
25 26 27 28
FetchOpHandle::FetchOpHandle(ir::Node *node, FetchResultType *data,
                             size_t offset, std::vector<Scope *> *local_scopes,
                             std::vector<Scope *> *local_exec_scopes,
                             bool return_merged)
X
Xin Pan 已提交
29 30 31
    : OpHandleBase(node),
      data_(data),
      offset_(offset),
32
      local_scopes_(local_scopes),
Z
Zhen Wang 已提交
33 34
      local_exec_scopes_(local_exec_scopes),
      return_merged_(return_merged) {}
Y
Yu Yang 已提交
35

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

C
chengduoZH 已提交
38
void FetchOpHandle::RecordWaitEventOnCtx(platform::DeviceContext *waited_ctx) {
Y
Yu Yang 已提交
39 40 41 42
  PADDLE_THROW("Nobody should wait FetchOp. Unexpceted Error");
}

void FetchOpHandle::WaitAndMergeCPUTensors() const {
Z
Zhen Wang 已提交
43 44 45 46 47 48 49 50 51 52 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
  if (return_merged_) {
    const auto &tensor_dims = tensors_[0].dims();
    for (size_t i = 1; i < tensors_.size(); i++) {
      const auto &ele_dims = tensors_[i].dims();
      PADDLE_ENFORCE_EQ(
          tensor_dims.size(), ele_dims.size(),
          platform::errors::Fatal("The dimension sizes of fetched Tensors are "
                                  "different from each other on different "
                                  "devices. And the error is caused by the %zu "
                                  "(th) fetched variable. Please set the "
                                  "parameter `return_merged = False` when you "
                                  "call the `Executor.run()` method.",
                                  offset_));
      for (int j = 1; j < tensor_dims.size(); j++) {
        PADDLE_ENFORCE_EQ(
            tensor_dims[j], ele_dims[j],
            platform::errors::Fatal("The dimensions of fetched Tensors are "
                                    "different from each other on different "
                                    "devices. And the error is caused by the "
                                    "%zu (th) fetched variable. Please set the "
                                    "parameter `return_merged = False` when "
                                    "you call the `Executor.run()` method.",
                                    offset_));
      }
    }
    std::vector<const LoDTensor *> tensors_ptr;
    tensors_ptr.reserve(tensors_.size());
    for (auto &t : tensors_) {
      tensors_ptr.emplace_back(&t);
    }
    auto &val = boost::get<FeedFetchList>(*data_);
    val.at(offset_).MergeLoDTensor(tensors_ptr, platform::CPUPlace());
  } else {
    auto &val = boost::get<FetchUnmergedList>(*data_);
    val.at(offset_) = std::move(tensors_);
Y
Yu Yang 已提交
78 79 80 81
  }
}

void FetchOpHandle::RunImpl() {
82
  platform::RecordEvent record_event(Name());
C
chengduoZH 已提交
83 84
  WaitInputVarGenerated(platform::CPUPlace());

Y
Yu Yang 已提交
85 86
  tensors_.resize(inputs_.size());
  platform::CPUPlace cpu;
87
  auto &scopes = *local_exec_scopes_;
Y
Yu Yang 已提交
88

C
chengduoZH 已提交
89 90
  for (size_t i = 0; i < inputs_.size(); ++i) {
    auto *var_handle = static_cast<VarHandle *>(inputs_[i]);
G
gongweibao 已提交
91
    auto &scope = scopes.at(var_handle->scope_idx());
92
    auto *var = scope->FindVar(var_handle->name());
Y
Yu Yang 已提交
93
    PADDLE_ENFORCE_NOT_NULL(var, "Cannot find variable %s in execution scope",
G
gongweibao 已提交
94
                            var_handle->name());
C
chengduoZH 已提交
95

Y
Yu Yang 已提交
96
    auto &t = var->Get<framework::LoDTensor>();
97 98
    if (t.IsInitialized() && t.numel() > 0) {
      if (platform::is_gpu_place(t.place())) {
Y
Yu Yang 已提交
99
#ifdef PADDLE_WITH_CUDA
100
        TensorCopy(t, cpu, &tensors_[i]);
Y
Yu Yang 已提交
101
#endif
102 103 104
      } else {
        tensors_[i].ShareDataWith(t);
      }
Y
Yu Yang 已提交
105
    } else {
106 107
      tensors_[i].clear();
      tensors_[i].Resize({0});
Y
Yu Yang 已提交
108
    }
F
fengjiayi 已提交
109
    tensors_[i].set_lod(t.lod());
Y
Yu Yang 已提交
110
  }
Y
Yu Yang 已提交
111 112

  this->WaitAndMergeCPUTensors();
Y
Yu Yang 已提交
113 114
}

C
chengduoZH 已提交
115 116 117
void FetchOpHandle::WaitInputVarGenerated(const platform::Place &place) {
  auto cpu_ctx = platform::DeviceContextPool::Instance().Get(place);
  for (auto *input : inputs_) {
X
Xin Pan 已提交
118 119
    if (input->GeneratedOp()) {
      input->GeneratedOp()->RecordWaitEventOnCtx(cpu_ctx);
C
chengduoZH 已提交
120 121 122 123
    }
  }
}

124 125
bool FetchOpHandle::IsMultiDeviceTransfer() { return true; }

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

Y
Yu Yang 已提交
128 129 130
}  // namespace details
}  // namespace framework
}  // namespace paddle