ProcessGroupNCCL.h 7.8 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 63 64 65
    NCCLTask(const std::vector<Place>& places,
             int rank,
             CommType comm_type,
             const std::vector<phi::DenseTensor>& inputs,
             bool is_sync,
             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 78

    virtual ~NCCLTask();

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

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

   private:
  };

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

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

99 100 101 102 103 104 105 106
  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
107
  std::shared_ptr<ProcessGroup::Task> AllReduce(
108 109
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors,
110 111 112
      const AllreduceOptions& = AllreduceOptions()) override;

  std::shared_ptr<ProcessGroup::Task> Broadcast(
113 114
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors,
115 116
      const BroadcastOptions& = BroadcastOptions()) override;

B
Baibaifan 已提交
117 118 119
  std::shared_ptr<ProcessGroup::Task> Barrier(
      const BarrierOptions& = BarrierOptions()) override;

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

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

126
  std::shared_ptr<ProcessGroup::Task> Send_Partial(phi::DenseTensor& tensors,
127 128
                                                   int dst_rank,
                                                   int offset,
129 130 131
                                                   int length) override;

  std::shared_ptr<ProcessGroup::Task> Recv_Partial(phi::DenseTensor& tensors,
132 133
                                                   int src_rank,
                                                   int offset,
134 135
                                                   int length) override;

136
  std::shared_ptr<ProcessGroup::Task> AllGather(
137 138
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors) override;
139

140 141 142 143 144 145
  std::shared_ptr<ProcessGroup::Task> AllGather_Partial(
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors,
      int offset,
      int length) override;

146
  std::shared_ptr<ProcessGroup::Task> AllToAll(
147 148
      std::vector<phi::DenseTensor>& in,
      std::vector<phi::DenseTensor>& out) override;
149

150 151 152 153 154 155
  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;

156
  std::shared_ptr<ProcessGroup::Task> Reduce(
157 158 159
      std::vector<phi::DenseTensor>& tensors,
      std::vector<phi::DenseTensor>& out_tensors,
      const ReduceOptions& opts) override;
160

161 162 163 164
  std::shared_ptr<ProcessGroup::Task> Scatter(
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors,
      const ScatterOptions&) override;
165

166 167 168 169 170 171 172 173 174
  std::shared_ptr<ProcessGroup::Task> _ReduceScatterBase(
      phi::DenseTensor&,  // NOLINT
      phi::DenseTensor&,  // NOLINT
      const ReduceScatterOptions&) override;

  static void GroupStart();

  static void GroupEnd();

L
LiYuRio 已提交
175 176
  ncclComm_t NCCLComm(const Place& place) const;

177 178
 protected:
  virtual std::shared_ptr<ProcessGroupNCCL::NCCLTask> CreateTask(
179 180 181
      std::vector<Place> places,
      int rank,
      CommType opType,
182
      const std::vector<phi::DenseTensor>& inputs);
183 184

 protected:
185
  std::shared_ptr<Store> store_;
186 187 188 189 190 191 192
  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 已提交
193
  std::unordered_map<std::string, std::vector<std::unique_ptr<phi::GPUContext>>>
194 195
      places_to_ctx_;

B
Baibaifan 已提交
196 197
  std::set<int> used_place_ids_;

198
 private:
199 200
  void BcastNCCLId(std::vector<ncclUniqueId>& nccl_ids,  // NOLINT
                   int root,                             // NOLINT
201 202 203 204 205 206
                   int server_fd);

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

  template <typename Fn>
  std::shared_ptr<ProcessGroup::Task> Collective(
207 208
      std::vector<phi::DenseTensor>& inputs,   // NOLINT
      std::vector<phi::DenseTensor>& outputs,  // NOLINT
209 210
      Fn fn,
      CommType op_type);
211

212 213 214 215 216 217 218 219 220
  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);

221
  template <typename Fn>
222 223 224
  void Collective(const phi::DenseTensor*,
                  phi::DenseTensor*,
                  Fn fn,
225 226
                  CommType op_type);

B
Baibaifan 已提交
227 228
  template <typename Fn>
  std::shared_ptr<ProcessGroup::Task> PointToPoint(
229
      std::vector<phi::DenseTensor>& tensors,  // NOLINT
230 231 232
      Fn fn,
      int dst_rank,
      CommType op_type);
B
Baibaifan 已提交
233

234 235
  void CreateNCCLManagerCache(const std::string& places_key,
                              const std::vector<Place>& places);
236

237
  void CheckSplitSizes(std::vector<int64_t>* split_sizes,
238
                       std::vector<int64_t> tensor_shape);
239 240 241 242
};

}  //  namespace distributed
}  //  namespace paddle