Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions deepspeed/runtime/zero/stage3.py
Original file line number Diff line number Diff line change
Expand Up @@ -1540,7 +1540,9 @@ def _apply_distributed_muon_update(self, communication_data_type: torch.dtype, b
start_offset = rank * chunk_sz
end_offset = start_offset + chunk_sz
if end_offset > param.grad.numel():
buffer_to_update = torch.zeros(chunk_sz, device=param.grad.device, dtype=param.grad.dtype)
buffer_to_update = torch.zeros(chunk_sz,
device=param.grad.device,
dtype=self.gradient_accumulation_dtype)
buffer_to_update[:param.grad.numel() -
start_offset] = gathered_momentum.view(-1).data[start_offset:param.grad.numel()]
else:
Expand Down Expand Up @@ -1584,8 +1586,8 @@ def __avg_scatter_contiguous_grads(self, buffer_to_reduce: Tensor,
if self.postscale_gradients and self.gradient_predivide_factor != world_sz:
buffer_to_reduce = buffer_to_reduce.mul(self.gradient_predivide_factor)

if communication_data_type != self.dtype:
buffer_to_reduce = buffer_to_reduce.to(self.dtype)
if communication_data_type != self.gradient_accumulation_dtype:
buffer_to_reduce = buffer_to_reduce.to(self.gradient_accumulation_dtype)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve fp32 dtype when padding contiguous partitions

When ZeRO-3 runs with contiguous_gradients=True, reduce_scatter=False, fp32 gradient accumulation, and a low-precision communication dtype, this conversion makes buffer_to_reduce fp32, but the padding branch below still allocates padded_partition with dtype=grad.dtype. For any parameter whose size is not evenly divisible by the data-parallel world size, the real tail partition is assigned into that low-precision padded tensor before partition_grads() copies it to the fp32 accumulation buffer, so those gradients are silently rounded back to fp16/bf16. The padded tensor should use the converted partition/buffer dtype, not the original parameter grad dtype.

Useful? React with 👍 / 👎.


grad_partitions = []
grad_offset_in_buffer = 0
Expand All @@ -1599,7 +1601,9 @@ def __avg_scatter_contiguous_grads(self, buffer_to_reduce: Tensor,

partition = buffer_to_reduce[start_offset:end_offset]
if param.partition_numel() != partition.numel():
padded_partition = torch.zeros(param.partition_numel(), device=grad.device, dtype=grad.dtype)
padded_partition = torch.zeros(param.partition_numel(),
device=grad.device,
dtype=self.gradient_accumulation_dtype)
if partition.numel() > 0:
padded_partition[:partition.numel()] = partition
grad_partitions.append(padded_partition)
Expand Down Expand Up @@ -1636,8 +1640,8 @@ def __avg_scatter_grads(self, params_to_reduce: List[Parameter],
self.dp_process_group):
grad_partitions_for_rank = [g.mul(self.gradient_predivide_factor) for g in grad_partitions_for_rank]

if communication_data_type != self.dtype:
grad_partitions_for_rank = [g.to(self.dtype) for g in grad_partitions_for_rank]
if communication_data_type != self.gradient_accumulation_dtype:
grad_partitions_for_rank = [g.to(self.gradient_accumulation_dtype) for g in grad_partitions_for_rank]

return grad_partitions_for_rank

Expand Down
Loading