process_group_bkcl.h 6.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 26
#include "paddle/fluid/distributed/store/store.h"
#include "paddle/fluid/platform/device/xpu/xpu_header.h"
#include "paddle/fluid/platform/gen_comm_id_helper.h"
27
#include "paddle/phi/common/place.h"
J
james 已提交
28 29 30
#include "paddle/phi/core/device_context.h"

#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 70 71 72 73 74
                         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:
  ProcessGroupBKCL(const std::shared_ptr<Store>& store,
                   int rank,
                   int size,
                   int gid);

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

J
james 已提交
78 79 80 81
  std::string GetBackendName() const override {
    return std::string(BKCL_BACKEND_NAME);
  }

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

84 85
  phi::DeviceContext* GetDeviceContext(const Place& place,
                                       bool use_calc_stream) const override;
J
james 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

  std::shared_ptr<ProcessGroup::Task> AllReduce(
      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> Broadcast(
      phi::DenseTensor* out_tensor,
      const phi::DenseTensor& in_tensor,
      const BroadcastOptions& opts,
      bool sync_op,
      bool use_calc_stream) override;

  std::shared_ptr<ProcessGroup::Task> AllGather(
      phi::DenseTensor* out_tensor,
      const phi::DenseTensor& in_tensor,
104 105
      int64_t offset,  // for compatibility, no use now
      int64_t numel,   // for compatibility, no use now
J
james 已提交
106 107 108
      bool sync_op,
      bool use_calc_stream) override;

J
james 已提交
109 110 111 112 113 114
  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;

J
james 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
  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);

163
  void BroadcastUniqueBKCLID(BKCLUniqueId* bkcl_id);
J
james 已提交
164 165 166 167

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

  template <typename Fn>
168
  std::shared_ptr<ProcessGroup::Task> Collective(
J
james 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
      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:
  std::shared_ptr<Store> store_;
  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