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.

Parse each forward once in the per-step loss-kwargs walk (#13585)

Daniel Han447cb3c8a0b9
diff --git a/tests/test_accepts_loss_kwargs_head_walk.py b/tests/test_accepts_loss_kwargs_head_walk.py
index 2152f6cfdb6..e779fe3452f 100644
--- a/tests/test_accepts_loss_kwargs_head_walk.py
+++ b/tests/test_accepts_loss_kwargs_head_walk.py
@@ -1061,3 +1061,19 @@ def test_a_compiled_wrapper_keeps_the_value_and_its_marker_together(ns, mods):
inner = compiled._orig_mod.__dict__
assert inner["accepts_loss_kwargs"] is True and ns["_is_guess"](inner)
assert compiled.__dict__["accepts_loss_kwargs"] is True and ns["_is_guess"](compiled.__dict__)
+
+
+def test_each_forward_is_parsed_once_across_steps(ns, mods, monkeypatch):
+ import inspect
+
+ # _get_batch_samples walks this chain every step; re-reading and re-parsing the source each time
+ # cost milliseconds of host time per step.
+ head = mods.NativeForConditionalGeneration()
+ peft = mods.PeftModelForCausalLM(head)
+ first = [type(m).__name__ for m in ns["_loss_kwargs_chain"](peft)]
+ calls = []
+ original = inspect.getsource
+ monkeypatch.setattr(inspect, "getsource", lambda obj: calls.append(obj) or original(obj))
+ for _ in range(3):
+ assert [type(m).__name__ for m in ns["_loss_kwargs_chain"](peft)] == first
+ assert calls == []
diff --git a/tests/test_accepts_loss_kwargs_remote_mean_loss.py b/tests/test_accepts_loss_kwargs_remote_mean_loss.py
index a6050e23cfa..14b4b7dc529 100644
--- a/tests/test_accepts_loss_kwargs_remote_mean_loss.py
+++ b/tests/test_accepts_loss_kwargs_remote_mean_loss.py
@@ -75,6 +75,8 @@ _NAMES = {
"_CARRIER_MUTATORS",
"_nested_scope_ids",
"_pass_through_child",
+ "_PASS_THROUGH_ATTRS",
+ "_pass_through_attr",
"_local_assignments",
"_is_count_get",
"_forward_consumes_num_items_in_batch",
diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.py
index f31cb706dce..5e47316357c 100644
--- a/unsloth/models/_utils.py
+++ b/unsloth/models/_utils.py
@@ -5496,9 +5496,27 @@ def _loss_head(model):
return None
+# Unwrapped forward -> the child it hands the loss count to (or None): parsed once, not every step.
+_PASS_THROUGH_ATTRS = {}
+
+
def _pass_through_child(m, modules):
try:
- tree = ast.parse(textwrap.dedent(inspect.getsource(inspect.unwrap(type(m).forward))))
+ forward = inspect.unwrap(type(m).forward)
+ except Exception:
+ return None
+ try:
+ attr = _PASS_THROUGH_ATTRS[forward]
+ except KeyError:
+ attr = _PASS_THROUGH_ATTRS[forward] = _pass_through_attr(forward)
+ except TypeError:
+ attr = _pass_through_attr(forward)
+ return modules[attr] if attr is not None and attr in modules else None
+
+
+def _pass_through_attr(forward):
+ try:
+ tree = ast.parse(textwrap.dedent(inspect.getsource(forward)))
except Exception:
return None
node = next(
@@ -5521,7 +5539,6 @@ def _pass_through_child(m, modules):
isinstance(func, ast.Attribute)
and isinstance(func.value, ast.Name)
and func.value.id == "self"
- and func.attr in modules
):
return None
kwarg = node.args.kwarg.arg if node.args.kwarg is not None else None
@@ -5543,7 +5560,7 @@ def _pass_through_child(m, modules):
or (kw.arg == "num_items_in_batch" and carries_count(kw.value))
for kw in call.keywords
)
- return modules[func.attr] if forwards_count else None
+ return func.attr if forwards_count else None
def _call_forwards_count(node, call):