fleet_executor.cc 7.5 KB
Newer Older
L
LiYuRio 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright (c) 2021 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.

#include "paddle/fluid/distributed/fleet_executor/fleet_executor.h"
16
#include "paddle/fluid/distributed/fleet_executor/global_map.h"
17
#include "paddle/fluid/distributed/fleet_executor/message_bus.h"
L
LiYuRio 已提交
18
#include "paddle/fluid/distributed/fleet_executor/runtime_graph.h"
19
#include "paddle/fluid/distributed/fleet_executor/task_node.h"
20 21 22
#include "paddle/fluid/framework/executor_gc_helper.h"
#include "paddle/fluid/framework/op_desc.h"
#include "paddle/fluid/framework/op_registry.h"
23
#include "paddle/fluid/framework/operator.h"
L
LiYuRio 已提交
24
#include "paddle/fluid/framework/program_desc.h"
25 26
#include "paddle/fluid/framework/scope.h"
#include "paddle/fluid/framework/variable_helper.h"
L
LiYuRio 已提交
27 28 29 30 31

namespace paddle {
namespace distributed {

FleetExecutor::FleetExecutor(const std::string& exe_desc_str) {
L
LiYuRio 已提交
32 33 34
  bool parse_flag = exe_desc_.ParseFromString(exe_desc_str);
  PADDLE_ENFORCE(parse_flag, platform::errors::PreconditionNotMet(
                                 "Error occurs while parsing string to proto"));
L
LiYuRio 已提交
35 36
}

37 38
FleetExecutor::~FleetExecutor() {
  root_scope_->DropKids();
39 40 41
  for (const auto& item : runtime_graph_->carrier_id_to_interceptor_ids()) {
    GlobalMap<int64_t, Carrier>::Get(item.first)->Release();
  }
42
}
L
LiYuRio 已提交
43

44 45 46 47
void FleetExecutor::Init(
    const framework::ProgramDesc& program_desc, framework::Scope* scope,
    const platform::Place& place, const std::vector<TaskNode*>& task_nodes,
    const std::unordered_map<int64_t, int64_t>& task_id_to_rank) {
48 49 50 51 52 53 54 55
  PADDLE_ENFORCE_GT(task_nodes.size(), 0,
                    platform::errors::InvalidArgument(
                        "Fleet executor is inited with empty task node"));
  // TODO(fleet_exe devs): the unused_vars should be got from run time graph
  std::vector<std::unique_ptr<framework::OperatorBase>> ops;
  for (auto task_node : task_nodes) {
    for (auto op : task_node->ops()) {
      ops.emplace_back(std::unique_ptr<framework::OperatorBase>(op));
56
    }
57
  }
58 59 60
  auto unused_vars = framework::GetUnusedVars(program_desc.Block(0), ops, {});
  runtime_graph_ = std::make_shared<RuntimeGraph>();
  std::unordered_map<int64_t, TaskNode*> interceptor_id_to_task;
61 62 63
  std::unordered_map<int64_t, std::unordered_set<int64_t>>
      carrier_id_to_interceptor_ids;
  std::unordered_set<int64_t> interceptor_ids;
64 65 66 67
  for (auto task_node : task_nodes) {
    task_node->SetUnusedVars(unused_vars);
    int64_t interceptor_id = task_node->task_id();
    interceptor_id_to_task.emplace(interceptor_id, task_node);
68
    interceptor_ids.insert(interceptor_id);
69
  }
70
  carrier_id_to_interceptor_ids.emplace(0, interceptor_ids);
71 72
  runtime_graph_->SetInterceptorIdToRank(task_id_to_rank);
  runtime_graph_->SetInterceptorIdToNode(interceptor_id_to_task);
73
  runtime_graph_->SetCarrierIdToInterceptorIds(carrier_id_to_interceptor_ids);
74 75 76
  for (auto& unique_op : ops) {
    unique_op.release();
  }
77 78 79 80 81 82 83 84 85 86 87
  root_scope_ = scope;
  place_ = place;
  PADDLE_ENFORCE_NOT_NULL(root_scope_, platform::errors::InvalidArgument(
                                           "root_scope_ can not be nullptr"));
  minibatch_scope_ = &root_scope_->NewScope();
  int64_t num_micro_batches = exe_desc_.num_micro_batches();
  microbatch_scopes_.resize(num_micro_batches);
  for (int i = 0; i < num_micro_batches; ++i) {
    microbatch_scopes_[i] = &minibatch_scope_->NewScope();
    CopyParameters(i, program_desc);
  }
88
  VLOG(5) << runtime_graph_->DebugString();
89
  msg_bus_ = std::make_shared<MessageBus>();
90 91 92
  for (const auto& item : runtime_graph_->carrier_id_to_interceptor_ids()) {
    GlobalMap<int64_t, Carrier>::Create(item.first, item.first);
  }
93
  InitCarrier();
94
  InitMessageBus();
95

96 97
  // Wait for all message bus connected.
  msg_bus_->Barrier();
98 99
}

100
void FleetExecutor::InitCarrier() {
101 102 103 104 105 106 107 108 109
  for (const auto& item : runtime_graph_->carrier_id_to_interceptor_ids()) {
    Carrier* carrier = GlobalMap<int64_t, Carrier>::Get(item.first);
    PADDLE_ENFORCE_NOT_NULL(carrier, platform::errors::InvalidArgument(
                                         "Carrier has not been created."));
    carrier->SetMsgBus(msg_bus_);
    carrier->Init(exe_desc_.cur_rank(),
                  runtime_graph_->interceptor_id_to_rank(), item.second,
                  runtime_graph_->interceptor_id_to_node(), root_scope_,
                  minibatch_scope_, microbatch_scopes_, place_);
110 111 112
  }
}

113 114 115 116 117 118 119
void FleetExecutor::InitMessageBus() {
  std::stringstream ss;
  ss << "\nThe DNS table of the message bus is: \n";
  int64_t cur_rank = exe_desc_.cur_rank();
  std::unordered_map<int64_t, std::string> rank_to_addr;
  std::string addr;
  for (const auto& rank_info : exe_desc_.cluster_info()) {
120
    // init the dns map
121 122 123 124 125 126 127 128
    int64_t rank = rank_info.rank();
    std::string ip_port = rank_info.ip_port();
    ss << rank << "\t->\t" << ip_port << "\n";
    rank_to_addr.insert(std::make_pair(rank, ip_port));
    if (rank == cur_rank) {
      addr = ip_port;
    }
  }
129 130
  if (addr == "") {
    PADDLE_ENFORCE_EQ(
131
        rank_to_addr.size(), 1,
132 133 134 135 136 137 138 139 140 141
        platform::errors::NotFound("Empty address is not valid for "
                                   "paddle.distributed.launch method."));
    PADDLE_ENFORCE_EQ(
        cur_rank, 0,
        platform::errors::NotFound("Address is empty but cur rank is not 0."));
  }
  VLOG(3) << "Current rank is " << cur_rank << " and the ip_port is "
          << (addr == "" ? "empty" : addr) << ".";
  VLOG(3) << "The number of ranks are "
          << (rank_to_addr.size() == 0 ? 1 : rank_to_addr.size()) << ".";
142
  VLOG(5) << ss.str();
143
  if (!msg_bus_->IsInit()) {
144
    msg_bus_->Init(cur_rank, rank_to_addr, addr);
145
  }
L
LiYuRio 已提交
146 147 148
}

void FleetExecutor::Run() {
149 150 151
  for (const auto& item : runtime_graph_->carrier_id_to_interceptor_ids()) {
    GlobalMap<int64_t, Carrier>::Get(item.first)->Start();
  }
Y
Yuang Liu 已提交
152 153 154 155 156 157 158 159 160
  for (auto* micro_scop : microbatch_scopes_) {
    // By default, we should delete all kid scopes after run executor because
    // some operators may create local scope when running, such as while_op.
    // But when while_op also create a local executor to run it's sub block,
    // the sub scopes it created should not be dropped immediately, because
    // while_grad_op will use some variables created during while_op run, so
    // we need to keep the kids and wait for the outer executor to drop them.
    micro_scop->DropKids();
  }
L
LiYuRio 已提交
161 162
}

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
void FleetExecutor::CopyParameters(int microbatch_id,
                                   const framework::ProgramDesc& program) {
  auto& global_block = program.Block(0);

  for (auto& var : global_block.AllVars()) {
    if (var->Persistable() && microbatch_id == 0) {
      auto* ptr = root_scope_->Var(var->Name());
      InitializeVariable(ptr, var->GetType());
      VLOG(5) << "Create persistable var: " << var->Name()
              << ", which pointer is " << ptr;
    } else if (!var->Persistable()) {
      auto* ptr = microbatch_scopes_[microbatch_id]->Var(var->Name());
      VLOG(5) << "Create variable " << var->Name() << " for microbatch "
              << microbatch_id << ", which pointer is " << ptr << ".";
      InitializeVariable(ptr, var->GetType());
    }
  }
L
LiYuRio 已提交
180 181 182 183
}

}  // namespace distributed
}  // namespace paddle