gather_op_handle.cc 4.5 KB
Newer Older
C
chengduoZH 已提交
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/gather_op_handle.h"
Y
Yu Yang 已提交
16 17
#include "paddle/fluid/framework/details/container_cast.h"
#include "paddle/fluid/framework/details/variable_visitor.h"
C
chengduoZH 已提交
18 19 20 21 22 23 24 25 26 27

namespace paddle {
namespace framework {
namespace details {

GatherOpHandle::GatherOpHandle(const std::vector<Scope *> &local_scopes,
                               const std::vector<platform::Place> &places)
    : local_scopes_(local_scopes), places_(places) {}

void GatherOpHandle::RunImpl() {
C
chengduoZH 已提交
28
  if (places_.size() == 1) return;
C
chengduoZH 已提交
29
  // the input and output may have dummy var.
Y
Yu Yang 已提交
30
  auto in_var_handles = DynamicCast<VarHandle>(inputs_);
C
chengduoZH 已提交
31

C
chengduoZH 已提交
32
  PADDLE_ENFORCE_EQ(
33 34 35
      in_var_handles.size(), places_.size(),
      "The number of output should equal to the number of places.");

Y
Yu Yang 已提交
36 37 38 39 40 41 42 43
  VarHandle *out_var_handle;
  {
    auto out_var_handles = DynamicCast<VarHandle>(outputs_);
    PADDLE_ENFORCE_EQ(out_var_handles.size(), 1,
                      "The number of output should be one.");
    out_var_handle = out_var_handles.front();
  }

C
chengduoZH 已提交
44 45 46 47 48
  std::vector<const Scope *> var_scopes;
  for (auto *s : local_scopes_) {
    var_scopes.emplace_back(s->FindVar(kLocalExecScopeName)->Get<Scope *>());
  }

Y
Yu Yang 已提交
49
  auto in_0_handle = in_var_handles[0];
50
  auto pre_in_var =
C
chengduoZH 已提交
51 52
      var_scopes.at(in_0_handle->scope_idx_)->FindVar(in_0_handle->name_);
  PADDLE_ENFORCE_NOT_NULL(pre_in_var);
C
chengduoZH 已提交
53

54 55
  PADDLE_ENFORCE(pre_in_var->IsType<framework::SelectedRows>(),
                 "Currently, gather_op only can gather SelectedRows.");
56

C
chengduoZH 已提交
57
  // Wait input done, this Wait is asynchronous operation
Y
Yu Yang 已提交
58
  WaitInputVarGenerated(in_var_handles);
C
chengduoZH 已提交
59

C
chengduoZH 已提交
60
  auto &pre_in_value = pre_in_var->Get<framework::SelectedRows>();
C
chengduoZH 已提交
61
  std::vector<int64_t> out_rows;
62
  std::vector<Tensor> in_tensors;
C
chengduoZH 已提交
63

C
chengduoZH 已提交
64
  // Gather the inputs
Y
Yu Yang 已提交
65 66
  for (auto *in_handle : in_var_handles) {
    auto *in_var =
C
chengduoZH 已提交
67
        var_scopes.at(in_handle->scope_idx_)->FindVar(in_handle->name_);
C
chengduoZH 已提交
68
    PADDLE_ENFORCE_NOT_NULL(in_var);
C
chengduoZH 已提交
69
    VariableVisitor::EnforceShapeAndDTypeEQ(*in_var, *pre_in_var);
C
chengduoZH 已提交
70 71

    auto &in_sr_value = in_var->Get<framework::SelectedRows>();
72

C
chengduoZH 已提交
73
    auto &in_sr_rows = in_sr_value.rows();
74
    out_rows.insert(out_rows.end(), in_sr_rows.begin(), in_sr_rows.end());
C
chengduoZH 已提交
75
    in_tensors.emplace_back(in_sr_value.value());
C
chengduoZH 已提交
76 77
  }

C
chengduoZH 已提交
78 79 80 81 82 83 84 85 86
  // NOTE: The Places of all input tensor must be all on CPU or all on GPU.
  platform::Place t_out_p = out_var_handle->place_;
  if (platform::is_gpu_place(pre_in_value.place())) {
    PADDLE_ENFORCE(platform::is_gpu_place(t_out_p),
                   "Places of input and output must be all on GPU.");
  } else {
    t_out_p = platform::CPUPlace();
  }

C
chengduoZH 已提交
87 88 89 90 91 92
  auto out_var =
      var_scopes.at(out_var_handle->scope_idx_)->FindVar(out_var_handle->name_);
  PADDLE_ENFORCE_NOT_NULL(out_var);
  auto out_value = out_var->GetMutable<framework::SelectedRows>();
  out_value->set_height(pre_in_value.height());
  out_value->set_rows(out_rows);
93
  size_t rows = out_rows.size();
C
chengduoZH 已提交
94
  DDim out_dim = pre_in_value.GetCompleteDims();
95
  out_dim[0] = static_cast<int64_t>(rows);
C
chengduoZH 已提交
96
  out_value->mutable_value()->Resize(out_dim).mutable_data(
C
chengduoZH 已提交
97
      t_out_p, pre_in_value.value().type());
C
chengduoZH 已提交
98
  Tensor *out_tensor = out_value->mutable_value();
99 100

  // copy
C
chengduoZH 已提交
101 102 103
  auto dev_ctx = dev_ctxes_[out_var_handle->place_];
  RunAndRecordEvent(out_var_handle->place_, [in_tensors, out_tensor, &dev_ctx,
                                             t_out_p] {
C
chengduoZH 已提交
104 105 106 107
    int s = 0, e = 0;
    for (size_t j = 0; j < in_tensors.size(); ++j) {
      e += in_tensors[j].dims()[0];
      auto sub_out = out_tensor->Slice(s, e);
C
chengduoZH 已提交
108
      paddle::framework::TensorCopy(in_tensors[j], t_out_p, *dev_ctx, &sub_out);
C
chengduoZH 已提交
109 110 111
      s = e;
    }
  });
C
chengduoZH 已提交
112 113
}

Y
Yu Yang 已提交
114
void GatherOpHandle::WaitInputVarGenerated(
C
chengduoZH 已提交
115 116 117
    const std::vector<VarHandle *> &in_var_handles) {
  for (auto *in : in_var_handles) {
    if (in->generated_op_) {
Y
Yu Yang 已提交
118 119 120
      for (auto pair : dev_ctxes_) {
        in->generated_op_->Wait(pair.second);
      }
C
chengduoZH 已提交
121 122 123 124
    }
  }
}

C
chengduoZH 已提交
125
std::string GatherOpHandle::Name() const { return "gather"; }
C
chengduoZH 已提交
126 127 128
}  // namespace details
}  // namespace framework
}  // namespace paddle