unsloth/tests/utils
cherry77-cloud 7f4736d383
fix: preserve padding-free DPO sample boundaries in fast Llama/Qwen2 (#14251)
With TRL 0.28.0 and `padding_free=True`, DPOTrainer flattens
chosen/rejected inputs and passes reset `position_ids` without
`packed_seq_lengths`. Fast Llama/Qwen2 attention currently sees no
packed metadata, so rejected tokens can attend to earlier chosen tokens.
Selecting FlashAttention 2 does not prevent this: the missing metadata
selects the dense rather than varlen backend.

Recover lengths from reset positions at the shared LlamaModel entry,
before overlength truncation and decoder layers. Reuse the existing
position-boundary helper and pass the inferred `packed_seq_lengths`
through the existing packed attention path. Inference is opt-in for a
single row without a padding mask or incoming KV cache; explicit lengths
take precedence. Doing this at model entry also preserves the original
padding-mask distinction before the training path may discard that mask
and avoids repeating boundary inference in each layer.

Scope: the confirmed issue is the older TRL 0.28.0 padding-free DPO
path. [Current TRL documents that DPO padding-free is disabled and falls
back to
padding](https://github.com/huggingface/trl/blob/main/docs/source/reducing_memory_usage.md);
this is not a claim about its default behavior.

Validation: 14 local CPU regression cases passed using real TRL 0.28.0
`DPOTrainer.concatenated_forward`, source-extracted production
model-entry/fast-attention functions, and production attention dispatch
with PyTorch SDPA. Identity RoPE isolates sample-boundary behavior.
Checks include log-prob and gradient agreement with independent/padded
inputs, zero rejected-to-chosen gradients, a control reproducing leakage
without the fix, MHA/GQA, both packed SDPA routes, explicit metadata
precedence, ordinary positions, padding masks, KV cache, and preserving
a flattened batch past the length check. FA2 dispatch is checked to
select `FLASH_VARLEN`; GPU FA2 kernel execution was not tested.

Repository-pinned formatting, Ruff lint, and `git diff --check` passed.
This PR contains only the two production files; regression scripts
remain local.

---------

Co-authored-by: Daniel Han <23090290+danielhanchen@users.noreply.github.com>
2026-10-20 08:58:33 -07:00
..
__init__.py Revert "[FIX] Vllm guided decoding params (#3662)" 2025-12-01 05:43:45 -08:00
aime_eval.md reroute merge logic language models + comprehensive tests + eval kits (#2673) 2025-06-02 20:32:57 -07:00
aime_eval.py Trim comments in tests (#13844) 2026-10-15 05:30:27 -07:00
cleanup_utils.py Trim comments in tests (#13844) 2026-10-15 05:30:27 -07:00
data_utils.py Trim comments in tests, scripts, CI helpers and unsloth_cli (#10113) 2026-09-04 15:31:50 -07:00
generate_dataset_with_none.py Trim comments in tests (#13844) 2026-10-15 05:30:27 -07:00
hf_utils.py Reduce and tighten comments and docstrings across the test suite (#6429) 2026-06-18 01:07:09 -07:00
ocr_eval.md Fix Typos in Documentation and Comments (#2721) 2025-06-17 04:34:51 -07:00
ocr_eval.py Trim comments in tests (#13844) 2026-10-15 05:30:27 -07:00
os_utils.py Trim comments in tests (#13844) 2026-10-15 05:30:27 -07:00
perplexity_eval.md reroute merge logic language models + comprehensive tests + eval kits (#2673) 2025-06-02 20:32:57 -07:00
perplexity_eval.py Trim comments in tests (#13844) 2026-10-15 05:30:27 -07:00
run_none_detect_tests.py Trim comments in tests (#13844) 2026-10-15 05:30:27 -07:00
test_attention_dispatch_bidirectional.py Trim comments in tests (#13844) 2026-10-15 05:30:27 -07:00
test_attention_dispatch_dora_dtype.py Trim comments in tests (#13844) 2026-10-15 05:30:27 -07:00
test_attention_dispatch_eval_backward.py Support every PEFT init_lora_weights option, with fast PiSSA and MiCA init (#6879) 2026-10-06 13:01:09 -07:00
test_attention_dispatch_mixed_devices.py Pick the attention backend per device on mixed-generation multi-GPU hosts (#13698) 2026-10-14 09:29:09 -07:00
test_attention_masks.py Trim comments in tests (#13844) 2026-10-15 05:30:27 -07:00
test_attention_native_routing.py Attention: GQA never forces SDPA math, cuDNN-over-varlen on sm100 only, native before xformers (#14158) 2026-10-19 04:32:43 -07:00
test_attn_mask_compat.py Trim comments in tests (#13844) 2026-10-15 05:30:27 -07:00
test_attn_mask_upstream_drift.py Trim comments in tests (#13844) 2026-10-15 05:30:27 -07:00
test_batched_leftpad_generation_gpu.py Accept bf16 tie-flips in the batched left-padded generation guard (#13408) 2026-10-12 08:14:18 -07:00
test_create_optimizer_model_arg.py Trim comments in tests (#13844) 2026-10-15 05:30:27 -07:00
test_dataset_num_proc.py Trim comments in tests (#13844) 2026-10-15 05:30:27 -07:00
test_flash_adamw_optim.py Optimizer parking outside inference mode; refuse fp16 params with unquantized adamw_flash moments (#14201) 2026-10-19 11:51:00 -07:00
test_flash_impl.py Keep torch's bundled flash on Blackwell for short documents under UNSLOTH_FLASH_ATTENTION_IMPL=auto (#13631) 2026-10-14 00:55:47 -07:00
test_hybrid_packing_default.py Hybrid packing + block compile: fix the guard crash, the skipped varlen, and the cross-document leak (#13956) 2026-10-18 23:31:09 -07:00
test_init_lora_validation.py Support every PEFT init_lora_weights option, with fast PiSSA and MiCA init (#6879) 2026-10-06 13:01:09 -07:00
test_lora_ga_save_conversion.py Save LoRA-GA adapters relative to the original base so they reload correctly (#13322) 2026-10-11 20:30:37 -07:00
test_packed_flash_documents.py Keep packed documents apart on Unsloth's own models (flash path, eval, cached eval) and match transformers' Mistral window (#13313) 2026-10-12 03:58:00 -07:00
test_packing.py fix: preserve padding-free DPO sample boundaries in fast Llama/Qwen2 (#14251) 2026-10-20 08:58:33 -07:00
test_packing_overflow_guard.py Fix bfd_split truncating overflow before packing (#13770) 2026-10-15 06:17:11 -07:00
test_prepare_inputs_leftpad.py Trim comments in tests (#13844) 2026-10-15 05:30:27 -07:00
test_q_galore.py Trim comments in tests (#13844) 2026-10-15 05:30:27 -07:00
test_qat.py Trim comments in tests (#13844) 2026-10-15 05:30:27 -07:00
test_qwen4_exp_ple_packing.py Fix packed document boundaries in Qwen PLE (#14011) 2026-10-18 09:26:08 -07:00
test_rope_scaling_drift.py Trim comments in tests (#13844) 2026-10-15 05:30:27 -07:00
test_rope_scaling_user_arg.py Apply an explicit rope_scaling to the model and to vLLM (#13315) 2026-10-11 08:45:52 -07:00
test_sdpa_packed_segments.py Attention: GQA never forces SDPA math, cuDNN-over-varlen on sm100 only, native before xformers (#14158) 2026-10-19 04:32:43 -07:00
test_single_row_cached_attention.py Native decode: read GQA KV heads in place instead of copying them per query head (#13271) 2026-10-13 12:59:47 -07:00
test_torch_varlen_packed.py Packed SDPA rows: one torch varlen_attn call instead of one SDPA call per segment (#13221) 2026-10-10 23:54:24 -07:00
test_train_on_responses_only_num_proc.py Trim comments in tests (#13844) 2026-10-15 05:30:27 -07:00
test_trunc_normal_patch.py Trim comments in tests (#13844) 2026-10-15 05:30:27 -07:00
test_truncation_attestation.py Trim comments in tests (#13844) 2026-10-15 05:30:27 -07:00
test_unsloth_optimizer_embedding_optim_bits.py Trim comments in tests (#13844) 2026-10-15 05:30:27 -07:00
test_unsloth_optimizer_full_finetune_embedding_lr.py Apply embedding_learning_rate to embeddings under full finetuning (#13171) 2026-10-09 21:06:08 -07:00
test_unsloth_optimizer_loraplus.py Add loraplus_lr_ratio for LoRA+ training (#13062) 2026-10-08 11:50:49 -07:00
test_unsloth_optimizer_weight_decay.py Trim comments in tests (#13844) 2026-10-15 05:30:27 -07:00
test_varlen_int32_overflow_guard.py Trim comments in tests (#13844) 2026-10-15 05:30:27 -07:00
test_xformers_broken_message.py Say when xformers is installed but cannot load, and how to fix it (#13410) 2026-10-12 07:10:06 -07:00
test_xformers_capability_gate.py Trim comments in tests (#13844) 2026-10-15 05:30:27 -07:00