ProcessGroupNCCL.h 6.1 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 24
// 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>

#include "paddle/fluid/distributed/collective/ProcessGroup.h"
25
#include "paddle/fluid/distributed/store/store.h"
26 27 28 29 30 31 32
#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"
#include "paddle/fluid/platform/stream/cuda_stream.h"

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

#ifdef PADDLE_WITH_RCCL
#include "paddle/fluid/platform/dynload/rccl.h"
#else
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#include "paddle/fluid/platform/dynload/nccl.h"
#endif

constexpr const char* NCCL_BACKEND_NAME = "NCCL";

namespace paddle {
namespace distributed {

using Place = paddle::platform::Place;
using CUDAStream = platform::stream::CUDAStream;
using CUDADeviceContext = paddle::platform::CUDADeviceContext;

class ProcessGroupNCCL : public ProcessGroup {
 public:
  class NCCLTask : public ProcessGroup::Task,
                   public std::enable_shared_from_this<NCCLTask> {
   public:
    NCCLTask(const std::vector<Place>& places, int rank, CommType CommType,
58
             const std::vector<phi::DenseTensor>& inputs);
59 60 61 62 63 64 65 66 67

    bool IsCompleted();

    void SynchronizeStreams();

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

    void Synchronize();

68
    void SetOutputs(std::vector<phi::DenseTensor>& outputs);  // NOLINT
69 70 71 72

    virtual ~NCCLTask();

    std::vector<EventManager> control_events_;
73
    std::vector<phi::DenseTensor> barrierTensors_;
74 75 76 77

   protected:
    std::vector<Place> places_;
    std::vector<std::shared_ptr<NCCLCommManager>> ncclComms_;
78
    std::shared_ptr<std::vector<phi::DenseTensor>> outputs_;
79 80 81 82

   private:
  };

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

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

  std::shared_ptr<ProcessGroup::Task> AllReduce(
91 92
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors,
93 94 95
      const AllreduceOptions& = AllreduceOptions()) override;

  std::shared_ptr<ProcessGroup::Task> Broadcast(
96 97
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors,
98 99
      const BroadcastOptions& = BroadcastOptions()) override;

B
Baibaifan 已提交
100 101 102
  std::shared_ptr<ProcessGroup::Task> Barrier(
      const BarrierOptions& = BarrierOptions()) override;

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

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

109 110 111 112 113 114 115 116
  std::shared_ptr<ProcessGroup::Task> Send_Partial(phi::DenseTensor& tensors,
                                                   int dst_rank, int offset,
                                                   int length) override;

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

117
  std::shared_ptr<ProcessGroup::Task> AllGather(
118 119
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors) override;
120 121

  std::shared_ptr<ProcessGroup::Task> AllToAll(
122 123
      std::vector<phi::DenseTensor>& in,
      std::vector<phi::DenseTensor>& out) override;
124 125

  std::shared_ptr<ProcessGroup::Task> Reduce(
126 127 128
      std::vector<phi::DenseTensor>& tensors,
      std::vector<phi::DenseTensor>& out_tensors,
      const ReduceOptions& opts) override;
129

130 131 132 133
  std::shared_ptr<ProcessGroup::Task> Scatter(
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors,
      const ScatterOptions&) override;
134

135 136 137
 protected:
  virtual std::shared_ptr<ProcessGroupNCCL::NCCLTask> CreateTask(
      std::vector<Place> places, int rank, CommType opType,
138
      const std::vector<phi::DenseTensor>& inputs);
139 140

 protected:
141
  std::shared_ptr<Store> store_;
142 143 144 145 146 147 148 149 150 151 152
  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_;

  std::unordered_map<std::string,
                     std::vector<std::unique_ptr<CUDADeviceContext>>>
      places_to_ctx_;

B
Baibaifan 已提交
153 154
  std::set<int> used_place_ids_;

155 156 157 158 159 160 161 162
 private:
  void BcastNCCLId(std::vector<ncclUniqueId>& nccl_ids, int root,  // NOLINT
                   int server_fd);

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

  template <typename Fn>
  std::shared_ptr<ProcessGroup::Task> Collective(
163 164
      std::vector<phi::DenseTensor>& inputs,   // NOLINT
      std::vector<phi::DenseTensor>& outputs,  // NOLINT
165 166
      Fn fn, CommType op_type);

167 168 169 170
  template <typename Fn>
  void Collective(const phi::DenseTensor*, phi::DenseTensor*, Fn fn,
                  CommType op_type);

B
Baibaifan 已提交
171 172
  template <typename Fn>
  std::shared_ptr<ProcessGroup::Task> PointToPoint(
173
      std::vector<phi::DenseTensor>& tensors,  // NOLINT
B
Baibaifan 已提交
174 175
      Fn fn, int dst_rank, CommType op_type);

176 177 178 179 180 181
  void CreateNCCLManagerCache(const std::string& places_key,
                              const std::vector<Place>& places);
};

}  //  namespace distributed
}  //  namespace paddle