MCPcopy Create free account
hub / github.com/deepspeedai/DeepSpeed / launchAllGather

Method launchAllGather

csrc/compile/z3.cpp:68–117  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

66 }
67
68 void launchAllGather(at::Tensor output_buf,
69 long ds_id,
70 c10::intrusive_ptr<c10d::symmetric_memory::SymmetricMemory> symm_mem)
71 {
72 const DSParam& param = param_registry_->getParam(ds_id);
73 at::Tensor ds_tensor = param.getDSTensor();
74
75 if (ds_tensor.scalar_type() != output_buf.scalar_type()) {
76 at::cuda::CUDAStreamGuard guard(ag_stream_);
77 ds_tensor = ds_tensor.to(output_buf.scalar_type(), true, true);
78 }
79
80 if (symm_mem == nullptr) {
81 // Fast path: assume uniform shard sizes (ZeRO-3 partitions are padded to uniform size)
82 const int world_size = process_group_->getSize();
83 const int64_t shard_elems = ds_tensor.numel();
84
85 // Perform all-gather directly into the pre-allocated padded output buffer
86 // NCCL requires contiguous storage; use .contiguous() explicitly
87 ncclResult_t result = ncclAllGather(ds_tensor.contiguous().data_ptr(),
88 output_buf.data_ptr(),
89 shard_elems,
90 get_nccl_data_type(ds_tensor.scalar_type()),
91 nccl_comm_,
92 ag_stream_);
93
94 if (result != ncclSuccess) { throw std::runtime_error("NCCL AllGather failed"); }
95 } else {
96 at::cuda::CUDAStreamGuard guard(ag_stream_);
97 int world_size = process_group_->getSize();
98 int rank = process_group_->getRank();
99
100 at::Tensor local_buf =
101 symm_mem->get_buffer(rank, ds_tensor.sizes(), ds_tensor.scalar_type(), 0);
102 local_buf.copy_(ds_tensor, true);
103
104 symm_mem->barrier(0, TIMEOUT_SYMMETRIC_MEMORY_BARRIER);
105 auto chunks = output_buf.flatten().chunk(world_size);
106 for (int step = 0; step < world_size; step++) {
107 int remote_rank = (rank - step + world_size) % world_size;
108 auto src_buf = symm_mem->get_buffer(
109 remote_rank, ds_tensor.sizes(), ds_tensor.scalar_type(), 0);
110 chunks[remote_rank].copy_(src_buf.flatten(), true);
111 }
112 symm_mem->barrier(0, TIMEOUT_SYMMETRIC_MEMORY_BARRIER);
113 }
114
115 param_registry_->registerGatheredParam(ds_id, output_buf);
116 param_registry_->setValid(ds_id, true);
117 }
118
119 at::Tensor allgatherParam(long ds_id,
120 std::optional<at::ScalarType> dtype,

Callers

nothing calls this directly

Calls 13

get_nccl_data_typeFunction · 0.85
getDSTensorMethod · 0.80
getSizeMethod · 0.80
contiguousMethod · 0.80
registerGatheredParamMethod · 0.80
setValidMethod · 0.80
toMethod · 0.45
numelMethod · 0.45
data_ptrMethod · 0.45
get_bufferMethod · 0.45
sizesMethod · 0.45
copy_Method · 0.45

Tested by

no test coverage detected