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(trainer): warn loudly when requested packing is silently disabled (#7234)

Elia2e1862cbe261
diff --git a/tests/utils/test_packing.py b/tests/utils/test_packing.py
index 0be3018cde9..3ee4ba5c555 100644
--- a/tests/utils/test_packing.py
+++ b/tests/utils/test_packing.py
@@ -26,6 +26,7 @@ from unsloth.utils.packing import (
patch_hybrid_linear_attention_varlen,
)
+import logging
from contextlib import ExitStack
from types import SimpleNamespace
from unittest.mock import patch
@@ -1248,3 +1249,67 @@ def test_resolve_string_model_config_merges_top_level_trust_remote_code(monkeypa
)
trainer_module._resolve_string_model_config("org/remote-hybrid", config_arg)
assert captured.get("trust_remote_code") is False
+
+
+class _WarnConfig(SimpleNamespace):
+ # get_transformers_model_type() resolves through to_dict(), which SimpleNamespace lacks.
+ def to_dict(self):
+ return dict(self.__dict__)
+
+
+def _warn_text_model():
+ return SimpleNamespace(
+ config = _WarnConfig(architectures = ["LlamaForCausalLM"], model_type = "llama"),
+ max_seq_length = 16,
+ )
+
+
+def test_packing_skip_warning_is_accurate(monkeypatch, caplog):
+ # Two things the message used to get wrong: it blamed a "custom data collator" for
+ # UNSLOTH_RETURN_LOGITS (which unsloth sets itself for compute_metrics), and it quoted a
+ # token limit read before max_seq_length / max_length / the model limit are reconciled.
+ monkeypatch.setenv("UNSLOTH_RETURN_LOGITS", "1")
+ fake_trainer = _patch_fake_sft_trainer()
+ config = SimpleNamespace(
+ packing = True,
+ padding_free = None,
+ remove_unused_columns = True,
+ max_seq_length = 4096,
+ max_length = 512,
+ )
+
+ with caplog.at_level(logging.WARNING, logger = "unsloth.trainer"):
+ fake_trainer(
+ model = _warn_text_model(),
+ args = config,
+ train_dataset = Dataset.from_dict({"text": ["sample"]}),
+ )
+
+ messages = [r.message for r in caplog.records if "packing=True ignored" in r.message]
+ assert len(messages) == 1
+ assert "UNSLOTH_RETURN_LOGITS" in messages[0]
+ assert "custom data collator" not in messages[0]
+ assert "4096" not in messages[0] and "512" not in messages[0]
+ # compute_metrics is one of several setters, so the message must not name it.
+ assert "compute_metrics" not in messages[0]
+
+
+def test_packing_skip_warning_keeps_custom_collator_reason(monkeypatch, caplog):
+ # A passed collator must still be named as the cause; the env-var fallback is only for
+ # the case where nothing else blocks packing.
+ monkeypatch.delenv("UNSLOTH_RETURN_LOGITS", raising = False)
+ fake_trainer = _patch_fake_sft_trainer()
+ config = SimpleNamespace(packing = True, padding_free = None, remove_unused_columns = True)
+
+ with caplog.at_level(logging.WARNING, logger = "unsloth.trainer"):
+ fake_trainer(
+ model = _warn_text_model(),
+ args = config,
+ data_collator = lambda features: features,
+ train_dataset = Dataset.from_dict({"text": ["sample"]}),
+ )
+
+ messages = [r.message for r in caplog.records if "packing=True ignored" in r.message]
+ assert len(messages) == 1
+ assert "custom data collator" in messages[0]
+ assert "UNSLOTH_RETURN_LOGITS" not in messages[0]
diff --git a/unsloth/trainer.py b/unsloth/trainer.py
index 1c301923011..7b715e98144 100644
--- a/unsloth/trainer.py
+++ b/unsloth/trainer.py
@@ -769,8 +769,11 @@ def _patch_sft_trainer_auto_packing(trl_module):
reason = "hybrid linear-attention model"
elif is_unsupported_model:
reason = f"unsupported model type(s): {', '.join(model_types)}"
- message = f"Unsloth: Sample packing skipped ({reason} detected)."
- print(message)
+ elif data_collator is None:
+ # compute_metrics, preprocess_logits_for_metrics, for_inference() and the
+ # user can all set it, so name the flag and not a setter.
+ reason = "UNSLOTH_RETURN_LOGITS=1"
+ logger.warning(f"Unsloth: packing=True ignored ({reason}).")
packing_active = False
if _should_pack(config_arg) and not blocked: