Skip to content

(feat) Fold the CP axis into the GTP_remat weight-sharding group - #6886

Closed
fanshiqing wants to merge 5 commits into
NVIDIA:mainfrom
fanshiqing:shiqingf/gtp_cp_support_mr
Closed

fanshiqing wants to merge 5 commits into
NVIDIA:mainfrom
fanshiqing:shiqingf/gtp_cp_support_mr

Conversation

@fanshiqing

@fanshiqing fanshiqing commented Aug 26, 2026 •

Copy link
Copy Markdown
Member
  • I, the PR author, have personally reviewed every line of this PR.

What does this PR do?

Adds GTP_remat + Context Parallelism (CP) support: optionally fold the CP axis into the
dense GTP_remat weight-sharding group
, so CP ranks also shard dense weights.

  • --gtp-remat-fold-cp (default off): dense weight group becomes cp × gtp_remat
    (at GTP_remat 1: the CP group). Off keeps the plain gtp_remat group, giving a clean
    with/without-folding baseline on the same config. Expert weights never fold.
  • Correctness under folding: DDP bucket skips CP for folded params
    (excludes_cp_from_bucket); checkpoint writer election excludes CP when folded, includes it
    when not; init seeds use the folded group.
  • Optimizer checkpoint fix: dp_reshardable keys of folded buffers get a .cp_rank_{i}
    suffix — previously CP ranks' FP32 masters / Adam moments aliased on one key. Other keys
    unchanged; fully_reshardable unaffected.
  • Docs: §2.1 knob table, §3.8 rewritten to match the code.

Test plan

  • Grad parity, folded vs unfolded vs exact fp32 reference (CP4×GTP_remat1, CP2×GTP_remat2)
  • Rank-distinct ckpt round-trips (optimizer dp_reshardable + model weights), folded and unfolded
  • Distinct init seeds; parallel_state group shapes with the knob on/off; Muon parity at CP4
  • Full GTP UT suite green on 4 GPUs (basics, dcp, muon, grad_correctness, partial_cg, tp_gtp, moe_egtp, muon_dcp)
  • 8-GPU shapes (skipped locally) and an end-to-end CP+GTP_remat training run

Convergence Test

To be added.

Issue tracking

Linked issue: Related to #6844

Contribution process

Pre-checks

  • I have added relevant unit tests
  • I have added relevant functional tests
  • I have added proper typing to my code Typing guidelines
  • I have added relevant documentation
  • I have run the autoformatter.sh on my PR

@fanshiqing
fanshiqing requested review from a team as code owners August 26, 2026 11:52
@copy-pr-bot

copy-pr-bot Bot commented Aug 26, 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.

@svcnvidia-nemo-ci
svcnvidia-nemo-ci marked this pull request as draft August 26, 2026 11:53
@github-actions

Copy link
Copy Markdown
Contributor

This PR has been automatically converted to draft because all PRs must start as drafts.

When you are ready for review, click Ready for Review to begin the review process. This will:

  1. Add the oncall reviewer (optional reviewer)
  2. Add required review teams based on your changes

See the contribution guide for more details.

@fanshiqing

Copy link
Copy Markdown
Member Author

/claude review

@fanshiqing fanshiqing mentioned this pull request Aug 26, 2026
@fanshiqing fanshiqing linked an issue Aug 26, 2026 that may be closed by this pull request
@fanshiqing
fanshiqing marked this pull request as ready for review August 26, 2026 11:55
@fanshiqing

Copy link
Copy Markdown
Member Author

/ok to test 5e799be

@claude

This comment was marked as outdated.

@yaox12

yaox12 commented Aug 27, 2026

Copy link
Copy Markdown
Member

/claude strict-review

Comment thread megatron/core/process_groups_config.py Resolved
Comment thread megatron/core/optimizer/param_layout.py Resolved
Comment thread megatron/core/process_groups_config.py Resolved
@fanshiqing
fanshiqing requested review from a team as code owners September 23, 2026 17:12
fanshiqing and others added 3 commits September 23, 2026 16:32
Signed-off-by: Shiqing Fan <shiqingf@nvidia.com>
CP ranks of a CP-folded GTP weight shared one checkpoint key, so their
optimizer state silently aliased on save/load. Key now includes the CP
rank for those buffers only.

Co-authored-by: Dingqing Yang <dingqingy@nvidia.com>
Signed-off-by: Shiqing Fan <shiqingf@nvidia.com>
Signed-off-by: Dingqing Yang <dingqingy@nvidia.com>
CP folding into the GTP_remat weight group is now opt-in, at any
GTP_remat size including 1. Also fix init seeds and checkpoint writer
election for these modes, and restore a broken test_gtp_muon.py.

