Skip to content

refactor(refit): restore canonical HF pre/post hooks - #4525

Merged
terrykong merged 26 commits into
mainfrom
codex/refit-source-pre-hooks
Oct 17, 2026
Merged

terrykong merged 26 commits into
mainfrom
codex/refit-source-pre-hooks

Conversation

@youngeunkwon0405

@youngeunkwon0405 youngeunkwon0405 commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Restore the pre/base/post contract for nccl_reshard: source hooks prepare current weights in canonical HF layout, and destination hooks assemble and commit received weights, including quantization and scale updates. Transfer loops remain independent of local parameter storage.

Quantized sources decode to BF16; unquantized BF16/FP16/FP32 sources preserve their dtype. LocalParamSpec.wire_dtype and materialize() keep buffers, metadata, receiver allocation, and byte accounting consistent. Remove _refit_layer_source_cache and refit_payload_mode; each preparation reads live parameters. Misc export excludes only source quantization scales associated with bulk weights, preserving destination-generated scales and unrelated entries such as KV-cache scales.

Grouped expert preparation packs block-aligned encoded weights and logical scales, decodes the group once, then selects the HF projection. Unaligned blockwise FP8 experts retain per-expert decoding of partial edge blocks. Gate and up prepare independently without a shared cache. vLLM keeps MXFP8 and square-block FP8 quantization in separate hooks, honors refined runtime MoE scale grids, and quantizes aligned expert groups in one call. Matching unmerged unquantized destinations receive in place; cast, fused, quantized, and TRTLLM layout conversions retain staging. Other refit transports retain their existing transport paths.

Setup rejects unquantized FP32 → vLLM MXFP8 because the quantizer accepts BF16/FP16 input. TE FP8 compute over unquantized Megatron generation parameters requires the effective model_overrides.disable_parameter_transpose_cache option, preventing reuse of stale cached compute weights.

Validation

Current head c82c334b7a3f893c779235d7f20b3f91c5b1d6a0 includes Terry's refined-grid and config-message suggestions, the blockwise partial-block fallback, a shared FP8-storage predicate, and updated documentation. It merges main at d93b4eac7 to satisfy the CI freshness gate; the refit methods are unchanged by this merge.

  • Exact-head GPU validation at c82c334b7a3f893c779235d7f20b3f91c5b1d6a0: 355 passed, 0 failed, 0 skipped; the job completed with exit 0:0. All 978 source hashes matched this commit. Coverage includes source/destination hooks, grouped experts, metadata, refined 32/64 MoE scale grids, repeated refits, and real TE partial-block FP8 decoding. The TE cache diagnostic also passed. The .1006 test environments used the exact upstream-pinned nemo-lens revision 4d045c03186b90afdc815db46b671f6a21bc7e98 installed with --no-deps; the audit verified no other product-package changes. This resolved an initial pre-collection import failure caused by the image's older Lens version. The review-fix commit 6f688bdce also passed all 355 tests before the main merge.
  • CPU utility tests: 165 passed. Receiver-hook emulation with the real blockwise quantizer: 14 passed, including refined 32/64 grids. Both new refined-grid cases fail when the runtime-grid lookup is disabled. CPU checks bypass optional backend imports; GPU results above cover the real container imports.
  • Ruff 0.9.9 lint, import ordering and formatting, git diff --check, and focused Pyrefly 0.24.2 checking passed.
  • Exact-head L1 CI passed lint and branch freshness and is waiting in the test approval queue. The preceding run was superseded after its branch-freshness check failed: the branch was 22 commits behind main.
  • The preceding head 43a166f9b1fecb29f11453e7ec48933da75461e1 passed L1 CI, attempt 3.

Full-model GRPO validation is pinned to the preceding head 9013d0c152cdb86a2afbaa908d259b195a3692d0, not the current head. Runs used async non-colocated Megatron → vLLM, nccl_reshard, online W&B, and a 30-minute job limit. That head also passed 340 GPU unit tests with no failures or skips.

Model BF16 → BF16 BF16 → MXFP8 MXFP8 → MXFP8
Qwen3-30B-A3B, explicit TRTLLM 1.020–1.023, no NaNs 1.032–1.045 finite; NaN at step 1 1.038–1.044 finite; NaNs at steps 1, 3–5
Nemotron Nano-v3, explicit TRTLLM 1.035–1.054, no NaNs Finite; maximum 175.4 Finite; maximum 4,412.8
Qwen3.5-35B-A3B, explicit TRTLLM 1.017–1.020, no NaNs 1.030–1.037, no NaNs 30-minute timeout during step 6

Values are token_mult_prob_error. Actual flashinfer_trtllm MoE backend selection was verified on all eight generation workers in each case. Completed runs have the expected refits on all workers: 10 training steps for Qwen3/Nano and 6 for Qwen3.5. The preceding Qwen3.5 MXFP8 run completed six steps with finite errors 1.032–1.037 and HF fused-expert coverage. Qwen3 BF16, Qwen3 MXFP8, Nano MXFP8, completed Qwen3.5 MXFP8.

Earlier matched Qwen3 base/PR controls showed comparable BF16 logprob agreement near 1.02, and MXFP8's initial NaN also occurred on base/null. The latest expanded matrix has no matching baseline for every anomaly; completion or finite metrics alone does not establish numerical equivalence. BF16/MXFP8 are the acceptance focus. The prior blockwise full-model failures remain unvalidated end to end; partial source blocks and refined destination scale grids are addressed by this follow-up and focused regression tests.

Historical matched Nano-v3 Megatron-generation nccl_reshard runs measured a steps 2–10 median timing/train/prepare_for_generation/transfer_and_update_weights of 0.7815 s on base d7f5215f3 and 0.7480 s on refactor d0433fdf6. This measures transfer and destination update, not raw NCCL latency, and is not a current-head benchmark. The design document labels its older physical-FP8 performance table as predating this refactor.

Commit 43a166f9b1fecb29f11453e7ec48933da75461e1 addresses the per-expert vLLM blockwise quantization overhead by flattening aligned experts for one quantizer call per group, then restoring the weight and scale shapes. Regression tests compare this grouped result exactly with independent expert quantization. The historical timing measurements above predate this optimization.

Matched Nano-v3 TRTLLM null controls completed at the same historical 9013d0c152cdb86a2afbaa908d259b195a3692d0 revision, with identical image, model pins, seed42, ten steps, and async topology. Only transport and logging destinations differed. Both controls completed with ten metric steps, eleven successful refits on each of eight training and eight generation workers, and no NaN/Inf error metrics. Actual source/destination storage and FLASHINFER_TRTLLM selection were verified.

Nano precision nccl_reshard median / maximum null median / maximum
BF16 → MXFP8 1.0671 / 175.4053 (step5) 1.0639 / 1.0731 (step9)
MXFP8 → MXFP8 1.1392 / 4,412.7798 (step3) 1.1617 / 307,114,656 (step4)

The MXFP8 → MXFP8 spike also occurs with null, so that symptom is not exclusive to nccl_reshard. The BF16 → MXFP8 control did not reproduce its historical nccl_reshard spike; that accuracy question remains inconclusive. One matched stochastic run cannot establish absence of rare spikes or prove a regression. These controls address the historical comparison and do not constitute full-model validation of the merged head.

@copy-pr-bot

copy-pr-bot Bot commented Oct 6, 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.

@github-actions github-actions Bot added the Documentation Improvements or additions to documentation label Oct 6, 2026
@youngeunkwon0405 youngeunkwon0405 self-assigned this Oct 6, 2026
@youngeunkwon0405 youngeunkwon0405 changed the title refactor(refit): materialize nccl_reshard training sources through LocalParamSpec.pre refactor(refit): use canonical BF16 buffers in nccl_reshard hooks Oct 8, 2026

@youngeunkwon0405 youngeunkwon0405 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Self-review (agent-assisted). The core contract checks out: source hooks dequantize before HF slicing and read the live parameter each refit; scale exclusion is consistent; the vLLM destination hooks match vLLM 0.29's checkpoint-loader layout bit-for-bit in CPU emulation (including refined/rectangular grids and repeated refits); TE quantized destinations commit whole and in place; and the refit_payload_mode removal is complete, also in a trial merge with main (no semantic conflict with #3724 or the Megatron-Bridge bump).

Inline: two CI blockers in the new test file (isort hook; [mxfp8] on H100), a validation relaxation that newly admits a config where refits are lost (with a pinning test), and a perf-evidence ask.

All suggested code was applied together to this head: ruff 0.9.9 clean; weight-sync + refit-signature tests 389 passed (388 at this head plus the new pinning case). CPU only; GPU and vLLM runtime behavior not verified here.

Generated by Claude Code

Comment thread docs/design-docs/nccl-reshard-refit.md Resolved
Comment thread docs/design-docs/nccl-reshard-refit.md Resolved
Comment thread docs/design-docs/nccl-reshard-refit.md Resolved
@youngeunkwon0405 youngeunkwon0405 added the CI:L1 Run doctests, unit tests, and functional tests label Oct 13, 2026
@youngeunkwon0405 youngeunkwon0405 changed the title refactor(refit): use canonical BF16 buffers in nccl_reshard hooks refactor(refit): restore canonical HF pre/post hooks Oct 13, 2026
@youngeunkwon0405

Copy link
Copy Markdown
Contributor Author

/ok to test adde433

@youngeunkwon0405

Copy link
Copy Markdown
Contributor Author

/ok to test 2b3b5c8

@youngeunkwon0405

Copy link
Copy Markdown
Contributor Author

/ok to test b566d6b

@youngeunkwon0405

Copy link
Copy Markdown
Contributor Author

/ok to test 503e360

@youngeunkwon0405

Copy link
Copy Markdown
Contributor Author

/ok to test 7aa6183

@youngeunkwon0405

Copy link
Copy Markdown
Contributor Author

/ok to test 5344232

@youngeunkwon0405

Copy link
Copy Markdown
Contributor Author

/ok to test 9853d1c

@youngeunkwon0405

Copy link
Copy Markdown
Contributor Author

/ok to test db834af

@youngeunkwon0405 youngeunkwon0405 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Please address those comments. Those are all doc/comment modification requests or variable renaming request, no logic changes.
Also, please check the test files. I believe that, due to this refactoring, many of the existing tests related to nccl-reshard refit path might become outdated or useless. It might be better to purge the stale test cases.

Comment thread docs/design-docs/nccl-reshard-refit.md Resolved
Comment thread docs/design-docs/nccl-reshard-refit.md Resolved
@youngeunkwon0405

Copy link
Copy Markdown
Contributor Author

/ok to test 9013d0c

@youngeunkwon0405 youngeunkwon0405 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Round-2 self-review (agent-assisted; 6 reviewers + 2 devil's advocates, all findings re-verified from source). Thanks for addressing the round-1 items: 25 of the 28 resolved threads are implemented at this head, the removal of refit_payload_mode and the source cache is complete, the TE 2.18 scale packing and the MCore/Bridge/vLLM 0.29 API usage check out with permalinks, and the stale-test purge is sound (13 deletions, all justified; expert ordering is still pinned by test_build_hf_to_local_param_map_train_side).

What this review asks for (each inline, with a verified suggestion):

  1. Transpose-cache escape hatch is inert (nccl_reshard_utils.py): the guard and the doc footnote point at a key nothing applies to the inference provider. Suggested: read it from mcore_generation_config.model_overrides, plus the test fixture and footnote.
  2. FP32 training → vLLM MXFP8 passes setup and fails inside the first refit on SM100; main rejected it at setup and the table footnote calls it unsupported. Suggested: setup-time rejection, flip the one grid cell in the test, reword the footnote.
  3. vLLM direct params lost in-place receive (_default_param_spec always stages); the Megatron side and the design doc still describe in-place. Suggested: dispatch mirroring direct_spec, with the two test adjustments (or a doc reword if always-staging is intended).
  4. Dead _scale_inv skip + stale comment in _iter_local_hf_param_shards.
  5. One duplicate check_nccl_reshard_refit_support test.

Verification on a clean copy with all five applied: ruff 0.9.9 check / check --select I / format --check clean; test_nccl_reshard_utils.py 159 passed, test_nccl_reshard_backend.py 54 passed (vLLM import stub), test_megatron_worker.py 106 passed, test_group_experts.py 19 passed, test_megatron_bf16_refit.py 25 passed (CPU, torch 2.10; CUDA/TE-only cases skipped or failing identically at head). The changed pinning tests fail at this head exactly where expected (3 guard cases, 4 in-place cases) and pass with the fixes.

Deferred, not blocking the draft: the refit-timing numbers from thread 5 (scoped to "before Ready for review"), and refreshing the description's validation pins (db834af4e / 6f0bbc7ef; the exact-head /ok to test is comment 6178393103) before un-drafting. Low-severity notes not posted inline: the blockwise 128-alignment check in _dequantize_refit_source_group is stricter than TE/main and raises at first refit rather than at prepare (a documented scope limit), and vLLM 0.29 refines MoE FP8 block grids to 64×64 when intermediate/TP % 128 != 0 (the moe_block_shape read was reverted after thread 16), so a doc sentence stating the 128-grid requirement for vLLM blockwise destinations would help.

Generated by Claude Code

Comment thread docs/design-docs/nccl-reshard-refit.md Resolved
@youngeunkwon0405
youngeunkwon0405 marked this pull request as ready for review October 15, 2026 03:37
@youngeunkwon0405

Copy link
Copy Markdown
Contributor Author

/ok to test 74295f6

@youngeunkwon0405

Copy link
Copy Markdown
Contributor Author

/okay to test 43a166f

@terrykong terrykong left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for this refactor. The source side is much simpler now: every source spec has its own pre hook, the per-layer source cache and the refit_payload_mode plumbing are gone (that plumbing had already broken the collective recovery path once), and LocalParamSpec.materialize() puts the dtype check in one place for all three transfer loops. Keeping MXFP8 and blockwise FP8 in separate vLLM hooks is the right call, and the exact-match test of the grouped decode against TE's own dequantize is a good guard. Both self-review rounds are fully addressed at 74295f6. The later push 43a166f (one quantize cast per expert group) changes none of the findings below; permalinks stay pinned to 74295f6.

Inline: one setup-time regression for vLLM blockwise-FP8 MoE layers (the comment on vllm_backend.py, with a short fix), a decode that now rejects unaligned blockwise experts, an error message that leads users to drop their training model_overrides, and two small cleanups. Nothing was run on a GPU for this review; the GPU-side claims are traced in the pinned vLLM, TE and Megatron source.

Before merge (not inline):

  1. L1 green at the exact head. The final commit changes the vLLM receive path (same-dtype params now receive in place) after the full-model runs at 9013d0c. grpo_nccl_reshard_refit.sh covers it.
  2. An FP8 refit timing after the change. The FP8 path now sends BF16 (2 bytes per element) and re-quantizes, instead of sending physical FP8 (1 byte). One FP8-source run against base would do.
  3. A matched baseline (same config and seed on base, or refit_transport=null) for the Nemotron Nano-v3 MXFP8 maxima (175.4 and 4,412.8). L1 only runs dense Qwen3-0.6B in BF16, so it says nothing about these cells.

Visual explainer: https://terrykong.github.io/gh-pages-poc/terryk/pr-4525-fp8-wire-path.html
Visual explainer: https://terrykong.github.io/gh-pages-poc/terryk/pr-4525-moe-scale-grid.html

Generated by Claude Code

@youngeunkwon0405

Copy link
Copy Markdown
Contributor Author

/ok to test 6f688bd

@youngeunkwon0405

Copy link
Copy Markdown
Contributor Author

/ok to test c82c334

@youngeunkwon0405

youngeunkwon0405 commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor Author

Following up on the three before-merge checks in this review:

  1. Exact-head L1: The current head is c82c334b7a3f893c779235d7f20b3f91c5b1d6a0. L1 CI has passed lint and branch freshness and is waiting in the test approval queue. Separately, focused GPU validation on this exact head completed with 355 passed, 0 failed, 0 skipped, including source/destination hooks, grouped experts, and metadata; the TE cache diagnostic also passed. The requested L1 functional result is still pending.

  2. FP8 refit timing: 43a166f9 replaces per-expert blockwise FP8 receiver quantization with one quantizer call per aligned expert group, removing the associated per-expert temporary copies. This addresses receiver overhead; the BF16 wire payload and source dequantization remain unchanged. But this is an expected tradeoff we are willing to pay.

  3. Matched Nano controls: Both refit_transport=null controls completed at the same historical 9013d0c152cdb86a2afbaa908d259b195a3692d0 revision as the reported nccl_reshard runs, using the same image, model pins, configuration, seed 42, and async non-colocated Megatron → vLLM setup. Only transport and logging destinations differed. Each completed ten training steps and eleven successful refits on all eight training and eight generation workers. Actual source/destination storage and FLASHINFER_TRTLLM selection were verified; no NaN/Inf error metrics occurred.

token_mult_prob_error results:

Nano precision nccl_reshard median / maximum null median / maximum
BF16 → MXFP8 1.0671 / 175.4053 1.0639 / 1.0731
MXFP8 → MXFP8 1.1392 / 4,412.7798 1.1617 / 307,114,656

We regard these as occasional spikes in quantized async GRPO, with no established link to this refactoring PR. Large spikes also occur with refit_transport=null, and the median errors remain comparable. Understanding the cause and frequency of these spikes is worth investigating in future work.

@terrykong
terrykong enabled auto-merge (squash) October 16, 2026 21:08
@terrykong
terrykong merged commit 98d2db2 into main Oct 17, 2026
122 checks passed
@terrykong
terrykong deleted the codex/refit-source-pre-hooks branch October 17, 2026 02:33
alexandery-nvidia added a commit that referenced this pull request Oct 17, 2026
…failures-and-redispatch

main gained #4481, #4771, #4458, #4770, #4640, #4765, #4525 and #4670 since the
previous merge. #4640 (rubric-mean and fixed-baseline GenRM scoring) rewrote the
metric assertions in tests/unit/experience/test_rollouts.py that this branch also
edits for nemo_gym_infra_redispatches: the resolution keeps main's per-key
assertions and adds the redispatch count beside them, and the expected
rollout_metrics dict carries both sides' new keys. test_rollouts.py,
test_rollout_manager.py and test_nemo_gym.py pass locally on the merged tree.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: alexandery <[email protected]>
alexandery-nvidia added a commit that referenced this pull request Oct 17, 2026
…andery/gym-health-check-during-collection

Carries the stack root's merge of main (8338530: #4481, #4771, #4458, #4770,
#4640, #4765, #4525 and #4670) forward so this head stays current with main.
No conflicts; test_rollouts.py, test_rollout_manager.py and test_nemo_gym.py
pass locally on the merged tree.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: alexandery <[email protected]>
alexandery-nvidia added a commit that referenced this pull request Oct 17, 2026
…xandery/gym-rollout-metrics-hook

Carries the stack's merge of main (8338530: #4481, #4771, #4458, #4770, #4640,
#4765, #4525 and #4670) forward. #4640 (rubric-mean and fixed-baseline GenRM
scoring) reports the shared GenRM fields once per group through
_nemo_gym_group_metrics and skips them in both per-agent loops, which this
branch had replaced with per_agent_result_metrics: the resolution keeps the
helper on both paths and adds the same _SHARED_GENRM_METRICS skip inside it,
so the manager no longer imports the constant. test_rollouts.py,
test_rollout_manager.py and test_nemo_gym.py pass locally on the merged tree.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: alexandery <[email protected]>
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 Documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants