Repository navigation
[5/8] Add the tvd_ce objective - #5
Conversation
There was a problem hiding this comment.
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_ceaccepts 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_cenormalizes 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, |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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
What does this PR do?
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_ceadds a CE term over the base's argmax token, weighted byexternal_tvd_alpha/external_ce_alpha. Works on both the dense and sparse paths, throughone shared
_mix_cehelper built onF.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_ceand 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_acceptanceis parametrized overtvd_ce, so onereference assertion pins the CE target, the one-position shift and both weights.
test_dense_tvd_ce_is_weighted_tvd_plus_ce_on_the_base_top1does the same for the densepath against the reference formula.
test_tvd_ce_ignores_rows_the_dump_left_emptycovers both empty-row guards, loss andaccuracy.
test_every_sparse_capable_loss_runs_and_backpropspicks uptvd_ceviaSPARSE_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"
CONTRIBUTING.md: N/A — no copied code, no new dependenciesAdditional Information
OSS provenance. No line added by this PR is copied from another project, with or
without modification. Nothing was added to the
insert-licenseexclude list, and there areno new PIP dependencies.