process_group_nccl.h 9.4 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
// 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>
#include <unordered_map>
#include <vector>

23 24
#include "paddle/fluid/distributed/collective/process_group.h"
#include "paddle/fluid/distributed/collective/process_group_with_stream.h"
25 26 27
#include "paddle/fluid/platform/device_event.h"
#include "paddle/phi/common/place.h"
#include "paddle/phi/core/device_context.h"
28
#include "paddle/phi/core/distributed/store/store.h"
29

S
ShenLiang 已提交
30
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
W
Wen Sun 已提交
31
#include "paddle/fluid/distributed/collective/nccl_tools.h"
S
ShenLiang 已提交
32 33 34
#endif

#ifdef PADDLE_WITH_RCCL
35
#include "paddle/phi/backends/dynload/rccl.h"
36
#else
37
#include "paddle/phi/backends/dynload/nccl.h"
38 39 40 41 42
#endif

namespace paddle {
namespace distributed {

43
using Place = phi::Place;
44

45
class ProcessGroupNCCL final : public ProcessGroupWithStream {
46
 public:
47
  class NCCLTask final : public ProcessGroupWithStream::TaskStream,
48
                         public std::enable_shared_from_this<NCCLTask> {
49
   public:
50 51 52 53 54 55 56 57 58 59 60 61
    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;

W
Wen Sun 已提交
62 63 64
    bool IsBlockCPUInWait() const { return block_cpu_in_wait_; }
    void SetBlockCPUInWait() { block_cpu_in_wait_ = true; }

65
    // TODO(sunyilun): methods below will be removed later
66 67 68
    NCCLTask(const std::vector<Place>& places,
             int rank,
             CommType CommType,
69
             const std::vector<phi::DenseTensor>& inputs);
70

71
   private:
W
Wen Sun 已提交
72 73 74
    bool block_cpu_in_wait_{false};
    platform::DeviceEvent comm_event_;  // event on comm stream
    Place task_place_;
75 76
  };

77
 public:
L
LiYuRio 已提交
78
  static std::shared_ptr<ProcessGroupNCCL> CreateProcessGroupNCCL(
79 80 81 82
      const std::shared_ptr<phi::distributed::Store>& store,
      int rank,
      int size,
      int gid);
L
LiYuRio 已提交
83

84
  ProcessGroupNCCL(const std::shared_ptr<phi::distributed::Store>& store,
85 86 87
                   int rank,
                   int size,
                   int gid);
88

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

91 92
  phi::DeviceContext* GetDeviceContext(const Place& place) const override;

93 94
  phi::DeviceContext* GetDeviceContext(const Place& place,
                                       bool use_calc_stream) const override;
L
LiYuRio 已提交
95

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

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

111 112 113 114 115 116 117 118
  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) override;

119 120 121 122 123 124 125
  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,
126 127 128
      bool sync_op,
      bool use_calc_stream) override;

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

  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) override;

  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) override;

148 149
  std::shared_ptr<ProcessGroup::Task> Recv(phi::DenseTensor* tensor,
                                           int src_rank,
150 151
                                           int64_t offset,
                                           int64_t numel,
152 153 154
                                           bool sync_op,
                                           bool use_calc_stream) override;

155
  std::shared_ptr<ProcessGroup::Task> Send(const phi::DenseTensor& tensor,
156
                                           int dst_rank,
157 158
                                           int64_t offset,
                                           int64_t numel,
159 160 161
                                           bool sync_op,
                                           bool use_calc_stream) override;

162 163 164 165 166 167
  static void GroupStart();

  static void GroupEnd();

  ncclComm_t NCCLComm(const Place& place) const;

168
  // TODO(liyurui): This API will be moved later
169
  std::shared_ptr<ProcessGroup::Task> AllReduce(
170 171
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors,
172 173
      const AllreduceOptions& = AllreduceOptions()) override;

174
  // TODO(sunyilun): methods below will be removed later
175
  std::shared_ptr<ProcessGroup::Task> Broadcast(
176 177
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors,
178 179
      const BroadcastOptions& = BroadcastOptions()) override;

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

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

186
  std::shared_ptr<ProcessGroup::Task> AllGather(
187 188
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors) override;
189 190

  std::shared_ptr<ProcessGroup::Task> AllToAll(
191 192 193
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors) override;

194
  std::shared_ptr<ProcessGroup::Task> Reduce(
195 196 197
      std::vector<phi::DenseTensor>& tensors,
      std::vector<phi::DenseTensor>& out_tensors,
      const ReduceOptions& opts) override;
198

199 200 201
  std::shared_ptr<ProcessGroup::Task> Scatter(
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors,
202 203
      const ScatterOptions& opts) override;

204 205 206 207 208 209
 private:
  std::shared_ptr<ProcessGroupNCCL::NCCLTask> CreateTask(const Place& place,
                                                         int rank,
                                                         CommType op_type,
                                                         bool sync_op,
                                                         bool use_calc_stream);
210

211
  void BroadcastUniqueNCCLID(ncclUniqueId* nccl_id);
212

213 214
  void CreateNCCLEnvCache(const Place& place, const std::string& place_key);

215 216
  void SyncCalcStream(const Place& place);

217 218 219
  std::shared_ptr<ProcessGroup::Task> RunFnInNCCLEnv(
      std::function<void(ncclComm_t, gpuStream_t)> fn,
      const phi::DenseTensor& tensor,
220 221 222
      CommType comm_type,
      bool sync_op,
      bool use_calc_stream);
L
LiYuRio 已提交
223

224 225
  // TODO(sunyilun): methods below will be removed later
  std::shared_ptr<ProcessGroupNCCL::NCCLTask> CreateTask(
226 227
      std::vector<Place> places,
      int rank,
228
      CommType op_type,
229
      const std::vector<phi::DenseTensor>& inputs);
230 231 232

  template <typename Fn>
  std::shared_ptr<ProcessGroup::Task> Collective(
233 234
      std::vector<phi::DenseTensor>& inputs,   // NOLINT
      std::vector<phi::DenseTensor>& outputs,  // NOLINT
235 236
      Fn fn,
      CommType op_type);
237

B
Baibaifan 已提交
238 239
  template <typename Fn>
  std::shared_ptr<ProcessGroup::Task> PointToPoint(
240
      std::vector<phi::DenseTensor>& tensors,  // NOLINT
241 242 243
      Fn fn,
      int dst_rank,
      CommType op_type);
B
Baibaifan 已提交
244

245 246
  void CreateNCCLManagerCache(const std::string& places_key,
                              const std::vector<Place>& places);
247

248
 private:
249
  std::shared_ptr<phi::distributed::Store> store_;
250

W
Wen Sun 已提交
251 252
  std::unordered_map<std::string, platform::DeviceEvent>
      place_to_calc_event_;  // event on calc stream
253 254 255 256 257 258 259
  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_;
260 261 262 263
};

}  //  namespace distributed
}  //  namespace paddle