Repository navigation
fix(ddp): bucket_size=True silently disables grad bucketing - #374
Conversation
DistributedDataParallelConfig.bucket_size expects Optional[int] (the size threshold at which grads are flushed as a bucket; None = use Megatron's default). Passing True implicitly converts to int 1, capping every bucket at one element and effectively turning bucketed allreduce into per-tensor allreduce during DP training. The misconfiguration has been latent since the field was introduced. Switch to None so Megatron applies its default bucket size. No failing test added: a faithful regression test would need multi-GPU training plus introspection of Megatron's internal DDP bucket layout, and the fix is a one-token correction of an Optional[int] field that was being given a bool — the type error is itself the proof. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Greptile SummaryThis PR fixes a one-line bug in Confidence Score: 5/5Safe to merge — one-token fix correcting a type mismatch with no logic changes beyond restoring intended bucket behavior. The change is a single-line correction of a bool being passed where Optional[int] is expected, restoring Megatron's default bucket sizing. No new logic is introduced, no interfaces change, and the fix aligns with the documented intent of the field. No files require special attention. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["apply_megatron_ddp()"] --> B["DistributedDataParallelConfig\nbucket_size=None"]
B --> C["Megatron DDP\napplies default bucket size"]
C --> D["Gradient buckets grouped\nby Megatron default threshold"]
D --> E["Efficient bucketed allreduce\nacross DP ranks"]
style B fill:#d4edda,stroke:#28a745
style E fill:#d4edda,stroke:#28a745
subgraph before ["Before (bug)"]
F["bucket_size=True\n(True == 1 in Python)"]
G["Each bucket capped at\n1 gradient element"]
H["Per-tensor allreduce\n(inflated comms time)"]
F --> G --> H
end
style F fill:#f8d7da,stroke:#dc3545
style H fill:#f8d7da,stroke:#dc3545
Reviews (1): Last reviewed commit: "fix(ddp): bucket_size=True silently disa..." | Re-trigger Greptile |
Summary
apply_megatron_ddpin examples/commons/distributed/sharding.py:89 constructsDistributedDataParallelConfig(..., bucket_size=True). The field is typedOptional[int](size threshold for grad bucket flush;None= use Megatron's default).Trueis abool, which slips past Python's runtime becauseboolis a subclass ofintand would compare as1if it ever reached the bucketing path.This PR corrects the typo:
True→None.Current runtime behavior (verified, Megatron core_v0.12.1)
The repo pins Megatron at
core_v0.12.1(docker/Dockerfile:39). Tracingbucket_size=Truethrough Megatron with our exact config (overlap_grad_reduce=False):distributed_data_parallel.py:54-75param_and_grad_buffer.pybucket-split test:Because step (2) zeros
bucket_sizetoNone, the size-based split short-circuits in the buffer. At runtime today this config behaves identically tobucket_size=None— no per-tensor allreduce, no inflated comm time. The bug is masked, not active.Why this still matters (latent footgun)
The
overlap_grad_reduce=Falseclobber is the only thing keepingTruefrom reaching the comparison. The moment anyone flipsoverlap_grad_reduce=True(the obvious next optimization for DP training overlap), step (2) is skipped, and(param_end_index - bucket_start_index) >= Trueis evaluated as>= 1per parameter — every parameter triggers a fresh bucket, degenerating into per-tensor allreduce. The breakage would surface as a sudden DP comm-time regression at the moment overlap is enabled, with no obvious code change to blame.Fixing it now closes the trap before someone walks into it.
Change
One line in examples/commons/distributed/sharding.py:89.
Why no regression test
A faithful regression test would need (a)
overlap_grad_reduce=True, (b) a multi-GPU DP run, (c) introspection of_ParamAndGradBuffer.bucketsto assert the bucket count isO(num_dtype_groups)rather thanO(num_params). The fix is a one-token correction of anOptional[int]field given abool; the type error itself is the proof, and the test infrastructure cost dwarfs the change.Test plan
overlap_grad_reduce=False)overlap_grad_reduce=Trueflipped locally to confirm bucketing now activates correctlyGenerated with Claude Code.