Skip to content

feat(training): support configurable loss fn via training args#542

Merged
shanjiaz merged 2 commits into
vllm-project:mainfrom
christinaexyou:config-loss-fn
May 28, 2026
Merged

feat(training): support configurable loss fn via training args#542
shanjiaz merged 2 commits into
vllm-project:mainfrom
christinaexyou:config-loss-fn

Conversation

@christinaexyou

Copy link
Copy Markdown
Contributor

PLEASE FILL IN THE PR DESCRIPTION HERE ENSURING ALL CHECKLIST ITEMS (AT THE BOTTOM) HAVE BEEN CONSIDERED.

Purpose

This PR addresses #541

Description

It adds a --loss-fn argument and input validation to scripts/train.py. If defined, it passes the resolved loss function to get_trainer_kwargs() so each model's compute_metrics call uses arg.loss_fn. If args.loss_fn=None, it falls back to the previously hardcoded loss fn.

Tests

I added unit tests here tests/unit/train/test_cli_args.py to test that arg.loss_fn is passed correctly to each model's get_trainer_kwargs() and that we maintain backward compatibility

I have filled in:

  • The purpose of the PR, such as "Fix some issue (link existing issues this PR will resolve)".
  • The test plan/results, such as providing test command and pasting the results.
  • (Optional) The necessary documentation update.
  • I (a human) have written or reviewed the code in this pr to the best of my ability.

@coderabbitai

coderabbitai Bot commented May 21, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: afabf081-c732-4d5a-925a-fb5b548634a3

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Walkthrough

This PR adds configurable loss function selection across draft model training via a new --loss-fn CLI argument. A resolve_loss_fn utility maps string names (kl, ce) to loss callables. The CLI parses and validates the argument, then each model family (DFlash, Eagle3, PEagle) threads the resolved loss function through training/validation kwargs and metric computations.

Changes

Loss function configurability

Layer / File(s) Summary
Loss resolution foundation
src/speculators/models/metrics.py
Introduces resolve_loss_fn(name: str) that maps "kl" and "ce" to kl_div_loss and ce_loss respectively, with validation for unknown names.
CLI argument and validation
scripts/train.py
Adds --loss-fn CLI option (choices kl, ce), imports resolve_loss_fn, and validates the argument after parsing.
DFlash loss function threading
src/speculators/models/dflash/core.py, src/speculators/models/dflash/metrics.py
DFlash model imports loss utilities, adds loss_fn parameter (default ce_loss) to forward(), updates get_trainer_kwargs() to resolve and return loss_fn in both train and validation kwargs, and passes loss_fn to compute_metrics().
Eagle3 loss function threading
src/speculators/models/eagle3/core.py, src/speculators/models/eagle3/metrics.py
Eagle3 model imports loss utilities, adds loss_fn parameter (default kl_div_loss) to forward(), updates get_trainer_kwargs() to resolve and return loss_fn in both train and validation kwargs, and passes loss_fn to per-step compute_metrics().
PEagle loss function threading
src/speculators/models/peagle/core.py, src/speculators/models/peagle/metrics.py
PEagle model imports loss utilities, adds loss_fn parameter (default kl_div_loss) to forward(), updates get_trainer_kwargs() to resolve and return loss_fn in both train and validation kwargs, and passes loss_fn to compute_metrics().
CLI argument validation tests
tests/unit/train/test_cli_args.py
Unit tests validate --loss-fn propagation through each model's get_trainer_kwargs(), confirming model-specific defaults (ce for DFlash, kl for Eagle3/PEagle) and correct override behavior.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Suggested labels

enhancement, training, eagle3, dflash, two-reviews

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 48.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'feat(training): support configurable loss fn via training args' accurately summarizes the main change—adding configurable loss function support via CLI arguments for training.
Description check ✅ Passed The description clearly explains the purpose (addressing #541), details the changes (--loss-fn argument, validation, passing to get_trainer_kwargs), mentions tests added, and notes backward compatibility maintenance.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@mergify

mergify Bot commented May 21, 2026

Copy link
Copy Markdown

Merge Protections

Your pull request matches the following merge protections and will not be merged until they are valid.

🟢 Require two reviews

Wonderful, this rule succeeded.

PRs labelled "two-reviews" must have at least two approving reviews before merging.

  • #approved-reviews-by >= 2

@christinaexyou

christinaexyou commented May 21, 2026

Copy link
Copy Markdown
Contributor Author

I noticed that there were no tests to verify CLI args so I created a new file but lmk if you want me to rename/move the tests :)

@shanjiaz shanjiaz self-requested a review May 25, 2026 02:02
@mergify

mergify Bot commented May 25, 2026

Copy link
Copy Markdown

The quality checks have failed. Please run make style and make quality under
the root directory to address the lint failures. You will need to install the
dev optional install to get the required linting packages:
https://github.com/vllm-project/speculators/blob/main/CONTRIBUTING.md

Comment thread src/speculators/models/peagle/core.py

@shanjiaz shanjiaz 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.

Looks great! Pending quality check.

Comment thread scripts/train.py Outdated
Comment thread src/speculators/models/dflash/core.py Outdated
Comment thread src/speculators/models/eagle3/core.py
Comment thread src/speculators/models/peagle/core.py
@mergify

mergify Bot commented May 26, 2026

Copy link
Copy Markdown

The quality checks have failed. Please run make style and make quality under
the root directory to address the lint failures. You will need to install the
dev optional install to get the required linting packages:
https://github.com/vllm-project/speculators/blob/main/CONTRIBUTING.md

@fynnsu fynnsu 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! Should be almost good to merge

Comment thread src/speculators/models/dflash/core.py Outdated
Comment thread scripts/train.py Outdated
@mergify

mergify Bot commented May 27, 2026

Copy link
Copy Markdown

The quality checks have failed. Please run make style and make quality under
the root directory to address the lint failures. You will need to install the
dev optional install to get the required linting packages:
https://github.com/vllm-project/speculators/blob/main/CONTRIBUTING.md

@shanjiaz shanjiaz 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.

Great job! Thanks for contributing.

@shanjiaz shanjiaz enabled auto-merge (squash) May 27, 2026 14:50
@mergify

mergify Bot commented May 27, 2026

Copy link
Copy Markdown

The quality checks have failed. Please run make style and make quality under
the root directory to address the lint failures. You will need to install the
dev optional install to get the required linting packages:
https://github.com/vllm-project/speculators/blob/main/CONTRIBUTING.md

auto-merge was automatically disabled May 27, 2026 19:14

Head branch was pushed to by a user without write access

@mergify mergify Bot removed the quality-failed label May 27, 2026
@mergify

mergify Bot commented May 27, 2026

Copy link
Copy Markdown

The quality checks have failed. Please run make style and make quality under
the root directory to address the lint failures. You will need to install the
dev optional install to get the required linting packages:
https://github.com/vllm-project/speculators/blob/main/CONTRIBUTING.md

@mergify

mergify Bot commented May 27, 2026

Copy link
Copy Markdown

The quality checks have failed. Please run make style and make quality under
the root directory to address the lint failures. You will need to install the
dev optional install to get the required linting packages:
https://github.com/vllm-project/speculators/blob/main/CONTRIBUTING.md

Comment thread src/speculators/models/dflash/metrics.py Outdated
Signed-off-by: Christina Xu <chrxu@redhat.com>
@shanjiaz shanjiaz enabled auto-merge (squash) May 28, 2026 13:24
@mergify mergify Bot removed the quality-failed label May 28, 2026
@fynnsu fynnsu disabled auto-merge May 28, 2026 13:48
@shanjiaz shanjiaz merged commit e99dadc into vllm-project:main May 28, 2026
14 checks passed
Sawyer117 added a commit to Sawyer117/speculators that referenced this pull request Jun 22, 2026
DFlash's hardcoded default was ce_loss until PR vllm-project#542, which per issue vllm-project#541 was
only meant to make the loss configurable but accidentally flipped DFlash's
default to kl_div (never validated for DFlash). Pass --loss-fn explicitly via a
new LOSS_FN config var (default "ce") so our runs use the validated objective;
override with LOSS_FN=kl_div. Uses the upstream --loss-fn flag (no core-code
divergence). The core default fix will go via a separate upstream issue/PR.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sawyer117 added a commit to Sawyer117/speculators that referenced this pull request Jun 22, 2026
Re-pins BASELINE.md links to 6b03432 (the commit that defaults DFlash loss to
ce) and updates the hyperparameter table + notes: loss is now --loss-fn ce (the
validated DFlash default per issue vllm-project#541), not the unvalidated kl_div vllm-project#542
default. noise-std/scheduler remain upstream defaults.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sawyer117 added a commit to Sawyer117/speculators that referenced this pull request Jun 22, 2026
 denom, vllm-project#542, vocab)

Reference doc resolving the recurring "~3.5 vs ~12 loss" confusion:
- The reported loss is a decay-weighted reduction; PR vllm-project#496 (0d73ed0) changed the
  denominator Σ(mask·decay) -> Σ(mask), scaling reported loss + gradient ~3.4x
  (12/3.5 = Σmask/Σdecay). Pre-vllm-project#496 ~12, post-vllm-project#496 ~3.5. Implies ~3.4x lower
  effective LR post-vllm-project#496.
- Not ce-vs-kl (both ~3.5 after the reduction); vllm-project#542 separately flipped the
  default ce->kl_div contra vllm-project#541.
- Debunks the "vLLM defaults vocab to 32000" claim: launch_vllm never sets vocab,
  run uses full 151,936; ln(32000) vs ln(151936) is ~1.5 apart, not 12 vs 3.5.
- SpecForge ref (soft-CE, ÷B*T, no decay; eagle3 uses LK loss) not comparable.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.

3 participants