ProcessGroupNCCL.h 11.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// 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 <map>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>

24
#include "paddle/fluid/distributed/collective/ProcessGroupStream.h"
25
#include "paddle/fluid/distributed/store/store.h"
26 27 28 29 30 31
#include "paddle/fluid/platform/cuda_device_guard.h"
#include "paddle/fluid/platform/device_context.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/gen_comm_id_helper.h"
#include "paddle/fluid/platform/place.h"

S
ShenLiang 已提交
32
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
33
#include "paddle/fluid/distributed/collective/NCCLTools.h"
S
ShenLiang 已提交
34 35 36 37 38
#endif

#ifdef PADDLE_WITH_RCCL
#include "paddle/fluid/platform/dynload/rccl.h"
#else
39 40 41 42 43 44 45 46 47 48
#include "paddle/fluid/platform/dynload/nccl.h"
#endif

constexpr const char* NCCL_BACKEND_NAME = "NCCL";

namespace paddle {
namespace distributed {

using Place = paddle::platform::Place;

49
class ProcessGroupNCCL : public ProcessGroupStream {
50
 public:
51
  class NCCLTask : public ProcessGroupStream::TaskStream,
52 53
                   public std::enable_shared_from_this<NCCLTask> {
   public:
54 55 56
    NCCLTask(const std::vector<Place>& places,
             int rank,
             CommType CommType,
57
             const std::vector<phi::DenseTensor>& inputs);
58

59 60 61 62
    NCCLTask(const std::vector<Place>& places,
             int rank,
             CommType comm_type,
             const std::vector<phi::DenseTensor>& inputs,
63
             bool sync_op,
64 65
             bool use_calc_stream);

66 67 68 69 70 71 72 73
    bool IsCompleted();

    void SynchronizeStreams();

    bool Wait(std::chrono::milliseconds timeout = kWaitTimeout);

    void Synchronize();

74
    void SetOutputs(std::vector<phi::DenseTensor>& outputs);  // NOLINT
75 76 77

    virtual ~NCCLTask();

78 79
    void UpdateWaitChain(const phi::DeviceContext& ctx) override;

80
    std::vector<EventManager> control_events_;
81
    std::vector<phi::DenseTensor> barrierTensors_;
82 83 84 85

   protected:
    std::vector<Place> places_;
    std::vector<std::shared_ptr<NCCLCommManager>> ncclComms_;
86
    std::shared_ptr<std::vector<phi::DenseTensor>> outputs_;
87 88 89 90

   private:
  };

91 92 93 94 95
  ProcessGroupNCCL(const std::shared_ptr<Store>& store,
                   int rank,
                   int size,
                   const platform::Place& place,
                   int gid);
96 97 98 99 100

  const std::string GetBackendName() const override {
    return std::string(NCCL_BACKEND_NAME);
  }

101
  const phi::DeviceContext& GetDeviceContext(const Place& place) const override;
L
LiYuRio 已提交
102

103 104
  const phi::DeviceContext& GetDeviceContext(
      const Place& place, bool use_calc_stream) const override;
105

106 107 108 109 110 111 112 113
  std::shared_ptr<ProcessGroup::Task> AllReduce(
      std::vector<phi::DenseTensor>& in_tensors,   // NOLINT
      std::vector<phi::DenseTensor>& out_tensors,  // NOLINT
      const AllreduceOptions& options,
      bool sync_op,
      bool use_calc_stream) override;

  // TODO(liyurui): This API will be moved later
114
  std::shared_ptr<ProcessGroup::Task> AllReduce(
115 116
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors,
117 118 119
      const AllreduceOptions& = AllreduceOptions()) override;

