|
| 1 | +// Copyright (c) 2019 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 <algorithm> |
| 16 | +#include <string> |
| 17 | +#include <vector> |
| 18 | + |
| 19 | +#include "paddle/fluid/framework/details/all_reduce_op_handle.h" |
| 20 | +#include "paddle/fluid/framework/details/container_cast.h" |
| 21 | +#include "paddle/fluid/framework/details/fused_all_reduce_op_handle.h" |
| 22 | +#include "paddle/fluid/framework/details/multi_devices_helper.h" |
| 23 | +#include "paddle/fluid/framework/ir/graph_helper.h" |
| 24 | + |
| 25 | +namespace paddle { |
| 26 | +namespace framework { |
| 27 | +namespace details { |
| 28 | + |
| 29 | +class FuseAllReduceOpPass : public ir::Pass { |
| 30 | + protected: |
| 31 | + std::unique_ptr<ir::Graph> ApplyImpl( |
| 32 | + std::unique_ptr<ir::Graph> graph) const override { |
| 33 | + ir::Graph &result = *graph; |
| 34 | + |
| 35 | + auto &places = Get<const std::vector<platform::Place>>(kPlaces); |
| 36 | + auto &local_scopes = Get<const std::vector<Scope *>>(kLocalScopes); |
| 37 | +#if defined(PADDLE_WITH_CUDA) && !defined(_WIN32) |
| 38 | + auto *nccl_ctxs = &Get<platform::NCCLContextMap>(kNCCLCtxs); |
| 39 | +#endif |
| 40 | + |
| 41 | + std::unordered_set<std::string> grads; |
| 42 | + auto ¶ms_grads = result.Get<ParamsAndGrads>(kParamsAndGrads); |
| 43 | + size_t num_of_all_reduce = params_grads.size(); |
| 44 | + grads.reserve(num_of_all_reduce); |
| 45 | + for (auto p_g : params_grads) { |
| 46 | + grads.insert(p_g.second); |
| 47 | + } |
| 48 | + |
| 49 | + // find all reduce op |
| 50 | + // the gradient doesn't have sparse type |
| 51 | + // |
| 52 | + size_t num_place = places.size(); |
| 53 | + std::vector<std::string> all_reduce_grads; |
| 54 | + std::vector<ir::Node *> all_reduce_ops; |
| 55 | + all_reduce_ops.reserve(grads.size()); |
| 56 | + all_reduce_grads.reserve(grads.size()); |
| 57 | + for (auto &node : result.Nodes()) { |
| 58 | + if (node->IsOp()) { |
| 59 | + PADDLE_ENFORCE(node->IsWrappedBy<OpHandleBase>()); |
| 60 | + auto *all_reduce_op_handle = |
| 61 | + dynamic_cast<AllReduceOpHandle *>(&node->Wrapper<OpHandleBase>()); |
| 62 | + if (all_reduce_op_handle) { |
| 63 | + auto inputs = DynamicCast<VarHandle>(all_reduce_op_handle->Inputs()); |
| 64 | + PADDLE_ENFORCE_EQ(all_reduce_op_handle->NoDummyInputSize(), |
| 65 | + num_place); |
| 66 | + // TODO(zcd): The inputs' name should be the same. |
| 67 | + |
| 68 | + PADDLE_ENFORCE_NE(grads.count(inputs.at(0)->name()), 0); |
| 69 | + all_reduce_ops.emplace_back(node); |
| 70 | + all_reduce_grads.emplace_back(inputs.at(0)->name()); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + VLOG(10) << "Find all_reduce_ops: " << all_reduce_ops.size(); |
| 75 | + if (all_reduce_ops.size() == 0) { |
| 76 | + return std::move(graph); |
| 77 | + } |
| 78 | + |
| 79 | + PADDLE_ENFORCE_EQ(all_reduce_ops.size(), grads.size()); |
| 80 | + VLOG(10) << "Insert fused_all_reduce"; |
| 81 | + |
| 82 | + std::vector<VarHandleBase *> inputs; |
| 83 | + std::vector<VarHandleBase *> outputs; |
| 84 | + for (auto &op : all_reduce_ops) { |
| 85 | + auto &op_handle = op->Wrapper<OpHandleBase>(); |
| 86 | + inputs.insert(inputs.end(), op_handle.Inputs().begin(), |
| 87 | + op_handle.Inputs().end()); |
| 88 | + // Remove output |
| 89 | + std::for_each(op_handle.Inputs().begin(), op_handle.Inputs().end(), |
| 90 | + [&op_handle](VarHandleBase *var_handle) { |
| 91 | + var_handle->RemoveOutput(&op_handle, op_handle.Node()); |
| 92 | + }); |
| 93 | + |
| 94 | + outputs.insert(outputs.end(), op_handle.Outputs().begin(), |
| 95 | + op_handle.Outputs().end()); |
| 96 | + // Remove Input |
| 97 | + std::for_each( |
| 98 | + op_handle.Outputs().begin(), op_handle.Outputs().end(), |
| 99 | + [](VarHandleBase *var_handle) { var_handle->ClearGeneratedOp(); }); |
| 100 | + |
| 101 | + result.RemoveNode(op_handle.Node()); |
| 102 | + } |
| 103 | + |
| 104 | +#if defined(PADDLE_WITH_CUDA) && !defined(_WIN32) |
| 105 | + CreateFusedAllReduceOp(inputs, outputs, num_of_all_reduce, places, |
| 106 | + local_scopes, nccl_ctxs, &result); |
| 107 | +#else |
| 108 | + CreateFusedAllReduceOp(inputs, outputs, num_of_all_reduce, places, |
| 109 | + local_scopes, &result); |
| 110 | +#endif |
| 111 | + |
| 112 | + return std::move(graph); |
| 113 | + } |
| 114 | + |
| 115 | + private: |
| 116 | + void CreateFusedAllReduceOp(const std::vector<VarHandleBase *> &inputs, |
| 117 | + const std::vector<VarHandleBase *> &outputs, |
| 118 | + const size_t num_of_all_reduce, |
| 119 | + const std::vector<platform::Place> &places, |
| 120 | + const std::vector<Scope *> &local_scopes, |
| 121 | +#if defined(PADDLE_WITH_CUDA) && !defined(_WIN32) |
| 122 | + const platform::NCCLContextMap *nccl_ctxs, |
| 123 | +#endif |
| 124 | + ir::Graph *result) const { |
| 125 | +#if defined(PADDLE_WITH_CUDA) && !defined(_WIN32) |
| 126 | + auto *op_handle = new FusedAllReduceOpHandle( |
| 127 | + result->CreateEmptyNode("fused_all_reduce", ir::Node::Type::kOperation), |
| 128 | + local_scopes, places, num_of_all_reduce, nccl_ctxs); |
| 129 | +#else |
| 130 | + auto *op_handle = new FusedAllReduceOpHandle( |
| 131 | + result->CreateEmptyNode("fused_all_reduce", ir::Node::Type::kOperation), |
| 132 | + local_scopes, places, num_of_all_reduce); |
| 133 | +#endif |
| 134 | + |
| 135 | + for (auto in : inputs) { |
| 136 | + op_handle->AddInput(in); |
| 137 | + } |
| 138 | + |
| 139 | + for (auto out : outputs) { |
| 140 | + op_handle->AddOutput(out); |
| 141 | + } |
| 142 | + |
| 143 | +#if defined(PADDLE_WITH_CUDA) && !defined(_WIN32) |
| 144 | + if (!nccl_ctxs) { |
| 145 | + SetCommunicationContext(places, op_handle); |
| 146 | + } |
| 147 | +#else |
| 148 | + SetCommunicationContext(places, op_handle); |
| 149 | +#endif |
| 150 | + } |
| 151 | + |
| 152 | + void SetCommunicationContext(const std::vector<platform::Place> &places, |
| 153 | + FusedAllReduceOpHandle *op_handle) const { |
| 154 | + for (size_t i = 0; i < places.size(); ++i) { |
| 155 | + op_handle->SetDeviceContext( |
| 156 | + places[i], platform::DeviceContextPool::Instance().Get(places[i])); |
| 157 | + } |
| 158 | + } |
| 159 | +}; |
| 160 | + |
| 161 | +} // namespace details |
| 162 | +} // namespace framework |
| 163 | +} // namespace paddle |
| 164 | + |
| 165 | +REGISTER_PASS(fuse_all_reduce_op_pass, |
| 166 | + paddle::framework::details::FuseAllReduceOpPass); |
0 commit comments