Skip to content

[5/8] Add the tvd_ce objective - #5

Merged
sjmaple merged 1 commit into
mainfrom
cr/pr-2639
Oct 20, 2026
Merged

sjmaple merged 1 commit into
mainfrom
cr/pr-2639

Conversation

@sjmaple

@sjmaple sjmaple commented Oct 20, 2026

Copy link
Copy Markdown
Owner

What does this PR do?

Series [5/8]. Rebased onto main now that [1/8] through [4/8] have merged, so this branch carries a single commit and the diff is this slice only.

Type of change: new feature

TVD is bounded, so it discriminates weakly between tokens inside the base's support: moving
probability between two in-support tokens costs it little. Cross-entropy's log penalty has
no such ceiling, so tvd_ce adds a CE term over the base's argmax token, weighted by
external_tvd_alpha / external_ce_alpha. Works on both the dense and sparse paths, through
one shared _mix_ce helper built on F.cross_entropy.

On the sparse path the CE target and the reported accuracy both skip rows the dump left
empty. Such a row has no teacher top-1 -- its argmax is slot 0, i.e. whatever the zero-fill
holds -- and CE's penalty is unbounded, so an unguarded target would drag the draft toward
token id 0. TVD only degrades to its bounded out-of-support term there, which is why this
matters for tvd_ce and not for the other objectives.

Testing

Unit tests only; this slice adds an objective and changes no training entrypoint.

  • test_sparse_tvd_matches_one_minus_acceptance is parametrized over tvd_ce, so one
    reference assertion pins the CE target, the one-position shift and both weights.
  • test_dense_tvd_ce_is_weighted_tvd_plus_ce_on_the_base_top1 does the same for the dense
    path against the reference formula.
  • test_tvd_ce_ignores_rows_the_dump_left_empty covers both empty-row guards, loss and
    accuracy.
  • test_every_sparse_capable_loss_runs_and_backprops picks up tvd_ce via
    SPARSE_CAPABLE_LOSSES.

Acceptance for the trained drafts was measured as part of the series' end-to-end run, which
is described in [1/8]; nothing in this slice changes that path.

Before your PR is "Ready for review"

  • Is this change backward compatible?: ✅
  • If you copied code from any other sources or added a new PIP dependency, did you follow guidance in CONTRIBUTING.md: N/A — no copied code, no new dependencies
  • Did you write any new necessary tests?: ✅
  • Did you update Changelog?: ✅ in [3/8] (one entry for the series)
  • Did you get Claude approval on this PR?: ❌ pending

Additional Information

OSS provenance. No line added by this PR is copied from another project, with or
without modification. Nothing was added to the insert-license exclude list, and there are
no new PIP dependencies.

@tessl-reviewer tessl-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Tessl Reviewer

Advisory findings (3)

The new tvd_ce objective is wired through the dense and sparse paths and includes targeted coverage for its teacher-top-1 and empty-row behavior. Its configuration accepts loss weights that make the objective unusable, sparse malformed token IDs can still reach cross-entropy despite the empty-row mask, and the CE term repeats full-vocabulary work already performed for TVD.

Severity Findings
Major 1
Minor 2

Advisory findings (3)

  • Major · Advisory · Non-blocking · tvd_ce accepts zero or non-finite loss weights
    modelopt/torch/speculative/config.py:506
  • Minor · Advisory · Non-blocking · Masked sparse rows can still send invalid token IDs to cross-entropy
    modelopt/torch/speculative/plugins/hf_external.py:363
  • Minor · Advisory · Non-blocking · tvd_ce normalizes the draft vocabulary twice, including excluded rows
    modelopt/torch/speculative/plugins/hf_external.py:314

Reviews are incremental. The first review shows every valid finding. A later round checks the latest changes and earlier findings, and adds a fresh finding when it still applies. It never repeats one you refuted or declined. Ask for a full review to inspect the whole change and show earlier findings again.

Mention @tessl-reviewer to run another review.


external_ce_alpha: float = ModeloptField(
default=0.1,
ge=0.0,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Major · Advisory · Non-blocking · tvd_ce accepts zero or non-finite loss weights

Both weights are independently constrained only to be non-negative. Consequently, selecting tvd_ce with both weights set to zero produces an identically zero loss and gradient, while an infinite weight passes the constraint and propagates a non-finite loss. These are accepted configurations that can let a training job complete without meaningful training. The selected mixed objective needs finite weights with a positive combined contribution.

Correctness And Data Integrity · Scale And Resilience

teacher_top1 = tok.gather(-1, prob.argmax(-1, keepdim=True)).squeeze(-1)
has_mass = mask * (prob.sum(-1) > 0)
if self.external_loss == "tvd_ce":
loss = self._mix_ce(loss, draft, teacher_top1, has_mass)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Minor · Advisory · Non-blocking · Masked sparse rows can still send invalid token IDs to cross-entropy

The sparse path derives has_mass to exclude empty teacher-policy rows, but _mix_ce invokes cross_entropy on every flattened target before multiplying by that mask. The sparse cache does not validate token-ID bounds, so an empty row containing -1 (or any out-of-range ID) is tolerated by the TVD gather or excluded from scoring, then raises an out-of-bounds target error in tvd_ce. Unscored rows must not be evaluated as CE targets, and scored targets need to be valid for the draft vocabulary.

Security And Privacy


def _mix_ce(self, tvd, draft_logits, target, mask):
"""``tvd_ce``: add CE on ``target``, since bounded TVD ranks in-support tokens weakly."""
ce = torch.nn.functional.cross_entropy(

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Minor · Advisory · Non-blocking · tvd_ce normalizes the draft vocabulary twice, including excluded rows

Both paths first materialize the draft softmax for TVD, then _mix_ce calls cross_entropy, which performs another full-vocabulary normalization for every batch and sequence position before applying the loss mask. At language-model vocabulary sizes this adds a full extra vocabulary-scale operation and allocation, including ordinary padding or context positions that cannot contribute to the loss. The mixed objective needs to reuse the distributional work available from TVD and avoid evaluating excluded rows.

Scale And Resilience

@sjmaple
sjmaple merged commit 94fcebb into main Oct 20, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant