Repository navigation
[2/2] Add --fake_quant_weight_cache to the Megatron-Bridge distill example - #3047
kevalmorabia97 wants to merge 1 commit into
Conversation
Open the fake-quant weight cache for each QAD step through Megatron-Bridge training callbacks, so every micro-batch and activation recomputation of a step reuses the student's fake-quantized weights. distill() takes no callbacks, so the script calls pretrain() with the same ModelOpt step. test_qad checks that a cached run saves bit-identical weights to an uncached one. Co-authored-by: Frank Sun <[email protected]> Co-authored-by: Sweta Priyadarshi <[email protected]> Co-authored-by: Keith Wyss <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Keval Morabia <[email protected]>
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. 🗂️ Base branches to auto review: 3
Please check the settings in the CodeRabbit UI or the
|
shengliangxu
left a comment
There was a problem hiding this comment.
Bot review (bedrock-claude-opus-5-5) — DM the bot to share feedback.
Nudge: the callback integration looks correct and the bit-identical cached-vs-uncached test is solid, but every run, flagged or not, now bypasses Bridge's distill(). A human with Megatron-Bridge context should confirm that's safe.
Needs action:
- Confirm that calling
pretrain(config, forward_step_modelopt, callbacks=...)matches Bridge'sdistill()in the nemo:26.08 and 26.10 containers. The only check it skips should be the mrope context-parallel one, which this script doesn't need. - Run
test_qad[qwen3_5_moe_vl]andtest_qad[qwen3_lora_nvfp4]on multiple GPUs. The VLM case now passes--fake_quant_weight_cacheand, whenHAS_MROPE_CP_FIXis set, runs atcp_size>1with the Bridge check skipped. Neither case was run. - Rebase onto
mainonce #3034 merges, so only thedistill.py, README, CHANGELOG and test changes remain in this diff.
No action needed:
- The
test_mlflow_utils.pyassertion edit is justified: it tracks thedistill()→pretrain()call change and isn't weaker. - The
ExitStackreset inon_train_startcorrectly handles a scope left open after an in-process restart. - About 322 core-logic lines, including #3034's, which is under the 500-line budget. New files carry the standard header, and the code is NVIDIA-internal, not third-party.
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## kmorabia/fake-quant-weight-cache #3047 +/- ##
=================================================================
Coverage 79.09% 79.09%
=================================================================
Files 662 662
Lines 79097 79097
=================================================================
Hits 62559 62559
Misses 16538 16538
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Folded into #3034 (commit f272fd7), which now uses |
What does this PR do?
Type of change: new feature
Part 2 of 2, stacked on #3034 (merge that first). Until it merges, this PR also shows #3034's two commits; only the last commit (
Add --fake_quant_weight_cache ...) is new here.Adds
--fake_quant_weight_cachetoexamples/megatron_bridge/distill.py. It turns on the fake-quant weight cache from #3034 for QAD: each step fake-quantizes every student weight once and reuses it for all micro-batches and activation recomputations of that step.on_train_startenrolls the student's weight quantizers,on_train_step_start/on_train_step_endopen and close the scope), so no Bridge code is patched. The scope closes before evaluation and the next step, which see the updated weights.distill()takes no callbacks, so the script now callspretrain(config, forward_step_modelopt, callbacks=...)directly, which is whatdistill()does. The only thing skipped is a context-parallel check for distilling a whole mrope VLM, which this script never does (it distills only the language model).--student_megatron_pathand fails at startup otherwise.Why a flag and not always on: the cache keeps an extra BF16 copy of the student's quantized weights on every GPU. On the large-MoE run below that is about +21 GiB/GPU, leaving ~10 GiB of headroom; recipes that already run near the memory limit would go out of memory on upgrade. Some recipes also cannot use it (
pass_through_bwd=False, composed quantizers such as W4A8) and enrollment rejects them.Usage
torchrun --nproc_per_node 8 distill.py \ --student_megatron_path /output/qwen3_8b_nvfp4_ptq ... \ --fake_quant_weight_cacheTesting
tests/examples/megatron_bridge/test_qad.py: the non-LoRA LLM case now trains with--fake_quant_weight_cache, then trains again without it, and checks that every saved model tensor is bit-identical.test_qad[qwen3]passes innemo:26.08(1 GPU), and fails when the cache's backward is changed to return a zero gradient. I did not run the LoRA and VLM cases (they do not use the cache path).tests/examples/megatron_bridge/test_mlflow_utils.py: 75 passed.QAD of a large MoE model (GGML IQ2_S + NVFP4 weight-only student, 32 nodes × 4 GB300, GBS 3840, 6 iterations each, 846 cached quantizers per rank):
−16% step time on average, about −20% by iterations 5-6. KD loss matched within 0.1% per iteration, the same as run-to-run noise (iteration 1 already differs by 1e-4). GGML caches its packed codes and only re-decodes per forward, so formats with more expensive fake quant should gain more.
Before your PR is "Ready for review"
CONTRIBUTING.md: N/A (adapted from NVIDIA-internal code by the co-authors; no third-party code or new dependency)Additional Information
Part 1: #3034. Co-authored with Frank Sun, Sweta Priyadarshi and Keith Wyss, whose training-loop integration this follows.
🤖 Generated with Claude Code
Summary by CodeRabbit