process_group.h 18.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Copyright (c) 2022 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.

#pragma once

#include <chrono>
#include <memory>
#include <string>
20
#include <unordered_map>
21 22
#include <vector>

W
Wen Sun 已提交
23
#include "paddle/fluid/distributed/collective/types.h"
24 25 26 27 28
#include "paddle/fluid/eager/api/utils/tensor_utils.h"  // NOTE: this header is required somewhere
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/device_context.h"
#include "paddle/phi/core/enforce.h"
#include "paddle/phi/core/errors.h"
29 30 31 32 33 34

constexpr auto kWaitTimeout = std::chrono::milliseconds(0);

namespace paddle {
namespace distributed {

35
constexpr int kIgnoreId = -1;
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

enum class CommType : std::uint8_t {
  BROADCAST = 0,
  ALLREDUCE = 1,
  ALLREDUCE_SPARSE = 2,  // TODO(shenliang03): to support sparse in allreduce
  REDUCE = 3,
  ALLGATHER = 4,
  GATHER = 5,
  SCATTER = 6,
  REDUCE_SCATTER = 7,
  ALLTOALL = 8,
  SEND = 9,
  RECV = 10,
  BARRIER = 11,
  UNKNOWN = 100,
};

class ProcessGroup {
 public:
  class Task {
   public:
57 58 59
    Task(int rank, CommType comm_type, bool sync_op)
        : rank_(rank), comm_type_(comm_type), sync_op_(sync_op) {}
    virtual ~Task() = default;
60 61

    virtual bool IsCompleted();
62 63 64 65 66
    virtual bool Wait(std::chrono::milliseconds timeout = kWaitTimeout) {
      return false;
    }
    virtual void Synchronize() {}
    virtual void UpdateWaitChain(const phi::DeviceContext& ctx) {}
67
    bool IsSync() const { return sync_op_; }
68

69 70 71
    // TODO(sunyilun): methods below will be removed later
    Task(int rank,
         const std::vector<phi::DenseTensor>& inputs,
72 73
         CommType comm_type)
        : rank_(rank), comm_type_(comm_type) {}
74 75 76
    Task(int rank,
         const std::vector<phi::DenseTensor>& inputs,
         CommType comm_type,
77 78
         bool sync_op)
        : rank_(rank), comm_type_(comm_type), sync_op_(sync_op) {}
79

80 81
   protected:
    const int rank_;
82
    CommType comm_type_{CommType::UNKNOWN};
83
    std::mutex mutex_;
84 85 86 87
    bool is_completed_{false};

   private:
    bool sync_op_{true};
88 89
  };

90
 public:
L
LiYuRio 已提交
91
  ProcessGroup(int rank, int size, int gid);
92
  virtual ~ProcessGroup() = default;
W
wuhuachaocoding 已提交
93

94 95 96 97
  int GetRank() const { return rank_; }

  int GetSize() const { return size_; }

L
LiYuRio 已提交
98
  virtual std::string GetBackendName() const = 0;
99

100
  virtual phi::DeviceContext* GetDeviceContext(const Place& place) const {
101
    PADDLE_THROW(phi::errors::Unimplemented(
102
        "ProcessGroup%s does not support get device_context.",
L
LiYuRio 已提交
103 104
        GetBackendName()));
  }
105

106 107 108 109 110 111 112 113
  virtual phi::DeviceContext* GetDeviceContext(const Place& place,
                                               bool use_calc_stream) const {
    PADDLE_THROW(phi::errors::Unimplemented(
        "ProcessGroup%s does not support get device_context.",
        GetBackendName()));
  }

