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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Fix bfd_split truncating overflow before packing (#13770)
cherry77-cloud275d2011d129
diff --git a/.github/workflows/consolidated-tests-ci.yml b/.github/workflows/consolidated-tests-ci.ymlindex 3aff9cf156c..22fe57ea6de 100644--- a/.github/workflows/consolidated-tests-ci.yml+++ b/.github/workflows/consolidated-tests-ci.yml@@ -465,7 +465,8 @@ jobs:run: |python -m pytest -v --tb=short \tests/utils/test_dataset_num_proc.py \- tests/utils/test_train_on_responses_only_num_proc.py+ tests/utils/test_train_on_responses_only_num_proc.py \+ tests/utils/test_packing_overflow_guard.pyPYTHONPATH=studio/backend python -m pytest -v --tb=short \studio/backend/tests/test_dataset_map_num_proc.py \studio/backend/tests/test_compressed_tensors_signature_drift.pydiff --git a/tests/utils/test_packing_overflow_guard.py b/tests/utils/test_packing_overflow_guard.pynew file mode 100644index 00000000000..57a83b5d5b4--- /dev/null+++ b/tests/utils/test_packing_overflow_guard.py@@ -0,0 +1,55 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2023-present the Unsloth team. All rights reserved.++import ast+from pathlib import Path+from types import SimpleNamespace++import pytest++_SOURCE = Path(__file__).resolve().parents[2] / "unsloth" / "models" / "rl_replacements.py"+++def _setup_source():+ tree = ast.parse(_SOURCE.read_text(encoding = "utf-8"))+ for node in tree.body:+ if isinstance(node, ast.Assign) and any(+ getattr(t, "id", None) == "_WRAPPED_PACKING_SETUP" for t in node.targets+ ):+ return ast.literal_eval(node.value)+ raise AssertionError("_WRAPPED_PACKING_SETUP not found")+++def _skips_truncation(packing, strategy):+ def pack_dataset(+ dataset,+ seq_length,+ strategy = "bfd",+ map_kwargs = None,+ ):+ pass++ body = (+ "def _f(packing, args, pack_dataset):\n"+ + _setup_source()+ + " return _unsloth_wrapped_packing\n"+ )+ namespace = {}+ exec(body, namespace)+ return namespace["_f"](packing, SimpleNamespace(packing_strategy = strategy), pack_dataset)+++# TRL packs without truncating first; only plain bfd truncates overflow itself, so pre-truncating it is equivalent.[email protected]("strategy", ["wrapped", "bfd_split", "bfd-requeue"])+def test_overflow_keeping_strategies_skip_early_truncation(strategy):+ assert _skips_truncation(True, strategy) is True++[email protected]("strategy", ["bfd", None])+def test_bfd_still_truncates_early(strategy):+ assert not _skips_truncation(True, strategy)++[email protected]("strategy", ["wrapped", "bfd_split", "bfd-requeue"])+def test_no_packing_always_truncates(strategy):+ assert not _skips_truncation(False, strategy)diff --git a/unsloth/models/rl_replacements.py b/unsloth/models/rl_replacements.pyindex afcd59900ce..91353a746f0 100644--- a/unsloth/models/rl_replacements.py+++ b/unsloth/models/rl_replacements.py@@ -547,7 +547,8 @@ _WRAPPED_PACKING_SETUP = (" except Exception:\n"" _unsloth_pack_has_strategy = True\n"" _unsloth_wrapped_packing = packing and (\n"- ' getattr(args, "packing_strategy", None) == "wrapped"\n'+ # bfd_split (TRL >= 1.0) and its TRL 0.29 name bfd-requeue split overflow into later rows, so early truncation drops it.+ ' getattr(args, "packing_strategy", None) in ("wrapped", "bfd_split", "bfd-requeue")\n'" or not _unsloth_pack_has_strategy\n"" )\n")