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.

Honour use_gradient_checkpointing=False over TRL's gradient_checkpointing=True default (#15145)

Daniel Han40840d721283
diff --git a/tests/test_gradient_checkpointing_off_honoured.py b/tests/test_gradient_checkpointing_off_honoured.py
new file mode 100644
index 00000000000..a97873a79fb
--- /dev/null
+++ b/tests/test_gradient_checkpointing_off_honoured.py
@@ -0,0 +1,215 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2023-present Daniel Han-Chen & the Unsloth team. All rights reserved.
+
+"""use_gradient_checkpointing = False must survive TRL's SFTConfig(gradient_checkpointing = True) default.
+
+The end to end test trains a tiny Qwen3 LoRA on CPU through the real Unsloth-patched SFTTrainer and counts
+torch checkpoint calls and decoder-layer forwards during train(), so it proves what ran, not only a flag.
+"""
+
+from __future__ import annotations
+
+import importlib.util
+import json
+import os
+import subprocess
+import sys
+import textwrap
+from pathlib import Path
+
+import pytest
+
+_REPO = Path(__file__).resolve().parents[1]
+_MODULE_PATH = _REPO / "unsloth" / "models" / "_gc_config_default.py"
+
+
+def _load_module():
+ spec = importlib.util.spec_from_file_location("_gc_config_default_under_test", _MODULE_PATH)
+ module = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(module)
+ module._NOTED.clear()
+ return module
+
+
+def _config_class():
+ class Config:
+ def __init__(
+ self,
+ output_dir = None,
+ gradient_checkpointing = True,
+ **kwargs,
+ ):
+ self.output_dir = output_dir
+ self.gradient_checkpointing = gradient_checkpointing
+
+ return Config
+
+
+class _Model:
+ def __init__(self, recorded):
+ self._unsloth_gradient_checkpointing = recorded
+
+
+def test_explicit_flag_kwarg_positional_and_default():
+ module = _load_module()
+ Config = _config_class()
+ assert module.track_explicit_gradient_checkpointing(Config)
+ assert not module.track_explicit_gradient_checkpointing(Config) # idempotent
+ assert Config().__dict__[module.EXPLICIT_FLAG] is False
+ assert Config(output_dir = "x").__dict__[module.EXPLICIT_FLAG] is False
+ assert Config(gradient_checkpointing = True).__dict__[module.EXPLICIT_FLAG] is True
+ assert Config("x", True).__dict__[module.EXPLICIT_FLAG] is True
+ assert Config("x").__dict__[module.EXPLICIT_FLAG] is False
+
+
+def test_honour_rules(capsys):
+ module = _load_module()
+ Config = _config_class()
+ module.track_explicit_gradient_checkpointing(Config)
+
+ default = Config()
+ assert module.honour_gradient_checkpointing_off(default, _Model(False))
+ assert default.gradient_checkpointing is False
+ assert "stays off" in capsys.readouterr().out
+
+ explicit = Config(gradient_checkpointing = True)
+ assert not module.honour_gradient_checkpointing_off(explicit, _Model(False))
+ assert explicit.gradient_checkpointing is True
+
+ for recorded in ("unsloth", True, None):
+ args = Config()
+ assert not module.honour_gradient_checkpointing_off(args, _Model(recorded))
+ assert args.gradient_checkpointing is True
+ args = Config()
+ assert not module.honour_gradient_checkpointing_off(args, object())
+ assert args.gradient_checkpointing is True
+
+ # A config class Unsloth did not build: origin unknown, so it is kept.
+ class Foreign:
+ gradient_checkpointing = True
+
+ foreign = Foreign()
+ assert not module.honour_gradient_checkpointing_off(foreign, _Model(False))
+ assert foreign.gradient_checkpointing is True
+
+
+_E2E = textwrap.dedent(
+ r"""
+ import json, os, sys, types
+ import unsloth # noqa: F401
+ # UNSLOTH_ALLOW_CPU=1 skips the TRL trainer rewrite; run it so SFTTrainer is Unsloth's, as on a GPU host.
+ os.environ["UNSLOTH_ALLOW_CPU"] = "0"
+ from unsloth.models.rl import PatchFastRL
+ PatchFastRL()
+ os.environ["UNSLOTH_ALLOW_CPU"] = "1"
+ import torch, transformers, trl
+ import torch.utils.checkpoint as tuc
+ import transformers.modeling_utils as hf_modeling_utils
+ from datasets import Dataset
+ from peft import LoraConfig, get_peft_model
+ from transformers import AutoModelForCausalLM, AutoTokenizer
+ from trl import SFTConfig, SFTTrainer
+
+ model_id = "trl-internal-testing/tiny-Qwen3ForCausalLM"
+ calls = {"ckpt": 0, "fwd": 0}
+ original_checkpoint = tuc.checkpoint
+
+ def counting_checkpoint(*args, **kwargs):
+ calls["ckpt"] += 1
+ return original_checkpoint(*args, **kwargs)
+
+ tuc.checkpoint = counting_checkpoint
+ if getattr(hf_modeling_utils, "checkpoint", None) is original_checkpoint:
+ hf_modeling_utils.checkpoint = counting_checkpoint
+
+ def run(recorded, config_kwargs):
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
+ model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype = torch.float32)
+ model = get_peft_model(model, LoraConfig(r = 8, target_modules = ["q_proj", "v_proj"], task_type = "CAUSAL_LM"))
+ # What FastLanguageModel.get_peft_model(use_gradient_checkpointing = ...) records.
+ model._unsloth_gradient_checkpointing = recorded
+ model.enable_input_require_grads()
+ layers = [m for m in model.modules() if type(m).__name__.endswith("DecoderLayer")]
+ for layer in layers:
+ layer.register_forward_pre_hook(lambda m, i: calls.__setitem__("fwd", calls["fwd"] + 1))
+ dataset = Dataset.from_dict({"text": [f"hello world this is sentence number {i}" for i in range(8)]})
+ kwargs = dict(output_dir = os.environ["E2E_OUT"], per_device_train_batch_size = 2, max_steps = 2,
+ report_to = "none", use_cpu = True, save_strategy = "no", bf16 = False, fp16 = False,
+ dataset_text_field = "text", **config_kwargs)
+ try:
+ args = SFTConfig(max_length = 64, **kwargs)
+ except TypeError:
+ args = SFTConfig(max_seq_length = 64, **kwargs)
+ key = "processing_class" if "processing_class" in SFTTrainer.__init__.__code__.co_varnames else "tokenizer"
+ trainer = SFTTrainer(model = model, args = args, train_dataset = dataset, **{key: tokenizer})
+ assert type(trainer).__name__.startswith("Unsloth"), type(trainer)
+ # TRL >= 1.x computes the SFT loss with a Triton fused LM head (no CPU driver); GC is decided elsewhere.
+ trainer.compute_loss = types.MethodType(transformers.Trainer.compute_loss, trainer)
+ calls.update(ckpt = 0, fwd = 0)
+ trainer.train()
+ return {"args_gc": bool(trainer.args.gradient_checkpointing), "ckpt": calls["ckpt"],
+ "fwd": calls["fwd"], "layers": len(layers)}
+
+ results = {
+ "off_default": run(False, {}),
+ "off_explicit_on": run(False, {"gradient_checkpointing": True}),
+ "unsloth_default": run("unsloth", {}),
+ "on_default": run(True, {}),
+ }
+ print("E2E_RESULT " + json.dumps(results))
+ """
+)
+
+
[email protected](scope = "module")
+def e2e_results(tmp_path_factory):
+ pytest.importorskip("trl")
+ pytest.importorskip("peft")
+ pytest.importorskip("datasets")
+ tmp = tmp_path_factory.mktemp("gc_off_e2e")
+ env = dict(os.environ)
+ env.update(
+ UNSLOTH_ALLOW_CPU = "1",
+ CUDA_VISIBLE_DEVICES = "",
+ UNSLOTH_COMPILE_LOCATION = str(tmp / "compiled"),
+ E2E_OUT = str(tmp / "out"),
+ PYTHONPATH = os.pathsep.join(p for p in (str(_REPO), env.get("PYTHONPATH", "")) if p),
+ )
+ script = tmp / "e2e.py"
+ script.write_text(_E2E, encoding = "utf-8")
+ proc = subprocess.run(
+ [sys.executable, str(script)],
+ env = env,
+ cwd = str(tmp),
+ capture_output = True,
+ text = True,
+ timeout = 900,
+ )
+ lines = [l for l in proc.stdout.splitlines() if l.startswith("E2E_RESULT ")]
+ assert proc.returncode == 0 and lines, (proc.stdout[-3000:], proc.stderr[-6000:])
+ return json.loads(lines[-1].split(" ", 1)[1]), proc.stdout
+
+
+def test_gc_off_model_trains_without_checkpointing(e2e_results):
+ results, stdout = e2e_results
+ off = results["off_default"]
+ assert off["args_gc"] is False
+ assert off["ckpt"] == 0, off
+ assert "so gradient checkpointing stays off" in stdout
+
+
+def test_explicit_config_true_still_checkpoints(e2e_results):
+ results, _ = e2e_results
+ on = results["off_explicit_on"]
+ off = results["off_default"]
+ assert on["args_gc"] is True
+ assert on["ckpt"] > 0
+ # Every checkpointed layer forward is recomputed in the backward.
+ assert on["fwd"] == 2 * off["fwd"], (on, off)
+
+
[email protected]("case", ["unsloth_default", "on_default"])
+def test_gc_on_models_unchanged(e2e_results, case):
+ results, _ = e2e_results
+ assert results[case]["args_gc"] is True
+ assert results[case]["ckpt"] > 0
diff --git a/unsloth/models/_gc_config_default.py b/unsloth/models/_gc_config_default.py
new file mode 100644
index 00000000000..57a7b97d3bd
--- /dev/null
+++ b/unsloth/models/_gc_config_default.py
@@ -0,0 +1,96 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2023-present Daniel Han-Chen & the Unsloth team. All rights reserved.
+
+"""Honour ``use_gradient_checkpointing = False`` when the trainer config only carries TRL's default.
+
+TRL configs default ``gradient_checkpointing = True``, so ``Trainer.train()`` would re-enable it (~25-30% slower).
+Unsloth configs record whether it was passed to ``__init__`` as ``_unsloth_gradient_checkpointing_explicit``,
+not compared to a default (which varies by TRL release); full rebuilds (``dataclasses.replace``,
+``Config(**args.to_dict())``) count as explicit, keeping the old behaviour.
+
+Rules, at trainer construction:
+- model ``False`` + config at TRL's default -> config set to ``False``.
+- config explicitly ``True`` -> kept.
+- config not an Unsloth class -> kept, with a hint.
+- model ``"unsloth"`` / ``True`` / unset -> untouched.
+"""
+
+import functools
+import inspect
+
+__all__ = [
+ "EXPLICIT_FLAG",
+ "track_explicit_gradient_checkpointing",
+ "honour_gradient_checkpointing_off",
+]
+
+EXPLICIT_FLAG = "_unsloth_gradient_checkpointing_explicit"
+_WRAPPED_FLAG = "_unsloth_tracks_explicit_gradient_checkpointing"
+_NOTED = set()
+
+
+def _note(key, message):
+ if key in _NOTED:
+ return
+ _NOTED.add(key)
+ print(message)
+
+
+def track_explicit_gradient_checkpointing(config_class):
+ """Wrap ``config_class.__init__`` to record whether ``gradient_checkpointing`` was passed. Idempotent."""
+ init = config_class.__dict__.get("__init__")
+ if init is None or getattr(init, _WRAPPED_FLAG, False):
+ return False
+ try:
+ names = list(inspect.signature(init).parameters)
+ except (TypeError, ValueError):
+ return False
+ if "gradient_checkpointing" not in names:
+ return False
+ position = names.index("gradient_checkpointing") - 1
+
+ @functools.wraps(init)
+ def __init__(self, *args, **kwargs):
+ explicit = "gradient_checkpointing" in kwargs or len(args) > position
+ init(self, *args, **kwargs)
+ # Not a dataclass field: to_dict(), to_json_string() and asdict() never see it.
+ self.__dict__[EXPLICIT_FLAG] = explicit
+
+ setattr(__init__, _WRAPPED_FLAG, True)
+ config_class.__init__ = __init__
+ return True
+
+
+def honour_gradient_checkpointing_off(args, model):
+ if args is None or model is None or not getattr(args, "gradient_checkpointing", False):
+ return False
+ try:
+ recorded = model._unsloth_gradient_checkpointing
+ except Exception:
+ return False
+ if recorded is not False:
+ return False
+ explicit = getattr(args, "__dict__", {}).get(EXPLICIT_FLAG, None)
+ if explicit is None:
+ _note(
+ "unknown",
+ "Unsloth: the model was set up with use_gradient_checkpointing = False, but the trainer config "
+ f"({type(args).__name__}) has gradient_checkpointing = True, so gradient checkpointing stays on. "
+ "Pass gradient_checkpointing = False in the config to train without it.",
+ )
+ return False
+ if explicit:
+ _note(
+ "explicit",
+ "Unsloth: the model was set up with use_gradient_checkpointing = False, but the trainer config sets "
+ "gradient_checkpointing = True explicitly, so gradient checkpointing stays on.",
+ )
+ return False
+ args.gradient_checkpointing = False
+ _note(
+ "off",
+ "Unsloth: the model was set up with use_gradient_checkpointing = False, so gradient checkpointing stays "
+ f"off (TRL's {type(args).__name__} defaults it to True). If you run out of memory, use "
+ 'use_gradient_checkpointing = "unsloth" (or pass gradient_checkpointing = True in the config).',
+ )
+ return True
diff --git a/unsloth/models/rl.py b/unsloth/models/rl.py
index 47820953eb4..031a1c675c2 100644
--- a/unsloth/models/rl.py
+++ b/unsloth/models/rl.py
@@ -643,6 +643,12 @@ class Unsloth{RLTrainer_name}(_Unsloth{RLTrainer_name}):
if getattr(args, "parallel_mode", None) == ParallelMode.NOT_DISTRIBUTED and args.n_gpu > 1:
if getattr(args, "_n_gpu", 1) != 1:
args._n_gpu = 1
+ if "model" in locals():
+ try:
+ from unsloth.models._gc_config_default import honour_gradient_checkpointing_off
+ honour_gradient_checkpointing_off(args, model)
+ except Exception:
+ pass
if "model" in locals() and hasattr(model, "for_training"):
_use_gc = model._unsloth_gradient_checkpointing if hasattr(model, '_unsloth_gradient_checkpointing') else getattr(args, 'gradient_checkpointing', True)
model.for_training(use_gradient_checkpointing=_use_gc)
@@ -4028,6 +4034,14 @@ def _patch_trl_rl_trainers_impl(trainer_file = "grpo_trainer"):
pass
_patched_config = getattr(created_module, f"Unsloth{RLConfig_name}", None)
_patch_config_pickle_identity(RLConfig, _patched_config)
+ if _patched_config is not None:
+ try:
+ from ._gc_config_default import track_explicit_gradient_checkpointing
+ track_explicit_gradient_checkpointing(_patched_config)
+ except Exception as e:
+ logger.info(
+ f"Unsloth: Could not track explicit gradient_checkpointing on {RLConfig_name}: {e}"
+ )
if _patched_config is not None:
_register_config_pickle_fallback(_displaced_config, _patched_config)