  // without stream APIs
W
Wen Sun 已提交
114 115 116 117
  virtual std::shared_ptr<ProcessGroup::Task> AllGather(
      phi::DenseTensor* out_tensor,
      const phi::DenseTensor& in_tensor,
      bool sync_op) {
118 119 120
    PADDLE_THROW(phi::errors::Unimplemented(
        "ProcessGroup%s does not support all_gather with sync_op flag.",
        GetBackendName()));
W
Wen Sun 已提交
121 122
  }

123 124 125
  virtual std::shared_ptr<ProcessGroup::Task> AllGather(
      phi::DenseTensor* out_tensor,
      const phi::DenseTensor& in_tensor,
126 127
      int64_t offset,
      int64_t numel,
128
      bool sync_op) {
129
    PADDLE_THROW(phi::errors::Unimplemented(
130
        "ProcessGroup%s does not support all_gather with sync_op flag.",
131 132 133 134 135 136 137 138
        GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> AllReduce(
      phi::DenseTensor* out_tensor,
      const phi::DenseTensor& in_tensor,
      const AllreduceOptions& opts,
      bool sync_op) {
139
    PADDLE_THROW(phi::errors::Unimplemented(
140
        "ProcessGroup%s does not support all_reduce with sync_op flag.",
141 142 143
        GetBackendName()));
  }

144 145 146 147 148 149
  virtual std::shared_ptr<ProcessGroup::Task> AllToAll(
      phi::DenseTensor* out_tensor,
      const phi::DenseTensor& in_tensor,
      const std::vector<int64_t>& out_size_each_rank,
      const std::vector<int64_t>& in_size_each_rank,
      bool sync_op) {
150
    PADDLE_THROW(phi::errors::Unimplemented(
151 152 153 154
        "ProcessGroup%s does not support all_to_all with sync_op flag.",
        GetBackendName()));
  }

155 156
  virtual std::shared_ptr<ProcessGroup::Task> Barrier(
      const BarrierOptions& = BarrierOptions()) {
157
    PADDLE_THROW(phi::errors::Unimplemented(
158
        "ProcessGroup%s does not support barrier.", GetBackendName()));
159 160 161 162 163 164 165
  }

  virtual std::shared_ptr<ProcessGroup::Task> Broadcast(
      phi::DenseTensor* out_tensor,
      const phi::DenseTensor& in_tensor,
      const BroadcastOptions& opts,
      bool sync_op) {
166
    PADDLE_THROW(phi::errors::Unimplemented(
167 168 169 170
        "ProcessGroup%s does not support broadcast with sync_op flag",
        GetBackendName()));
  }

171 172 173 174 175
  virtual std::shared_ptr<ProcessGroup::Task> Reduce(
      phi::DenseTensor* out_tensor,
      const phi::DenseTensor& in_tensor,
      const ReduceOptions& opts,
      bool sync_op) {
176
    PADDLE_THROW(phi::errors::Unimplemented(
177 178 179 180 181 182 183 184 185
        "ProcessGroup%s does not support reduce with sync_op flag.",
        GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> ReduceScatter(
      phi::DenseTensor* out_tensor,
      const phi::DenseTensor& in_tensor,
      const ReduceScatterOptions& opts,
      bool sync_op) {
186
    PADDLE_THROW(phi::errors::Unimplemented(
187 188 189 190 191 192 193 194 195
        "ProcessGroup%s does not support reduce_scatter with sync_op flag.",
        GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> Scatter(
      phi::DenseTensor* out_tensor,
      const phi::DenseTensor& in_tensor,
      const ScatterOptions& opts,
      bool sync_op) {
196
    PADDLE_THROW(phi::errors::Unimplemented(
197 198 199 200
        "ProcessGroup%s does not support scatter with sync_op flag.",
        GetBackendName()));
  }

W
Wen Sun 已提交
201 202 203
  virtual std::shared_ptr<ProcessGroup::Task> Recv(phi::DenseTensor* tensor,
                                                   int src_rank,
                                                   bool sync_op) {
204 205 206
    PADDLE_THROW(phi::errors::Unimplemented(
        "ProcessGroup%s does not support recv with sync_op flag.",
        GetBackendName()));
W
Wen Sun 已提交
207 208
  }

209 210
  virtual std::shared_ptr<ProcessGroup::Task> Recv(phi::DenseTensor* tensor,
                                                   int src_rank,
211 212
                                                   int64_t offset,
                                                   int64_t numel,
213
                                                   bool sync_op) {
214
    PADDLE_THROW(phi::errors::Unimplemented(
215
        "ProcessGroup%s does not support recv with sync_op flag.",
216 217 218
        GetBackendName()));
  }

W
Wen Sun 已提交
219 220
  virtual std::shared_ptr<ProcessGroup::Task> Send(
      const phi::DenseTensor& tensor, int dst_rank, bool sync_op) {
221 222 223
    PADDLE_THROW(phi::errors::Unimplemented(
        "ProcessGroup%s does not support send with sync_op flag.",
        GetBackendName()));
W
Wen Sun 已提交
224 225
  }

226 227 228 229 230 231
  virtual std::shared_ptr<ProcessGroup::Task> Send(
      const phi::DenseTensor& tensor,
      int dst_rank,
      int64_t offset,
      int64_t numel,
      bool sync_op) {
232
    PADDLE_THROW(phi::errors::Unimplemented(
233
        "ProcessGroup%s does not support send with sync_op flag.",
234 235 236
        GetBackendName()));
  }

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
  // stream APIs
  virtual std::shared_ptr<ProcessGroup::Task> AllGather(
      phi::DenseTensor* out_tensor,
      const phi::DenseTensor& in_tensor,
      bool sync_op,
      bool use_calc_stream) {
    PADDLE_THROW(
        phi::errors::Unimplemented("ProcessGroup%s does not support all_gather "
                                   "with sync_op and use_calc_stream flag.",
                                   GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> AllGather(
      phi::DenseTensor* out_tensor,
      const phi::DenseTensor& in_tensor,
      int64_t offset,
      int64_t numel,
      bool sync_op,
      bool use_calc_stream) {
    PADDLE_THROW(
        phi::errors::Unimplemented("ProcessGroup%s does not support all_gather "
                                   "with sync_op and use_calc_stream flag.",
                                   GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> AllReduce(
      phi::DenseTensor* out_tensor,
      const phi::DenseTensor& in_tensor,
      const AllreduceOptions& opts,
      bool sync_op,
      bool use_calc_stream) {
    PADDLE_THROW(
        phi::errors::Unimplemented("ProcessGroup%s does not support all_reduce "
                                   "with sync_op and use_calc_stream flag.",
                                   GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> AllToAll(
      phi::DenseTensor* out_tensor,
      const phi::DenseTensor& in_tensor,
      const std::vector<int64_t>& out_size_each_rank,
      const std::vector<int64_t>& in_size_each_rank,
      bool sync_op,
      bool use_calc_stream) {
    PADDLE_THROW(
        phi::errors::Unimplemented("ProcessGroup%s does not support all_to_all "
                                   "with sync_op and use_calc_stream flag.",
                                   GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> Broadcast(
      phi::DenseTensor* out_tensor,
      const phi::DenseTensor& in_tensor,
      const BroadcastOptions& opts,
      bool sync_op,
      bool use_calc_stream) {
    PADDLE_THROW(
        phi::errors::Unimplemented("ProcessGroup%s does not support broadcast "
                                   "with sync_op and use_calc_stream flag.",
                                   GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> Reduce(
      phi::DenseTensor* out_tensor,
      const phi::DenseTensor& in_tensor,
      const ReduceOptions& opts,
      bool sync_op,
      bool use_calc_stream) {
    PADDLE_THROW(
        phi::errors::Unimplemented("ProcessGroup%s does not support reduce "
                                   "with sync_op and use_calc_stream flag.",
                                   GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> ReduceScatter(
      phi::DenseTensor* out_tensor,
      const phi::DenseTensor& in_tensor,
      const ReduceScatterOptions& opts,
      bool sync_op,
      bool use_calc_stream) {
    PADDLE_THROW(phi::errors::Unimplemented(
        "ProcessGroup%s does not support reduce_scatter "
        "with sync_op and use_calc_stream flag.",
        GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> Scatter(
      phi::DenseTensor* out_tensor,
      const phi::DenseTensor& in_tensor,
      const ScatterOptions& opts,
      bool sync_op,
      bool use_calc_stream) {
    PADDLE_THROW(
        phi::errors::Unimplemented("ProcessGroup%s does not support scatter "
                                   "with sync_op and use_calc_stream flag.",
                                   GetBackendName()));
  }

zhenhailiu's avatar
zhenhailiu 已提交
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
  virtual std::shared_ptr<ProcessGroup::Task> Gather(
      phi::DenseTensor* out_tensor,
      const phi::DenseTensor& in_tensor,
      const GatherOptions& opts,
      bool sync_op,
      bool use_calc_stream) {
    PADDLE_THROW(
        phi::errors::Unimplemented("ProcessGroup%s does not support gather "
                                   "with sync_op and use_calc_stream flag.",
                                   GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> Gather(
      std::vector<phi::DenseTensor>* gather_tensors_ptr,
      const phi::DenseTensor& in_tensor,
      const GatherOptions& opts,
      bool sync_op,
      bool use_calc_stream) {
    PADDLE_THROW(
        phi::errors::Unimplemented("ProcessGroup%s does not support gather "
                                   "with sync_op and use_calc_stream flag.",
                                   GetBackendName()));
  }

359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
  virtual std::shared_ptr<ProcessGroup::Task> Recv(phi::DenseTensor* tensor,
                                                   int src_rank,
                                                   bool sync_op,
                                                   bool use_calc_stream) {
    PADDLE_THROW(
        phi::errors::Unimplemented("ProcessGroup%s does not support recv with "
                                   "sync_op and use_calc_stream flag.",
                                   GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> Recv(phi::DenseTensor* tensor,
                                                   int src_rank,
                                                   int64_t offset,
                                                   int64_t numel,
                                                   bool sync_op,
                                                   bool use_calc_stream) {
    PADDLE_THROW(
        phi::errors::Unimplemented("ProcessGroup%s does not support recv "
                                   "with sync_op and use_calc_stream flag.",
                                   GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> Send(
      const phi::DenseTensor& tensor,
      int dst_rank,
      bool sync_op,
      bool use_calc_stream) {
    PADDLE_THROW(
        phi::errors::Unimplemented("ProcessGroup%s does not support send "
                                   "with sync_op and use_calc_stream flag.",
                                   GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> Send(
      const phi::DenseTensor& tensor,
      int dst_rank,
      int64_t offset,
      int64_t numel,
      bool sync_op,
      bool use_calc_stream) {
    PADDLE_THROW(
        phi::errors::Unimplemented("ProcessGroup%s does not support send "
                                   "with sync_op and use_calc_stream flag.",
                                   GetBackendName()));
  }

  // legacy APIs
406
  // TODO(liyurui): This API will be moved later
407
  virtual std::shared_ptr<ProcessGroup::Task> AllReduce(
408 409
      std::vector<phi::DenseTensor>& /* input tensors */,   // NOLINT
      std::vector<phi::DenseTensor>& /* output tensors */,  // NOLINT
410
      const AllreduceOptions& = AllreduceOptions()) {
411
    PADDLE_THROW(phi::errors::InvalidArgument(
412 413 414
        "ProcessGroup%s does not support allreduce", GetBackendName()));
  }

415 416 417 418 419
  virtual std::shared_ptr<ProcessGroup::Task> AllReduce(
      std::vector<phi::DenseTensor>& /* input tensors */,   // NOLINT
      std::vector<phi::DenseTensor>& /* output tensors */,  // NOLINT
      const AllreduceOptions&,
      bool) {
420
    PADDLE_THROW(phi::errors::InvalidArgument(
421 422 423 424
        "ProcessGroup%s does not support allreduce with sync_op flag",
        GetBackendName()));
  }

425
  // TODO(sunyilun): methods below will be removed later
426
  virtual std::shared_ptr<ProcessGroup::Task> Broadcast(
427 428
      std::vector<phi::DenseTensor>& /* input tensors */,   // NOLINT
      std::vector<phi::DenseTensor>& /* output tensors */,  // NOLINT
429
      const BroadcastOptions& = BroadcastOptions()) {
430
    PADDLE_THROW(phi::errors::InvalidArgument(
B
Baibaifan 已提交
431 432 433
        "ProcessGroup%s does not support broadcast", GetBackendName()));
  }

434 435 436 437 438
  virtual std::shared_ptr<ProcessGroup::Task> Broadcast(
      std::vector<phi::DenseTensor>& /* input tensors */,   // NOLINT
      std::vector<phi::DenseTensor>& /* output tensors */,  // NOLINT
      const BroadcastOptions&,
      bool) {
439
    PADDLE_THROW(phi::errors::InvalidArgument(
440 441 442 443
        "ProcessGroup%s does not support broadcast with sync_op flag",
        GetBackendName()));
  }

B
Baibaifan 已提交
444
  virtual std::shared_ptr<ProcessGroup::Task> Send(
445
      std::vector<phi::DenseTensor>&, int) {  // NOLINT
446
    PADDLE_THROW(phi::errors::InvalidArgument(
B
Baibaifan 已提交
447 448 449 450
        "ProcessGroup%s does not support send", GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> Recv(
451
      std::vector<phi::DenseTensor>&, int) {  // NOLINT
452
    PADDLE_THROW(phi::errors::InvalidArgument(
453
        "ProcessGroup%s does not support recv", GetBackendName()));
454 455
  }

456
  virtual std::shared_ptr<ProcessGroup::Task> AllGather(
457 458
      std::vector<phi::DenseTensor>&,    // NOLINT
      std::vector<phi::DenseTensor>&) {  // NOLINT
459
    PADDLE_THROW(phi::errors::InvalidArgument(
460 461 462 463 464 465 466
        "ProcessGroup%s does not support all_gather", GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> AllGather(
      std::vector<phi::DenseTensor>&,  // NOLINT
      std::vector<phi::DenseTensor>&,  // NOLINT
      bool) {
467
    PADDLE_THROW(phi::errors::InvalidArgument(
468 469
        "ProcessGroup%s does not support all_gather with sync_op flag",
        GetBackendName()));
470 471 472
  }

  virtual std::shared_ptr<ProcessGroup::Task> AllToAll(
473 474
      std::vector<phi::DenseTensor>&,    // NOLINT
      std::vector<phi::DenseTensor>&) {  // NOLINT
475
    PADDLE_THROW(phi::errors::InvalidArgument(
476 477 478 479
        "ProcessGroup%s does not support AllToAll", GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> Reduce(
480 481 482
      std::vector<phi::DenseTensor>&,  // NOLINT
      std::vector<phi::DenseTensor>&,  // NOLINT
      const ReduceOptions& opts) {
483
    PADDLE_THROW(phi::errors::InvalidArgument(
484 485 486
        "ProcessGroup%s does not support reduce", GetBackendName()));
  }

487
  virtual std::shared_ptr<ProcessGroup::Task> Scatter(
488 489
      std::vector<phi::DenseTensor>&,  // NOLINT
      std::vector<phi::DenseTensor>&,  // NOLINT
490
      const ScatterOptions&) {
491
    PADDLE_THROW(phi::errors::InvalidArgument(
492 493 494
        "ProcessGroup%s does not support scatter", GetBackendName()));
  }

495
 protected:
L
LiYuRio 已提交
496 497 498
  int rank_;
  int size_;
  int gid_;
499 500
};

L
LiYuRio 已提交
501 502 503 504
class ProcessGroupIdMap
    : public std::unordered_map<int, std::shared_ptr<ProcessGroup>> {
 public:
  static ProcessGroupIdMap& GetInstance();
505
  static void DestroyProcessGroup();
L
LiYuRio 已提交
506 507 508
};

// TODO(dev): The following method will be removed soon.
L
lilong12 已提交
509 510
class ProcessGroupMapFromGid {
 public:
511
  bool has(int gid) { return map_.find(gid) != map_.end(); }
L
lilong12 已提交
512

513
  void insert(int gid, ProcessGroup* pg) { map_[gid] = pg; }
L
lilong12 已提交
514

515
  ProcessGroup* get(int gid) { return map_.find(gid)->second; }
L
lilong12 已提交
516 517 518 519 520 521 522 523 524 525 526 527 528

  static std::shared_ptr<ProcessGroupMapFromGid> getInstance() {
    static auto s_instance = std::make_shared<ProcessGroupMapFromGid>();
    return s_instance;
  }

  ProcessGroupMapFromGid() = default;
  ~ProcessGroupMapFromGid() = default;

 private:
  std::unordered_map<int, ProcessGroup*> map_;
};

529 530
}  //  namespace distributed
}  //  namespace paddle