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.

Keep fused-loss engagement counters out of traced code (#15575)

Daniel Han54657edbcb5b
diff --git a/tests/python/test_preference_chunked_compile.py b/tests/python/test_preference_chunked_compile.py
index ede8afb40c8..5b142a2d201 100644
--- a/tests/python/test_preference_chunked_compile.py
+++ b/tests/python/test_preference_chunked_compile.py
@@ -224,3 +224,53 @@ def test_traces_without_graph_breaks_and_matches_trl(kind, kwargs, split, masked
for a, b in zip(out + grads, got + got_grads):
torch.testing.assert_close(a, b, rtol = 1e-10, atol = 1e-12)
torch._dynamo.reset()
+
+
[email protected]("dynamic", [False, True, None], ids = ["static", "dynamic", "auto"])
+def test_engagement_counter_compiles_once(dynamic):
+ from torch._dynamo.testing import CompileCounterWithBackend
+
+ forward = _load(True)
+ trainer = _Trainer()
+ head, batch, states = _batch(False)
+
+ def body(states):
+ outputs, _ = forward(trainer, head, head, batch, PAIRS, states, "cpo")
+ return _scalar(outputs)
+
+ body(states).backward()
+ assert trainer._unsloth_pref_chunked_calls == 1
+ torch._dynamo.reset()
+ counter = CompileCounterWithBackend("aot_eager")
+ compiled = torch.compile(body, fullgraph = True, backend = counter, dynamic = dynamic)
+ compiled(states).backward()
+ first = counter.frame_count
+ for _ in range(9):
+ compiled(states).backward()
+ assert counter.frame_count == first
+ body(states).backward()
+ assert trainer._unsloth_pref_chunked_calls == 2
+ torch._dynamo.reset()
+
+
+def test_every_engagement_counter_skips_tracing():
+ import re
+
+ root = SOURCE.parent
+ sites = 0
+ for path in (SOURCE, root / "_fused_rl_logprobs.py"):
+ tree = ast.parse(path.read_text(encoding = "utf-8"))
+ guarded = set()
+ for node in ast.walk(tree):
+ if (
+ isinstance(node, ast.If)
+ and ast.unparse(node.test) == "not torch.compiler.is_compiling()"
+ ):
+ guarded.update(id(n) for n in ast.walk(node))
+ for node in ast.walk(tree):
+ if isinstance(node, ast.Assign) and re.search(
+ r"_unsloth_\w+_calls", ast.unparse(node.targets[0])
+ ):
+ sites += 1
+ assert id(node) in guarded, f"{path.name}:{node.lineno} counts inside traced code"
+ assert sites >= 10
diff --git a/tests/python/test_preference_chunked_logprobs.py b/tests/python/test_preference_chunked_logprobs.py
index 2da23c061fc..8afc042b064 100644
--- a/tests/python/test_preference_chunked_logprobs.py
+++ b/tests/python/test_preference_chunked_logprobs.py
@@ -318,7 +318,16 @@ def test_labels_differing_from_input_ids_take_a_second_nll_pass(monkeypatch):
def test_declines_without_kernel_or_on_unsupported_setups(monkeypatch):
- cls, _ = _trl_class("orpo")
+ stock, source = _trl_class("orpo")
+ cls = type(
+ "Patched",
+ (stock,),
+ {
+ "concatenated_forward": _compile(
+ rl.orpo_trainer_concatenated_forward("concatenated_forward", source)
+ )
+ },
+ )
model = _HiddenStatesLM(_ToyLM())
assert rl._unsloth_pref_chunked_head(_trainer(cls, "orpo"), model) is not None
for attr, value in (
diff --git a/unsloth/models/_fused_rl_logprobs.py b/unsloth/models/_fused_rl_logprobs.py
index 2ce8b6b310b..8de5eed0ad8 100644
--- a/unsloth/models/_fused_rl_logprobs.py
+++ b/unsloth/models/_fused_rl_logprobs.py
@@ -250,9 +250,10 @@ def ppo_score(
"forward returned logits, not hidden states",
)
return None
- route.trainer.__dict__["_unsloth_ppo_fused_calls"] = (
- route.trainer.__dict__.get("_unsloth_ppo_fused_calls", 0) + 1
- )
+ if not torch.compiler.is_compiling():
+ route.trainer.__dict__["_unsloth_ppo_fused_calls"] = (
+ route.trainer.__dict__.get("_unsloth_ppo_fused_calls", 0) + 1
+ )
return out
@@ -379,9 +380,10 @@ def rloo_fused_logps(
all_logps.append(out[0])
if compute_entropy:
all_entropies.append(out[1])
- self.__dict__["_unsloth_rloo_fused_calls"] = (
- self.__dict__.get("_unsloth_rloo_fused_calls", 0) + 1
- )
+ if not torch.compiler.is_compiling():
+ self.__dict__["_unsloth_rloo_fused_calls"] = (
+ self.__dict__.get("_unsloth_rloo_fused_calls", 0) + 1
+ )
logps = torch.cat(all_logps, dim = 0)
entropies = torch.cat(all_entropies, dim = 0) if compute_entropy else None
if traits["aux"]:
diff --git a/unsloth/models/rl_replacements.py b/unsloth/models/rl_replacements.py
index 8dacccab47d..5a2891f46e9 100644
--- a/unsloth/models/rl_replacements.py
+++ b/unsloth/models/rl_replacements.py
@@ -6413,11 +6413,12 @@ def _unsloth_gkd_chunked_loss(self, model, inputs, num_items_in_batch, layout):
scaling = scaler.get_scale() if scaler is not None and scaler.is_enabled() else None,
rows_per_chunk = int(rows) if rows.strip().isdigit() else None,
)
- try:
- self._unsloth_gkd_chunked_calls = getattr(self, "_unsloth_gkd_chunked_calls", 0) + 1
- self._unsloth_gkd_fused_calls = getattr(self, "_unsloth_gkd_fused_calls", 0) + 1
- except Exception:
- pass
+ if not torch.compiler.is_compiling():
+ try:
+ self._unsloth_gkd_chunked_calls = getattr(self, "_unsloth_gkd_chunked_calls", 0) + 1
+ self._unsloth_gkd_fused_calls = getattr(self, "_unsloth_gkd_fused_calls", 0) + 1
+ except Exception:
+ pass
return loss
loss, _entropy_sum, _n_valid = distillation_chunked_jsd(
student_states,
@@ -6438,10 +6439,11 @@ def _unsloth_gkd_chunked_loss(self, model, inputs, num_items_in_batch, layout):
temperature = 1.0,
**jsd_kwargs,
)
- try:
- self._unsloth_gkd_chunked_calls = getattr(self, "_unsloth_gkd_chunked_calls", 0) + 1
- except Exception:
- pass
+ if not torch.compiler.is_compiling():
+ try:
+ self._unsloth_gkd_chunked_calls = getattr(self, "_unsloth_gkd_chunked_calls", 0) + 1
+ except Exception:
+ pass
return loss
@@ -6722,10 +6724,11 @@ def _unsloth_pref_chunked_forward(self, model, head, concatenated_batch, len_cho
nll_loss = torch.tensor(0.0).to(self.accelerator.device)
else:
nll_loss = -(nll_per_token * chosen_nll_mask).sum() / chosen_nll_mask.sum()
- try:
- self._unsloth_pref_chunked_calls = getattr(self, "_unsloth_pref_chunked_calls", 0) + 1
- except Exception:
- pass
+ if not torch.compiler.is_compiling():
+ try:
+ self._unsloth_pref_chunked_calls = getattr(self, "_unsloth_pref_chunked_calls", 0) + 1
+ except Exception:
+ pass
# Stand-ins: TRL only reads chosen/rejected logits as .detach().mean().
return (
all_logps[:len_chosen],
@@ -6979,10 +6982,11 @@ def _unsloth_dpo_hidden_logps(self, chunked, hidden_states, labels, loss_mask):
lm_head_bias = head.bias,
return_mean_logits = True,
)
- try:
- self._unsloth_dpo_chunked_calls = getattr(self, "_unsloth_dpo_chunked_calls", 0) + 1
- except Exception:
- pass
+ if not torch.compiler.is_compiling():
+ try:
+ self._unsloth_dpo_chunked_calls = getattr(self, "_unsloth_dpo_chunked_calls", 0) + 1
+ except Exception:
+ pass
return per_token_logps.to(labels.device), mean_logits.unsqueeze(-1).to(labels.device)
@@ -7145,10 +7149,11 @@ def _unsloth_dpo_loss_stats(
return_is_top1 = not ref,
entropy_requires_grad = False,
)
- try:
- self._unsloth_dpo_chunked_calls = getattr(self, "_unsloth_dpo_chunked_calls", 0) + 1
- except Exception:
- pass
+ if not torch.compiler.is_compiling():
+ try:
+ self._unsloth_dpo_chunked_calls = getattr(self, "_unsloth_dpo_chunked_calls", 0) + 1
+ except Exception:
+ pass
if ref:
return out
logps, entropy, mean_logits, is_top1 = out
@@ -7419,10 +7424,11 @@ def _unsloth_kto_hidden_logps(self, chunked, hidden_states, labels, label_pad_to
return_mean_logits = True,
)
per_token_logps = per_token_logps[:, :-1]
- try:
- self._unsloth_dpo_chunked_calls = getattr(self, "_unsloth_dpo_chunked_calls", 0) + 1
- except Exception:
- pass
+ if not torch.compiler.is_compiling():
+ try:
+ self._unsloth_dpo_chunked_calls = getattr(self, "_unsloth_dpo_chunked_calls", 0) + 1
+ except Exception:
+ pass
completion_logps = (per_token_logps * mask).sum(-1)
logits_sum = (mean_logits * head.weight.shape[0]).unsqueeze(-1)
return completion_logps.to(out_device), logits_sum.to(out_device)
@@ -7640,10 +7646,11 @@ def _unsloth_fused_pass_logps(
return_mean_logits = stats,
entropy_requires_grad = False,
)
- try:
- self._unsloth_dpo_chunked_calls = getattr(self, "_unsloth_dpo_chunked_calls", 0) + 1
- except Exception:
- pass
+ if not torch.compiler.is_compiling():
+ try:
+ self._unsloth_dpo_chunked_calls = getattr(self, "_unsloth_dpo_chunked_calls", 0) + 1
+ except Exception:
+ pass
out = out if isinstance(out, tuple) else (out,)
out = tuple(torch.nn.functional.pad(x, (0, width - n)) if n != width else x for x in out)
if not stats:
@@ -8346,10 +8353,11 @@ def _unsloth_odpo_hidden_logps(
logit_scale_divide = divide,
logit_softcapping = softcap,
)
- try:
- self._unsloth_dpo_chunked_calls = getattr(self, "_unsloth_dpo_chunked_calls", 0) + 1
- except Exception:
- pass
+ if not torch.compiler.is_compiling():
+ try:
+ self._unsloth_dpo_chunked_calls = getattr(self, "_unsloth_dpo_chunked_calls", 0) + 1
+ except Exception:
+ pass
return logps.to(completion_ids.device)