Skip to content

Fix FileNotFoundError in DPO/KTO precompute_ref_log_probs - #6348

Merged
qgallouedec merged 1 commit into
huggingface:mainfrom
Functionhx:fix/dpo-precompute-cache
Jul 9, 2026
Merged

Fix FileNotFoundError in DPO/KTO precompute_ref_log_probs#6348
qgallouedec merged 1 commit into
huggingface:mainfrom
Functionhx:fix/dpo-precompute-cache

Conversation

@Functionhx

@Functionhx Functionhx commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #6291. When precompute_ref_log_probs=True, the computed cache_file path wasn't passed to dataset.map(), so datasets writes to a different random path than TRL reads from -- causing FileNotFoundError. Fixed in both DPOTrainer and KTOTrainer.

Before submitting

AI writing disclosure

  • AI-assisted

Who can review?

Anyone in the community.

In _precompute_ref_logps(), dataset.map() was called without
cache_file_name, causing the datasets library to write cache to a
different path than the one TRL computed and subsequently tried to
read from. Pass cache_file_name=cache_file so the cache is written
to the expected location.

Fixes KTOTrainer._precompute_ref_logps() which had the same bug.

Fixes #6291

Test Plan:
  python -c "
from datasets import Dataset
from trl import DPOConfig, DPOTrainer
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = 'danish-foundation-models/munin-apertus-8b'
tokenizer = AutoTokenizer.from_pretrained(model_name)
tokenizer.pad_token = tokenizer.eos_token
model = AutoModelForCausalLM.from_pretrained(model_name, device_map='cpu')
ds = Dataset.from_dict({'prompt': ['x']*4, 'chosen': ['a']*4, 'rejected': ['b']*4})
cfg = DPOConfig(output_dir='/tmp/test-dpo', precompute_ref_log_probs=True)
trainer = DPOTrainer(model=model, ref_model=None, args=cfg, train_dataset=ds, processing_class=tokenizer)
print('DPOTrainer initialized successfully')
  "
  # Expected: initializes without FileNotFoundError

Signed-off-by: Yuchen Fan <functionhx@gmail.com>
@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Your PR has been closed because it does not follow the required PR template. Please open a new PR following the template. See CONTRIBUTING.md for guidance.

@github-actions github-actions Bot closed this Jul 9, 2026
@qgallouedec

Copy link
Copy Markdown
Member

Thanks, I'll approve this pr exceptionally because the fix is correct and small, but please in the future remember that we don't welcome purely AI generated PRs

@qgallouedec qgallouedec reopened this Jul 9, 2026
@bot-ci-comment

bot-ci-comment Bot commented Jul 9, 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 c457d97 into huggingface:main Jul 9, 2026
12 of 13 checks passed
@Functionhx

Copy link
Copy Markdown
Contributor Author

Noted, thanks.

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.

DPOTrainer fails with FileNotFoundError when precompute_ref_log_probs=True

2 participants