ProcessGroupHeter.h 4.2 KB
Newer Older
L
lilong12 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
// 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"
#include "paddle/fluid/distributed/collective/ProcessGroupGloo.h"
#include "paddle/fluid/platform/device_context.h"

#ifdef PADDLE_WITH_GLOO
#include "paddle/fluid/framework/fleet/gloo_wrapper.h"
#endif

#include "paddle/fluid/distributed/store/store.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"

#if defined(PADDLE_WITH_NCCL)
#include "paddle/fluid/distributed/collective/NCCLTools.h"
#include "paddle/fluid/distributed/collective/ProcessGroupNCCL.h"
#include "paddle/fluid/platform/cuda_device_guard.h"
#include "paddle/fluid/platform/dynload/nccl.h"
#endif

#if defined(PADDLE_WITH_ASCEND_CL)
#include "paddle/fluid/distributed/collective/HCCLTools.h"
#include "paddle/fluid/distributed/collective/ProcessGroupHCCL.h"
#endif

50 51 52 53 54
#if defined(PADDLE_WITH_DISTRIBUTE) && defined(PADDLE_WITH_PSCORE) && \
    (defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_ASCEND_CL))
#include "paddle/fluid/distributed/ps/service/heter_client.h"
#endif

L
lilong12 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68
#include "paddle/fluid/distributed/collective/Common.h"

constexpr const char* HETER_BACKEND_NAME = "HETER_BACKEND";

namespace paddle {
namespace distributed {

using Place = paddle::platform::Place;

class ProcessGroupHeter : public ProcessGroup {
 public:
  class HeterTask : public ProcessGroup::Task,
                    public std::enable_shared_from_this<HeterTask> {
   public:
69 70
    HeterTask(int rank,
              CommType CommType,
71
              const std::vector<phi::DenseTensor>&);
L
lilong12 已提交
72 73 74 75 76 77 78 79 80 81 82 83

    bool IsCompleted();

    void SynchronizeStreams() {}

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

    void Synchronize() {}

    virtual ~HeterTask();
  };

84 85 86 87 88 89 90 91 92 93 94 95 96
  ProcessGroupHeter(const std::shared_ptr<Store>& store,
                    int rank,
                    int size,
                    const platform::Place& place,
                    int gid,
                    int local_rank,
                    int local_size,
                    int gloo_rank,
                    int gloo_size,
                    bool with_switch,
                    std::string switch_endpoints,
                    int src_rank,
                    int dst_rank);
L
lilong12 已提交
97 98 99 100 101 102

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

  std::shared_ptr<ProcessGroup::Task> AllReduce(
103 104
      std::vector<phi::DenseTensor>&,
      std::vector<phi::DenseTensor>&,
L
lilong12 已提交
105 106 107
      const AllreduceOptions& = AllreduceOptions()) override;

  std::shared_ptr<ProcessGroup::Task> Broadcast(
108 109
      std::vector<phi::DenseTensor>&,
      std::vector<phi::DenseTensor>&,
L
lilong12 已提交
110 111
      const BroadcastOptions& = BroadcastOptions()) override;

112 113 114 115 116 117
  std::shared_ptr<ProcessGroup::Task> Send(
      std::vector<phi::DenseTensor>& in_tensors, int peer) override;

  std::shared_ptr<ProcessGroup::Task> Recv(
      std::vector<phi::DenseTensor>& out_tensors, int peer) override;

L
lilong12 已提交
118 119
 protected:
  virtual std::shared_ptr<ProcessGroupHeter::HeterTask> CreateTask(
120
      int rank, CommType opType, const std::vector<phi::DenseTensor>& inputs);
L
lilong12 已提交
121 122 123 124 125 126 127 128 129 130 131

 private:
  std::shared_ptr<Store> store_;
  std::shared_ptr<ProcessGroup> inner_pg_;
  std::shared_ptr<ProcessGroupGloo> inter_pg_;

  int local_rank_;
  int local_size_;
  int gloo_rank_;
  int gloo_size_;
  bool with_switch_;
132
  std::string switch_endpoint_;
133 134 135 136
  int src_rank_;
  int dst_rank_;
  static int send_count;
  static int recv_count;
L
lilong12 已提交
137 138 139 140
};

}  //  namespace distributed
}  //  namespace paddle