Skip to content

fix(dpo): move chosen padding past rejected sequences in packed concatenation - #2372

Open
ApacheWang wants to merge 2 commits into
allenai:mainfrom
ApacheWang:fix/packed-dpo-trailing-padding
Open

ApacheWang wants to merge 2 commits into
allenai:mainfrom
ApacheWang:fix/packed-dpo-trailing-padding

Conversation

@ApacheWang

Copy link
Copy Markdown

Drafted by Antigravity and Codex.

Summary

Fixes concatenated_inputs in open_instruct/padding_free_collator.py so trailing padding from the packed chosen side is moved after the rejected tokens instead of sitting inside the first rejected sequence's attention document.

Root Cause

TensorDataCollatorWithFlatteningDPO pads chosen and rejected independently to max_seq_length while setting chosen_cu_seq_lens_k[-1] to the unpadded chosen token count. Previously, concatenated_inputs concatenated the full padded chosen tensor in front of rejected and offset rejected_cu_seq_lens_{q,k} by the padded width chosen_features["input_ids"].shape[-1]. As a result:

  • For a pair with chosen length 2, rejected length 3, and max_seq_length=5, concatenated_input_ids was [11, 12, 0, 0, 0, 21, 22, 23, 0, 0] and concatenated_cu_seq_lens_k was [0, 2, 8].
  • The first rejected document spanned [2, 8), causing the first rejected sequence to attend over the 3 leading chosen padding tokens (for example through olmo_core_utils.doc_lens_from_cu_seq_lens in open_instruct/dpo_utils.py).
  • Similarly, seq_idx added chosen_num_seqs to rejected padding entries (-1), turning padding indices into non-negative values.

Changes

  • open_instruct/padding_free_collator.py: Move chosen trailing padding ([chosen_length:]) after rejected for input_ids, labels, position_ids, and seq_idx, offset rejected_cu_seq_lens_{q,k} by the real chosen_length, and preserve -1 padding values in rejected_features["seq_idx"].
  • open_instruct/dpo_utils.py: Add packed_layout_version = 2 to compute_reference_cache_hash when args.packing is enabled so cached reference log-probs computed under the old padded layout are not reused.
  • open_instruct/test_packed_dpo_collation.py & open_instruct/test_padding_free_collator.py: Add regression test test_concatenated_dpo_padding_stays_outside_real_documents and update TestDPOPackingIndices.test_concatenated_cu_seq_lens_with_padding to assert compact document boundaries [0, ..., 100, 150, 200].

Verification

  • Verified RED -> GREEN on CPU:
    • Baseline ff2546d: open_instruct/test_packed_dpo_collation.py fails ([0, 2, 8] != [0, 2, 5] and chosen padding sits before rejected tokens).
    • With fix: open_instruct/test_packed_dpo_collation.py passes (1 passed).
  • ruff check and ruff format --check pass on all modified files.

Signed-off-by: bc15575202581 <bc15575202581@gmail.com>
Signed-off-by: bc15575202581 <bc15575202581@gmail.com>

This branch has not been deployed

No deployments
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