Skip to content

fix(ddp): bucket_size=True silently disables grad bucketing - #374

Merged
JacoCheung merged 1 commit into
NVIDIA:mainfrom
JacoCheung:junzhang/fix_ddp_config
Apr 29, 2026
Merged

JacoCheung merged 1 commit into
NVIDIA:mainfrom
JacoCheung:junzhang/fix_ddp_config

Conversation

@JacoCheung

@JacoCheung JacoCheung commented Apr 29, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

apply_megatron_ddp in examples/commons/distributed/sharding.py:89 constructs DistributedDataParallelConfig(..., bucket_size=True). The field is typed Optional[int] (size threshold for grad bucket flush; None = use Megatron's default). True is a bool, which slips past Python's runtime because bool is a subclass of int and would compare as 1 if 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). Tracing bucket_size=True through Megatron with our exact config (overlap_grad_reduce=False):

distributed_data_parallel.py:54-75

# 1) Default fallback: True is not None, so this branch is skipped
if ddp_config.bucket_size is None:
    ddp_config.bucket_size = max(40000000, 1000000 * dp_size)

# 2) overlap_grad_reduce=False -> bucket_size is FORCED to None here, clobbering True
if not ddp_config.overlap_grad_reduce:
    ddp_config.bucket_size = None

self.bucket_size = self.ddp_config.bucket_size  # -> None

param_and_grad_buffer.py bucket-split test:

if (bucket_size is not None and (param_end_index - bucket_start_index) >= bucket_size) \
    or _does_param_require_new_bucket(param):
    ...

Because step (2) zeros bucket_size to None, the size-based split short-circuits in the buffer. At runtime today this config behaves identically to bucket_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=False clobber is the only thing keeping True from reaching the comparison. The moment anyone flips overlap_grad_reduce=True (the obvious next optimization for DP training overlap), step (2) is skipped, and (param_end_index - bucket_start_index) >= True is evaluated as >= 1 per 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

-        bucket_size=True,
+        bucket_size=None,

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.buckets to assert the bucket count is O(num_dtype_groups) rather than O(num_params). The fix is a one-token correction of an Optional[int] field given a bool; the type error itself is the proof, and the test infrastructure cost dwarfs the change.

Test plan

  • CI green on existing HSTU + DLRM training jobs (no behavior change with the current overlap_grad_reduce=False)
  • (Optional sanity) Spot-check a DP training run with overlap_grad_reduce=True flipped locally to confirm bucketing now activates correctly

Generated with Claude Code.

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-apps

greptile-apps Bot commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a one-line bug in apply_megatron_ddp where bucket_size=True was passed to DistributedDataParallelConfig. Because Python's bool is a subclass of int and True == 1, this silently capped every gradient bucket at a single element, degrading bucketed allreduce into per-tensor allreduce for all DP training runs. Changing the value to None restores Megatron's default bucket sizing behavior.

Confidence Score: 5/5

Safe 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

Filename Overview
examples/commons/distributed/sharding.py Single-line fix: bucket_size=True → bucket_size=None in DistributedDataParallelConfig; corrects implicit bool-to-int coercion that capped buckets at 1 element and degraded allreduce efficiency.

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
Loading

Reviews (1): Last reviewed commit: "fix(ddp): bucket_size=True silently disa..." | Re-trigger Greptile

@JacoCheung
JacoCheung requested review from jiashuy and shijieliu April 29, 2026 07:27
@JacoCheung
JacoCheung merged commit e8518ca into NVIDIA:main Apr 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants