|
| 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 <string> |
| 16 | +#include <vector> |
| 17 | + |
| 18 | +#include "paddle/fluid/framework/details/computation_op_handle.h" |
| 19 | +#include "paddle/fluid/framework/details/multi_devices_helper.h" |
| 20 | +#include "paddle/fluid/framework/details/reference_count_pass.h" |
| 21 | + |
| 22 | +namespace paddle { |
| 23 | +namespace framework { |
| 24 | +namespace details { |
| 25 | + |
| 26 | +std::unique_ptr<ir::Graph> ReferenceCountPass::ApplyImpl( |
| 27 | + std::unique_ptr<ir::Graph> graph) const { |
| 28 | + auto &ref_cnts = Get<DeviceReferenceCountMap>(kGlobalReferenceCount); |
| 29 | + auto &cur_ref_cnts = Get<AtomicDeviceReferenceCountMap>(kCurReferenceCount); |
| 30 | + auto &gcs = Get<DeviceGarbageCollectorMap>(kGarbageCollector); |
| 31 | + |
| 32 | + // It is not easy to find the right reference counts of varaibles in graph |
| 33 | + // Step 1: Find all variables in computation ops |
| 34 | + // Step 2: Find all variables in non-computation ops which refers to variables |
| 35 | + // in computation ops |
| 36 | + std::unordered_set<std::string> names; |
| 37 | + auto get_ref_cnts_from_compute_op = [&]( |
| 38 | + const std::unique_ptr<OpHandleBase> &op, |
| 39 | + const std::vector<VarHandleBase *> &vars) { |
| 40 | + std::vector<std::string> var_names_in_op; |
| 41 | + auto *compute_op = dynamic_cast<ComputationOpHandle *>(op.get()); |
| 42 | + if (compute_op == nullptr || |
| 43 | + !platform::is_gpu_place(compute_op->GetPlace())) |
| 44 | + return var_names_in_op; |
| 45 | + auto place = boost::get<platform::CUDAPlace>(compute_op->GetPlace()); |
| 46 | + for (VarHandleBase *var_handle_base : vars) { |
| 47 | + auto *var_handle = dynamic_cast<VarHandle *>(var_handle_base); |
| 48 | + if (var_handle == nullptr || !var_handle->Node()->IsVar()) continue; |
| 49 | + |
| 50 | + if (!platform::is_gpu_place(var_handle->place_) || |
| 51 | + boost::get<platform::CUDAPlace>(var_handle->place_) != place) |
| 52 | + continue; |
| 53 | + |
| 54 | + VarDesc *var_desc = var_handle->Node()->Var(); |
| 55 | + auto var_name = var_handle->Node()->Name(); |
| 56 | + |
| 57 | + // This is wierd but there is really some variables without var_desc |
| 58 | + // in computation_op |
| 59 | + if (var_desc == nullptr) { |
| 60 | + if (compute_op->Node()->Op()->Block()->FindVar(var_name) == nullptr) |
| 61 | + continue; |
| 62 | + } else { |
| 63 | + if (var_desc->Persistable() || |
| 64 | + var_desc->Proto()->type().type() != proto::VarType::LOD_TENSOR) |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + // compute op only runs in one device |
| 69 | + if (ref_cnts[place.device]->count(var_name)) |
| 70 | + ++(*ref_cnts[place.device])[var_name]; |
| 71 | + else |
| 72 | + (*ref_cnts[place.device])[var_name] = 1; |
| 73 | + |
| 74 | + names.insert(var_name); |
| 75 | + var_names_in_op.push_back(var_name); |
| 76 | + } |
| 77 | + return var_names_in_op; |
| 78 | + }; |
| 79 | + |
| 80 | + auto update_ref_cnts_from_non_compute_op = [&]( |
| 81 | + const std::unique_ptr<OpHandleBase> &op, |
| 82 | + const std::vector<VarHandleBase *> &vars) { |
| 83 | + if (dynamic_cast<ComputationOpHandle *>(op.get()) != nullptr) return; |
| 84 | + for (VarHandleBase *var_handle_base : vars) { |
| 85 | + auto *var_handle = dynamic_cast<VarHandle *>(var_handle_base); |
| 86 | + if (var_handle == nullptr || !var_handle->Node()->IsVar()) continue; |
| 87 | + |
| 88 | + auto var_name = var_handle->Node()->Name(); |
| 89 | + auto var_place = var_handle->place_; |
| 90 | + if (!platform::is_gpu_place(var_place)) continue; |
| 91 | + auto place = boost::get<platform::CUDAPlace>(var_place); |
| 92 | + if (names.count(var_name) == 0) continue; |
| 93 | + if (ref_cnts.count(place.device) && |
| 94 | + ref_cnts[place.device]->count(var_name)) { |
| 95 | + ++(*ref_cnts[place.device])[var_name]; |
| 96 | + } |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + std::unordered_map<OpHandleBase *, ReferenceCountOpHandle *> |
| 101 | + compute_ref_cnt_map; |
| 102 | + auto &all_ops = graph->Get<GraphOps>(kGraphOps); |
| 103 | + for (auto &op : all_ops) { |
| 104 | + auto in_var_names = get_ref_cnts_from_compute_op(op, op->Inputs()); |
| 105 | + auto out_var_names = get_ref_cnts_from_compute_op(op, op->Outputs()); |
| 106 | + if (in_var_names.empty() && out_var_names.empty()) continue; |
| 107 | + in_var_names.insert(in_var_names.end(), out_var_names.begin(), |
| 108 | + out_var_names.end()); |
| 109 | + auto *compute_op = dynamic_cast<ComputationOpHandle *>(op.get()); |
| 110 | + auto place = boost::get<platform::CUDAPlace>(compute_op->GetPlace()); |
| 111 | + ir::Node *ref_cnt_node = |
| 112 | + graph->CreateEmptyNode("reference_count", ir::Node::Type::kOperation); |
| 113 | + auto *ref_cnt_handle = new ReferenceCountOpHandle( |
| 114 | + ref_cnt_node, compute_op->GetScope(), place, in_var_names, |
| 115 | + gcs[place.device].get(), cur_ref_cnts[place.device].get()); |
| 116 | + auto *dep_var = new DummyVarHandle(graph->CreateControlDepVar()); |
| 117 | + compute_op->AddOutput(dep_var); |
| 118 | + ref_cnt_handle->AddInput(dep_var); |
| 119 | + graph->Get<GraphDepVars>(kGraphDepVars).emplace(dep_var); |
| 120 | + compute_ref_cnt_map[compute_op] = ref_cnt_handle; |
| 121 | + } |
| 122 | + |
| 123 | + for (auto &op : all_ops) { |
| 124 | + update_ref_cnts_from_non_compute_op(op, op->Inputs()); |
| 125 | + update_ref_cnts_from_non_compute_op(op, op->Outputs()); |
| 126 | + } |
| 127 | + |
| 128 | + std::vector<std::unique_ptr<OpHandleBase>> new_all_ops; |
| 129 | + new_all_ops.reserve(compute_ref_cnt_map.size() + all_ops.size()); |
| 130 | + for (auto &op : all_ops) { |
| 131 | + new_all_ops.emplace_back(std::move(op)); |
| 132 | + auto it = compute_ref_cnt_map.find(new_all_ops.back().get()); |
| 133 | + if (it != compute_ref_cnt_map.end()) { |
| 134 | + new_all_ops.emplace_back(it->second); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + all_ops.swap(new_all_ops); |
| 139 | + return graph; |
| 140 | +} |
| 141 | + |
| 142 | +} // namespace details |
| 143 | +} // namespace framework |
| 144 | +} // namespace paddle |
| 145 | + |
| 146 | +REGISTER_PASS(reference_count_pass, |
| 147 | + paddle::framework::details::ReferenceCountPass) |
| 148 | + .RequirePassAttr(paddle::framework::details::kGlobalReferenceCount) |
| 149 | + .RequirePassAttr(paddle::framework::details::kCurReferenceCount) |
| 150 | + .RequirePassAttr(paddle::framework::details::kGarbageCollector); |
0 commit comments