fleet_executor.cc 7.1 KB
Newer Older
L
LiYuRio 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
// 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.
14
#include <algorithm>
L
LiYuRio 已提交
15 16

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

namespace paddle {
namespace distributed {

FleetExecutor::FleetExecutor(const std::string& exe_desc_str) {
L
LiYuRio 已提交
31 32 33
  bool parse_flag = exe_desc_.ParseFromString(exe_desc_str);
  PADDLE_ENFORCE(parse_flag, platform::errors::PreconditionNotMet(
                                 "Error occurs while parsing string to proto"));
34
  // Message bus will be created and inited only once
35 36 37 38 39 40 41
  GlobalVal<MessageBus>::Create();
  InitMessageBus();
}

FleetExecutor::FleetExecutor(const FleetExecutorDesc& exe_desc)
    : exe_desc_(exe_desc) {
  // Message bus will be created and inited only once
42 43
  GlobalVal<MessageBus>::Create();
  InitMessageBus();
L
LiYuRio 已提交
44 45
}

46
FleetExecutor::~FleetExecutor() {
47 48
  for (const auto& carrier_id : carrier_ids_) {
    GlobalMap<std::string, Carrier>::Get(carrier_id)->Release();
49
  }
50
}
L
LiYuRio 已提交
51

52
void FleetExecutor::Init(
53 54
    const std::string& carrier_id, const framework::ProgramDesc& program_desc,
    framework::Scope* scope, const platform::Place& place,
55
    int64_t num_micro_batches, const std::vector<TaskNode*>& task_nodes,
56 57
    const std::unordered_map<int64_t, int64_t>& task_id_to_rank,
    const std::vector<std::string>& inference_root_scope_vars) {
58 59 60 61 62 63 64 65
  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));
66
    }
67
  }
68
  auto unused_vars = framework::GetUnusedVars(program_desc.Block(0), ops, {});
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  // NOTE: For inference, the vars in inference_root_scope_vars
  // shouldn't be deleted during inf, for that they may be the result of the
  // inf. If they are GCed, it will cause error during ZeroCopy the result.
  std::vector<const framework::OperatorBase*> changed_ops;
  for (auto pair : unused_vars) {
    const framework::OperatorBase* op = pair.first;
    std::vector<std::string> unused = pair.second;
    for (auto name : inference_root_scope_vars) {
      auto iter = std::find(unused.begin(), unused.end(), name);
      if (iter != unused.end()) {
        VLOG(3) << "Removing var: [" << name
                << "] from the unused vars list of op: [" << op->Type() << "]";
        unused.erase(iter);
        if (std::find(changed_ops.begin(), changed_ops.end(), op) ==
            changed_ops.end()) {
          // record the op whose unused vars have been updated
          changed_ops.emplace_back(op);
        }
      }
    }
    // update the unused vars list in the map
    unused_vars[op] = unused;
  }
  for (auto op : changed_ops) {
    auto iter = unused_vars.find(op);
    if (iter->second.empty()) {
      // remove those ops in the map that have empty unused vars list
      VLOG(3) << "Removing op: [" << op->Type() << "] from unused_vars map.";
      unused_vars.erase(iter);
    }
  }
100 101 102 103 104 105 106 107 108 109 110 111
  runtime_graph_ = std::make_shared<RuntimeGraph>();
  std::unordered_map<int64_t, TaskNode*> interceptor_id_to_task;
  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);
  }
  runtime_graph_->SetInterceptorIdToRank(task_id_to_rank);
  runtime_graph_->SetInterceptorIdToNode(interceptor_id_to_task);
  for (auto& unique_op : ops) {
    unique_op.release();
  }
112
  VLOG(5) << runtime_graph_->DebugString();
113 114 115
  Carrier* carrier =
      GlobalMap<std::string, Carrier>::Create(carrier_id, carrier_id);
  carrier_ids_.insert(carrier_id);
116 117
  // Set current running carrier
  GlobalVal<std::string>::Set(new std::string(carrier_id));
118 119
  InitCarrier(carrier, scope, place, num_micro_batches, program_desc,
              inference_root_scope_vars);
120
  GlobalVal<MessageBus>::Get()->Barrier();
121 122
}

123 124 125 126
void FleetExecutor::InitCarrier(
    Carrier* carrier, framework::Scope* scope, const platform::Place& place,
    int64_t num_micro_batches, const framework::ProgramDesc& program_desc,
    const std::vector<std::string>& inference_root_scope_vars) {
127
  carrier->Init(exe_desc_.cur_rank(), runtime_graph_->interceptor_id_to_rank(),
128
                runtime_graph_->interceptor_id_to_node(), program_desc, scope,
129
                num_micro_batches, place, inference_root_scope_vars);
130 131
}

132 133 134 135 136 137 138
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()) {
139
    // init the dns map
140 141 142 143 144 145 146 147
    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;
    }
  }
148 149
  if (addr == "") {
    PADDLE_ENFORCE_EQ(
150
        rank_to_addr.size(), 1,
151 152 153 154 155 156 157 158 159 160
        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()) << ".";
161
  VLOG(5) << ss.str();
162
  GlobalVal<MessageBus>::Get()->Init(cur_rank, rank_to_addr, addr);
L
LiYuRio 已提交
163 164
}

165
void FleetExecutor::Run(const std::string& carrier_id) {
166 167 168 169 170 171 172
  Carrier* carrier = GlobalMap<std::string, Carrier>::Get(carrier_id);
  // Set current running carrier
  if (*GlobalVal<std::string>::Get() != carrier_id) {
    GlobalVal<std::string>::Set(new std::string(carrier_id));
    GlobalVal<MessageBus>::Get()->Barrier();
  }
  carrier->Start();
L
LiYuRio 已提交
173 174 175 176
}

}  // namespace distributed
}  // namespace paddle