collective_helper.h 14.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//   Copyright (c) 2018 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

17
#include <map>
18 19 20 21 22
#include <memory>
#include <string>
#include <vector>

#include "paddle/fluid/framework/data_type.h"
23
#include "paddle/fluid/platform/device/npu/dynload/hccl.h"
24 25
#include "paddle/fluid/platform/device_context.h"
#include "paddle/fluid/platform/enforce.h"
R
Ruibiao Chen 已提交
26
#include "paddle/utils/variant.h"
27 28 29
#if defined(PADDLE_WITH_CNCL)
#include "paddle/fluid/platform/device/mlu/device_context.h"
#endif
30 31 32 33

namespace paddle {
namespace platform {

34
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
// In order to apply hierarchical communication with NCCL, we need
// a communication ring contains NCCL communicators associated to a global
// ncclUniqueId. E.g. for a hierarchical case,
//
//    11 - 12   21 - 22
//     |    |    |    |
//    13 - 14 - 23 - 24
//          |    |
//    31 - 32 - 41 - 42
//     |    |    |    |
//    33 - 34   43 - 44
//
// we group (14,23,32,41) as the top, and (11,12,13,14), (21,22,23,24),
// (31,32,33,34), (41,42,43,44) as bottoms respectively.
//
// We could also use a single communication ring for the flatten case
//
// The NCCLComm instance is created and reversed in the NCCLCommContext
// singleton with a global user specified group id.
54

55 56 57 58 59
class NCCLComm {
 public:
  virtual int ring_id() const = 0;
  virtual int nranks() const = 0;
  virtual int rank() const = 0;
60
  virtual int device_id() const = 0;
61
  virtual ncclComm_t comm() const = 0;
62
  virtual gpuStream_t stream() const = 0;
W
WangXi 已提交
63 64
  virtual gpuEvent_t compute_event() const = 0;
  virtual gpuEvent_t comm_event() const = 0;
L
Leo Chen 已提交
65
  virtual phi::GPUContext* dev_context() const = 0;
66 67 68
  virtual ~NCCLComm() = default;
};

69
// A singleton NCCL communicator context reserves communication ring ids
70 71
class NCCLCommContext {
 public:
S
sneaxiy 已提交
72
  static NCCLCommContext& Instance();
73

74 75
  NCCLComm* CreateComm(
      ncclUniqueId* nccl_id, int nranks, int rank, int dev_id, int ring_id = 0);
76

77 78
  void CreateAllNCCLComms(const std::vector<int>& dev_ids, int ring_id = 0);

Y
yaoxuefeng 已提交
79
  void CreateNCCLCommMultiTrainer(const std::vector<int>& dev_ids,
80 81 82
                                  ncclUniqueId* nccl_id,
                                  int nranks,
                                  int rank,
Y
yaoxuefeng 已提交
83 84
                                  int ring_id);

85 86
  // a latter comm with the same dev_id and the same ring_id
  // will override the former
87 88
  NCCLComm* AssignNCCLComm(
      ncclComm_t comm, int nranks, int rank, int dev_id, int ring_id = 0);
89

90
  // retrieve a communicator by the ring id in multiprocessing mode
91
  NCCLComm* Get(int ring_id) const {
G
GaoWei8 已提交
92
    PADDLE_ENFORCE_GT(
93 94
        comm_map_.count(ring_id),
        0,
G
GaoWei8 已提交
95
        platform::errors::InvalidArgument(
G
GaoWei8 已提交
96
            "Communicator in ring id %d has not been initialized.", ring_id));
97 98
    PADDLE_ENFORCE_EQ(comm_map_.at(ring_id).size(),
                      1,
G
GaoWei8 已提交
99 100 101
                      platform::errors::InvalidArgument(
                          "One device id should be specified to retrieve from "
                          "multiple communicators."));
102 103 104
    return comm_map_.at(ring_id).begin()->second.get();
  }

105 106 107 108 109 110 111 112 113 114 115
  int GetRingId(ncclComm_t comm) const {
    for (const auto& pair : comm_map_) {
      for (const auto& p : pair.second) {
        if (p.second.get()->comm() == comm) {
          return pair.first;
        }
      }
    }
    return -1;
  }

116 117
  // retrieve a communicator by the ring id and the device id
  NCCLComm* Get(int ring_id, int dev_id) const {
G
GaoWei8 已提交
118
    PADDLE_ENFORCE_GT(
119 120
        comm_map_.count(ring_id),
        0,
G
GaoWei8 已提交
121
        platform::errors::InvalidArgument(
G
GaoWei8 已提交
122
            "Communicator of ring id %d has not been initialized.", ring_id));
123
    PADDLE_ENFORCE_GT(
124 125
        comm_map_.at(ring_id).count(dev_id),
        0,
G
GaoWei8 已提交
126
        platform::errors::InvalidArgument(
G
GaoWei8 已提交
127
            "Communicator at device id %d has not been initialized in ring %d.",
128 129
            dev_id,
            ring_id));
130 131 132 133 134
    return comm_map_.at(ring_id).at(dev_id).get();
  }

  // retrieve a communicator by the ring id and place
  NCCLComm* Get(int ring_id, Place place) const {
135
    return Get(ring_id, place.device);
136 137 138
  }

 private:
139 140 141 142
  std::once_flag once_flag_;
  std::mutex comm_map_mutex_;
  // ring id to dev-NCCLComm
  std::map<int, std::map<int, std::unique_ptr<NCCLComm>>> comm_map_;
143

144
  void ReleaseNCCLComms();
145 146

  NCCLCommContext() = default;
147
  DISABLE_COPY_AND_ASSIGN(NCCLCommContext);
148
};
149
#endif
150

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
#if defined(PADDLE_WITH_ASCEND_CL)
// In order to apply hierarchical communication with HCCL, we need
// a communication ring contains HCCL communicators associated to a global
// HCCLUniqueId. E.g. for a hierarchical case,
//
//    11 - 12   21 - 22
//     |    |    |    |
//    13 - 14 - 23 - 24
//          |    |
//    31 - 32 - 41 - 42
//     |    |    |    |
//    33 - 34   43 - 44
//
// we group (14,23,32,41) as the top, and (11,12,13,14), (21,22,23,24),
// (31,32,33,34), (41,42,43,44) as bottoms respectively.
//
// We could also use a single communication ring for the flatten case
//
// The HCCLComm instance is created and reversed in the HCCLCommContext
// singleton with a global user specified group id.
class NPUDeviceContext;

#define ENV_RANK_TABLE_FILE "RANK_TABLE_FILE"
#define ENV_RANK_ID "PADDLE_TRAINER_ID"

class HCCLComm {
 public:
  virtual int ring_id() const = 0;
  virtual int nranks() const = 0;
  virtual int rank() const = 0;
  virtual int device_id() const = 0;
  virtual HcclComm comm() const = 0;
  virtual aclrtStream stream() const = 0;
  virtual NPUDeviceContext* dev_context() const = 0;
  virtual ~HCCLComm() = default;
};

// A singleton HCCL communicator context reserves communication ring ids
class HCCLCommContext {
 public:
  static HCCLCommContext& Instance() {
    static HCCLCommContext comm_ctx;
    return comm_ctx;
  }

196 197
  HCCLComm* CreateHCCLComm(
      HcclRootInfo* hccl_id, int nranks, int rank, int dev_id, int ring_id);
198 199
  // a latter comm with the same dev_id and the same ring_id
  // will override the former
200 201
  HCCLComm* AssignHCCLComm(
      HcclComm comm, int nranks, int rank, int dev_id, int ring_id);
202 203 204 205

  // retrieve a communicator by the ring id in multiprocessing mode
  HCCLComm* Get(int ring_id) const {
    PADDLE_ENFORCE_GT(
206 207
        comm_map_.count(ring_id),
        0,
208 209
        platform::errors::InvalidArgument(
            "Communicator in ring id %d has not been initialized.", ring_id));
210 211
    PADDLE_ENFORCE_EQ(comm_map_.at(ring_id).size(),
                      1,
212 213 214 215 216 217 218 219 220
                      platform::errors::InvalidArgument(
                          "One device id should be specified to retrieve from "
                          "multiple communicators."));
    return comm_map_.at(ring_id).begin()->second.get();
  }

  // retrieve a communicator by the ring id and the device id
  HCCLComm* Get(int ring_id, int dev_id) const {
    PADDLE_ENFORCE_GT(
221 222
        comm_map_.count(ring_id),
        0,
223 224 225
        platform::errors::InvalidArgument(
            "Communicator of ring id %d has not been initialized.", ring_id));
    PADDLE_ENFORCE_GT(
226 227
        comm_map_.at(ring_id).count(dev_id),
        0,
228 229
        platform::errors::InvalidArgument(
            "Communicator at device id %d has not been initialized in ring %d.",
230 231
            dev_id,
            ring_id));
232 233 234 235 236
    return comm_map_.at(ring_id).at(dev_id).get();
  }

