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 gradient_checkpointing assigned on the config after construction (#15200)

Daniel Han0cbcf8dd7cd2
diff --git a/tests/test_gradient_checkpointing_off_honoured.py b/tests/test_gradient_checkpointing_off_honoured.py
index a97873a79fb..85f18e6e9f7 100644
--- a/tests/test_gradient_checkpointing_off_honoured.py
+++ b/tests/test_gradient_checkpointing_off_honoured.py
@@ -93,6 +93,36 @@ def test_honour_rules(capsys):
assert foreign.gradient_checkpointing is True
+def test_assignment_after_construction_is_explicit():
+ import dataclasses
+
+ module = _load_module()
+
+ @dataclasses.dataclass
+ class Config:
+ output_dir: str = None
+ gradient_checkpointing: bool = True
+
+ def __post_init__(self):
+ self.gradient_checkpointing = (
+ self.gradient_checkpointing
+ ) # assignments inside __init__ do not count
+
+ assert module.track_explicit_gradient_checkpointing(Config)
+ assert Config().__dict__[module.EXPLICIT_FLAG] is False
+
+ assigned = Config()
+ assigned.gradient_checkpointing = True
+ assert assigned.__dict__[module.EXPLICIT_FLAG] is True
+ assert not module.honour_gradient_checkpointing_off(assigned, _Model(False))
+ assert assigned.gradient_checkpointing is True
+
+ other = Config()
+ other.output_dir = "y"
+ assert module.honour_gradient_checkpointing_off(other, _Model(False))
+ assert other.gradient_checkpointing is False
+
+
_E2E = textwrap.dedent(
r"""
import json, os, sys, types
@@ -133,6 +163,7 @@ _E2E = textwrap.dedent(
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)]})
+ assign_gc = config_kwargs.pop("_assign_gc", None)
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)
@@ -140,6 +171,8 @@ _E2E = textwrap.dedent(
args = SFTConfig(max_length = 64, **kwargs)
except TypeError:
args = SFTConfig(max_seq_length = 64, **kwargs)
+ if assign_gc is not None:
+ args.gradient_checkpointing = assign_gc
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)
@@ -153,6 +186,7 @@ _E2E = textwrap.dedent(
results = {
"off_default": run(False, {}),
"off_explicit_on": run(False, {"gradient_checkpointing": True}),
+ "off_assigned_on": run(False, {"_assign_gc": True}),
"unsloth_default": run("unsloth", {}),
"on_default": run(True, {}),
}
@@ -208,6 +242,13 @@ def test_explicit_config_true_still_checkpoints(e2e_results):
assert on["fwd"] == 2 * off["fwd"], (on, off)
+def test_config_true_assigned_after_construction_still_checkpoints(e2e_results):
+ results, _ = e2e_results
+ assigned = results["off_assigned_on"]
+ assert assigned["args_gc"] is True
+ assert assigned["ckpt"] > 0
+
+
@pytest.mark.parametrize("case", ["unsloth_default", "on_default"])
def test_gc_on_models_unchanged(e2e_results, case):
results, _ = e2e_results
diff --git a/unsloth/models/_gc_config_default.py b/unsloth/models/_gc_config_default.py
index 57a7b97d3bd..73e3bc2d866 100644
--- a/unsloth/models/_gc_config_default.py
+++ b/unsloth/models/_gc_config_default.py
@@ -4,8 +4,8 @@
"""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``,
+Unsloth configs record whether it was passed to ``__init__`` (or assigned on the config afterwards, e.g.
+``args.gradient_checkpointing = True``) 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:
@@ -58,6 +58,17 @@ def track_explicit_gradient_checkpointing(config_class):
setattr(__init__, _WRAPPED_FLAG, True)
config_class.__init__ = __init__
+
+ # An assignment after construction (args.gradient_checkpointing = True) is a user choice too.
+ # Inside __init__ the flag is not set yet, so the dataclass's own field assignments do not count.
+ original_setattr = config_class.__setattr__
+
+ def __setattr__(self, name, value):
+ original_setattr(self, name, value)
+ if name == "gradient_checkpointing" and EXPLICIT_FLAG in self.__dict__:
+ self.__dict__[EXPLICIT_FLAG] = True
+
+ config_class.__setattr__ = __setattr__
return True