[PyTorch] Type the grouped wgrad from main_grad for distributed weights - #3397
[PyTorch] Type the grouped wgrad from main_grad for distributed weights#3397fanshiqing wants to merge 1 commit into
Conversation
c9a1f5a routed distributed weights into the no-fusion branch, which allocates at the compute dtype -- so under fp32 grad accumulation the reduce-scatter rounded to BF16 on every rank: wgrad alloc fp32 ---> reduce fp32 ---> accum fp32 <- this fix (was compute dtype ---> compute dtype ---> fp32) Two dispatchers beside finalize_weight_grads: weight_grad_buffers() GEMM writes into the implementer's buffer (no per-backward wgrad allocation) weight_grad_dtype() fallback for the single-grouped branches Signed-off-by: Shiqing Fan <shiqingf@nvidia.com>
Greptile SummaryThe PR routes grouped distributed-weight gradients through implementer-provided buffers or allocates them using Confidence Score: 4/5The PR should not merge until grouped backward remains compatible with distributed groups represented by a protocol-bearing leader and plain followers. The new buffer dispatcher checks only the leader for distributed capability but calls grad_buffer on every group member, causing an immediate backward exception for an input shape already accepted by the leader-based dispatcher contract. Files Needing Attention: transformer_engine/pytorch/distributed_weight.py Important Files Changed
Sequence DiagramsequenceDiagram
participant B as Grouped backward
participant D as weight_grad_buffers
participant L as Distributed leader
participant F as Plain follower
B->>D: "weights = [leader, follower]"
D->>L: is_distributed_weight(leader)
L-->>D: true
D->>L: grad_buffer()
L-->>D: buffer
D->>F: grad_buffer()
F--xD: AttributeError
Reviews (1): Last reviewed commit: "[PyTorch] Type the grouped wgrad from ma..." | Re-trigger Greptile |
| if is_distributed_weight(weights[0]): | ||
| buffers = [w.grad_buffer() for w in weights] |
There was a problem hiding this comment.
Leader-only groups break backward
When a distributed weight group uses a protocol-bearing leader with ordinary tensor followers, weight_grad_buffers checks only the leader but calls grad_buffer() on every member, causing backward to fail with AttributeError.
Knowledge Base Used: PyTorch Distributed/Parallel Training Support
|
/te-ci pytorch L1 |
| @@ -1863,6 +1745,7 @@ def _fuser_backward_grouped_tensor( | |||
|
|
|||
| # Get the right wgrad buffers for grouped gemm. | |||
| # Can be a GroupedTensor or list of tensors based on single_grouped_weight. | |||
| wgrad_dtype = weight_grad_dtype(weights, dtype) | |||
There was a problem hiding this comment.
Could we move the wgrad_dtype directly in the place where this is used similar to the grouped_mlp file?
Given that wgrad_dtype is only used only for the single_grouped_weight case, it would increase code readability if we directly move it inside that branch
| @@ -656,7 +575,7 @@ def _compute_grad_params( | |||
| shapes=[weight_shape] * num_groups, | |||
| quantizer=None, | |||
| device=device, | |||
| dtype=dtype, | |||
| dtype=weight_grad_dtype(weights, dtype), | |||
There was a problem hiding this comment.
I dont understand how this will help, since we will never enter this else condition of allocating using torch.empty, if main_grad is attached to the parameter.
There was a problem hiding this comment.
Actually we will, if accumulate_into_main_grad is False, but why are we creating main_grad parameter in that case in Megatron?
Description
c9a1f5a routed distributed weights into the no-fusion branch, which allocates at the compute dtype -- so under fp32 grad accumulation the reduce-scatter rounded to BF16 on every rank:
Two dispatchers beside finalize_weight_grads:
weight_grad_buffers()GEMM writes into the implementer's buffer (no per-backward wgrad allocation)weight_grad_dtype()fallback for the single-grouped branchesc9a1f5a9 routed distributed weights into the no-fusion branch, which allocates at the compute dtype -- so under fp32 grad accumulation the reduce-scatter rounded to BF16 on every rank:Two dispatchers beside finalize_weight_grads:
weight_grad_buffers()GEMM writes into the implementer's buffer (no per-backward wgrad allocation)weight_grad_dtype()fallback for the single-grouped branchesType of change
Changes
Please list the changes introduced in this PR:
Checklist: