-
Notifications
You must be signed in to change notification settings - Fork 603
Bring offline KD upgrades such as Ghost Token and Top-P to Megatron K… #2459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| import argparse | ||
| import contextlib | ||
| import os | ||
| import warnings | ||
|
|
||
| import torch | ||
| from export_distilled_megatron_to_hf import export_llm_to_hf, save_vlm_to_hf | ||
|
|
@@ -172,9 +173,23 @@ def get_args(): | |
| "--train_iters", type=int, required=True, help="Number of training iterations" | ||
| ) | ||
| parser.add_argument( | ||
| "--no_skip_lm_loss", action="store_true", help="Disable skipping language model loss" | ||
| "--no_skip_lm_loss", | ||
| action="store_true", | ||
| help="DEPRECATED and ignored. Whether the LM loss is skipped is derived from --kd_loss_alpha " | ||
| "(skipped iff alpha == 1.0).", | ||
| ) | ||
| parser.add_argument( | ||
| "--kd_loss_alpha", | ||
| type=float, | ||
| default=0.9, | ||
| help="KD loss weight alpha in (1 - alpha) * lm_loss + alpha * kd_loss. 1.0 skips the LM loss entirely.", | ||
| ) | ||
| parser.add_argument( | ||
| "--kd_loss_scale", | ||
| type=float, | ||
| default=None, | ||
| help="DEPRECATED and ignored. Use --kd_loss_alpha.", | ||
| ) | ||
| parser.add_argument("--kd_loss_scale", type=float, default=1.0, help="KD loss weight") | ||
| parser.add_argument( | ||
| "--no_async_save", | ||
| action="store_true", | ||
|
|
@@ -188,6 +203,19 @@ def get_args(): | |
| help="Restrict the logit KL loss to the teacher's top-k vocabulary entries, " | ||
| "replacing the full-vocab temporaries with [seq, k] ones.", | ||
| ) | ||
| parser.add_argument( | ||
| "--logit_kl_top_p", | ||
| type=float, | ||
| default=None, | ||
| help="Nucleus threshold in (0, 1] applied on top of --logit_kl_topk: only the smallest prefix " | ||
| "of the sorted top-k whose cumulative teacher probability reaches this value is distilled.", | ||
| ) | ||
| parser.add_argument( | ||
| "--logit_kl_top_p_min_k", | ||
| type=int, | ||
| default=1, | ||
| help="Minimum number of top-k entries kept per token when --logit_kl_top_p is active.", | ||
| ) | ||
| parser.add_argument("--lr", type=float, default=1e-4, help="Peak learning rate") | ||
| parser.add_argument("--min_lr", type=float, default=1e-5, help="Minimum learning rate") | ||
| parser.add_argument("--lr_warmup_iters", type=int, default=50, help="Number of LR warmup steps") | ||
|
|
@@ -434,10 +462,22 @@ def _build_model_provider(hf_path, load_weights=True, moe_grouped_gemm=True): | |
| f"sizes differ ({padded['student']} vs {padded['teacher']})." | ||
| ) | ||
|
|
||
| if args.kd_loss_scale is not None: | ||
| warnings.warn( | ||
| "--kd_loss_scale is deprecated and ignored; use --kd_loss_alpha instead.", | ||
| FutureWarning, | ||
| ) | ||
| if args.no_skip_lm_loss: | ||
| warnings.warn( | ||
| "--no_skip_lm_loss is deprecated and ignored; whether the LM loss is skipped is derived " | ||
| "from --kd_loss_alpha (skipped iff 1.0).", | ||
| FutureWarning, | ||
| ) | ||
| kd_config = ModelOptDistillConfig( | ||
| skip_lm_loss=not args.no_skip_lm_loss, | ||
| kd_loss_scale=args.kd_loss_scale, | ||
| kd_loss_alpha=args.kd_loss_alpha, | ||
| logit_kl_topk=args.logit_kl_topk, | ||
| logit_kl_top_p=args.logit_kl_top_p, | ||
| logit_kl_top_p_min_k=args.logit_kl_top_p_min_k, | ||
|
Comment on lines
+477
to
+480
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [IMPORTANT Compatibility] What. Both flags are still accepted by Why it matters. An existing launch script running Suggested fix. Warn when either flag was actually supplied. Both already have a detectable "not supplied" state — if args.kd_loss_scale is not None:
warnings.warn(
"--kd_loss_scale is deprecated and ignored; use --kd_loss_alpha instead.",
FutureWarning,
)
if args.no_skip_lm_loss:
warnings.warn(
"--no_skip_lm_loss is deprecated and ignored; whether the LM loss is skipped is derived "
"from --kd_loss_alpha (skipped iff 1.0).",
FutureWarning,
)[SUGGESTION] Separately: this call site exposes |
||
| ) | ||
|
|
||
| # HF VLM configs expose ``vision_config``; Megatron-Bridge nests the text model under | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[SUGGESTION] This help text no longer describes what
--logit_kl_topkdoes.TopKLogitsKLLoss.forwardnow begins withand then calls
_tp_logsumexpon both — so the loss materializes two full-vocab fp32 tensors and a full-vocab fp32 temporary inside the log-normalizer, and (becausepredictionsrequires grad) keeps a vocab-sized fp32 activation alive until backward. That's the intended cost of switching to full-vocab normalization, and the CHANGELOG documents the semantics change honestly. But "replacing the full-vocab temporaries with[seq, k]ones" is now the opposite of true, and it's the sentence a user reads when deciding whether to enable the flag on a memory-tight run.Suggest describing the actual benefit — the KL is restricted to the teacher's top-k support (a sparser, less noisy target), not that it avoids full-vocab tensors. The same claim in the class docstring's
NOTE:atmodelopt/torch/distill/plugins/megatron.py:422is worth a look for the same reason.