  std::shared_ptr<ProcessGroup::Task> Broadcast(
120 121
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors,
122 123
      const BroadcastOptions& = BroadcastOptions()) override;

124 125 126 127 128 129 130
  std::shared_ptr<ProcessGroup::Task> Broadcast(
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors,
      const BroadcastOptions& opts,
      bool sync_op,
      bool use_calc_stream) override;

B
Baibaifan 已提交
131 132 133
  std::shared_ptr<ProcessGroup::Task> Barrier(
      const BarrierOptions& = BarrierOptions()) override;

134 135
  std::shared_ptr<ProcessGroup::Task> Send(
      std::vector<phi::DenseTensor>& tensors, int dst_rank) override;
B
Baibaifan 已提交
136

137 138 139 140 141 142
  std::shared_ptr<ProcessGroup::Task> Send(
      std::vector<phi::DenseTensor>& tensors,
      int dst_rank,
      bool sync_op,
      bool use_calc_stream) override;

143 144
  std::shared_ptr<ProcessGroup::Task> Recv(
      std::vector<phi::DenseTensor>& tensors, int src_rank) override;
B
Baibaifan 已提交
145

146 147 148 149 150 151
  std::shared_ptr<ProcessGroup::Task> Recv(
      std::vector<phi::DenseTensor>& tensors,
      int src_rank,
      bool sync_op,
      bool use_calc_stream) override;

152
  std::shared_ptr<ProcessGroup::Task> Send_Partial(phi::DenseTensor& tensors,
153
                                                   int dst_rank,
154 155
                                                   int64_t offset,
                                                   int64_t length) override;
156

157 158 159
  std::shared_ptr<ProcessGroup::Task> Send_Partial(
      phi::DenseTensor& tensors,
      int dst_rank,
160 161
      int64_t offset,
      int64_t length,
162 163 164
      bool sync_op,
      bool use_calc_stream) override;

165
  std::shared_ptr<ProcessGroup::Task> Recv_Partial(phi::DenseTensor& tensors,
166
                                                   int src_rank,
167 168
                                                   int64_t offset,
                                                   int64_t length) override;
169

170 171 172
  std::shared_ptr<ProcessGroup::Task> Recv_Partial(
      phi::DenseTensor& tensors,
      int src_rank,
173 174
      int64_t offset,
      int64_t length,
175 176 177
      bool sync_op,
      bool use_calc_stream) override;

178
  std::shared_ptr<ProcessGroup::Task> AllGather(
179 180
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors) override;
181

182 183 184 185 186 187
  std::shared_ptr<ProcessGroup::Task> AllGather(
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors,
      bool sync_op,
      bool use_calc_stream) override;

188 189 190
  std::shared_ptr<ProcessGroup::Task> AllGather_Partial(
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors,
191 192
      int64_t offset,
      int64_t length) override;
193

194 195 196
  std::shared_ptr<ProcessGroup::Task> AllGather_Partial(
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors,
197 198
      int64_t offset,
      int64_t length,
199 200 201
      bool sync_op,
      bool use_calc_stream) override;

202
  std::shared_ptr<ProcessGroup::Task> AllToAll(
203 204 205 206 207 208 209 210
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors) override;

  std::shared_ptr<ProcessGroup::Task> AllToAll(
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors,
      bool sync_op,
      bool use_calc_stream) override;
211

212 213 214 215 216 217
  std::shared_ptr<ProcessGroup::Task> AllToAll_Single(
      std::vector<phi::DenseTensor>& in,
      std::vector<phi::DenseTensor>& out,
      std::vector<int64_t>& in_sizes,
      std::vector<int64_t>& out_sizes) override;

218 219 220 221 222 223 224 225
  std::shared_ptr<ProcessGroup::Task> AllToAllSingle(
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors,
      std::vector<int64_t>& in_sizes,
      std::vector<int64_t>& out_sizes,
      bool sync_op,
      bool use_calc_stream) override;

226
  std::shared_ptr<ProcessGroup::Task> Reduce(
227 228 229
      std::vector<phi::DenseTensor>& tensors,
      std::vector<phi::DenseTensor>& out_tensors,
      const ReduceOptions& opts) override;
230

231 232 233 234 235 236 237 238 239 240 241 242 243 244
  std::shared_ptr<ProcessGroup::Task> Reduce(
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors,
      const ReduceOptions& opts,
      bool sync_op,
      bool use_calc_stream) override;

  std::shared_ptr<ProcessGroup::Task> ReduceScatter(
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors,
      const ReduceScatterOptions& opts,
      bool sync_op,
      bool use_calc_stream) override;

245 246 247
  std::shared_ptr<ProcessGroup::Task> Scatter(
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors,
248 249 250 251 252 253 254 255
      const ScatterOptions& opts) override;

  std::shared_ptr<ProcessGroup::Task> Scatter(
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors,
      const ScatterOptions& opts,
      bool sync_op,
      bool use_calc_stream) override;
256

257 258 259 260 261 262 263 264 265
  std::shared_ptr<ProcessGroup::Task> _ReduceScatterBase(
      phi::DenseTensor&,  // NOLINT
      phi::DenseTensor&,  // NOLINT
      const ReduceScatterOptions&) override;

  static void GroupStart();

  static void GroupEnd();

L
LiYuRio 已提交
266 267
  ncclComm_t NCCLComm(const Place& place) const;

268 269
 protected:
  virtual std::shared_ptr<ProcessGroupNCCL::NCCLTask> CreateTask(
270 271
      std::vector<Place> places,
      int rank,
272
      CommType op_type,
273
      const std::vector<phi::DenseTensor>& inputs);
274

275 276 277 278 279 280 281 282
  virtual std::shared_ptr<ProcessGroupNCCL::NCCLTask> CreateTask(
      const std::vector<Place>& places,
      int rank,
      CommType op_type,
      const std::vector<phi::DenseTensor>& inputs,
      bool sync_op,
      bool use_calc_stream);

283
 protected:
284
  std::shared_ptr<Store> store_;
285 286 287 288 289 290 291
  std::shared_ptr<NCCLCommManager> nccl_comm_;
  std::mutex mutex_;
  std::unordered_map<std::string, std::vector<std::shared_ptr<NCCLCommManager>>>
      places_to_ncclcomm_;

  std::unordered_map<std::string, std::vector<EventManager>> places_to_events_;

L
Leo Chen 已提交
292
  std::unordered_map<std::string, std::vector<std::unique_ptr<phi::GPUContext>>>
293 294
      places_to_ctx_;

B
Baibaifan 已提交
295 296
  std::set<int> used_place_ids_;

297
 private:
298 299
  void BcastNCCLId(std::vector<ncclUniqueId>& nccl_ids,  // NOLINT
                   int root,                             // NOLINT
300 301 302 303 304 305
                   int server_fd);

  void BroadcastUniqueNCCLID(std::vector<ncclUniqueId>& nccl_ids);  // NOLINT

  template <typename Fn>
  std::shared_ptr<ProcessGroup::Task> Collective(
306 307
      std::vector<phi::DenseTensor>& inputs,   // NOLINT
      std::vector<phi::DenseTensor>& outputs,  // NOLINT
308 309
      Fn fn,
      CommType op_type);
310

311 312 313 314 315 316 317 318 319
  template <typename Fn>
  std::shared_ptr<ProcessGroupStream::Task> Collective(
      std::vector<phi::DenseTensor>& inputs,   // NOLINT
      std::vector<phi::DenseTensor>& outputs,  // NOLINT
      Fn fn,
      CommType comm_type,
      bool sync_op,
      bool use_calc_stream);

320
  template <typename Fn>
321 322 323
  void Collective(const phi::DenseTensor*,
                  phi::DenseTensor*,
                  Fn fn,
324 325
                  CommType op_type);

B
Baibaifan 已提交
326 327
  template <typename Fn>
  std::shared_ptr<ProcessGroup::Task> PointToPoint(
328
      std::vector<phi::DenseTensor>& tensors,  // NOLINT
329 330 331
      Fn fn,
      int dst_rank,
      CommType op_type);
B
Baibaifan 已提交
332

333 334 335 336 337 338 339 340 341
  template <typename Fn>
  std::shared_ptr<ProcessGroup::Task> PointToPoint(
      std::vector<phi::DenseTensor>& tensors,  // NOLINT
      Fn fn,
      int dst_rank,
      CommType op_type,
      bool sync_op,
      bool use_calc_stream);

342 343
  void CreateNCCLManagerCache(const std::string& places_key,
                              const std::vector<Place>& places);
344

345
  void CheckSplitSizes(std::vector<int64_t>* split_sizes,
346
                       std::vector<int64_t> tensor_shape);
347 348 349 350
};

}  //  namespace distributed
}  //  namespace paddle