Skip to content

perf: reduce DPO log-prob memory usage - #10720

Open
Tonystarkw12 wants to merge 1 commit into
hiyouga:mainfrom
Tonystarkw12:feat/dpo-memory-efficient-loss
Open

perf: reduce DPO log-prob memory usage#10720
Tonystarkw12 wants to merge 1 commit into
hiyouga:mainfrom
Tonystarkw12:feat/dpo-memory-efficient-loss

Conversation

@Tonystarkw12

Copy link
Copy Markdown

What does this PR do?

Reduces peak memory usage during DPO log-probability computation.

Instead of materializing logits.log_softmax(-1) for the full [batch, sequence, vocabulary] tensor, this computes target-token log probabilities in sequence chunks
using gather and logsumexp. This preserves the same outputs and gradients while limiting temporary float32 allocations.

LD-DPO keeps the existing implementation because it needs per-token log probabilities for its custom masking logic.

Motivation

The full-vocabulary float32 log-softmax tensor can cause out-of-memory failures during preference training, especially with long sequences and large vocabularies.
Chunking across the sequence dimension lowers temporary memory without changing model outputs.

Tests

  • Added numerical equivalence coverage against get_batch_logps
  • Added gradient equivalence coverage
  • ruff check and ruff format --check pass
  • WANDB_DISABLED=true pytest -q --import-mode=importlib tests/train/test_dpo_trainer.py `passes```

Compute target-token log probabilities in sequence chunks instead of
materializing a full-vocabulary log-softmax tensor. Preserve LD-DPO
behavior and add numerical and gradient equivalence coverage.

Co-Authored-By: Claude <noreply@anthropic.com>
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.

1 participant