ProcessGroupNCCL.h 11.7 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
#include "paddle/fluid/platform/cuda_device_guard.h"
27
#include "paddle/fluid/platform/device_event.h"
28
#include "paddle/fluid/platform/enforce.h"
29 30
#include "paddle/phi/common/place.h"
#include "paddle/phi/core/device_context.h"
31

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
#include "paddle/fluid/platform/dynload/nccl.h"
#endif

namespace paddle {
namespace distributed {

using Place = paddle::platform::Place;

47
class ProcessGroupNCCL final : public ProcessGroupStream {
48
 public:
49 50
  class NCCLTask final : public ProcessGroupStream::TaskStream,
                         public std::enable_shared_from_this<NCCLTask> {
51
   public:
52 53 54 55 56 57 58 59 60 61 62 63 64
    NCCLTask(const Place& place,
             int rank,
             CommType comm_type,
             bool sync_op,
             bool use_calc_stream);
    virtual ~NCCLTask();

    bool IsCompleted() override;
    bool Wait(std::chrono::milliseconds timeout = kWaitTimeout) override;
    void Synchronize() override;
    void UpdateWaitChain(const phi::DeviceContext& ctx) override;

    // TODO(sunyilun): methods below will be removed later
65 66 67
    NCCLTask(const std::vector<Place>& places,
             int rank,
             CommType CommType,
68
             const std::vector<phi::DenseTensor>& inputs);
69 70 71 72
    NCCLTask(const std::vector<Place>& places,
             int rank,
             CommType comm_type,
             const std::vector<phi::DenseTensor>& inputs,
73
             bool sync_op,
74 75
             bool use_calc_stream);

76 77 78
   public:
    bool barrier_{false};
    platform::DeviceEvent comm_event_;  // event on comm stream
79 80

   private:
81
    Place place_;
82 83
  };

84
 public:
85 86 87 88 89
  ProcessGroupNCCL(const std::shared_ptr<Store>& store,
                   int rank,
                   int size,
                   const platform::Place& place,
                   int gid);
90

L
LiYuRio 已提交
91
  std::string GetBackendName() const override { return "NCCL"; }
92

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

95 96
  const phi::DeviceContext& GetDeviceContext(
      const Place& place, bool use_calc_stream) const override;
97

98 99 100 101 102 103
  std::shared_ptr<ProcessGroup::Task> AllGather(
      phi::DenseTensor* out_tensor,
      const phi::DenseTensor& in_tensor,
      bool sync_op,
      bool use_calc_stream) override;

104
  std::shared_ptr<ProcessGroup::Task> AllReduce(
105 106 107 108 109 110 111 112 113 114 115 116 117
      phi::DenseTensor* out_tensor,
      const phi::DenseTensor& in_tensor,
      const AllreduceOptions& opts,
      bool sync_op,
      bool use_calc_stream) override;

  std::shared_ptr<ProcessGroup::Task> Barrier(
      const BarrierOptions& = BarrierOptions()) override;

  std::shared_ptr<ProcessGroup::Task> Broadcast(
      phi::DenseTensor* out_tensor,
      const phi::DenseTensor& in_tensor,
      const BroadcastOptions& opts,
118 119 120
      bool sync_op,
      bool use_calc_stream) override;

121 122 123 124 125 126
  static void GroupStart();

  static void GroupEnd();

  ncclComm_t NCCLComm(const Place& place) const;

127
  // TODO(liyurui): This API will be moved later
128
  std::shared_ptr<ProcessGroup::Task> AllReduce(
129 130
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors,
131 132
      const AllreduceOptions& = AllreduceOptions()) override;

133
  // TODO(sunyilun): methods below will be removed later
134
  std::shared_ptr<ProcessGroup::Task> Broadcast(
135 136
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors,
137 138
      const BroadcastOptions& = BroadcastOptions()) override;

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

142 143 144 145 146 147
  std::shared_ptr<ProcessGroup::Task> Send(
      std::vector<phi::DenseTensor>& tensors,
      int dst_rank,
      bool sync_op,
      bool use_calc_stream) override;

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

151 152 153 154 155 156
  std::shared_ptr<ProcessGroup::Task> Recv(
      std::vector<phi::DenseTensor>& tensors,
      int src_rank,
      bool sync_op,
      bool use_calc_stream) override;

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

162 163 164
  std::shared_ptr<ProcessGroup::Task> Send_Partial(
      phi::DenseTensor& tensors,
      int dst_rank,
165 166
      int64_t offset,
      int64_t length,
167 168 169
      bool sync_op,
      bool use_calc_stream) override;

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

175 176 177
  std::shared_ptr<ProcessGroup::Task> Recv_Partial(
      phi::DenseTensor& tensors,
      int src_rank,
178 179
      int64_t offset,
      int64_t length,
180 181 182
      bool sync_op,
      bool use_calc_stream) override;

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

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

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

201
  std::shared_ptr<ProcessGroup::Task> AllToAll(
202 203 204 205 206 207 208 209
      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;
210

211 212 213 214 215 216
  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;

217 218 219 220 221 222 223 224
  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;

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

230 231 232 233 234 235 236 237 238 239 240 241 242 243
  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;

244 245 246
  std::shared_ptr<ProcessGroup::Task> Scatter(
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors,
247 248 249 250 251 252 253 254
      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;
255

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

261 262 263 264 265 266
 private:
  std::shared_ptr<ProcessGroupNCCL::NCCLTask> CreateTask(const Place& place,
                                                         int rank,
                                                         CommType op_type,
                                                         bool sync_op,
                                                         bool use_calc_stream);
267

268
  void BroadcastUniqueNCCLID(ncclUniqueId* nccl_id);
269

270 271 272 273 274 275 276 277 278 279
  void CreateNCCLEnvCache(const Place& place, const std::string& place_key);

  template <typename Fn>
  std::shared_ptr<ProcessGroupStream::Task> Collective(
      phi::DenseTensor* out_tensor,
      const phi::DenseTensor& in_tensor,
      Fn fn,
      CommType comm_type,
      bool sync_op,
      bool use_calc_stream);
L
LiYuRio 已提交
280

281 282 283 284 285
  void SyncCalcStream(const Place& place,
                      const std::shared_ptr<platform::DeviceEvent>& event);

  // TODO(sunyilun): methods below will be removed later
  std::shared_ptr<ProcessGroupNCCL::NCCLTask> CreateTask(
286 287
      std::vector<Place> places,
      int rank,
288
      CommType op_type,
289
      const std::vector<phi::DenseTensor>& inputs);
290

291
  std::shared_ptr<ProcessGroupNCCL::NCCLTask> CreateTask(
292 293 294 295 296 297 298
      const std::vector<Place>& places,
      int rank,
      CommType op_type,
      const std::vector<phi::DenseTensor>& inputs,
      bool sync_op,
      bool use_calc_stream);

299 300
  template <typename Fn>
  std::shared_ptr<ProcessGroup::Task> Collective(
301 302
      std::vector<phi::DenseTensor>& inputs,   // NOLINT
      std::vector<phi::DenseTensor>& outputs,  // NOLINT
303 304
      Fn fn,
      CommType op_type);
305

306 307 308 309 310 311 312 313 314
  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);

315
  template <typename Fn>
316 317 318
  void Collective(const phi::DenseTensor*,
                  phi::DenseTensor*,
                  Fn fn,
319 320
                  CommType op_type);

B
Baibaifan 已提交
321 322
  template <typename Fn>
  std::shared_ptr<ProcessGroup::Task> PointToPoint(
323
      std::vector<phi::DenseTensor>& tensors,  // NOLINT
324 325 326
      Fn fn,
      int dst_rank,
      CommType op_type);
B
Baibaifan 已提交
327

328 329 330 331 332 333 334 335 336
  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);

337 338
  void CreateNCCLManagerCache(const std::string& places_key,
                              const std::vector<Place>& places);
339

340
  void CheckSplitSizes(std::vector<int64_t>* split_sizes,
341
                       std::vector<int64_t> tensor_shape);
342 343 344 345 346 347 348 349 350 351 352

 private:
  std::shared_ptr<Store> store_;
  std::shared_ptr<platform::DeviceEvent> calc_event_;  // event on calc stream
  std::unordered_map<std::string, phi::GPUContext*> place_to_calc_ctx_;
  std::unordered_map<std::string, std::unique_ptr<phi::GPUContext>>
      place_to_comm_ctx_;

  // TODO(sunyilun): attrs below will be removed later
  std::mutex mutex_;
  std::unordered_map<std::string, std::vector<phi::GPUContext*>> places_to_ctx_;
353 354 355 356
};

}  //  namespace distributed
}  //  namespace paddle