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(sft): honor max_length below the model context limit (#11266)
cherry77-cloud289daaa5261a
diff --git a/tests/version_compat/test_trl_padding_free_max_length.py b/tests/version_compat/test_trl_padding_free_max_length.pyindex 60c9f2eca9b..1d0e40cb77a 100644--- a/tests/version_compat/test_trl_padding_free_max_length.py+++ b/tests/version_compat/test_trl_padding_free_max_length.py@@ -186,18 +186,175 @@ def test_default_sft_construction_does_not_trip_the_guard(tmp_path, trl_has_guardef test_explicit_max_length_resolves_the_same_on_every_trl(tmp_path, trl_has_guard):- """The swap only moves the already-resolved length across to `max_seq_length`;- it must never reinstate the raw user `max_length`."""+ """An explicit limit below the model cap survives the padding-free handoff.++ Every TRL must truncate to the user's limit, even when `max_length` is cleared.+ """trainer = _build(tmp_path, max_length = _USER_MAX_LENGTH)args = trainer.argsassert args.padding_free is True- assert args.max_seq_length == _MODEL_MAX_SEQ_LENGTH+ assert args.max_seq_length == _USER_MAX_LENGTHif trl_has_guard:assert args.max_length is Noneelse:- assert args.max_length == _MODEL_MAX_SEQ_LENGTH- assert _longest(trainer) == _MODEL_MAX_SEQ_LENGTH+ assert args.max_length == _USER_MAX_LENGTH+ assert _longest(trainer) == _USER_MAX_LENGTH+++def _trl_default_max_length():+ import dataclasses+ for field in dataclasses.fields(_pristine_sft_config_cls()):+ if field.name == "max_length":+ return field.default+ return None+++# 1025 is the first cap above TRL's 1024 default; 2048 is from_pretrained's own default.[email protected]("model_cap", [1025, 2048, 8192])+def test_an_untouched_max_length_default_does_not_cap_the_model_context(+ tmp_path, trl_has_guard, model_cap+):+ """Honouring a limit the caller never set is the same bug in the other direction.++ `SFTConfig.max_length` defaults to 1024 on every TRL from 0.22 onwards, so a cap+ keyed on "is this positive" reads every default config as a request for 1024.+ `_MODEL_MAX_SEQ_LENGTH = 128` sits below that default, where `min(128, 1024)` is 128+ either way, which is why the default-construction test above cannot see this.+ """+ from datasets import Dataset++ default_max_length = _trl_default_max_length()+ assert default_max_length is None or default_max_length > 0, (+ "this TRL defaults max_length to something falsy, so the regression "+ "cannot be reproduced here"+ )++ def _long_text(tok):+ return Dataset.from_list([{"text": "The quick brown fox. " * 4000}] * 4)++ trainer = _build(tmp_path, dataset = _long_text, model_max_seq_length = model_cap)+ args = trainer.args++ assert args.max_seq_length == model_cap+ if trl_has_guard:+ assert args.max_length is None+ else:+ assert args.max_length == model_cap+ assert _longest(trainer) == model_cap, (+ f"a {model_cap}-token context was truncated to {_longest(trainer)}; the "+ "untouched config default was read as an explicit request"+ )++[email protected]("path", ["cli", "clone"])+def test_a_rebuilt_default_config_still_does_not_cap_the_model(tmp_path, trl_has_guard, path):+ """The two ways an untouched config comes back carrying its RESOLVED default.++ `HfArgumentParser` builds one argparse argument per `dataclasses.fields()` entry and+ passes every value through, and `dataclasses.replace` re-inits from the current field+ values, so on both paths a config nobody capped arrives holding `max_length = 1024`.+ Reading omission from the value is what keeps these safe: a constructor-provenance+ marker is set on the original object and forged by both of these.+ """+ import dataclasses++ from datasets import Dataset+ from trl import SFTConfig++ default_max_length = _trl_default_max_length()+ if default_max_length is None or default_max_length <= 0:+ pytest.skip("this TRL has no positive max_length default, so there is nothing to confuse")++ if path == "cli":+ from transformers import HfArgumentParser+ (cfg,) = HfArgumentParser((SFTConfig,)).parse_args_into_dataclasses(+ ["--output_dir", str(tmp_path)]+ )+ else:+ cfg = dataclasses.replace(SFTConfig(output_dir = str(tmp_path)), learning_rate = 1e-4)+ assert cfg.max_length == default_max_length++ model, tok = _load_plain(8192)+ text = "The quick brown fox. " * 4000+ for key, value in (+ ("per_device_train_batch_size", 2),+ ("max_steps", 1),+ ("report_to", "none"),+ ("save_strategy", "no"),+ ("use_cpu", True),+ ("dataset_text_field", "text"),+ ("fp16", False),+ ("bf16", False),+ ("optim", "adamw_torch"),+ ):+ setattr(cfg, key, value)+ from trl import SFTTrainer++ trainer = SFTTrainer(+ model = model,+ processing_class = tok,+ args = cfg,+ train_dataset = Dataset.from_list([{"text": text}] * 4),+ )+ assert _longest(trainer) == 8192, (+ f"a config rebuilt via {path} carried its resolved default back in and capped an "+ "8192-token context at it"+ )+++def test_an_explicit_max_length_equal_to_the_default_keeps_the_model_length(tmp_path):+ """The one case this cannot decide, pinned so it is a known limit and not a surprise.++ `SFTConfig(max_length = 1024)` is indistinguishable from an untouched config once the+ value is all you have, and every marker that could tell them apart is forged by the+ CLI and clone paths above. This resolves to the model's length, which is what the+ merge base did too, so it is a gap this change does not close rather than one it opens.+ """+ from datasets import Dataset+ from trl import SFTConfig++ default_max_length = _trl_default_max_length()+ if default_max_length is None or default_max_length <= 0:+ pytest.skip("this TRL has no positive max_length default")++ model, tok = _load_plain(8192)+ cfg = SFTConfig(+ output_dir = str(tmp_path),+ per_device_train_batch_size = 2,+ max_steps = 1,+ report_to = "none",+ save_strategy = "no",+ use_cpu = True,+ dataset_text_field = "text",+ fp16 = False,+ bf16 = False,+ optim = "adamw_torch",+ max_length = default_max_length,+ )+ from trl import SFTTrainer++ trainer = SFTTrainer(+ model = model,+ processing_class = tok,+ args = cfg,+ train_dataset = Dataset.from_list([{"text": "The quick brown fox. " * 4000}] * 4),+ )+ assert _longest(trainer) == 8192+++def test_the_cap_reads_an_explicit_max_length_not_a_positive_one():+ """The behavioural test above needs a TRL whose default is positive; this one does not."""+ from unsloth.models import rl++ source = inspect.getsource(rl)+ assert "_unsloth_explicit_max_length" in source+ assert "_unsloth_default_max_length" in source+ # Not a bare truthiness test on the value, which every default passes.+ assert (+ "min(model.max_seq_length, args.max_length) "+ "if (getattr(args, 'max_length', None) or 0) > 0" not in source+ )def test_max_seq_length_still_beats_max_length(tmp_path, trl_has_guard):diff --git a/unsloth/models/rl.py b/unsloth/models/rl.pyindex aeb143d1c24..a336ef2c294 100644--- a/unsloth/models/rl.py+++ b/unsloth/models/rl.py@@ -1964,6 +1964,34 @@ def _patch_trl_rl_trainers_impl(trainer_file = "grpo_trainer"):extra_args += warnings_issued_checkif "model" in call_args:+ model_length_default = "model.max_seq_length"+ explicit_max_length = ""+ if trainer_file == "sft_trainer":+ # A model limit must not widen a limit the CALLER asked for, and `SFTConfig.max_length`+ # defaults to 1024 on every TRL from 0.22 to 1.x, so capping on "positive" instead+ # would cap every run that named no length at all down to 1024.+ explicit_max_length = (+ "_unsloth_explicit_max_length = None\n"+ "try:\n"+ " import dataclasses as _unsloth_dc\n"+ " _unsloth_cfg_cls = type(args)\n"+ # Back off the generated subclass to TRL's own dataclass, where the default lives.+ " while '_unsloth_patched_rl_config' in _unsloth_cfg_cls.__dict__ or _unsloth_cfg_cls.__name__.startswith('Unsloth'):\n"+ " _unsloth_cfg_cls = _unsloth_cfg_cls.__bases__[0]\n"+ " _unsloth_default_max_length = None\n"+ " for _unsloth_field in _unsloth_dc.fields(_unsloth_cfg_cls):\n"+ " if _unsloth_field.name == 'max_length': _unsloth_default_max_length = _unsloth_field.default\n"+ " _unsloth_given_max_length = getattr(args, 'max_length', None)\n"+ " if (_unsloth_given_max_length or 0) > 0 and _unsloth_given_max_length != _unsloth_default_max_length:\n"+ " _unsloth_explicit_max_length = _unsloth_given_max_length\n"+ "except Exception:\n"+ " _unsloth_explicit_max_length = None\n"+ )+ model_length_default = (+ "min(model.max_seq_length, _unsloth_explicit_max_length) "+ "if _unsloth_explicit_max_length else model.max_seq_length"+ )+ extra_args += explicit_max_lengthlength_check = ("if 'max_seq_length' not in locals() and not hasattr(args, 'max_seq_length'):\n"" pass\n"@@ -1971,7 +1999,7 @@ def _patch_trl_rl_trainers_impl(trainer_file = "grpo_trainer"):" model_max_seq_length = getattr(model, 'max_seq_length', None)\n"" args_max_seq_length = getattr(args, 'max_seq_length', None)\n"" if args_max_seq_length is None and model_max_seq_length is not None:\n"- " max_seq_length = model.max_seq_length\n"+ f" max_seq_length = {model_length_default}\n"" if hasattr(args, 'max_seq_length'): args.max_seq_length = max_seq_length\n"" elif args_max_seq_length is not None and model_max_seq_length is not None:\n"" if args_max_seq_length > model_max_seq_length:\n"@@ -1994,7 +2022,7 @@ def _patch_trl_rl_trainers_impl(trainer_file = "grpo_trainer"):" model_max_length = getattr(model, 'max_seq_length', None)\n"" if model_max_length is None: model_max_length = getattr(model, 'max_length', None)\n"" if model_max_length is not None:\n"- " args.max_length = model_max_length\n"+ " args.max_length = min(_unsloth_explicit_max_length, model_max_length) if _unsloth_explicit_max_length else model_max_length\n"" max_length = args.max_length\n"" elif hasattr(args, 'max_length') and args.max_length is not None:\n"" max_length = args.max_length\n"