|
| 1 | +// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "paddle/fluid/framework/details/reduce_op_handle.h" |
| 16 | +#include "paddle/fluid/framework/details/reduce_and_gather.h" |
| 17 | + |
| 18 | +namespace paddle { |
| 19 | +namespace framework { |
| 20 | +namespace details { |
| 21 | + |
| 22 | +void ReduceOpHandle::RunImpl() { |
| 23 | + // the input and output may have dummy var. |
| 24 | + std::vector<VarHandle *> in_var_handles = GetValidVarHandles(inputs_); |
| 25 | + std::vector<VarHandle *> out_var_handles = GetValidVarHandles(outputs_); |
| 26 | + |
| 27 | + PADDLE_ENFORCE_EQ( |
| 28 | + in_var_handles.size(), places_.size(), |
| 29 | + "The number of output should equal to the number of places."); |
| 30 | + PADDLE_ENFORCE_EQ(out_var_handles.size(), 1, |
| 31 | + "The number of output should be one."); |
| 32 | + |
| 33 | + // Wait input done, this Wait is asynchronous operation |
| 34 | + WaitEvents(in_var_handles); |
| 35 | + |
| 36 | + // check in the same place |
| 37 | + auto in_0_handle = in_var_handles[0]; |
| 38 | + auto pre_place = in_0_handle->place_; |
| 39 | + |
| 40 | + std::vector<platform::Place> in_places; |
| 41 | + for (auto *in_handle : in_var_handles) { |
| 42 | + auto in_p = in_handle->place_; |
| 43 | + PADDLE_ENFORCE_EQ(in_p.which(), pre_place.which(), |
| 44 | + "Places must be all on CPU or all on CUDA."); |
| 45 | + in_places.emplace_back(in_p); |
| 46 | + } |
| 47 | + |
| 48 | + auto out_var = local_scopes_[out_var_handles[0]->scope_idx_]->FindVar( |
| 49 | + out_var_handles[0]->name_); |
| 50 | + |
| 51 | + auto pre_in_var = |
| 52 | + local_scopes_[in_0_handle->scope_idx_]->FindVar(in_0_handle->name_); |
| 53 | + |
| 54 | + if (pre_in_var->IsType<framework::SelectedRows>()) { |
| 55 | + auto &pre_in = pre_in_var->Get<framework::SelectedRows>(); |
| 56 | + std::vector<const SelectedRows *> in_selected_rows; |
| 57 | + |
| 58 | + for (auto *in_handle : in_var_handles) { |
| 59 | + auto in_var = |
| 60 | + local_scopes_.at(in_handle->scope_idx_)->FindVar(in_handle->name_); |
| 61 | + auto &in_sr = in_var->Get<framework::SelectedRows>(); |
| 62 | + |
| 63 | + PADDLE_ENFORCE_EQ(in_sr.value().type(), pre_in.value().type(), |
| 64 | + "The type of input is not consistent."); |
| 65 | + |
| 66 | + in_selected_rows.emplace_back(&in_sr); |
| 67 | + } |
| 68 | + auto trg = out_var->GetMutable<framework::SelectedRows>(); |
| 69 | + GatherSelectedRows(in_selected_rows, in_places, dev_ctxes_, |
| 70 | + out_var_handles[0]->place_, trg); |
| 71 | + } else { |
| 72 | + auto pre_in = pre_in_var->Get<framework::LoDTensor>(); |
| 73 | + std::vector<LoDTensor> lod_tensors; |
| 74 | + |
| 75 | + // can be refined |
| 76 | + for (auto *in_handle : in_var_handles) { |
| 77 | + auto in_var = |
| 78 | + local_scopes_.at(in_handle->scope_idx_)->FindVar(in_handle->name_); |
| 79 | + auto &in_sr = in_var->Get<framework::LoDTensor>(); |
| 80 | + |
| 81 | + PADDLE_ENFORCE_EQ(in_sr.type(), pre_in.type(), |
| 82 | + "The type of input is not consistent."); |
| 83 | + |
| 84 | + lod_tensors.emplace_back(in_sr); |
| 85 | + } |
| 86 | + |
| 87 | + auto trg = out_var->GetMutable<framework::LoDTensor>(); |
| 88 | + trg->Resize(pre_in.dims()); |
| 89 | + trg->mutable_data(out_var_handles[0]->place_, pre_in.type()); |
| 90 | + |
| 91 | + if (paddle::platform::is_cpu_place(pre_place)) { |
| 92 | + ReduceLoDTensor func(lod_tensors, trg); |
| 93 | + VisitDataType(ToDataType(lod_tensors[0].type()), func); |
| 94 | + } else if (paddle::platform::is_gpu_place(pre_place)) { |
| 95 | +#ifdef PADDLE_WITH_CUDA |
| 96 | + auto out_p = out_var_handles[0]->place_; |
| 97 | + int root = boost::get<platform::CUDAPlace>(out_p).device; |
| 98 | + |
| 99 | + std::vector<std::function<void()>> all_reduce_calls; |
| 100 | + for (size_t i = 0; i < local_scopes_.size(); ++i) { |
| 101 | + auto &p = in_places[i]; |
| 102 | + auto &lod_tensor = lod_tensors[i]; |
| 103 | + |
| 104 | + int dev_id = boost::get<platform::CUDAPlace>(p).device; |
| 105 | + auto &nccl_ctx = nccl_ctxs_->at(dev_id); |
| 106 | + auto stream = nccl_ctx.stream(); |
| 107 | + auto comm = nccl_ctx.comm_; |
| 108 | + |
| 109 | + void *buffer = const_cast<void *>(lod_tensor.data<void>()); |
| 110 | + void *recvbuffer = nullptr; |
| 111 | + if (root == dev_id) { |
| 112 | + recvbuffer = trg->mutable_data(out_var_handles[0]->place_); |
| 113 | + } |
| 114 | + |
| 115 | + all_reduce_calls.emplace_back([=] { |
| 116 | + PADDLE_ENFORCE(platform::dynload::ncclReduce( |
| 117 | + buffer, recvbuffer, static_cast<size_t>(lod_tensor.numel()), |
| 118 | + platform::ToNCCLDataType(lod_tensor.type()), ncclSum, root, comm, |
| 119 | + stream)); |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + this->RunAndRecordEvent([&] { |
| 124 | + platform::NCCLGroupGuard guard; |
| 125 | + for (auto &call : all_reduce_calls) { |
| 126 | + call(); |
| 127 | + } |
| 128 | + }); |
| 129 | +#else |
| 130 | + PADDLE_THROW("CUDA is not support."); |
| 131 | +#endif |
| 132 | + } else { |
| 133 | + PADDLE_THROW("Place should be CPUPlace or CUDAPlace."); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +void ReduceOpHandle::WaitEvents( |
| 139 | + const std::vector<VarHandle *> &in_var_handles) { |
| 140 | + if (in_var_handles[0]->generated_op_) { |
| 141 | + for (auto *in : in_var_handles) { |
| 142 | + in_var_handles[0]->generated_op_->Wait(dev_ctxes_[in->place_]); |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +std::vector<VarHandle *> ReduceOpHandle::GetValidVarHandles( |
| 148 | + const std::vector<VarHandleBase *> &inputs) { |
| 149 | + std::vector<VarHandle *> in_var_handles; |
| 150 | + for (auto *in : inputs) { |
| 151 | + auto *in_handle = dynamic_cast<VarHandle *>(in); |
| 152 | + if (in_handle) { |
| 153 | + in_var_handles.push_back(in_handle); |
| 154 | + } |
| 155 | + } |
| 156 | + return in_var_handles; |
| 157 | +} |
| 158 | +std::string ReduceOpHandle::Name() const { return "reduce"; } |
| 159 | +} // namespace details |
| 160 | +} // namespace framework |
| 161 | +} // namespace paddle |
0 commit comments