Skip to content

megabatch: harden global_comm_dim_size contract#77

Merged
JohnLangford merged 1 commit into
mainfrom
fix/megabatch-padded-size-followup
May 6, 2026
Merged

megabatch: harden global_comm_dim_size contract#77
JohnLangford merged 1 commit into
mainfrom
fix/megabatch-padded-size-followup

Conversation

@JohnLangford

Copy link
Copy Markdown
Contributor

Follow-up to #75 addressing three review nits without changing the perf win or the API contract.

Changes

  1. assert → explicit raise in megabatch_orthogonalize_async. Both the missing-arg check and the FSDP2 sharding-invariant check vanish under python -O; without them, a missing arg degenerates into a confusing TypeError deep inside the ceil-div, and a violated invariant silently produces the wrong pad size. Now ValueError / RuntimeError so the contract holds regardless of optimization level.

  2. Drop the = None default on global_comm_dim_size. The signature said optional but the function requires it on the sharded path; all three production callers (dion2, muon, normuon) already pass it. Forcing explicit pass-through prevents silent regressions if a new caller forgets the arg.

  3. isinstance(X[0], DTensor) checks at the three caller extraction sites. The comment "X[0] is still a DTensor here" is a load-bearing precondition — .shape returns the global size only for DTensor. A future refactor that moves to_local(X) above the extraction would silently produce a local-size-based pad target. The RuntimeError in megabatch_base would eventually catch it, but from a confusing site; the new TypeError at the caller fails fast with a clear message.

Also: switch the ceil-div from -(-x // y) to (x + y - 1) // y for readability, and update the NOTE comment that referenced the removed assert.

Tests

Adds a CPU-only single-rank gloo test that drives the sharded branch with global_comm_dim_size=None and pins the new ValueError. Catches python -O / future-refactor regressions at the unit level without burning a GPU slot.

Existing test_megabatch_empty_shard tests remain unchanged.

Follow-up to #75. Three small hardening fixes for the new
``global_comm_dim_size`` parameter:

1. Replace ``assert`` with explicit raise. Both the missing-arg check and
   the FSDP2 sharding-invariant check disappear under ``python -O``;
   without them, a missing arg degenerates into a confusing
   ``TypeError`` deep inside the ceil-div, and a violated invariant
   silently produces the wrong pad size. Use ``ValueError`` /
   ``RuntimeError`` so the contract holds regardless of optimization.

2. Drop the ``= None`` default on ``global_comm_dim_size``. The signature
   said optional but the function actually requires it on the sharded
   path; all three production callers (``dion2``, ``muon``, ``normuon``)
   already pass it. Forcing explicit pass-through prevents silent
   regressions where a new caller forgets the arg.

3. Add ``isinstance(X[0], DTensor)`` checks at the three caller
   extraction sites. The comment "X[0] is still a DTensor here" is a
   load-bearing precondition (``.shape`` returns the global size only
   for DTensor); a future refactor that moves ``to_local(X)`` above the
   extraction would silently produce a local-size-based pad target. The
   ``RuntimeError`` in megabatch_base would eventually catch it, but
   from a confusing site -- the TypeError at the caller fails fast with
   a clear message.

Also switch the ceil-div from ``-(-x // y)`` to the more readable
``(x + y - 1) // y``, and tighten the NOTE comment that referenced the
removed assert.

Tests: add a CPU-only single-rank gloo test pinning the new ValueError
contract, so ``python -O`` regressions are caught at the unit-test
level.
@JohnLangford JohnLangford merged commit 4dc5728 into main May 6, 2026
3 checks passed
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.

1 participant