Co-authored-by: Dingqing Yang <dingqingy@nvidia.com>
Signed-off-by: Shiqing Fan <shiqingf@nvidia.com>
Signed-off-by: Dingqing Yang <dingqingy@nvidia.com>
@fanshiqing
fanshiqing force-pushed the shiqingf/gtp_cp_support_mr branch from cda0ad5 to 9de4bef Compare September 23, 2026 23:38
@fanshiqing

Copy link
Copy Markdown
Member Author

/ok to test 9de4bef

@fanshiqing

Copy link
Copy Markdown
Member Author

/claude review

@claude

This comment was marked as resolved.

- LayerWiseDistributedOptimizer.count_zeros(): drop the gtp_remat_group /
  expt_gtp_remat_group kwargs that count_zeros_fp32 never accepted
  (TypeError on --log-num-zeros-in-grad), plus the now-dead attributes.
- DistributedOptimizer: reject CP-folded GTP buffers in the dp_zero /
  fully_reshardable save and load paths. They gather over the global DP
  group keyed by gbuf_idx, which can't represent per-CP-rank distinct
  shards; point users to dp_reshardable.
- gtp_replica_rank: fix the stale error message so it names the right
  replica group (dp / dp_cp / expt_dp).
- Docs: fix stale TOC entries for section 3.8.
- Add regression tests for count_zeros and the fully_reshardable rejection.

Signed-off-by: Shiqing Fan <shiqingf@nvidia.com>
@fanshiqing

Copy link
Copy Markdown
Member Author

/ok to test cd54188

@fanshiqing

Copy link
Copy Markdown
Member Author

/claude review

Comment thread megatron/training/initialize.py Resolved

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Light review — read the diff, skills/mcore-testing/SKILL.md, skills/pr-review/references/light.md, and style-guide.md.

The design is coherent and unusually well-documented: the CP-free vs CP-folded group split, the per-param gtp_bucket_dp_divisor / excludes_cp_from_bucket stamps, and the .cp_rank_{i} optimizer-key suffix all line up with each other and with 3.8. Test coverage for the new behavior is genuinely good — grad parity against an exact fp32 reference, writer-election gap checks, rank-distinct optimizer round-trips, and a fully_reshardable rejection test.

One blocking issue.

--gtp-remat-fold-cp is never registered with argparse (critical). megatron/training/initialize.py:405 reads args.gtp_remat_fold_cp, but no add_argument for it exists in arguments.py or yaml_arguments.py. Since that line is unconditional, every pretrain_*.py run that reaches _initialize_distributed raises AttributeError — regardless of CP or GTP settings. The unit tests pass gtp_remat_fold_cp straight to ps.initialize_model_parallel() and never go through arguments.py, which is why the suite is green. Details and a suggested add_argument block are in the inline comment.

This also means the is_gtp_remat_active(args) extension in common_utils.py:607 and the validate_args gate at arguments.py:1545 can never observe a true gtp_remat_fold_cp, so none of the GTP validation (--fp4-param-gather, --ddp-average-in-collective off, --ckpt-format) currently applies to a fold-CP-only config once the flag does exist. Worth confirming those gates behave as intended at gtp_weight_remat_size == 1 with folding on — the one shape where folding alone activates dense GTP.

Notes, non-blocking, no action needed:

  • distributed_data_parallel.py:292 calls the average_in_collective branch unreachable for excludes_cp_from_bucket. That holds: the assert at line 245 rejects average_in_collective whenever is_gtp_remat_active(process_group_dict), and a folded group always has size() > 1. The comment is accurate.
  • resolve_buffer_dp_world_size divides the caller-supplied dp_cp size by the divisor to recover dp, matching the group DDP assigns at line 321. The two stay consistent.
  • No new direct parallel_state.get_*_group() reads outside the allowed compatibility points — the new get_gtp_weight_remat_group_no_cp accessor lives in parallel_state.py and is plumbed through ProcessGroupCollection, with finalize_model_grads preferring pg_collection.gtp_remat_no_cp and falling back only when absent. The random.py and generalized_tensor_parallelism.py MPU reads replace existing ones rather than adding new coupling.
  • No GPU kernel changes, so no tests/unit_tests/determinism/kernels/ entry is required.

The PR description already lists 8-GPU shapes and an end-to-end CP+GTP_remat training run as outstanding; the argparse gap is presumably why the latter has not run yet.

Signed-off-by: Shiqing Fan <shiqingf@nvidia.com>
@fanshiqing

Copy link
Copy Markdown
Member Author

Will close this MR due to simpler feature been implemented in #7748 by @Mellonta

@fanshiqing
fanshiqing marked this pull request as draft October 9, 2026 02:17
@fanshiqing fanshiqing closed this Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GTP+CP

3 participants