Skip to content

Align KTO with DPO: Log entropy metric#6257

Merged
qgallouedec merged 4 commits into
mainfrom
align-kto-dpo-entropy-metric
Jul 3, 2026
Merged

Align KTO with DPO: Log entropy metric#6257
qgallouedec merged 4 commits into
mainfrom
align-kto-dpo-entropy-metric

Conversation

@qgallouedec

@qgallouedec qgallouedec commented Jul 2, 2026

Copy link
Copy Markdown
Member

Part of the effort to align KTOTrainer (experimental) with DPOTrainer. #4786

DPOTrainer logs an entropy metric (mean per-token predictive entropy over completion tokens); KTOTrainer did not. Added it for parity, using the same token-weighted cross-rank averaging as DPO:

  • Import entropy_from_logits from trl.trainer.utils.
  • In _compute_loss, compute per-token entropy over the completion tokens (completion_mask), gather sums and token counts across ranks, and log the weighted mean as entropy.

Only added to _compute_loss, not _compute_loss_liger: the Liger path does not materialize logits, so entropy cannot be computed there (same as DPO).

No change to the loss or existing metrics.


Note

Low Risk
Observability-only metrics logging reusing the same helper and aggregation pattern as DPO; no loss or training logic changes.

Overview
Experimental KTOTrainer now logs mean per-token predictive entropy over completion tokens, matching DPOTrainer behavior for alignment work (#4786).

The change imports entropy_from_logits and, in _compute_loss, computes entropy from detached shift_logits, masks with completion_mask[:, 1:], and records a token-weighted mean after gather_for_metrics across ranks under the entropy metric key. KTO loss and existing metrics are unchanged.

Entropy is not added on the Liger path (_compute_loss_liger), because logits are not materialized there—same limitation as DPO.

Reviewed by Cursor Bugbot for commit 2fdb24f. Bugbot is set up for automated code reviews on this repo. Configure here.

@bot-ci-comment

bot-ci-comment Bot commented Jul 2, 2026

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@qgallouedec qgallouedec merged commit 6ef48c5 into main Jul 3, 2026
5 checks passed
@qgallouedec qgallouedec deleted the align-kto-dpo-entropy-metric branch July 3, 2026 16:06
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.

2 participants