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

Method flushReduceBucket

csrc/compile/z3.cpp:252–339  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

250 }
251
252 void flushReduceBucket(at::ScalarType scalar_type) override
253 {
254 if (!hasKey(reduce_tasks_, scalar_type)) { return; }
255
256 blockCopyEvents(scalar_type);
257
258 // Calculate temporary buffer size for accumulated gradients or
259 // communication/storage dtype mismatches.
260 int64_t tmp_recv_numel = 0;
261 for (const ReduceTask& t : reduce_tasks_.at(scalar_type)) {
262 auto recv_buf = param_registry_->getParam(t.getDSId()).getGradBuffer();
263 int64_t recv_numel = recv_buf.numel();
264 bool use_tmp_recv = recv_numel > 0 && (has_acc_grad_.at(t.getDSId()) ||
265 recv_buf.scalar_type() != scalar_type);
266 if (use_tmp_recv) { tmp_recv_numel += recv_numel; }
267 }
268
269 // Allocate temporary buffer if needed
270 at::Tensor tmp_recv_buf = at::Tensor();
271 if (tmp_recv_numel > 0) {
272 at::cuda::CUDAStreamGuard guard(rs_stream_);
273 tmp_recv_buf = torch::empty({tmp_recv_numel},
274 at::TensorOptions().dtype(scalar_type).device(at::kCUDA));
275 }
276
277 applyPreDivision(scalar_type);
278
279 // NCCL ReduceScatter operation
280 ncclGroupStart();
281 int64_t offset = 0;
282 for (const ReduceTask& t : reduce_tasks_.at(scalar_type)) {
283 auto recv_buf = param_registry_->getParam(t.getDSId()).getGradBuffer();
284 bool acc_grad = has_acc_grad_.at(t.getDSId());
285 int64_t recv_numel = recv_buf.numel();
286 bool use_tmp_recv =
287 recv_numel > 0 && (acc_grad || recv_buf.scalar_type() != scalar_type);
288
289 if (use_tmp_recv) {
290 recv_buf =
291 tmp_recv_buf.index({torch::indexing::Slice(offset, offset + recv_numel)});
292 }
293
294 ncclResult_t result = ncclReduceScatter(t.getSendBuf().data_ptr(),
295 recv_buf.data_ptr(),
296 recv_numel,
297 get_nccl_data_type(scalar_type),
298 getReductionOp(),
299 nccl_comm_,
300 rs_stream_);
301 if (result != ncclSuccess) { throw std::runtime_error("NCCL ReduceScatter failed"); }
302
303 if (use_tmp_recv) { offset += recv_numel; }
304 }
305 ncclGroupEnd();
306
307 // Move temporary receive results into the ZeRO grad buffer.
308 {
309 at::cuda::CUDAStreamGuard guard(rs_stream_);

Callers

nothing calls this directly

Calls 12

hasKeyFunction · 0.85
get_nccl_data_typeFunction · 0.85
getGradBufferMethod · 0.80
getDSIdMethod · 0.80
getSendBufMethod · 0.80
record_streamMethod · 0.80
numelMethod · 0.45
deviceMethod · 0.45
dtypeMethod · 0.45
data_ptrMethod · 0.45
toMethod · 0.45
copy_Method · 0.45

Tested by

no test coverage detected