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.

Stop a comment in unsloth_zoo from silently disabling the patched SFT trainer (#10854)

Daniel Han89a10db0d98e
diff --git a/tests/utils/test_packing.py b/tests/utils/test_packing.py
index c8d55ff3634..aecc56b5c91 100644
--- a/tests/utils/test_packing.py
+++ b/tests/utils/test_packing.py
@@ -1247,6 +1247,42 @@ def test_require_replace_raises_on_missing_anchor():
assert _require_replace("abc", "z", "Z", required = False, where = "optional") == "abc"
+def test_require_replace_survives_a_trailing_comment_on_the_anchor():
+ """A comment appearing on an anchored line is not a code change.
+
+ unsloth_zoo #1192 put `# noqa: F821` on the `pack_dataset(` call while
+ building a lint gate. The literal anchor stopped matching, the required edit
+ raised, and every SFT run silently fell back to TRL's own trainer -- losing
+ the packing and truncation fixes this module exists to apply. The code the
+ anchor points at never moved.
+ """
+ from unsloth.models.rl_replacements import _require_replace
+
+ anchor = "dataset = pack_dataset(\n a,\n b,\n)"
+ replacement = "dataset = pack_dataset(\n a,\n **kw,\n)"
+
+ # The exact shape that broke: a trailing comment on the first anchored line.
+ commented = "x = 1\ndataset = pack_dataset( # noqa: F821 -- reached only past the probe\n a,\n b,\n)\ny = 2"
+ assert _require_replace(commented, anchor, replacement) == f"x = 1\n{replacement}\ny = 2"
+
+ # A comment on any other anchored line is tolerated too.
+ inner = "dataset = pack_dataset(\n a, # the columns\n b,\n)"
+ assert _require_replace(inner, anchor, replacement) == replacement
+
+ # Tolerance must not reach across a real code change: `b` -> `c` still raises.
+ with pytest.raises(RuntimeError):
+ _require_replace(
+ "dataset = pack_dataset(\n a,\n c,\n)",
+ anchor,
+ replacement,
+ where = "changed argument",
+ )
+
+ # A `#` inside a string literal is not a comment and must still match exactly.
+ hashed = 'sep = "#"\n'
+ assert _require_replace(hashed + anchor, anchor, replacement) == hashed + replacement
+
+
def test_resolve_string_model_config_forwards_token(monkeypatch):
import transformers
diff --git a/unsloth/models/rl_replacements.py b/unsloth/models/rl_replacements.py
index b5240078c51..d57473b18d2 100644
--- a/unsloth/models/rl_replacements.py
+++ b/unsloth/models/rl_replacements.py
@@ -457,6 +457,24 @@ def _warn_once(where, message):
logger.warning(message)
+def _comment_tolerant_anchor(old):
+ """`old` as a regex that still matches once a line grows a trailing comment.
+
+ A literal anchor stops matching the moment the Zoo appends a comment to one of
+ its lines, and this repo edits comments in bulk: unsloth_zoo #1192 put a
+ `# noqa: F821` on the `pack_dataset(` call while building a lint gate, which
+ was enough to drop the whole patched SFT trainer. Nothing about the code
+ changed, so nothing about the match should have.
+
+ Every line is allowed an optional trailing comment, including lines that
+ already carry one, so a comment being added, reworded or removed upstream is
+ drift this survives.
+ """
+ return re.compile(
+ "\n".join(re.escape(line.rstrip()) + r"[ \t]*(?:\#[^\n]*)?" for line in old.split("\n"))
+ )
+
+
def _require_replace(
function,
old,
@@ -466,20 +484,35 @@ def _require_replace(
required = True,
where = "",
):
- """str.replace that never silently no-ops a load-bearing source edit. Plain str.replace returns the source unchanged when the anchor is absent, so a drifted anchor in a newer TRL / unsloth_zoo would skip the edit while later edits still reference helper variables it should have introduced (NameError at runtime). Fail loudly for a required edit, warn once and skip for an optional one."""
- if old not in function:
- detail = f" ({where})" if where else ""
- if required:
- raise RuntimeError(
- f"Unsloth: source anchor not found{detail}; the patched function is out "
- "of sync with this TRL / unsloth_zoo version. Please file a bug report."
- )
- _warn_once(
- where,
- f"Unsloth: skipped an optional source edit{detail} (anchor not found).",
- )
+ """str.replace that never silently no-ops a load-bearing source edit. Plain str.replace returns the source unchanged when the anchor is absent, so a drifted anchor in a newer TRL / unsloth_zoo would skip the edit while later edits still reference helper variables it should have introduced (NameError at runtime). Fail loudly for a required edit, warn once and skip for an optional one.
+
+ The literal anchor is tried first and a comment-tolerant form of the same
+ anchor second, so the only drift that reaches the raise below is drift in the
+ code itself. `re.sub` would read backslashes in `new` as group references, so
+ the replacement is handed over as a function.
+ """
+ if old in function:
+ return function.replace(old, new, count)
+
+ function, applied = _comment_tolerant_anchor(old).subn(
+ lambda _match: new,
+ function,
+ count = count,
+ )
+ if applied:
return function
- return function.replace(old, new, count)
+
+ detail = f" ({where})" if where else ""
+ if required:
+ raise RuntimeError(
+ f"Unsloth: source anchor not found{detail}; the patched function is out "
+ "of sync with this TRL / unsloth_zoo version. Please file a bug report."
+ )
+ _warn_once(
+ where,
+ f"Unsloth: skipped an optional source edit{detail} (anchor not found).",
+ )
+ return function
# The one line every unsloth_zoo sft_prepare_dataset ends its worker count on, as a regex so a renamed right-hand side still matches and the indentation carries over. Unchanged since Aug 2025 (#257) while the block around it was rewritten repeatedly, hence a fallback anchor.