Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

fix: preserve padding-free DPO sample boundaries in fast Llama/Qwen2 (#14251)

cherry77-cloud7f4736d3837e
diff --git a/tests/utils/test_packing.py b/tests/utils/test_packing.py
index cf8c6353555..8908a1a7d75 100644
--- a/tests/utils/test_packing.py
+++ b/tests/utils/test_packing.py
@@ -295,6 +295,27 @@ def test_varlen_from_position_ids():
assert packing_module._varlen_from_position_ids(None) is None
+def test_get_packed_info_infers_lengths_from_reset_position_ids():
+ cpu = torch.device("cpu")
+ lengths, cu_seqlens, max_seqlen = packing_module.get_packed_info_from_kwargs(
+ {}, cpu, torch.tensor([[0, 1, 2, 0, 1]])
+ )
+ assert lengths.tolist() == [3, 2]
+ assert cu_seqlens.tolist() == [0, 3, 5]
+ assert max_seqlen == 3
+ # Opt-in only, and ordinary or batched positions are not a pack.
+ assert packing_module.get_packed_info_from_kwargs({}, cpu) is None
+ assert packing_module.get_packed_info_from_kwargs({}, cpu, torch.arange(5)[None]) is None
+ assert (
+ packing_module.get_packed_info_from_kwargs({}, cpu, torch.tensor([[0, 1], [0, 1]])) is None
+ )
+ explicit = torch.tensor([4, 1], dtype = torch.int32)
+ lengths, _, _ = packing_module.get_packed_info_from_kwargs(
+ {"packed_seq_lengths": explicit}, cpu, torch.tensor([[0, 1, 2, 0, 1]])
+ )
+ assert lengths.tolist() == [4, 1]
+
+
def test_seq_idx_from_cu_seqlens_handles_trailing_pad():
cu = torch.tensor([0, 2, 5], dtype = torch.int32)
boundaries, seq_idx = packing_module._seq_idx_from_cu_seqlens(cu, total = 8)
@@ -1823,6 +1844,44 @@ def test_enable_padding_free_metadata():
assert trainer.args.remove_unused_columns is False
+# has_real_accelerator: the body falls back to xpu, so this covers XPU too.
[email protected](not has_real_accelerator(), reason = "builds a real model on an accelerator")
+def test_position_only_padding_free_keeps_samples_apart():
+ # TRL's padding-free DPO flattens chosen + rejected into one row with reset position_ids only.
+ device = torch.device("cuda" if torch.cuda.is_available() else "xpu")
+ try:
+ model, _ = FastLanguageModel.from_pretrained(
+ model_name = "hf-internal-testing/tiny-random-LlamaForCausalLM",
+ max_seq_length = 64,
+ load_in_4bit = False,
+ dtype = torch.float32,
+ )
+ except OSError as exc: # pragma: no cover - offline CI
+ pytest.skip(f"Requires access to tiny llama checkpoint: {exc}")
+ model.to(device)
+ model.train()
+ first, second = 13, 9
+ ids = torch.randint(5, 900, (1, first + second), generator = torch.Generator().manual_seed(0))
+ ids = ids.to(device)
+ arange = lambda n: torch.arange(n, device = device)[None]
+
+ def hidden(input_ids, position_ids):
+ with torch.no_grad():
+ out = model.model(input_ids = input_ids, position_ids = position_ids, use_cache = False)
+ return out.last_hidden_state.float()
+
+ with ExitStack() as stack:
+ stack.enter_context(patch.object(attention_dispatch_utils, "HAS_FLASH_ATTENTION", False))
+ stack.enter_context(patch.object(attention_dispatch_utils, "HAS_XFORMERS", False))
+ packed = hidden(ids, torch.cat([arange(first), arange(second)], dim = 1))[0, first:]
+ alone = hidden(ids[:, first:], arange(second))[0]
+ crossed = hidden(ids, arange(first + second))[0, first:]
+ leak = (packed - alone).abs().max().item()
+ control = (crossed - alone).abs().max().item()
+ assert control > 0
+ assert leak <= 1e-3 * control
+
+
# has_real_accelerator: the body falls back to xpu, so this covers XPU too.
@pytest.mark.skipif(not has_real_accelerator(), reason = "builds a real 4bit model on an accelerator")
def test_packing_sdpa(tmp_path, monkeypatch):
diff --git a/unsloth/models/llama.py b/unsloth/models/llama.py
index bf1406beb9c..aba2f68328e 100644
--- a/unsloth/models/llama.py
+++ b/unsloth/models/llama.py
@@ -1211,6 +1211,20 @@ def LlamaModel_fast_forward(
seq_length_with_past = seq_length
+ # TRL padding-free DPO sends reset position_ids only: infer lengths before the overlength cut.
+ if (
+ kwargs.get("packed_seq_lengths") is None
+ and batch_size == 1
+ and attention_mask is None
+ and past_key_values is None
+ and position_ids is not None
+ # Context-parallel shards see shard-local positions; boundaries from them are wrong.
+ and get_cp_manager() is None
+ ):
+ seq_info = get_packed_info_from_kwargs(kwargs, position_ids.device, position_ids)
+ if seq_info is not None:
+ kwargs["packed_seq_lengths"] = seq_info[0]
+
# Fix out of bounds tokenization unless we were given packed metadata
allow_overlength = (
getattr(self, "_unsloth_allow_packed_overlength", False)
diff --git a/unsloth/utils/packing.py b/unsloth/utils/packing.py
index 5ab59a8ad90..3715883e3b8 100644
--- a/unsloth/utils/packing.py
+++ b/unsloth/utils/packing.py
@@ -1678,13 +1678,22 @@ def _wrap_generate_clears_varlen(model, hybrid_modules) -> None:
def get_packed_info_from_kwargs(
- kwargs: dict, device: torch.device
+ kwargs: dict,
+ device: torch.device,
+ position_ids: Optional[torch.Tensor] = None,
) -> Optional[Tuple[torch.Tensor, torch.Tensor, int]]:
- """Return packed sequence metadata expected by the attention kernels."""
+ """Return packed metadata, optionally inferring a mask-free prefill's boundaries."""
seq_lengths = kwargs.get("packed_seq_lengths")
if seq_lengths is None:
- return None
+ # Callers must exclude padding masks and cached decoding before opting in.
+ if position_ids is None or position_ids.dim() != 2:
+ return None
+ varlen = _varlen_from_position_ids(position_ids)
+ if varlen is None:
+ return None
+ cu_seqlens, _ = varlen
+ seq_lengths = cu_seqlens[1:] - cu_seqlens[:-1]
entry = _PACKED_INFO_CACHE.get(device)
if entry is not None and entry["seq_lengths"] is seq_lengths: