Skip to content

Align KTO with DPO: Log num_tokens metric#6256

Merged
qgallouedec merged 6 commits into
mainfrom
align-kto-dpo-num-tokens-metric
Jul 3, 2026
Merged

Align KTO with DPO: Log num_tokens metric#6256
qgallouedec merged 6 commits into
mainfrom
align-kto-dpo-num-tokens-metric

Conversation

@qgallouedec

@qgallouedec qgallouedec commented Jul 2, 2026

Copy link
Copy Markdown
Member

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

DPOTrainer logs a num_tokens metric (running count of training tokens seen); KTOTrainer did not. Added it for parity:

  • Initialize self._total_train_tokens = 0 in __init__ (next to self._metrics).
  • Accumulate and log num_tokens in both _compute_loss and _compute_loss_liger, matching DPO.

The counter is only incremented in train mode; the metric is logged in both modes.

No change to the loss or existing metrics.


Note

Low Risk
Observability-only metrics wiring with no changes to loss computation or training logic.

Overview
KTOTrainer now reports a running num_tokens training metric so it matches DPOTrainer behavior.

A _total_train_tokens counter is initialized at startup. On each forward in _compute_loss and _compute_loss_liger, training batches add the distributed sum of attention_mask token counts; num_tokens is written into the trainer metrics (train increments the counter; eval logs the current cumulative total without updating it).

No changes to KTO loss or existing reward/KL metrics.

Reviewed by Cursor Bugbot for commit 95cdac0. 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 312efef into main Jul 3, 2026
5 checks passed
@qgallouedec qgallouedec deleted the align-kto-dpo-num-tokens-metric branch July 3, 2026 19:03
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