process_group_bkcl.h 7.3 KB
Newer Older
J
james 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// 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>

22 23
#include "paddle/fluid/distributed/collective/process_group.h"
#include "paddle/fluid/distributed/collective/process_group_with_stream.h"
J
james 已提交
24 25
#include "paddle/fluid/platform/device/xpu/xpu_header.h"
#include "paddle/fluid/platform/gen_comm_id_helper.h"
26
#include "paddle/phi/common/place.h"
J
james 已提交
27
#include "paddle/phi/core/device_context.h"
28
#include "paddle/phi/core/distributed/store/store.h"
J
james 已提交
29 30

#if defined(PADDLE_WITH_XPU)
W
Wen Sun 已提交
31
#include "paddle/fluid/distributed/collective/bkcl_tools.h"
J
james 已提交
32 33 34 35 36 37 38
#endif

constexpr const char* BKCL_BACKEND_NAME = "BKCL";

namespace paddle {
namespace distributed {

39
using Place = phi::Place;
J
james 已提交
40 41

// BKCL funcs use separate communication stream by default
42
class ProcessGroupBKCL : public ProcessGroupWithStream {
J
james 已提交
43
 public:
44
  class BKCLTask final : public ProcessGroupWithStream::TaskStream,
J
james 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
                         public std::enable_shared_from_this<BKCLTask> {
   public:
    BKCLTask(const Place& place,
             int rank,
             CommType CommType,
             bool sync_op,
             bool use_calc_stream);
    virtual ~BKCLTask();

    // TODO(zhangxiaoci): XPU do not support event query for now
    bool IsCompleted() override;
    bool Wait(std::chrono::milliseconds timeout = kWaitTimeout) override;
    void Synchronize() override;

    void SynchronizeStreams();

   public:
    bool barrier_{false};
    std::shared_ptr<XPUEventManager> comm_event_;  // event on comm stream

   private:
    Place place_;
  };

 public:
70
  ProcessGroupBKCL(const std::shared_ptr<phi::distributed::Store>& store,
J
james 已提交
71 72 73 74
                   int rank,
                   int size,
                   int gid);

L
LiYuRio 已提交
75
  static std::shared_ptr<ProcessGroupBKCL> CreateProcessGroupBKCL(
76 77 78 79
      const std::shared_ptr<phi::distributed::Store>& store,
      int rank,
      int size,
      int gid);
L
LiYuRio 已提交
80

J
james 已提交
81 82 83 84
  std::string GetBackendName() const override {
    return std::string(BKCL_BACKEND_NAME);
  }

85
  phi::DeviceContext* GetDeviceContext(const Place& place) const override;
J
james 已提交
86

87 88
  phi::DeviceContext* GetDeviceContext(const Place& place,
                                       bool use_calc_stream) const override;
J
james 已提交
89

90
  std::shared_ptr<ProcessGroup::Task> AllGather(
J
james 已提交
91 92
      phi::DenseTensor* out_tensor,
      const phi::DenseTensor& in_tensor,
93 94
      int64_t offset,  // for compatibility, no use now
      int64_t numel,   // for compatibility, no use now
J
james 已提交
95 96 97
      bool sync_op,
      bool use_calc_stream) override;

98
  std::shared_ptr<ProcessGroup::Task> AllReduce(
J
james 已提交
99 100
      phi::DenseTensor* out_tensor,
      const phi::DenseTensor& in_tensor,
101
      const AllreduceOptions& opts,
J
james 已提交
102 103 104
      bool sync_op,
      bool use_calc_stream) override;

105
  std::shared_ptr<ProcessGroup::Task> Broadcast(
J
james 已提交
106 107
      phi::DenseTensor* out_tensor,
      const phi::DenseTensor& in_tensor,
108
      const BroadcastOptions& opts,
J
james 已提交
109 110 111
      bool sync_op,
      bool use_calc_stream) override;

J
james 已提交
112 113 114 115 116 117
  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;

118 119 120 121 122 123 124
  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;

125 126 127 128 129 130 131 132 133 134 135 136 137 138
  std::shared_ptr<ProcessGroup::Task> Recv(phi::DenseTensor* tensor,
                                           int src_rank,
                                           int64_t offset,
                                           int64_t numel,
                                           bool sync_op,
                                           bool use_calc_stream) override;

  std::shared_ptr<ProcessGroup::Task> Send(const phi::DenseTensor& tensor,
                                           int dst_rank,
                                           int64_t offset,
                                           int64_t numel,
                                           bool sync_op,
                                           bool use_calc_stream) override;

J
james 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
  std::shared_ptr<ProcessGroup::Task> Barrier(
      const BarrierOptions& = BarrierOptions()) override;

  static void GroupStart();

  static void GroupEnd();

  BKCLContext_t BKCLComm(const Place& place) const;

  // below are old apis
  std::shared_ptr<ProcessGroup::Task> AllReduce(
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors,
      const AllreduceOptions& = AllreduceOptions()) override;

  std::shared_ptr<ProcessGroup::Task> AllReduce(
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors,
      const AllreduceOptions& options,
      bool sync_op) override;

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

  std::shared_ptr<ProcessGroup::Task> Broadcast(
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors,
      const BroadcastOptions&,
      bool sync_op) override;

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

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

 private:
  std::shared_ptr<ProcessGroupBKCL::BKCLTask> CreateTask(const Place& place,
                                                         int rank,
                                                         CommType op_type,
                                                         bool sync_op,
                                                         bool use_calc_stream);

187
  void BroadcastUniqueBKCLID(BKCLUniqueId* bkcl_id);
J
james 已提交
188 189 190 191

  void CreateBKCLEnvCache(const Place& place, const std::string& place_key);

  template <typename Fn>
192
  std::shared_ptr<ProcessGroup::Task> Collective(
J
james 已提交
193 194 195 196 197 198 199 200 201 202
      phi::DenseTensor* out_tensor,
      const phi::DenseTensor& in_tensor,
      Fn fn,
      CommType comm_type,
      bool sync_op,
      bool use_calc_stream);

  void SyncCalcStream(const Place& place);

 private:
203
  std::shared_ptr<phi::distributed::Store> store_;
J
james 已提交
204 205 206 207 208 209 210 211 212
  std::mutex mutex_;
  std::shared_ptr<XPUEventManager> calc_event_;  // event on calc stream
  std::unordered_map<std::string, phi::XPUContext*> place_to_calc_ctx_;
  std::unordered_map<std::string, std::unique_ptr<phi::XPUContext>>
      place_to_comm_ctx_;
};

}  //  namespace distributed
}  //  namespace paddle