Skip to content

Align KTO with DPO: Validate Liger kernel configuration before trainer initialization#6227

Merged
albertvillanova merged 4 commits into
mainfrom
align-kto-dpo-liger-validation
Jul 3, 2026
Merged

Align KTO with DPO: Validate Liger kernel configuration before trainer initialization#6227
albertvillanova merged 4 commits into
mainfrom
align-kto-dpo-liger-validation

Conversation

@albertvillanova

@albertvillanova albertvillanova commented Jun 30, 2026

Copy link
Copy Markdown
Member

This PR moves the validation of the use_liger_kernel configuration in the experimental KTOTrainer to before super().__init__(), aligning it with DPOTrainer.

Motivation

The Liger kernel checks (kernel availability, unsupported loss type, precompute_ref_log_probs, and PEFT) depend only on the config and the input model, so there is no reason to run them after the expensive super().__init__() (model loading, dataset preparation, reference model creation, accelerator setup). DPOTrainer already validates these before super().__init__(), while KTOTrainer did it after, so an invalid configuration was rejected only at the end of a costly initialization.

Solution

The validation checks are moved ahead of super().__init__() so an invalid Liger configuration fails fast. The construction of the fused Liger loss stays after super().__init__() because it depends on self.ref_model, which is only resolved during the reference model setup that runs after the parent initialization.

Changes

  • Move the Liger kernel validation checks (availability, loss type, precompute_ref_log_probs, PEFT) before super().__init__() in KTOTrainer
  • Keep the LigerFusedLinearKTOLoss construction after super().__init__(), where self.ref_model is available
  • Use the local model instead of self.model for the PEFT check, since self.model is not yet set before super().__init__()

Note

Low Risk
Init-order refactor only; validation rules are unchanged aside from using the local model for the PEFT check before self.model exists.

Overview
KTOTrainer now runs all use_liger_kernel validation (package install, loss type, compute_metrics, precompute_ref_log_probs, PEFT) before super().__init__(), matching DPOTrainer so bad Liger settings fail immediately instead of after model load, dataset prep, and accelerator setup.

LigerFusedLinearKTOLoss is still constructed only after super().__init__() and reference-model setup, with a short comment that it depends on self.ref_model. The PEFT guard uses the local model argument instead of self.model, which is not assigned until the parent init runs.

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

@bot-ci-comment

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.

Comment on lines +690 to +694
if self.precompute_ref_logps:
raise ValueError(
"You cannot use `precompute_ref_log_probs=True` with liger kernel. Please set "
"`precompute_ref_log_probs=False`."
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

in dpo it's

"Liger DPO loss does not support precomputing reference log probabilities. Either disable "
"`precompute_ref_log_probs` or set `use_liger_kernel` to False."

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This PR is just a move of the Liger validation checks, to before super().__init__(); no error message modification.

Individual error message alignment is handled in other PR:

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want higher recall? High effort reviews run extra passes and find more bugs. A team admin can switch effort levels in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit a6c4cb3. Configure here.

Comment thread trl/experimental/kto/kto_trainer.py
@albertvillanova albertvillanova merged commit ae290f2 into main Jul 3, 2026
5 checks passed
@albertvillanova albertvillanova deleted the align-kto-dpo-liger-validation branch July 3, 2026 16:11
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