Skip to content

fix: use minimum sequence divisibility factor - #3563

Open
NolenLiang wants to merge 1 commit into
mainfrom
agent/fix-sequence-length-divisibility
Open

NolenLiang wants to merge 1 commit into
mainfrom
agent/fix-sequence-length-divisibility

Conversation

@NolenLiang

@NolenLiang NolenLiang commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Supersedes #3028 with the minimum sequence-length divisibility constraint derived from the actual Megatron sharding order.

  • Context parallelism requires the global sequence length S to be divisible by 2 * CP.
  • TP sequence parallelism runs on the CP-local sequence, so it requires (S / CP) % TP == 0, equivalently S % (CP * TP) == 0.
  • The minimum joint factor is therefore:
lcm(2 * CP, CP * TP) = CP * lcm(2, TP)

This is not lcm(2 * CP, TP), which can be too small, and it is not generally 2 * CP * TP, which overpads by 2x for even TP.

Root cause

Megatron first reshapes each sequence into 2 * cp_size balanced chunks and selects two chunks per CP rank, leaving a CP-local length of S / CP (source). When sequence parallelism is enabled, the embedding path passes that local sequence to the TP group (source); its split explicitly requires the first dimension to be divisible by TP size (source).

The previous product calculation treated the TP constraint as an independent global factor and multiplied it by the full CP factor, double-counting the factor of 2 when TP is even.

Changes

  • Add a backend-neutral helper that computes lcm(2 * CP, CP * TP) and validates positive multiples.
  • Use the same validator in the Megatron packed-sequence path and both DTensor setup paths.
  • Reject DTensor CP together with TP sequence parallelism before model/distributed initialization because that combination remains unsupported.
  • Add decisive coverage for CP=2, TP=4, SP=true: factor 4 is rejected, while factors 8 and 16 are accepted.
  • Update Megatron parameter coverage so factor 8 is accepted for both CP=4, TP=2 and CP=2, TP=4.

8-GPU validation

Megatron sequence-divisibility probe

The probe called the exact MCore CP partition, TP sequence-parallel scatter, and backward all-gather primitives on 8 H100 GPUs with CP=2 and TP=4.

Global sequence length CP partition TP/SP forward Backward all-gather
4 Pass Fail Not run
8 Pass Pass Pass
16 Pass Pass Pass

This directly rejects lcm(2*CP, TP)=4 and shows that the product 2*CP*TP=16 is not necessary; the derived minimum lcm(2*CP, CP*TP)=8 passes end to end.

Experiment source version: Megatron-LM 002255075c3728fded9a2e435677840b08560d55. Runtime: PyTorch 2.13.0a0+8145d630e8.nv26.06. The corresponding PyTorch 2.11 source was separately audited and has the same CP divisibility behavior.

@copy-pr-bot

copy-pr-bot Bot commented Aug 10, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@NolenLiang

Copy link
Copy Markdown
Contributor Author

/ok to test 4319b6a

@NolenLiang
NolenLiang marked this pull request as ready for review August 10, 2026 01:57
@NolenLiang
NolenLiang requested review from a team as code owners August 10, 2026 01:57
@NolenLiang NolenLiang added the CI:L1 Run doctests, unit tests, and functional tests label Aug 10, 2026
@NolenLiang

Copy link
Copy Markdown
Contributor Author

/ok to test 4319b6a

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:L1 Run doctests, unit tests, and functional tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant