|
| 1 | +// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#pragma once |
| 16 | + |
| 17 | +#include <error.h> |
| 18 | +#include <string> |
| 19 | + |
| 20 | +#include "boost/variant.hpp" |
| 21 | +#include "paddle/fluid/framework/data_type.h" |
| 22 | +#include "paddle/fluid/framework/variable.h" |
| 23 | +#include "paddle/fluid/platform/collective_helper.h" |
| 24 | +#include "paddle/fluid/platform/device/npu/enforce_npu.h" |
| 25 | +#include "paddle/fluid/platform/device/npu/npu_info.h" |
| 26 | +#include "paddle/fluid/platform/device_context.h" |
| 27 | +#include "paddle/fluid/platform/enforce.h" |
| 28 | + |
| 29 | +namespace paddle { |
| 30 | +namespace distributed { |
| 31 | + |
| 32 | +class NPUEventManager { |
| 33 | + public: |
| 34 | + NPUEventManager() = default; |
| 35 | + |
| 36 | + ~NPUEventManager() { |
| 37 | + if (is_created_) { |
| 38 | + platform::NPUDeviceGuard guard(device_index_); |
| 39 | + platform::NPUEventDestroy(event_); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + NPUEventManager(const NPUEventManager&) = delete; |
| 44 | + NPUEventManager& operator=(const NPUEventManager&) = delete; |
| 45 | + |
| 46 | + NPUEventManager(NPUEventManager&& other) { |
| 47 | + std::swap(is_created_, other.is_created_); |
| 48 | + std::swap(device_index_, other.device_index_); |
| 49 | + std::swap(event_, other.event_); |
| 50 | + } |
| 51 | + |
| 52 | + NPUEventManager& operator=(NPUEventManager&& other) { |
| 53 | + std::swap(is_created_, other.is_created_); |
| 54 | + std::swap(device_index_, other.device_index_); |
| 55 | + std::swap(event_, other.event_); |
| 56 | + return *this; |
| 57 | + } |
| 58 | + |
| 59 | + bool IsCreated() const { return is_created_; } |
| 60 | + bool DeviceId() const { return device_index_; } |
| 61 | + aclrtEvent GetRawNPUEvent() const { return event_; } |
| 62 | + |
| 63 | + void Record(const paddle::platform::NPUDeviceContext& ctx) { |
| 64 | + auto device_index = ctx.GetPlace().device; |
| 65 | + if (!is_created_) { |
| 66 | + CreateEvent(device_index); |
| 67 | + } |
| 68 | + PADDLE_ENFORCE_EQ(device_index, device_index_, |
| 69 | + platform::errors::PreconditionNotMet( |
| 70 | + "NPUDeviceContext's device %d does not match" |
| 71 | + "Event's device %d", |
| 72 | + device_index, device_index_)); |
| 73 | + |
| 74 | + platform::NPUDeviceGuard guard(device_index_); |
| 75 | + platform::NPUEventRecord(event_, ctx.stream()); |
| 76 | + } |
| 77 | + |
| 78 | + bool Query() const { |
| 79 | + aclrtEventStatus status = ACL_EVENT_STATUS_COMPLETE; |
| 80 | + platform::NPUEventQuery(event_, &status); |
| 81 | + if (status == ACL_EVENT_STATUS_COMPLETE) { |
| 82 | + return true; |
| 83 | + } |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + void Block(const paddle::platform::NPUDeviceContext& ctx) const { |
| 88 | + if (is_created_) { |
| 89 | + auto device_index = ctx.GetPlace().device; |
| 90 | + PADDLE_ENFORCE_EQ(device_index, device_index_, |
| 91 | + platform::errors::PreconditionNotMet( |
| 92 | + "CUDADeviceContext's device %d does not match" |
| 93 | + "Event's device %d", |
| 94 | + device_index, device_index_)); |
| 95 | + platform::NPUDeviceGuard guard(device_index_); |
| 96 | + platform::NPUStreamWaitEvent(ctx.stream(), event_); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private: |
| 101 | + bool is_created_{false}; |
| 102 | + aclrtEvent event_{}; |
| 103 | + int8_t device_index_{0}; |
| 104 | + |
| 105 | + private: |
| 106 | + void CreateEvent(int device_index) { |
| 107 | + device_index_ = device_index; |
| 108 | + platform::NPUDeviceGuard guard(device_index); |
| 109 | + platform::NPUEventCreate(&event_); |
| 110 | + is_created_ = true; |
| 111 | + } |
| 112 | +}; |
| 113 | + |
| 114 | +class HCCLCommManager { |
| 115 | + public: |
| 116 | + explicit HCCLCommManager(HcclComm hcclComm) : hccl_comm_(hcclComm) {} |
| 117 | + |
| 118 | + HCCLCommManager() : HCCLCommManager(nullptr) {} |
| 119 | + |
| 120 | + ~HCCLCommManager() noexcept { |
| 121 | + std::unique_lock<std::mutex> lock(mutex_); |
| 122 | + if (hccl_comm_) { |
| 123 | + platform::dynload::HcclCommDestroy(hccl_comm_); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + static std::shared_ptr<HCCLCommManager> Create(int num_ranks, int rank, |
| 128 | + HcclRootInfo* comm_id, |
| 129 | + HcclComm hccl_comm) { |
| 130 | + auto hccl_manager = std::make_shared<HCCLCommManager>(); |
| 131 | + auto ret = platform::dynload::HcclCommInitRootInfo(num_ranks, comm_id, rank, |
| 132 | + &hccl_comm); |
| 133 | + using __NPU_STATUS_TYPE__ = decltype(ret); |
| 134 | + constexpr auto __success_type__ = |
| 135 | + platform::details::NPUStatusType<__NPU_STATUS_TYPE__>::kSuccess; |
| 136 | + if (UNLIKELY(ret != __success_type__)) { |
| 137 | + VLOG(0) << "Error: create hccl_id error."; |
| 138 | + exit(-1); |
| 139 | + } |
| 140 | + |
| 141 | + hccl_manager->hccl_id_ = comm_id; |
| 142 | + hccl_manager->rank_ = rank; |
| 143 | + hccl_manager->hccl_comm_ = hccl_comm; |
| 144 | + return hccl_manager; |
| 145 | + } |
| 146 | + |
| 147 | + HcclRootInfo* GetHcclId() const { |
| 148 | + std::unique_lock<std::mutex> lock(mutex_); |
| 149 | + return hccl_id_; |
| 150 | + } |
| 151 | + |
| 152 | + HcclComm GetHcclComm() const { |
| 153 | + std::unique_lock<std::mutex> lock(mutex_); |
| 154 | + return hccl_comm_; |
| 155 | + } |
| 156 | + |
| 157 | + HCCLCommManager(const HCCLCommManager&) = delete; |
| 158 | + HCCLCommManager& operator=(const HCCLCommManager&) = delete; |
| 159 | + HCCLCommManager& operator=(HCCLCommManager&& other) = delete; |
| 160 | + |
| 161 | + HCCLCommManager(HCCLCommManager&& other) { |
| 162 | + std::unique_lock<std::mutex> lock(other.mutex_); |
| 163 | + std::swap(hccl_comm_, other.hccl_comm_); |
| 164 | + } |
| 165 | + |
| 166 | + protected: |
| 167 | + HcclComm hccl_comm_; |
| 168 | + HcclRootInfo* hccl_id_; |
| 169 | + int rank_; |
| 170 | + mutable std::mutex mutex_; |
| 171 | +}; |
| 172 | + |
| 173 | +} // namespace distributed |
| 174 | +} // namespace paddle |
0 commit comments