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.

Accept a list train_dataset again on TRL 1.10+ (fixes vision notebooks) (#12628)

Daniel Hane4621185354c
diff --git a/tests/test_rl_list_train_dataset.py b/tests/test_rl_list_train_dataset.py
new file mode 100644
index 00000000000..74cb1c31096
--- /dev/null
+++ b/tests/test_rl_list_train_dataset.py
@@ -0,0 +1,191 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+"""TRL 1.10+ rejects a plain list train_dataset, which every vision notebook passes
+(list of PIL conversations + skip_prepare_dataset=True). The compiled SFT / GRPO / RLOO
+trainers must accept it again, while DPO / KTO / Reward keep TRL's check."""
+
+import ast
+import importlib.util
+import inspect
+import re
+import sys
+import textwrap
+from pathlib import Path
+
+import pytest
+import torch
+
+MODELS = Path(__file__).resolve().parents[1] / "unsloth" / "models"
+
+
+def _lift(path, names, namespace):
+ tree = ast.parse(path.read_text(encoding = "utf-8"))
+ nodes = [
+ node
+ for node in tree.body
+ if (isinstance(node, ast.FunctionDef) and node.name in names)
+ or (
+ isinstance(node, ast.Assign)
+ and any(isinstance(t, ast.Name) and t.id in names for t in node.targets)
+ )
+ ]
+ exec(compile(ast.Module(body = nodes, type_ignores = []), str(path), "exec"), namespace)
+ return namespace
+
+
+_rl = _lift(
+ MODELS / "rl.py",
+ ("_LIST_TRAIN_DATASET_TRAINERS", "_TRL_TRAIN_DATASET_TYPE_CHECK", "_allow_list_train_dataset"),
+ {"re": re},
+)
+allow_list = _rl["_allow_list_train_dataset"]
+column_names = _lift(MODELS / "_utils.py", ("_unsloth_dataset_column_names",), {})[
+ "_unsloth_dataset_column_names"
+]
+
+
+class Dataset:
+ pass
+
+
+class IterableDataset:
+ pass
+
+
+class TorchDataset(torch.utils.data.Dataset):
+ def __len__(self):
+ return 1
+
+ def __getitem__(self, i):
+ if i >= 1:
+ raise IndexError(i)
+ return {"messages": []}
+
+
+class TorchStream(torch.utils.data.IterableDataset):
+ def __iter__(self):
+ return iter([{"prompt": "x"}])
+
+
+SFT_INIT = """
+def __init__(self, train_dataset):
+ if train_dataset is None:
+ raise ValueError("`train_dataset` is required")
+ elif isinstance(train_dataset, IterableDataset):
+ pass
+ elif not isinstance(train_dataset, Dataset):
+ raise TypeError("`train_dataset` must be a `Dataset` or `IterableDataset`")
+ return "ok"
+"""
+
+GRPO_INIT = """
+def __init__(self, train_dataset):
+ if train_dataset is None:
+ raise ValueError("`train_dataset` is required")
+ elif not isinstance(train_dataset, (Dataset, IterableDataset)):
+ raise TypeError("`train_dataset` must be a `Dataset` or `IterableDataset`")
+ return "ok"
+"""
+
+
+def _build(source):
+ namespace = {"Dataset": Dataset, "IterableDataset": IterableDataset, "torch": torch}
+ exec(textwrap.dedent(source), namespace)
+ return namespace["__init__"]
+
+
+LIST_ROWS = ([{"messages": []}], ({"messages": []},))
+BAD = ({"train": []}, "text", 3) # a DatasetDict is a dict
+
+
+ "trainer_file, init",
+ [("sft_trainer", SFT_INIT), ("grpo_trainer", GRPO_INIT), ("rloo_trainer", GRPO_INIT)],
+ ids = ["sft", "grpo", "rloo"],
+)
+def test_list_like_datasets_are_accepted(trainer_file, init):
+ with pytest.raises(TypeError):
+ _build(init)(None, LIST_ROWS[0])
+ patched = allow_list("__init__", init, trainer_file)
+ assert allow_list("__init__", patched, trainer_file) == patched
+ fn = _build(patched)
+ for good in (*LIST_ROWS, Dataset(), IterableDataset()):
+ assert fn(None, good) == "ok"
+ for bad in (*BAD, TorchDataset(), TorchStream()):
+ with pytest.raises(TypeError, match = "must be a `Dataset`"):
+ fn(None, bad)
+
+
[email protected]("trainer_file", ["dpo_trainer", "kto_trainer", "reward_trainer"])
+def test_trainers_that_always_map_keep_the_check(trainer_file):
+ assert allow_list("__init__", SFT_INIT, trainer_file) == SFT_INIT
+
+
+def test_other_functions_untouched():
+ assert allow_list("compute_loss", SFT_INIT, "sft_trainer") == SFT_INIT
+
+
+def test_skip_prepare_label_guard_uses_the_helper():
+ source = "def f(dataset):\n cols = get_dataset_column_names(dataset)\n return cols\n"
+ patched = allow_list("_reject_skip_prepare_without_labels", source, "sft_trainer")
+ assert "cols = _unsloth_dataset_column_names(dataset)" in patched
+
+
+class _WithColumns:
+ column_names = ["input_ids", "labels"]
+
+
+ "dataset, expected",
+ [
+ (_WithColumns(), ["input_ids", "labels"]),
+ ([{"input_ids": [1], "completion_mask": [1]}], ["input_ids", "completion_mask"]),
+ (({"messages": []},), ["messages"]),
+ (TorchDataset(), ["messages"]),
+ (TorchStream(), ["prompt"]),
+ ([], []),
+ ((), []),
+ ([("a", "b")], []),
+ ],
+ ids = [
+ "column_names",
+ "list",
+ "tuple",
+ "torch",
+ "torch_stream",
+ "empty_list",
+ "empty_tuple",
+ "non_mapping",
+ ],
+)
+def test_dataset_column_names(dataset, expected):
+ assert column_names(dataset) == expected
+
+
+def test_matches_installed_trl():
+ trl = pytest.importorskip("trl")
+ checked = 0
+ for trainer_file, name in (
+ ("sft_trainer", "SFTTrainer"),
+ ("grpo_trainer", "GRPOTrainer"),
+ ("rloo_trainer", "RLOOTrainer"),
+ ):
+ spec = importlib.util.find_spec(f"trl.trainer.{trainer_file}")
+ if spec is None or spec.origin is None:
+ continue
+ text = Path(spec.origin).read_text(encoding = "utf-8")
+ if "must be a `Dataset` or `IterableDataset`" not in text:
+ continue
+ widened = "list, tuple)"
+ assert widened in allow_list("__init__", text, trainer_file), name
+ checked += 1
+ # tests/conftest.py imports unsloth, which swaps in the compiled trainers.
+ if "unsloth" in sys.modules:
+ compiled = [c for c in getattr(trl, name).__mro__ if c.__name__ == f"_Unsloth{name}"]
+ assert compiled, name
+ assert widened in inspect.getsource(compiled[0].__init__), name
+ if checked == 0:
+ pytest.skip(
+ reason = f"trl {trl.__version__} predates the 1.10 train_dataset type check, nothing to widen"
+ )
diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.py
index 3ebbb240780..5da8c40d4a9 100644
--- a/unsloth/models/_utils.py
+++ b/unsloth/models/_utils.py
@@ -302,6 +302,17 @@ def _unsloth_install_pretrain_detector(model):
return model
+def _unsloth_dataset_column_names(dataset):
+ columns = getattr(dataset, "column_names", None)
+ if columns is not None:
+ return columns
+ try:
+ row = dataset[0] if isinstance(dataset, (list, tuple)) else next(iter(dataset))
+ except (IndexError, StopIteration):
+ return []
+ return list(row.keys()) if hasattr(row, "keys") else []
+
+
def _unsloth_reset_stray_compile_cache(self):
# A manual forward/backward under torch.compile BEFORE trainer.train() caches a forward plus AOTAutograd backward graph in a one-off context, and reusing it poisons training with NaN/zero gradients, so drop the cache. Module-level, so the SFT auto-packing wrapper and the plain-Trainer loop can run it too.
import os
diff --git a/unsloth/models/rl.py b/unsloth/models/rl.py
index 8a353f475ee..e2bbec049a3 100644
--- a/unsloth/models/rl.py
+++ b/unsloth/models/rl.py
@@ -383,6 +383,10 @@ try:
from unsloth.models._utils import _unsloth_reset_stray_compile_cache
except Exception:
def _unsloth_reset_stray_compile_cache(self): pass
+try:
+ from unsloth.models._utils import _unsloth_dataset_column_names
+except Exception:
+ def _unsloth_dataset_column_names(dataset): return dataset.column_names
# Drops/renames config arguments the installed TRL no longer accepts, so a
# script pinned to an older TRL keeps working after an upgrade. Falls back to
# the historical raw passthrough so this can never break trainer construction.
@@ -2786,14 +2790,14 @@ def _patch_trl_rl_trainers_impl(trainer_file = "grpo_trainer"):
"__tokenizer = processing_class if 'processing_class' in locals() else tokenizer\n"
"from unsloth_zoo.vision_utils import UnslothVisionDataCollator\n"
"if not isinstance(data_collator, UnslothVisionDataCollator):\n"
- " if isinstance(data_collator, DataCollatorForSeq2Seq) and 'labels' not in train_dataset.column_names:\n"
+ " if isinstance(data_collator, DataCollatorForSeq2Seq) and 'labels' not in _unsloth_dataset_column_names(train_dataset):\n"
" data_collator = TransformersDataCollatorForLanguageModeling(\n"
" __tokenizer,\n"
" mlm = False,\n"
" mlm_probability = 0.0,\n"
" pad_to_multiple_of = getattr(args, 'pad_to_multiple_of', None),\n"
" )\n"
- " elif isinstance(data_collator, TransformersDataCollatorForLanguageModeling) and 'labels' in train_dataset.column_names:\n"
+ " elif isinstance(data_collator, TransformersDataCollatorForLanguageModeling) and 'labels' in _unsloth_dataset_column_names(train_dataset):\n"
" data_collator = DataCollatorForSeq2Seq(\n"
" __tokenizer,\n"
" pad_to_multiple_of = getattr(args, 'pad_to_multiple_of', None),\n"
@@ -3588,6 +3592,27 @@ def _patch_trl_rl_trainers_impl(trainer_file = "grpo_trainer"):
)
+# TRL 1.10+ rejects the list train_dataset the vision notebooks pass; Dataset.from_list is no fix (re-encodes images).
+# Only list / tuple: a torch IterableDataset would skip TRL's streaming handling (dispatch_batches, RepeatSampler).
+_LIST_TRAIN_DATASET_TRAINERS = frozenset(("sft_trainer", "grpo_trainer", "rloo_trainer"))
+_TRL_TRAIN_DATASET_TYPE_CHECK = re.compile(
+ r"(elif\s+not\s+isinstance\(\s*train_dataset\s*,\s*)\(?\s*(Dataset(?:\s*,\s*IterableDataset)?)\s*\)?(\s*\)\s*:)"
+)
+
+
+def _allow_list_train_dataset(function, source, trainer_file):
+ if trainer_file not in _LIST_TRAIN_DATASET_TRAINERS:
+ return source
+ if function == "__init__":
+ return _TRL_TRAIN_DATASET_TYPE_CHECK.sub(r"\1(\2, list, tuple)\3", source)
+ if function == "_reject_skip_prepare_without_labels":
+ return source.replace(
+ "cols = get_dataset_column_names(dataset)",
+ "cols = _unsloth_dataset_column_names(dataset)",
+ )
+ return source
+
+
def patch_functions(RLTrainer, trainer_file, RLTrainer_name, all_imports, imports):
init = inspect.getsource(RLTrainer.__init__)
old_init = init
@@ -3789,6 +3814,7 @@ def patch_functions(RLTrainer, trainer_file, RLTrainer_name, all_imports, import
for edit_function in edit_functions:
source = edit_function(function, source)
+ source = _allow_list_train_dataset(function, source, trainer_file)
"""
import torch