ProcessGroupHCCL.h 4.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
// 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>

24
#include "paddle/fluid/distributed/collective/HCCLTools.h"
25
#include "paddle/fluid/distributed/collective/ProcessGroup.h"
26
#include "paddle/fluid/distributed/store/store.h"
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#include "paddle/fluid/platform/device/npu/npu_stream.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"

namespace paddle {
namespace distributed {

using Place = paddle::platform::Place;
using NPUStream = platform::stream::NPUStream;
using NPUDeviceContext = paddle::platform::NPUDeviceContext;

class ProcessGroupHCCL : public ProcessGroup {
 public:
  class HCCLTask : public ProcessGroup::Task,
                   public std::enable_shared_from_this<HCCLTask> {
   public:
45 46 47
    HCCLTask(const std::vector<Place>& places,
             int rank,
             CommType CommType,
48
             const std::vector<phi::DenseTensor>& inputs);
49 50 51 52 53 54 55 56 57

    bool IsCompleted();

    void SynchronizeStreams();

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

    void Synchronize();

58
    void SetOutputs(std::vector<phi::DenseTensor>& outputs);  // NOLINT
59 60 61 62 63 64 65 66

    virtual ~HCCLTask();

    std::vector<NPUEventManager> control_events_;

   protected:
    std::vector<Place> places_;
    std::vector<std::shared_ptr<HCCLCommManager>> hcclComms_;
67
    std::shared_ptr<std::vector<phi::DenseTensor>> outputs_;
68 69 70 71

   private:
  };

72 73 74 75 76
  ProcessGroupHCCL(const std::shared_ptr<Store>& store,
                   int rank,
                   int size,
                   const platform::Place& place,
                   int gid);
77

L
LiYuRio 已提交
78
  std::string GetBackendName() const override { return "HCCL"; }
79 80

  std::shared_ptr<ProcessGroup::Task> AllReduce(
81 82
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors,
83 84 85
      const AllreduceOptions& = AllreduceOptions()) override;

  std::shared_ptr<ProcessGroup::Task> Broadcast(
86 87
      std::vector<phi::DenseTensor>& in_tensors,
      std::vector<phi::DenseTensor>& out_tensors,
88 89 90 91
      const BroadcastOptions& = BroadcastOptions()) override;

 protected:
  virtual std::shared_ptr<ProcessGroupHCCL::HCCLTask> CreateTask(
92 93 94
      std::vector<Place> places,
      int rank,
      CommType opType,
95
      const std::vector<phi::DenseTensor>& inputs);
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

  std::shared_ptr<Store> store_;
  std::shared_ptr<HCCLCommManager> hccl_comm_;
  std::mutex mutex_;
  std::unordered_map<std::string, std::vector<std::shared_ptr<HCCLCommManager>>>
      places_to_hcclcomm_;

  std::unordered_map<std::string, std::vector<NPUEventManager>>
      places_to_events_;

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

  std::set<int> used_place_ids_;

 private:
L
LiYuRio 已提交
113 114
  void BcastHCCLId(std::vector<HcclRootInfo>& hccl_ids,  // NOLINT
                   int root,                             // NOLINT
115 116 117 118 119 120
                   int server_fd);

  void BroadcastUniqueHCCLID(std::vector<HcclRootInfo>& hccl_ids);  // NOLINT

  template <typename Fn>
  std::shared_ptr<ProcessGroup::Task> Collective(
121 122
      std::vector<phi::DenseTensor>& inputs,   // NOLINT
      std::vector<phi::DenseTensor>& outputs,  // NOLINT
123 124
      Fn fn,
      CommType op_type);
125 126 127 128 129 130 131

  void CreateHCCLManagerCache(const std::string& places_key,
                              const std::vector<Place>& places);
};

}  //  namespace distributed
}  //  namespace paddle