broadcast_op_handle.cc 4.2 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 {

21 22 23 24 25 26 27 28 29 30 31
Tensor *GetTensorFromVar(Variable *in_var) {
  if (in_var->IsType<LoDTensor>()) {
    return in_var->GetMutable<LoDTensor>();
  } else if (in_var->IsType<SelectedRows>()) {
    return in_var->GetMutable<SelectedRows>()->mutable_value();
  } else {
    PADDLE_THROW("Var should be LoDTensor or SelectedRows");
  }
  return nullptr;
}

C
chengduoZH 已提交
32
BroadcastOpHandle::BroadcastOpHandle(const std::vector<Scope *> &local_scopes,
C
chengduoZH 已提交
33 34
                                     const std::vector<platform::Place> &places)
    : local_scopes_(local_scopes), places_(places) {}
C
chengduoZH 已提交
35

C
chengduoZH 已提交
36
void BroadcastOpHandle::RunImpl() {
C
chengduoZH 已提交
37 38 39 40
  // the input and output may have dummy var.
  std::vector<VarHandle *> in_var_handle = GetValidVarHandles(inputs_);
  std::vector<VarHandle *> out_var_handles = GetValidVarHandles(outputs_);

41
  PADDLE_ENFORCE_EQ(in_var_handle.size(), 1,
C
chengduoZH 已提交
42 43
                    "The number of input should be one.");
  PADDLE_ENFORCE_EQ(
44
      out_var_handles.size(), places_.size(),
C
chengduoZH 已提交
45
      "The number of output should equal to the number of places.");
C
chengduoZH 已提交
46

C
chengduoZH 已提交
47 48 49
  // Wait input done, this Wait is asynchronous operationplatform::Place
  // &in_place;
  WaitEvents(out_var_handles, in_var_handle);
C
chengduoZH 已提交
50

C
chengduoZH 已提交
51
  auto in_place = in_var_handle[0]->place_;
52
  auto in_scope_idx = in_var_handle[0]->scope_idx_;
C
chengduoZH 已提交
53 54
  auto in_var =
      local_scopes_.at(in_scope_idx)->FindVar(in_var_handle[0]->name_);
C
chengduoZH 已提交
55 56
  Tensor *in_tensor = GetTensorFromVar(in_var);

57 58
  for (auto *out : out_var_handles) {
    auto &out_p = out->place_;
C
chengduoZH 已提交
59
    auto out_var = local_scopes_.at(out->scope_idx_)->FindVar(out->name_);
60

C
chengduoZH 已提交
61
    PADDLE_ENFORCE_EQ(out_p.which(), in_place.which(),
C
chengduoZH 已提交
62
                      "Places must be all on CPU or all on CUDA.");
C
chengduoZH 已提交
63 64

    if (in_var->IsType<framework::SelectedRows>()) {
C
chengduoZH 已提交
65 66 67 68 69 70 71
      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 已提交
72
    } else if (in_var->IsType<framework::LoDTensor>()) {
C
chengduoZH 已提交
73 74 75 76 77 78
      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 已提交
79
    } else {
C
chengduoZH 已提交
80
      PADDLE_THROW("Var should be LoDTensor or SelectedRows.");
C
chengduoZH 已提交
81 82
    }

C
chengduoZH 已提交
83 84 85 86 87
    auto dev_ctx = dev_ctxes_[out_p];
    RunAndRecordEvent(out_p, [in_tensor, out_var, dev_ctx, out_p] {
      Tensor *out_tensor = GetTensorFromVar(out_var);
      paddle::framework::TensorCopy(*in_tensor, out_p, *(dev_ctx), out_tensor);
    });
C
chengduoZH 已提交
88 89 90
  }
}

C
chengduoZH 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
void BroadcastOpHandle::WaitEvents(
    const std::vector<VarHandle *> &out_var_handles,
    const std::vector<VarHandle *> &in_var_handle) {
  if (in_var_handle[0]->generated_op_) {
    for (auto *out : out_var_handles) {
      auto &out_p = out->place_;
      in_var_handle[0]->generated_op_->Wait(dev_ctxes_[out_p]);
    }
  }
}

std::vector<VarHandle *> BroadcastOpHandle::GetValidVarHandles(
    const std::vector<VarHandleBase *> &inputs) {
  std::vector<VarHandle *> in_var_handle;
  for (auto *in : inputs) {
    auto *out_handle = dynamic_cast<VarHandle *>(in);
    if (out_handle) {
      in_var_handle.push_back(out_handle);
    }
  }
  return in_var_handle;
}

C
chengduoZH 已提交
114
std::string BroadcastOpHandle::Name() const { return "broadcast"; }
C
chengduoZH 已提交
115 116 117
}  // namespace details
}  // namespace framework
}  // namespace paddle