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
Pack Qwen3.5 text SFT with the processor, and fix eval_loss on packed text runs (#13624)
Daniel Han3822d7046e16
diff --git a/tests/test_processor_text_packing_gate.py b/tests/test_processor_text_packing_gate.pyindex 2a3bea66468..d558240c5aa 100644--- a/tests/test_processor_text_packing_gate.py+++ b/tests/test_processor_text_packing_gate.py@@ -61,7 +61,7 @@ class _Model(nn.Module):return None-def _patched(raise_on = None):+def _patched(raise_on = None, collator = None):calls = []class _StubSFTTrainer:@@ -78,6 +78,13 @@ def _patched(raise_on = None):raise ValueError(raise_on)self.model = modelself.args = args+ self.data_collator = collator++ def get_eval_dataloader(self, eval_dataset = None):+ return self.data_collator++ def get_test_dataloader(self, test_dataset):+ return self.data_collatormodule = SimpleNamespace(SFTTrainer = _StubSFTTrainer)trainer_module._patch_sft_trainer_auto_packing(module)@@ -202,6 +209,19 @@ def test_verified_families_are_unblocked(model_type):assert not _blocked(model = _Model(_Cfg(model_type = model_type)))[email protected]("model_type", ["qwen3_5", "qwen3_5_moe"])+def test_qwen3_5_processor_on_text_is_unblocked_despite_mrope(model_type):+ """Qwen3.5 text models take 2D position_ids as the text row, so packed text rows stay per document."""+ cfg = _Cfg(+ model_type = model_type,+ text_config = {"rope_parameters": {"mrope_section": [11, 11, 10], "mrope_interleaved": True}},+ )+ assert _uses_multimodal_rope(cfg)+ assert not _blocked(model = _Model(cfg))+ img = datasets.Dataset.from_dict({"text": ["a"], "images": [[0]]})+ assert _blocked(model = _Model(cfg), train_dataset = img)++@pytest.mark.parametrize("value", ["false", "OFF", "no"])def test_kill_switch_spellings(monkeypatch, value):monkeypatch.setenv("UNSLOTH_PROCESSOR_TEXT_PACKING", value)@@ -277,6 +297,26 @@ def test_a_trl_refusal_falls_back_to_the_old_block(message):assert t.args.padding_free is False and t.args.packing is False+class _TextCollator:+ """TRL's DataCollatorForLanguageModeling exposes `padding_free` as a plain field."""++ padding_free = True++[email protected]("packing", [False, True])+def test_evaluation_keeps_the_text_collator(packing):+ """Only UnslothVisionDataCollator evaluates from an unpacked copy; a text collator copy+ would drop the packed_seq_lengths wrapper and eval_loss would cross documents."""+ collator = _TextCollator()+ module, _ = _patched(collator = collator)+ t = module.SFTTrainer(+ model = _Model(), args = _config(packing), train_dataset = TEXT, processing_class = _Proc()+ )+ assert t.args.padding_free is True+ assert t.get_eval_dataloader() is collator+ assert t.get_test_dataloader(TEXT) is collator++def test_an_unrelated_value_error_still_propagates():module, _ = _patched(raise_on = "something else entirely")with pytest.raises(ValueError, match = "something else"):diff --git a/unsloth/trainer.py b/unsloth/trainer.pyindex 43bdcd6ebe6..9d99105e874 100644--- a/unsloth/trainer.py+++ b/unsloth/trainer.py@@ -482,8 +482,15 @@ def _uses_multimodal_rope(model_config) -> bool:return False-_PROCESSOR_TEXT_PACKING_MODELS = frozenset(- {"gemma3", "gemma3_text", "gemma4", "gemma4_text", "gemma4_unified", "gemma4_unified_text"}+# mRoPE families whose text model expands 2D position_ids into the text row (transformers 5.2+),+# so packed text rows get the per-document mask, exactly as with processing_class=processor.tokenizer.+_PROCESSOR_TEXT_PACKING_MROPE_MODELS = frozenset({"qwen3_5", "qwen3_5_moe"})++_PROCESSOR_TEXT_PACKING_MODELS = (+ frozenset(+ {"gemma3", "gemma3_text", "gemma4", "gemma4_text", "gemma4_unified", "gemma4_unified_text"}+ )+ | _PROCESSOR_TEXT_PACKING_MROPE_MODELS)@@ -501,7 +508,9 @@ def _processor_text_only(return Falseif data_collator is not None:return False- if _uses_multimodal_rope(model_config):+ if getattr(+ model_config, "model_type", None+ ) not in _PROCESSOR_TEXT_PACKING_MROPE_MODELS and _uses_multimodal_rope(model_config):return Falseif getattr(processing_class, "tokenizer", None) is None:return False@@ -1983,7 +1992,8 @@ def _patch_sft_trainer_auto_packing(trl_module):setattr(trainer_args, "padding_free", False)collator = getattr(self, "data_collator", None)- if getattr(collator, "padding_free", False):+ # TRL's text collator also has `padding_free`; it must keep its wrapped, per-document eval batches.+ if isinstance(collator, UnslothVisionDataCollator) and collator.padding_free:reason = _collator_packing_conflict(self, eval_logits_wanted, forward_rejects_packing)if reason is not None:collator._padding_free = False