|
| 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/data_balance_op_handle.h" |
| 16 | +#include <algorithm> |
| 17 | +#include "paddle/fluid/framework/details/container_cast.h" |
| 18 | + |
| 19 | +namespace paddle { |
| 20 | +namespace framework { |
| 21 | +namespace details { |
| 22 | + |
| 23 | +DataBalanceOpHandle::DataBalanceOpHandle( |
| 24 | + const std::vector<Scope *> &local_scopes, |
| 25 | + const std::vector<platform::Place> &places) |
| 26 | + : local_scopes_(local_scopes), places_(places) {} |
| 27 | + |
| 28 | +std::string DataBalanceOpHandle::Name() const { return "data balance"; } |
| 29 | + |
| 30 | +std::vector<std::array<int, 3>> DataBalanceOpHandle::GetBalancePlan( |
| 31 | + const std::vector<int> &device_sizes) { |
| 32 | + int device_num = device_sizes.size(); |
| 33 | + int total_size = 0; |
| 34 | + int empty_num = 0; |
| 35 | + std::vector<std::array<int, 2>> size_device_vec; |
| 36 | + size_device_vec.reserve(device_num); |
| 37 | + for (int i = 0; i < device_num; ++i) { |
| 38 | + if (device_sizes[i] == 0) { |
| 39 | + ++empty_num; |
| 40 | + } |
| 41 | + total_size += device_sizes[i]; |
| 42 | + size_device_vec.push_back({{device_sizes[i], i}}); |
| 43 | + } |
| 44 | + std::vector<std::array<int, 3>> res; |
| 45 | + if (empty_num == 0) { |
| 46 | + // No need to do data balance. |
| 47 | + return res; |
| 48 | + } |
| 49 | + if (total_size < device_num) { |
| 50 | + // No enough data. |
| 51 | + PADDLE_THROW("There is no next data."); |
| 52 | + } |
| 53 | + std::sort(size_device_vec.begin(), size_device_vec.end(), |
| 54 | + [](const std::array<int, 2> &a, const std::array<int, 2> &b) { |
| 55 | + return a[0] > b[0]; |
| 56 | + }); |
| 57 | + int expected_device_size = total_size / device_num; |
| 58 | + int src_idx = 0; |
| 59 | + for (int dst_idx = device_num - empty_num; dst_idx < device_num; ++dst_idx) { |
| 60 | + if (size_device_vec[src_idx][0] <= expected_device_size) { |
| 61 | + ++src_idx; |
| 62 | + PADDLE_ENFORCE_LT(src_idx, device_num - empty_num); |
| 63 | + } |
| 64 | + size_device_vec[src_idx][0] -= expected_device_size; |
| 65 | + size_device_vec[dst_idx][0] += expected_device_size; |
| 66 | + res.push_back({{size_device_vec[src_idx][1], size_device_vec[dst_idx][1], |
| 67 | + expected_device_size}}); |
| 68 | + } |
| 69 | + return res; |
| 70 | +} |
| 71 | + |
| 72 | +void DataBalanceOpHandle::RunImpl() { |
| 73 | + if (places_.size() == 1) { |
| 74 | + return; |
| 75 | + } |
| 76 | + auto in_var_handles = DynamicCast<VarHandle>(inputs_); |
| 77 | + auto out_var_handles = DynamicCast<VarHandle>(outputs_); |
| 78 | + PADDLE_ENFORCE(in_var_handles.size() % places_.size() == 0); |
| 79 | + PADDLE_ENFORCE_EQ( |
| 80 | + in_var_handles.size(), out_var_handles.size(), |
| 81 | + "The NoDummyInputSize and NoDummyOutputSize should be equal."); |
| 82 | + int data_num = in_var_handles.size() / places_.size(); |
| 83 | + WaitInputVarGenerated(); |
| 84 | + |
| 85 | + std::vector<std::vector<LoDTensor *>> lod_tensors; |
| 86 | + std::vector<int> device_sizes; |
| 87 | + for (int i = 0; i < static_cast<int>(in_var_handles.size()); ++i) { |
| 88 | + PADDLE_ENFORCE_EQ(in_var_handles[i]->name_, out_var_handles[i]->name_, |
| 89 | + "The name of input and output should be equal."); |
| 90 | + int place_idx = i / data_num; |
| 91 | + int data_idx = i % data_num; |
| 92 | + auto *local_scope = |
| 93 | + local_scopes_[place_idx]->FindVar(kLocalExecScopeName)->Get<Scope *>(); |
| 94 | + auto *tensor_var = local_scope->FindVar(in_var_handles[i]->name_); |
| 95 | + PADDLE_ENFORCE(tensor_var->IsType<LoDTensor>()); |
| 96 | + auto *tensor = tensor_var->GetMutable<LoDTensor>(); |
| 97 | + PADDLE_ENFORCE(places_[place_idx] == tensor->place()); |
| 98 | + lod_tensors[data_idx].push_back(tensor); |
| 99 | + int ins_size = |
| 100 | + tensor->lod().empty() ? tensor->dims()[0] : tensor->NumElements(); |
| 101 | + if (data_idx == 0) { |
| 102 | + device_sizes.emplace_back(ins_size); |
| 103 | + } else { |
| 104 | + PADDLE_ENFORCE_EQ(ins_size, device_sizes.at(place_idx)); |
| 105 | + } |
| 106 | + } |
| 107 | + const auto &balance_plan = GetBalancePlan(device_sizes); |
| 108 | + |
| 109 | + for (const auto &trans : balance_plan) { |
| 110 | + for (int data_idx = 0; data_idx < data_num; ++data_idx) { |
| 111 | + LoDTensor *src_tensor = lod_tensors[data_idx][trans[0]]; |
| 112 | + LoDTensor *dst_tensor = lod_tensors[data_idx][trans[1]]; |
| 113 | + int trans_ins_size = trans[2]; |
| 114 | + LoD src_lod = src_tensor->lod(); |
| 115 | + int src_ins_size = |
| 116 | + src_lod.empty() ? src_tensor->dims()[0] : src_tensor->NumElements(); |
| 117 | + int cut_point = src_ins_size - trans_ins_size; |
| 118 | + if (!src_lod.empty()) { |
| 119 | + for (auto &level : src_lod) { |
| 120 | + cut_point = level[cut_point]; |
| 121 | + } |
| 122 | + } |
| 123 | + TensorCopySync(src_tensor->Slice(cut_point, src_tensor->dims()[0]), |
| 124 | + dst_tensor->place(), dst_tensor); |
| 125 | + src_tensor->ShareDataWith(src_tensor->Slice(0, cut_point)); |
| 126 | + if (!src_lod.empty()) { |
| 127 | + dst_tensor->set_lod(SliceInLevel( |
| 128 | + src_lod, 0, src_ins_size - trans_ins_size, src_ins_size)); |
| 129 | + src_tensor->set_lod( |
| 130 | + SliceInLevel(src_lod, 0, 0, src_ins_size - trans_ins_size)); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +} // namespace details |
| 137 | +} // namespace framework |
| 138 | +} // namespace paddle |
0 commit comments