broadcast_op_handle.cc 4.3 KB
Newer Older
C
chengduoZH 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//   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.

C
chengduoZH 已提交
15
#include "paddle/fluid/framework/details/broadcast_op_handle.h"
C
chengduoZH 已提交
16 17 18 19 20

namespace paddle {
namespace framework {
namespace details {

C
chengduoZH 已提交
21
BroadcastOpHandle::BroadcastOpHandle(const std::vector<Scope *> &local_scopes,
C
chengduoZH 已提交
22 23
                                     const std::vector<platform::Place> &places)
    : local_scopes_(local_scopes), places_(places) {}
C
chengduoZH 已提交
24

C
chengduoZH 已提交
25
void BroadcastOpHandle::RunImpl() {
C
chengduoZH 已提交
26 27 28 29 30
  PADDLE_ENFORCE_EQ(this->inputs_.size(), 1,
                    "The number of input should be one.");
  PADDLE_ENFORCE_EQ(
      this->outputs_.size(), places_.size(),
      "The number of output should equal to the number of places.");
C
chengduoZH 已提交
31 32 33 34

  // Wait input done, this Wait is asynchronous operation
  auto in_var_handle = static_cast<VarHandle *>(this->inputs_[0]);
  auto &in_place = in_var_handle->place_;
C
chengduoZH 已提交
35
  if (inputs_[0]->generated_op_) {
C
chengduoZH 已提交
36
    inputs_[0]->generated_op_->Wait(dev_ctxes_[in_place]);
C
chengduoZH 已提交
37 38 39 40 41 42
    for (auto *out : outputs_) {
      auto out_handle = static_cast<VarHandle *>(out);
      auto &out_p = out_handle->place_;
      inputs_[0]->generated_op_->Wait(dev_ctxes_[out_p]);
    }
  }
C
chengduoZH 已提交
43

C
chengduoZH 已提交
44
  auto in_scope_idx = in_var_handle->scope_idx_;
C
chengduoZH 已提交
45 46 47
  PADDLE_ENFORCE_LT(in_scope_idx, local_scopes_.size(),
                    "The input(%s) is not in the local_scopes.",
                    in_var_handle->name_);
C
chengduoZH 已提交
48
  auto in_var = local_scopes_[in_scope_idx]->FindVar(in_var_handle->name_);
C
chengduoZH 已提交
49 50 51 52 53 54

  Tensor *in_tensor = GetTensorFromVar(in_var);
  for (auto *out : outputs_) {
    auto out_handle = static_cast<VarHandle *>(out);
    auto &out_p = out_handle->place_;

C
chengduoZH 已提交
55
    auto out_scope_idx = out_handle->scope_idx_;
C
chengduoZH 已提交
56
    PADDLE_ENFORCE_LT(out_scope_idx, local_scopes_.size(),
57
                      "%s is not in the local_scopes ", out_handle->name_);
C
chengduoZH 已提交
58
    auto *s = local_scopes_[out_scope_idx];
C
chengduoZH 已提交
59
    auto out_var = s->FindVar(out_handle->name_);
C
chengduoZH 已提交
60 61
    PADDLE_ENFORCE_EQ(out_p.which(), in_place.which(),
                      "The place of input and output should be the same.");
C
chengduoZH 已提交
62 63

    if (in_var->IsType<framework::SelectedRows>()) {
C
chengduoZH 已提交
64 65 66 67 68 69 70
      auto &in_sr = in_var->Get<framework::SelectedRows>();
      auto out_sr = out_var->GetMutable<framework::SelectedRows>();
      if (&in_sr == out_sr) continue;
      out_sr->set_height(in_sr.height());
      out_sr->set_rows(in_sr.rows());
      out_sr->mutable_value()->Resize(in_sr.value().dims());
      out_sr->mutable_value()->mutable_data(out_p, in_sr.value().type());
C
chengduoZH 已提交
71
    } else if (in_var->IsType<framework::LoDTensor>()) {
C
chengduoZH 已提交
72 73 74 75 76 77
      auto in_lod = in_var->Get<framework::LoDTensor>();
      auto out_lod = out_var->GetMutable<framework::LoDTensor>();
      if (&in_lod == out_lod) continue;
      out_lod->set_lod(in_lod.lod());
      out_lod->Resize(in_lod.dims());
      out_lod->mutable_data(out_p, in_lod.type());
C
chengduoZH 已提交
78
    } else {
C
chengduoZH 已提交
79
      PADDLE_THROW("Var should be LoDTensor or SelectedRows.");
C
chengduoZH 已提交
80 81 82
    }

    Tensor *out_tensor = GetTensorFromVar(out_var);
C
chengduoZH 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    if (platform::is_cpu_place(in_place)) {
      paddle::framework::TensorCopy(*in_tensor, out_p, *(dev_ctxes_[in_place]),
                                    out_tensor);
    } else if (platform::is_gpu_place(in_place)) {
#ifdef PADDLE_WITH_CUDA
      auto src_gpu_place = boost::get<platform::CUDAPlace>(in_place);
      auto dst_gpu_place = boost::get<platform::CUDAPlace>(out_p);
      void *dst_ptr = out_tensor->mutable_data(out_p);
      void *src_ptr = in_tensor->data<void>();
      int64_t size = in_tensor->numel();
      memory::Copy(
          dst_gpu_place, dst_ptr, src_gpu_place, src_ptr, size,
          reinterpret_cast<platform::CUDADeviceContext *>(dev_ctxes_[out_p])
              ->stream());
#else
      PADDLE_THROW("CUDAPlace is not supported in CPU device.");
#endif
    }
C
chengduoZH 已提交
101 102 103
  }
}

C
chengduoZH 已提交
104
std::string BroadcastOpHandle::Name() const { return "broadcast"; }
C
chengduoZH 已提交
105 106 107
}  // namespace details
}  // namespace framework
}  // namespace paddle