collective_helper.h 11.0 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
#include <memory>
#include <string>
#include <vector>
21
#include <atomic>
22 23

#include "boost/variant.hpp"
24
#include "paddle/fluid/platform/enforce.h"
25
#include "paddle/fluid/framework/data_type.h"
26
#include "paddle/fluid/platform/dynload/hccl.h"
27 28 29 30 31
#include "paddle/fluid/platform/device_context.h"

namespace paddle {
namespace platform {

32
#if defined(PADDLE_WITH_NCCL)
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
// 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.
52 53
class CUDADeviceContext;

54 55 56 57 58
class NCCLComm {
 public:
  virtual int ring_id() const = 0;
  virtual int nranks() const = 0;
  virtual int rank() const = 0;
59
  virtual int device_id() const = 0;
60 61
  virtual ncclComm_t comm() const = 0;
  virtual cudaStream_t stream() const = 0;
62
  virtual CUDADeviceContext* dev_context() const = 0;
63 64 65
  virtual ~NCCLComm() = default;
};

66
// A singleton NCCL communicator context reserves communication ring ids
67 68 69 70 71 72 73 74 75 76
class NCCLCommContext {
 public:
  static NCCLCommContext& Instance() {
    static NCCLCommContext comm_ctx;
    return comm_ctx;
  }

  NCCLComm* CreateNCCLComm(ncclUniqueId* nccl_id, int nranks, int rank,
                           int dev_id, int ring_id = 0);

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

79 80 81 82 83
  // a latter comm with the same dev_id and the same ring_id
  // will override the former
  NCCLComm* AssignNCCLComm(ncclComm_t comm, int nranks, int rank, int dev_id,
                           int ring_id = 0);

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

  // retrieve a communicator by the ring id and the device id
  NCCLComm* Get(int ring_id, int dev_id) const {
G
GaoWei8 已提交
99 100 101
    PADDLE_ENFORCE_GT(
        comm_map_.count(ring_id), 0,
        platform::errors::InvalidArgument(
G
GaoWei8 已提交
102
            "Communicator of ring id %d has not been initialized.", ring_id));
103 104
    PADDLE_ENFORCE_GT(
        comm_map_.at(ring_id).count(dev_id), 0,
G
GaoWei8 已提交
105
        platform::errors::InvalidArgument(
G
GaoWei8 已提交
106
            "Communicator at device id %d has not been initialized in ring %d.",
G
GaoWei8 已提交
107
            dev_id, ring_id));
108 109 110 111 112
    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 {
113
    return Get(ring_id, BOOST_GET_CONST(CUDAPlace, place).device);
114 115 116
  }

 private:
117 118 119 120
  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_;
121

122
  void ReleaseNCCLComms();
123 124

  NCCLCommContext() = default;
125
  DISABLE_COPY_AND_ASSIGN(NCCLCommContext);
126
};
127
#endif
128

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
#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;

151
#define ENV_RANK_TABLE_FILE "RANK_TABLE_FILE"
152
#define ENV_RANK_ID "PADDLE_TRAINER_ID"
153

154 155
class HCCLComm {
 public:
156 157 158 159
  virtual int ring_id() const = 0;
  virtual int nranks() const = 0;
  virtual int rank() const = 0;
  virtual int device_id() const = 0;
L
lw921014 已提交
160
  virtual HcclComm comm() const = 0;
161 162 163 164 165 166 167 168 169 170 171 172 173
  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;
  }

L
lw921014 已提交
174 175
  HCCLComm* CreateHCCLComm(HcclRootInfo* hccl_id, int nranks,
                          int rank, int dev_id, int ring_id);
176 177
  // a latter comm with the same dev_id and the same ring_id
  // will override the former
L
lw921014 已提交
178 179
  HCCLComm* AssignHCCLComm(HcclComm comm, int nranks, int rank,
                                          int dev_id, int ring_id);
180 181 182 183 184 185 186 187 188 189 190 191 192

  // retrieve a communicator by the ring id in multiprocessing mode
  HCCLComm* Get(int ring_id) const {
    PADDLE_ENFORCE_GT(
        comm_map_.count(ring_id), 0,
        platform::errors::InvalidArgument(
            "Communicator in ring id %d has not been initialized.", ring_id));
    PADDLE_ENFORCE_EQ(comm_map_.at(ring_id).size(), 1,
                      platform::errors::InvalidArgument(
                          "One device id should be specified to retrieve from "
                          "multiple communicators."));
    return comm_map_.at(ring_id).begin()->second.get();
  }
193

194 195 196 197 198 199 200 201 202 203 204 205 206
  // retrieve a communicator by the ring id and the device id
  HCCLComm* Get(int ring_id, int dev_id) const {
    PADDLE_ENFORCE_GT(
        comm_map_.count(ring_id), 0,
        platform::errors::InvalidArgument(
            "Communicator of ring id %d has not been initialized.", ring_id));
    PADDLE_ENFORCE_GT(
        comm_map_.at(ring_id).count(dev_id), 0,
        platform::errors::InvalidArgument(
            "Communicator at device id %d has not been initialized in ring %d.",
            dev_id, ring_id));
    return comm_map_.at(ring_id).at(dev_id).get();
  }
207 208

  // retrieve a communicator by the ring id and place
209 210
  HCCLComm* Get(int ring_id, Place place) const {
    return Get(ring_id, BOOST_GET_CONST(NPUPlace, place).device);
211
  }
212

213

214
 private:
215
  // Init global hcom
L
lw921014 已提交
216 217 218
  HCCLCommContext() {}
  // we may use group feature in the feature
  // HCCLCommContext() { InitHcomWorldGroup(); }
219

L
lw921014 已提交
220
  HcclComm comm_;
221

222
public:
L
lw921014 已提交
223
  ~HCCLCommContext(){ }
224

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

L
lw921014 已提交
230
  // void InitHcomWorldGroup();
231
  void ReleaseHCCLComms();
232 233 234 235 236 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

  DISABLE_COPY_AND_ASSIGN(HCCLCommContext);
};
#endif

#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;
  }

  BKCLComm* CreateBKCLComm(BKCLUniqueId* bkcl_id, int nranks, int rank,
                           int dev_id, int ring_id = 0);

  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
  BKCLComm* AssignBKCLComm(BKCLContext_t comm, int nranks, int rank, int dev_id,
                           int ring_id = 0);

  // retrieve a communicator by the ring id in multiprocessing mode
  BKCLComm* Get(int ring_id) const {
    PADDLE_ENFORCE_GT(
        comm_map_.count(ring_id), 0,
        platform::errors::InvalidArgument(
            "Communicator in ring id %d has not been initialized.", ring_id));
    PADDLE_ENFORCE_EQ(comm_map_.at(ring_id).size(), 1,
                      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(
        comm_map_.count(ring_id), 0,
        platform::errors::InvalidArgument(
            "Communicator of ring id %d has not been initialized.", ring_id));
    PADDLE_ENFORCE_GT(
        comm_map_.at(ring_id).count(dev_id), 0,
        platform::errors::InvalidArgument(
            "Communicator at device id %d has not been initialized in ring %d.",
            dev_id, ring_id));
    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 {
    return Get(ring_id, BOOST_GET_CONST(XPUPlace, place).device);
  }

 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_;
324

325 326 327 328 329
  void ReleaseBKCLComms();

  BKCLCommContext() = default;
  DISABLE_COPY_AND_ASSIGN(BKCLCommContext);
};
330
#endif
331 332 333

}  // namespace platform
}  // namespace paddle