  // retrieve a communicator by the ring id and place
  HCCLComm* Get(int ring_id, Place place) const {
237
    return Get(ring_id, place.device);
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
  }

 private:
  // Init global hcom
  HCCLCommContext() {}
  // we may use group feature in the feature
  // HCCLCommContext() { InitHcomWorldGroup(); }

  HcclComm comm_;

 public:
  ~HCCLCommContext() {}

  std::once_flag once_flag_;
  std::mutex comm_map_mutex_;
  // ring id to dev-HCCLComm
  std::map<int, std::map<int, std::unique_ptr<HCCLComm>>> comm_map_;

  // void InitHcomWorldGroup();
  void ReleaseHCCLComms();

  DISABLE_COPY_AND_ASSIGN(HCCLCommContext);
};
#endif

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
#if defined(PADDLE_WITH_XPU_BKCL)
// In order to apply hierarchical communication with BKCL, we need
// a communication ring contains BKCL communicators associated to a global
// BKCLUniqueId. E.g. for a hierarchical case,
//
//    11 - 12   21 - 22
//     |    |    |    |
//    13 - 14 - 23 - 24
//          |    |
//    31 - 32 - 41 - 42
//     |    |    |    |
//    33 - 34   43 - 44
//
// we group (14,23,32,41) as the top, and (11,12,13,14), (21,22,23,24),
// (31,32,33,34), (41,42,43,44) as bottoms respectively.
//
// We could also use a single communication ring for the flatten case
//
// The BKCLComm instance is created and reversed in the BKCLCommContext
// singleton with a global user specified group id.
class BKCLComm {
 public:
  virtual int ring_id() const = 0;
  virtual int nranks() const = 0;
  virtual int rank() const = 0;
  virtual int device_id() const = 0;
  virtual BKCLContext_t comm() const = 0;
  virtual XPUStream stream() const = 0;
  virtual XPUDeviceContext* dev_context() const = 0;
  virtual ~BKCLComm() = default;
};

// A singleton BKCL communicator context reserves communication ring ids
class BKCLCommContext {
 public:
  static BKCLCommContext& Instance() {
    static BKCLCommContext comm_ctx;
    return comm_ctx;
  }

303 304
  BKCLComm* CreateComm(
      BKCLUniqueId* bkcl_id, int nranks, int rank, int dev_id, int ring_id = 0);
305 306 307 308 309

  void CreateAllBKCLComms(const std::vector<int>& dev_ids, int ring_id = 0);

  // a latter comm with the same dev_id and the same ring_id
  // will override the former
310 311
  BKCLComm* AssignBKCLComm(
      BKCLContext_t comm, int nranks, int rank, int dev_id, int ring_id = 0);
312

313 314 315
  // retrieve a communicator by the ring id in multiprocessing mode
  BKCLComm* Get(int ring_id) const {
    PADDLE_ENFORCE_GT(
316 317
        comm_map_.count(ring_id),
        0,
318 319
        platform::errors::InvalidArgument(
            "Communicator in ring id %d has not been initialized.", ring_id));
320 321
    PADDLE_ENFORCE_EQ(comm_map_.at(ring_id).size(),
                      1,
322 323 324 325 326 327 328 329 330
                      platform::errors::InvalidArgument(
                          "One device id should be specified to retrieve from "
                          "multiple communicators."));
    return comm_map_.at(ring_id).begin()->second.get();
  }

  // retrieve a communicator by the ring id and the device id
  BKCLComm* Get(int ring_id, int dev_id) const {
    PADDLE_ENFORCE_GT(
331 332
        comm_map_.count(ring_id),
        0,
333 334 335
        platform::errors::InvalidArgument(
            "Communicator of ring id %d has not been initialized.", ring_id));
    PADDLE_ENFORCE_GT(
336 337
        comm_map_.at(ring_id).count(dev_id),
        0,
338 339
        platform::errors::InvalidArgument(
            "Communicator at device id %d has not been initialized in ring %d.",
340 341
            dev_id,
            ring_id));
342 343 344 345 346
    return comm_map_.at(ring_id).at(dev_id).get();
  }

  // retrieve a communicator by the ring id and place
  BKCLComm* Get(int ring_id, Place place) const {
347
    return Get(ring_id, place.device);
348 349 350 351 352 353 354 355 356 357 358 359 360
  }

 private:
  std::once_flag once_flag_;
  std::mutex comm_map_mutex_;
  // ring id to dev-BKCLComm
  std::map<int, std::map<int, std::unique_ptr<BKCLComm>>> comm_map_;

  void ReleaseBKCLComms();

  BKCLCommContext() = default;
  DISABLE_COPY_AND_ASSIGN(BKCLCommContext);
};
361
#endif
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
#if defined(PADDLE_WITH_CNCL)
// In order to apply hierarchical communication with CNCL, we need
// a communication ring contains CNCL communicators associated to a global
// cnclUniqueId. E.g. for a hierarchical case,
//
//    11 - 12   21 - 22
//     |    |    |    |
//    13 - 14 - 23 - 24
//          |    |
//    31 - 32 - 41 - 42
//     |    |    |    |
//    33 - 34   43 - 44
//
// we group (14,23,32,41) as the top, and (11,12,13,14), (21,22,23,24),
// (31,32,33,34), (41,42,43,44) as bottoms respectively.
//
// We could also use a single communication ring for the flatten case
//
// The CNCLComm instance is created and reversed in the CNCLCommContext
// singleton with a global user specified group id.
class MLUDeviceContext;

class CNCLComm {
 public:
  virtual int ring_id() const = 0;
  virtual int nranks() const = 0;
  virtual int rank() const = 0;
  virtual int device_id() const = 0;
  virtual cnclComm_t comm() const = 0;
  virtual mluStream stream() const = 0;
  virtual MLUDeviceContext* dev_context() const = 0;
  virtual ~CNCLComm() = default;
};

// A singleton CNCL communicator context reserves communication ring ids
class CNCLCommContext {
 public:
  static CNCLCommContext& Instance() {
    static CNCLCommContext comm_ctx;
    return comm_ctx;
  }

405 406
  CNCLComm* CreateComm(
      cnclCliqueId* cncl_id, int nranks, int rank, int dev_id, int ring_id = 0);
407 408 409 410
  void CreateAllCNCLComms(const std::vector<int>& dev_ids, int ring_id = 0);

  // a latter comm with the same dev_id and the same ring_id
  // will override the former
411 412
  CNCLComm* AssignCNCLComm(
      cnclComm_t comm, int nranks, int rank, int dev_id, int ring_id = 0);
413 414 415 416

  // retrieve a communicator by the ring id in multiprocessing mode
  CNCLComm* Get(int ring_id) const {
    PADDLE_ENFORCE_GT(
417 418
        comm_map_.count(ring_id),
        0,
419 420
        platform::errors::InvalidArgument(
            "Communicator in ring id %d has not been initialized.", ring_id));
421 422
    PADDLE_ENFORCE_EQ(comm_map_.at(ring_id).size(),
                      1,
423 424 425 426 427 428 429 430 431
                      platform::errors::InvalidArgument(
                          "One device id should be specified to retrieve from "
                          "multiple communicators."));
    return comm_map_.at(ring_id).begin()->second.get();
  }

  // retrieve a communicator by the ring id and the device id
  CNCLComm* Get(int ring_id, int dev_id) const {
    PADDLE_ENFORCE_GT(
432 433
        comm_map_.count(ring_id),
        0,
434 435 436
        platform::errors::InvalidArgument(
            "Communicator of ring id %d has not been initialized.", ring_id));
    PADDLE_ENFORCE_GT(
437 438
        comm_map_.at(ring_id).count(dev_id),
        0,
439 440
        platform::errors::InvalidArgument(
            "Communicator at device id %d has not been initialized in ring %d.",
441 442
            dev_id,
            ring_id));
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
    return comm_map_.at(ring_id).at(dev_id).get();
  }

  // retrieve a communicator by the ring id and place
  CNCLComm* Get(int ring_id, Place place) const {
    return Get(ring_id, place.device);
  }

 private:
  std::once_flag once_flag_;
  std::mutex comm_map_mutex_;
  // ring id to dev-CNCLComm
  std::map<int, std::map<int, std::unique_ptr<CNCLComm>>> comm_map_;

  void ReleaseCNCLComms();

  CNCLCommContext() = default;
  DISABLE_COPY_AND_ASSIGN(CNCLCommContext);
};

#endif

465 466
}  // namespace platform
}  // namespace paddle