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 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 <map>
#include <memory>
#include <string>
#include <unordered_map>

W
Wen Sun 已提交
23
#include "paddle/fluid/distributed/collective/process_group_stream.h"
J
james 已提交
24 25 26 27 28 29 30 31
#include "paddle/fluid/distributed/store/store.h"
#include "paddle/fluid/platform/device/xpu/xpu_header.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/gen_comm_id_helper.h"
#include "paddle/fluid/platform/place.h"
#include "paddle/phi/core/device_context.h"

#if defined(PADDLE_WITH_XPU)
W
Wen Sun 已提交
32
#include "paddle/fluid/distributed/collective/bkcl_tools.h"
J
james 已提交
33 34 35 36 37 38 39 40 41 42 43 44 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 75
#endif

constexpr const char* BKCL_BACKEND_NAME = "BKCL";

namespace paddle {
namespace distributed {

using Place = paddle::platform::Place;

// BKCL funcs use separate communication stream by default
class ProcessGroupBKCL : public ProcessGroupStream {
 public:
  class BKCLTask final : public ProcessGroupStream::TaskStream,
                         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 已提交
76 77 78
  static std::shared_ptr<ProcessGroupBKCL> CreateProcessGroupBKCL(
      const std::shared_ptr<Store>& store, int rank, int size, int gid);

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

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

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

  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,
105 106
      int64_t offset,  // for compatibility, no use now
      int64_t numel,   // for compatibility, no use now
J
james 已提交
107 108 109
      bool sync_op,
      bool use_calc_stream) override;

J
james 已提交
110 111 112 113 114 115
  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 已提交
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
  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);

  void BroadcastUniqueBKCLID(BKCLUniqueId* bkcl_id);  // NOLINT

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

  template <typename Fn>
  std::shared_ptr<ProcessGroupStream::Task> Collective(
      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