Merged
support coalescing send/recv #30547
adelaideliuAdd on Feb 5
support coalescing send/recv #30547
Merged
Merged fromadelaideliu/pytorchv2.7.1_devintoAscend/pytorchv2.7.1
Total: 2 files changed+126-52
| @@ -4027,7 +4027,7 @@ c10::intrusive_ptr<c10d::Work> ProcessGroupHCCL::pointToPoint( | |||
| 4027 | std::string key; | 4027 | std::string key; |
| 4028 | std::vector<std::shared_ptr<HCCLComm>> hcclComms; | 4028 | std::vector<std::shared_ptr<HCCLComm>> hcclComms; |
| 4029 | 4029 | ||
| 4030 | - if (hcclCommInitRootInfoConfigExist() && c10_npu::option::OptionsManager::GetP2PBufferSize() != 0) { | 4030 | + if (hcclCommInitRootInfoConfigExist() && c10_npu::option::OptionsManager::GetP2PBufferSize() != 0 && coalescing_state_ == 0) { |
| 4031 | key = getKeySendRecv(rank_, peer); | 4031 | key = getKeySendRecv(rank_, peer); |
| 4032 | p2pRank = rank_ <= peer ? 0 : 1; | 4032 | p2pRank = rank_ <= peer ? 0 : 1; |
| 4033 | isSendRecvSelf = rank_ == peer; | 4033 | isSendRecvSelf = rank_ == peer; |
| @@ -4042,22 +4042,66 @@ c10::intrusive_ptr<c10d::Work> ProcessGroupHCCL::pointToPoint( | |||
| 4042 | 4042 | ||
| 4043 | // Bump the logical operation counter regardless of whether this op is | 4043 | // Bump the logical operation counter regardless of whether this op is |
| 4044 | // coalesced or individual | 4044 | // coalesced or individual |
| 4045 | - seqP2P_++; | ||
| 4046 | op_id_++; | 4045 | op_id_++; |
| 4047 | 4046 | ||
| 4047 | + // Handle coalescing state for P2P operations | ||
| 4048 | + if (coalescing_state_ & CoalActive) { | ||
| 4049 | + // Bump seqP2P_ once per coalesced group, not once per individual op. | ||
| 4050 | + if ((coalescing_state_ & CoalP2P) == 0) { | ||
| 4051 | + seqP2P_++; | ||
| 4052 | + } | ||
| 4053 | + coalescing_state_ |= CoalP2P; | ||
| 4054 | + // Verify device consistency across coalesced operations | ||
| 4055 | + // For HCCL, we support multiple devices, so we check if devices match | ||
| 4056 | + if (coalescedDevice_.index() < 0) { | ||
| 4057 | + // First P2P op in coalesced group, set the device | ||
| 4058 | + // For multi-device support, we use the first device as reference | ||
| 4059 | + coalescedDevice_ = devices[0]; | ||
| 4060 | + } else { | ||
| 4061 | + // Verify all devices match the coalesced device | ||
| 4062 | + for (const auto& device : devices) { | ||
| 4063 | + TORCH_CHECK( | ||
| 4064 | + coalescedDevice_.index() == device.index(), | ||
| 4065 | + "Expecting same device across coalesced P2P operations. " | ||
| 4066 | + "Got device ", device.index(), " but expected ", coalescedDevice_.index()); | ||
| 4067 | + } | ||
| 4068 | + } | ||
| 4069 | + | ||
| 4070 | + // Verify communicator consistency | ||
| 4071 | + if (coalescedComm_ == nullptr) { | ||
| 4072 | + coalescedComm_ = hcclComms[0]; | ||
| 4073 | + } else { | ||
| 4074 | + // For multi-device, we check if the first comm matches | ||
| 4075 | + TORCH_CHECK( | ||
| 4076 | + coalescedComm_ == hcclComms[0], | ||
| 4077 | + "Expecting same communicator across coalesced P2P operations."); | ||
| 4078 | + } | ||
| 4079 | + } else { | ||
| 4080 | + // Not coalescing, bump seqP2P_ normally | ||
| 4081 | + seqP2P_++; | ||
| 4082 | + } | ||
| 4083 | + | ||
| 4048 | // First let HCCL streams wait for input tensors allocation streams | 4084 | // First let HCCL streams wait for input tensors allocation streams |
| 4049 | syncStreams(devices, hcclEvents_[key], hcclStreams_[key]); | 4085 | syncStreams(devices, hcclEvents_[key], hcclStreams_[key]); |
| 4050 | 4086 | ||
| 4051 | // Work itself will create the CUDA events on all NPUs of tensors | 4087 | // Work itself will create the CUDA events on all NPUs of tensors |
| 4052 | - auto work = initWork(devices, rank_, opType, "", tensors, tensors, true); | 4088 | + c10::intrusive_ptr<ProcessGroupHCCL::WorkHCCL> work; |
| 4053 | - // This bypasses something in Work() that crashes if {tensor} is given as | 4089 | + if (coalescing_state_) { |
| 4054 | - // output, not sure what | 4090 | + // When coalescing, we don't create a Work object for individual ops. |
| 4055 | - work->outputs_ = std::make_shared<std::vector<at::Tensor>>(tensors); | 4091 | + // Instead, we stash the tensors for allocator safety and the Work |
| 4092 | + // will be created in endCoalescing. | ||
| 4093 | + coalescedTensors_.stash(tensors); | ||
| 4094 | + } else { | ||
| 4095 | + work = initWork(devices, rank_, opType, "", tensors, tensors, true); | ||
| 4096 | + // This bypasses something in Work() that crashes if {tensor} is given as | ||
| 4097 | + // output, not sure what | ||
| 4098 | + work->outputs_ = std::make_shared<std::vector<at::Tensor>>(tensors); | ||
| 4099 | + } | ||
| 4056 | 4100 | ||
| 4057 | // is npuGuard needed for the if block below, or can i swap them | 4101 | // is npuGuard needed for the if block below, or can i swap them |
| 4058 | c10_npu::OptionalNPUGuard npuGuard; | 4102 | c10_npu::OptionalNPUGuard npuGuard; |
| 4059 | 4103 | ||
| 4060 | - if (desyncDebug_) { | 4104 | + if (!coalescing_state_ && desyncDebug_) { |
| 4061 | for (const auto i : c10::irange(devices.size())) { | 4105 | for (const auto i : c10::irange(devices.size())) { |
| 4062 | c10_npu::NPUStream& hcclStream = hcclStreams_[key][i]; | 4106 | c10_npu::NPUStream& hcclStream = hcclStreams_[key][i]; |
| 4063 | (*(work->hcclStartEvents_))[i].record(hcclStream); | 4107 | (*(work->hcclStartEvents_))[i].record(hcclStream); |
| @@ -4075,7 +4119,9 @@ c10::intrusive_ptr<c10d::Work> ProcessGroupHCCL::pointToPoint( | |||
| 4075 | } | 4119 | } |
| 4076 | } | 4120 | } |
| 4077 | 4121 | ||
| 4078 | - pre(hcclStreams_[key], work); | 4122 | + if (!coalescing_state_) { |
| 4123 | + pre(hcclStreams_[key], work); | ||
| 4124 | + } | ||
| 4079 | 4125 | ||
| 4080 | if (nslb_path != nullptr && !nslb_is_end) { | 4126 | if (nslb_path != nullptr && !nslb_is_end) { |
| 4081 | auto nslb_num = c10_npu::option::OptionsManager::GetNslbCntVal(); | 4127 | auto nslb_num = c10_npu::option::OptionsManager::GetNslbCntVal(); |
| @@ -4139,7 +4185,7 @@ c10::intrusive_ptr<c10d::Work> ProcessGroupHCCL::pointToPoint( | |||
| 4139 | // See [Sync Streams]. | 4185 | // See [Sync Streams]. |
| 4140 | auto multi_stream_memory_reuse_mode = c10_npu::option::OptionsManager::GetMultiStreamMemoryReuse(); | 4186 | auto multi_stream_memory_reuse_mode = c10_npu::option::OptionsManager::GetMultiStreamMemoryReuse(); |
| 4141 | c10_npu::NPUCachingAllocator::recordStream(tensors[i].storage().data_ptr(), hcclStream); | 4187 | c10_npu::NPUCachingAllocator::recordStream(tensors[i].storage().data_ptr(), hcclStream); |
| 4142 | - if (multi_stream_memory_reuse_mode != c10_npu::option::CLOSE) { | 4188 | + if (!coalescing_state_ && multi_stream_memory_reuse_mode != c10_npu::option::CLOSE) { |
| 4143 | work->recorded_inputs_.push_back(std::make_pair(tensors[i].storage().getWeakStorageImpl(), hcclStream)); | 4189 | work->recorded_inputs_.push_back(std::make_pair(tensors[i].storage().getWeakStorageImpl(), hcclStream)); |
| 4144 | if (multi_stream_memory_reuse_mode == c10_npu::option::ERASE_RECORD_STREAM_WITH_OPTIMIZE) { | 4190 | if (multi_stream_memory_reuse_mode == c10_npu::option::ERASE_RECORD_STREAM_WITH_OPTIMIZE) { |
| 4145 | auto block_ptr = c10_npu::NPUCachingAllocator::getBlockPtr(tensors[i].storage().data_ptr()); | 4191 | auto block_ptr = c10_npu::NPUCachingAllocator::getBlockPtr(tensors[i].storage().data_ptr()); |
| @@ -4155,43 +4201,55 @@ c10::intrusive_ptr<c10d::Work> ProcessGroupHCCL::pointToPoint( | |||
| 4155 | // insert sync point fluxLimit(key, i) | 4201 | // insert sync point fluxLimit(key, i) |
| 4156 | c10_npu::NPUStream& hcclStream = hcclStreams_[key][i]; | 4202 | c10_npu::NPUStream& hcclStream = hcclStreams_[key][i]; |
| 4157 | hcclUs startut = std::chrono::steady_clock::now(); | 4203 | hcclUs startut = std::chrono::steady_clock::now(); |
| 4158 | - HCCL_CHECK_ERROR(fn(tensors[i], hcclComms[i]->getHcclComm(), hcclStream, work->is_dispatched, p2pTargetRank), opTypeToString(opType).c_str()); | 4204 | + // For coalescing, we use a dummy is_dispatched pointer |
| 4205 | + std::shared_ptr<bool> is_dispatched = coalescing_state_ ? std::make_shared<bool>(false) : work->is_dispatched; | ||
| 4206 | + if (coalescing_state_) { | ||
| 4207 | + hcclGroupStart(); | ||
| 4208 | + } | ||
| 4209 | + HCCL_CHECK_ERROR(fn(tensors[i], hcclComms[i]->getHcclComm(), hcclStream, is_dispatched, p2pTargetRank), opTypeToString(opType).c_str()); | ||
| 4210 | + if (coalescing_state_) { | ||
| 4211 | + hcclGroupEnd(); | ||
| 4212 | + } | ||
| 4159 | } | 4213 | } |
| 4160 | } | 4214 | } |
| 4161 | - post(hcclStreams_[key], work); | ||
| 4162 | 4215 | ||
| 4163 | - // Future only needs to be created and marked completed with outputs for | 4216 | + if (!coalescing_state_) { |
| 4164 | - // recv(), but still create future for use cases such as profiling even for | 4217 | + post(hcclStreams_[key], work); |
| 4165 | - // send(). | ||
| 4166 | - { | ||
| 4167 | - c10_npu::NPUMultiStreamGuard guard(hcclStreams_[key]); | ||
| 4168 | - work->future_ = c10::make_intrusive<at::ivalue::Future>( | ||
| 4169 | - c10::ListType::create(c10::TensorType::get()), | ||
| 4170 | - devices); | ||
| 4171 | - work->future_->markCompleted(at::IValue(*work->outputs_)); | ||
| 4172 | - } | ||
| 4173 | 4218 | ||
| 4174 | - // End event should only be recorded after the hcclGroupEnd() | 4219 | + // Future only needs to be created and marked completed with outputs for |
| 4175 | - for (const auto i : c10::irange(tensors.size())) { | 4220 | + // recv(), but still create future for use cases such as profiling even for |
| 4176 | - c10_npu::NPUStream& hcclStream = hcclStreams_[key][i]; | 4221 | + // send(). |
| 4177 | - (*(work->hcclEndEvents_))[i].record(hcclStream); | 4222 | + { |
| 4178 | - work->hcclComms_[i] = hcclComms[i]; | 4223 | + c10_npu::NPUMultiStreamGuard guard(hcclStreams_[key]); |
| 4179 | - work->blockingWait_ = blockingWait_; | 4224 | + work->future_ = c10::make_intrusive<at::ivalue::Future>( |
| 4180 | - work->opTimeout_ = options_->timeout; | 4225 | + c10::ListType::create(c10::TensorType::get()), |
| 4181 | - work->store_ = store_; | 4226 | + devices); |
| 4182 | - // Record size info for debug. We only record the size on the first device | 4227 | + work->future_->markCompleted(at::IValue(*work->outputs_)); |
| 4183 | - // as multi-device per process is deprecated | 4228 | + } |
| 4184 | - work->numelIn_ = work->numelOut_ = static_cast<size_t>(tensors[i].numel()); | 4229 | + |
| 4185 | - } | 4230 | + // End event should only be recorded after the hcclGroupEnd() |
| 4231 | + for (const auto i : c10::irange(tensors.size())) { | ||
| 4232 | + c10_npu::NPUStream& hcclStream = hcclStreams_[key][i]; | ||
| 4233 | + (*(work->hcclEndEvents_))[i].record(hcclStream); | ||
| 4234 | + work->hcclComms_[i] = hcclComms[i]; | ||
| 4235 | + work->blockingWait_ = blockingWait_; | ||
| 4236 | + work->opTimeout_ = options_->timeout; | ||
| 4237 | + work->store_ = store_; | ||
| 4238 | + // Record size info for debug. We only record the size on the first device | ||
| 4239 | + // as multi-device per process is deprecated | ||
| 4240 | + work->numelIn_ = work->numelOut_ = static_cast<size_t>(tensors[i].numel()); | ||
| 4241 | + } | ||
| 4186 | 4242 | ||
| 4187 | - c10_npu::NPUGraph::inc_pending_event_queries(); | 4243 | + c10_npu::NPUGraph::inc_pending_event_queries(); |
| 4188 | - if (asyncErrorHandling_ != NoHandling && capture_status == c10_npu::CaptureStatus::None) { | 4244 | + if (asyncErrorHandling_ != NoHandling && capture_status == c10_npu::CaptureStatus::None) { |
| 4189 | - workEnqueue(work); | 4245 | + workEnqueue(work); |
| 4190 | - } else { | 4246 | + } else { |
| 4191 | - c10_npu::NPUGraph::dec_pending_event_queries(); | 4247 | + c10_npu::NPUGraph::dec_pending_event_queries(); |
| 4248 | + } | ||
| 4192 | } | 4249 | } |
| 4193 | 4250 | ||
| 4194 | - return work; | 4251 | + // When coalescing, return nullptr as the Work will be created in endCoalescing |
| 4252 | + return coalescing_state_ ? nullptr : work; | ||
| 4195 | } | 4253 | } |
| 4196 | 4254 | ||
| 4197 | template <typename Fn> | 4255 | template <typename Fn> |
| @@ -18,11 +18,12 @@ from torch.distributed.distributed_c10d import _get_default_group, get_group_ran | |||
| 18 | ProcessGroup, default_pg_timeout, ReduceScatterOptions, _unregister_process_group, _check_valid_timeout, \ | 18 | ProcessGroup, default_pg_timeout, ReduceScatterOptions, _unregister_process_group, _check_valid_timeout, \ |
| 19 | _find_pg_by_ranks_and_tag, is_initialized, _get_split_source, BackendConfig, is_mpi_available, is_gloo_available, \ | 19 | _find_pg_by_ranks_and_tag, is_initialized, _get_split_source, BackendConfig, is_mpi_available, is_gloo_available, \ |
| 20 | is_nccl_available, is_ucc_available, is_xccl_available, get_debug_level, DebugLevel, _process_group_color, \ | 20 | is_nccl_available, is_ucc_available, is_xccl_available, get_debug_level, DebugLevel, _process_group_color, \ |
| 21 | - _create_process_group_wrapper, _GLOO_AVAILABLE | 21 | + _create_process_group_wrapper, _GLOO_AVAILABLE, _check_p2p_op_list, isend |
| 22 | 22 | ||
| 23 | from torch._C._distributed_c10d import PrefixStore, _register_process_group, _DistributedBackendOptions | 23 | from torch._C._distributed_c10d import PrefixStore, _register_process_group, _DistributedBackendOptions |
| 24 | 24 | ||
| 25 | from torch_npu.utils._error_code import ErrCode, dist_error | 25 | from torch_npu.utils._error_code import ErrCode, dist_error |
| 26 | +from torch_npu.utils import get_cann_version | ||
| 26 | 27 | ||
| 27 | if is_mpi_available(): | 28 | if is_mpi_available(): |
| 28 | from torch.distributed.distributed_c10d import ProcessGroupMPI | 29 | from torch.distributed.distributed_c10d import ProcessGroupMPI |
| @@ -46,28 +47,43 @@ def _batch_isend_irecv(p2p_op_list): | |||
| 46 | group = p2p_op_list[0].group | 47 | group = p2p_op_list[0].group |
| 47 | device = p2p_op_list[0].tensor.device | 48 | device = p2p_op_list[0].tensor.device |
| 48 | is_multi_pg = True | 49 | is_multi_pg = True |
| 50 | + | ||
| 51 | + def peer_kwarg(op): | ||
| 52 | + key = "group_dst" if op.op is isend else "group_src" | ||
| 53 | + return {key: op.group_peer} | ||
| 54 | + | ||
| 49 | if device.type == "cuda": | 55 | if device.type == "cuda": |
| 50 | with _coalescing_manager(group, device, async_ops=True) as cm: | 56 | with _coalescing_manager(group, device, async_ops=True) as cm: |
| 51 | for p2p_op in p2p_op_list: | 57 | for p2p_op in p2p_op_list: |
| 52 | p2p_op.op(p2p_op.tensor, p2p_op.peer, p2p_op.group, p2p_op.tag) | 58 | p2p_op.op(p2p_op.tensor, p2p_op.peer, p2p_op.group, p2p_op.tag) |
| 53 | return cm.works | 59 | return cm.works |
| 54 | elif device.type == "npu": | 60 | elif device.type == "npu": |
| 61 | + cann_version = get_cann_version("CANN") | ||
| 55 | if group is None: | 62 | if group is None: |
| 56 | group = _get_default_group() | 63 | group = _get_default_group() |
| 57 | is_multi_pg = False | 64 | is_multi_pg = False |
| 58 | _group = group._get_backend(device) | 65 | _group = group._get_backend(device) |
| 59 | - op_type = [] | 66 | + if isinstance(group, ProcessGroup) and group._get_backend(device).supports_coalescing and cann_version >= "9.0.0": |
| 60 | - tensors = [] | 67 | + # coalescing manager |
| 61 | - remote_rank_list = [] | 68 | + _check_p2p_op_list(p2p_op_list) |
| 62 | - for p2p_op in p2p_op_list: | 69 | + with _coalescing_manager(group=group, device=device, async_ops=True) as cm: |
| 63 | - if p2p_op.tensor.device.type != "npu": | 70 | + for p2p_op in p2p_op_list: |
| 64 | - deviceType = p2p_op.tensor.device.type | 71 | + p2p_op.op(p2p_op.tensor, group=p2p_op.group, tag=p2p_op.tag, **peer_kwarg(p2p_op)) |
| 65 | - raise RuntimeError(f"No backend type associated with device type {deviceType}" + dist_error(ErrCode.PARAM)) | 72 | + return cm.works |
| 66 | - op_type.append(p2p_op.op.__name__) | 73 | + else: |
| 67 | - tensors.append(p2p_op.tensor) | 74 | + # batch_isend_irecv |
| 68 | - rank_for_op = get_group_rank(group, p2p_op.peer) if is_multi_pg else p2p_op.peer | 75 | + op_type = [] |
| 69 | - remote_rank_list.append(rank_for_op) | 76 | + tensors = [] |
| 70 | - return [_group.batch_isend_irecv(op_type, tensors, remote_rank_list)] | 77 | + remote_rank_list = [] |
| 78 | + for p2p_op in p2p_op_list: | ||
| 79 | + if p2p_op.tensor.device.type != "npu": | ||
| 80 | + deviceType = p2p_op.tensor.device.type | ||
| 81 | + raise RuntimeError(f"No backend type associated with device type {deviceType}" + dist_error(ErrCode.PARAM)) | ||
| 82 | + op_type.append(p2p_op.op.__name__) | ||
| 83 | + tensors.append(p2p_op.tensor) | ||
| 84 | + rank_for_op = get_group_rank(group, p2p_op.peer) if is_multi_pg else p2p_op.peer | ||
| 85 | + remote_rank_list.append(rank_for_op) | ||
| 86 | + return [_group.batch_isend_irecv(op_type, tensors, remote_rank_list)] | ||
| 71 | else: | 87 | else: |
| 72 | # Backward support for Gloo | 88 | # Backward support for Gloo |
| 73 | reqs = [] | 89 | reqs = [] |