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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
tests(version_compat): AST-based symbol checks and per-model drift coverage (#6740)
Suchitra Malimbadaa1ff98b84732
diff --git a/tests/version_compat/_fetch.py b/tests/version_compat/_fetch.pyindex 706b26efade..104f1ac3a74 100644--- a/tests/version_compat/_fetch.py+++ b/tests/version_compat/_fetch.py@@ -1,9 +1,11 @@# SPDX-License-Identifier: AGPL-3.0-only# Copyright 2026-present the Unsloth AI Inc. team.-"""Shared helpers for version-compat suites: GitHub raw fetch + regex symbol grep."""+"""Shared helpers for version-compat suites: GitHub raw fetch + AST symbol lookup with regex fallback."""from __future__ import annotations+import ast+import functoolsimport osimport reimport urllib.error@@ -12,6 +14,7 @@ import urllib.requestimport pytest[email protected]_cache(maxsize = None)def fetch_text(repo: str, ref: str, path: str) -> str | None:"""Fetch a file from GitHub raw. None on 404; skips on transient network errors."""url = f"https://raw.githubusercontent.com/{repo}/{ref}/{path}"@@ -30,29 +33,153 @@ def fetch_text(repo: str, ref: str, path: str) -> str | None:pytest.skip(f"GitHub fetch failed ({e}) for {url}")+def first_match(repo: str, ref: str, paths: list[str]) -> tuple[str, str] | None:+ """Return (path, src) for the first existing candidate path, else None."""+ for p in paths:+ src = fetch_text(repo, ref, p)+ if src is not None:+ return (p, src)+ return None+++def _parse(src: str) -> ast.Module | None:+ try:+ return ast.parse(src)+ except SyntaxError:+ return None+++def _walk_module_stmts(stmts):+ # Descend into If/Try/With (gated bindings) but not into def/class (new scope).+ for node in stmts:+ yield node+ if isinstance(node, ast.If):+ yield from _walk_module_stmts(node.body)+ yield from _walk_module_stmts(node.orelse)+ elif isinstance(node, ast.Try):+ yield from _walk_module_stmts(node.body)+ for h in node.handlers:+ yield from _walk_module_stmts(h.body)+ yield from _walk_module_stmts(node.orelse)+ yield from _walk_module_stmts(node.finalbody)+ elif isinstance(node, ast.With):+ yield from _walk_module_stmts(node.body)+++def _module_bindings(tree: ast.Module) -> set[str]:+ out: set[str] = set()+ for node in _walk_module_stmts(tree.body):+ if isinstance(node, (ast.ClassDef, ast.FunctionDef, ast.AsyncFunctionDef)):+ out.add(node.name)+ elif isinstance(node, ast.Assign):+ for t in node.targets:+ if isinstance(t, ast.Name):+ out.add(t.id)+ elif isinstance(node, ast.AnnAssign) and isinstance(node.target, ast.Name):+ out.add(node.target.id)+ elif isinstance(node, (ast.Import, ast.ImportFrom)):+ for a in node.names:+ out.add(a.asname or a.name.split(".")[0])+ return out+++def _re_is_bound(src: str, name: str) -> bool:+ n = re.escape(name)+ patterns = (+ rf"^\s*class\s+{n}\b",+ rf"^\s*(?:async\s+)?def\s+{n}\b",+ rf"^\s*{n}\s*[:=]",+ rf"^\s*from\s+\S+\s+import\s+[^#\n]*\b{n}\b",+ rf"^\s*import\s+[^#\n]*\b{n}\b",+ rf"\bas\s+{n}\b",+ )+ return any(re.search(p, src, re.MULTILINE) for p in patterns)+++def _re_has_def(src: str, name: str, kind: str) -> bool:+ n = re.escape(name)+ if kind in ("any", "class") and re.search(rf"^\s*class\s+{n}\b", src, re.MULTILINE):+ return True+ if kind in ("any", "func") and re.search(rf"^\s*(?:async\s+)?def\s+{n}\b", src, re.MULTILINE):+ return True+ return kind == "any" and _re_is_bound(src, name)+++def _re_function_params(src: str, name: str) -> tuple[str, ...] | None:+ n = re.escape(name)+ m = re.search(rf"^\s*(?:async\s+)?def\s+{n}\s*\(([^)]*)\)", src, re.MULTILINE | re.DOTALL)+ if m is None:+ return None+ out: list[str] = []+ for part in m.group(1).split(","):+ p = part.strip().lstrip("*")+ for sep in (":", "="):+ i = p.find(sep)+ if i >= 0:+ p = p[:i]+ p = p.strip()+ if p and p != "/":+ out.append(p)+ return tuple(out)+++def is_bound(src: str, name: str) -> bool:+ """True if `name` is bound at module scope (def/class/assign/import-as)."""+ tree = _parse(src)+ if tree is None:+ return _re_is_bound(src, name)+ return name in _module_bindings(tree)++def has_def(src: str,name: str,kind: str = "any",) -> bool:- """Grep for `class Name`, `def name`, or `Name = ...` at any indent (no ast.parse)."""- if kind in ("any", "class") and re.search(- rf"^\s*class\s+{re.escape(name)}\b", src, re.MULTILINE- ):- return True- if kind in ("any", "func") and re.search(- rf"^\s*(?:async\s+)?def\s+{re.escape(name)}\b", src, re.MULTILINE- ):- return True- if kind == "any" and re.search(rf"^\s*{re.escape(name)}\s*[:=]", src, re.MULTILINE):- return True- return False+ """`class name` / `def name` at any depth; with kind="any" also any module binding."""+ tree = _parse(src)+ if tree is None:+ return _re_has_def(src, name, kind)+ want_class = kind in ("any", "class")+ want_func = kind in ("any", "func")+ for node in ast.walk(tree):+ if want_class and isinstance(node, ast.ClassDef) and node.name == name:+ return True+ if (+ want_func+ and isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))+ and node.name == name+ ):+ return True+ return kind == "any" and name in _module_bindings(tree)-def first_match(repo: str, ref: str, paths: list[str]) -> tuple[str, str] | None:- """Return (path, src) for the first existing candidate path, else None."""- for p in paths:- src = fetch_text(repo, ref, p)- if src is not None:- return (p, src)+def function_params(+ src: str,+ name: str,+ *,+ cls: str | None = None,+) -> tuple[str, ...] | None:+ """Param names of the first `def name(...)`; scoped to `class cls:` if given. None if absent."""+ tree = _parse(src)+ if tree is None:+ if cls is not None:+ # Regex fallback scopes to text after `class cls`: first matching def there.+ m = re.search(rf"^\s*class\s+{re.escape(cls)}\b", src, re.MULTILINE)+ if m is None:+ return None+ src = src[m.end() :]+ return _re_function_params(src, name)+ scope: ast.AST | None = tree+ if cls is not None:+ scope = next(+ (n for n in ast.walk(tree) if isinstance(n, ast.ClassDef) and n.name == cls),+ None,+ )+ if scope is None:+ return None+ for node in ast.walk(scope):+ if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and node.name == name:+ a = node.args+ return tuple(p.arg for p in (*a.posonlyargs, *a.args, *a.kwonlyargs))return Nonediff --git a/tests/version_compat/test_bitsandbytes_pinned_symbols.py b/tests/version_compat/test_bitsandbytes_pinned_symbols.pyindex 51d9edadecb..6daa82cec43 100644--- a/tests/version_compat/test_bitsandbytes_pinned_symbols.py+++ b/tests/version_compat/test_bitsandbytes_pinned_symbols.py@@ -10,7 +10,7 @@ from pathlib import Pathimport pytest-from tests.version_compat._fetch import fetch_text, first_match, has_def+from tests.version_compat._fetch import fetch_text, first_match, function_params, has_def, is_bounddef _super_init_call(source: str, class_name: str) -> ast.Call | None:@@ -74,7 +74,7 @@ def test_bnb_functional_4bit(tag: str):assert hit is not None, f"{tag}: bitsandbytes/functional[.py|/__init__.py] both missing"_, src = hitneeded = ("dequantize_4bit", "quantize_4bit")- missing = [n for n in needed if not has_def(src, n, "func") and n not in src]+ missing = [n for n in needed if not is_bound(src, n)]assert not missing, (f"{tag}: bnb.functional missing {missing}; " f"unsloth-zoo dequant kernels rely on these")@@ -92,9 +92,9 @@ def test_bnb_nn_linear4bit_classes(tag: str):src = fetch_text("bitsandbytes-foundation/bitsandbytes", tag, p)if src is None:continue- if has_def(src, "Linear4bit", "class") or "Linear4bit" in src:+ if is_bound(src, "Linear4bit"):found_linear = True- if has_def(src, "Params4bit", "class") or "Params4bit" in src:+ if is_bound(src, "Params4bit"):found_params = Trueif found_linear and found_params:return@@ -110,7 +110,7 @@ def test_bnb_matmul_4bit_top_level(tag: str):src = fetch_text("bitsandbytes-foundation/bitsandbytes", tag, "bitsandbytes/__init__.py")if src is None:pytest.skip(f"{tag}: bitsandbytes/__init__.py missing")- assert "matmul_4bit" in src, (+ assert is_bound(src, "matmul_4bit"), (f"{tag}: bitsandbytes.matmul_4bit not exported at package root; "f"unsloth/kernels/utils.py + zoo/temporary_patches/moe call paths break")@@ -149,7 +149,7 @@ def test_bnb_functional_get_ptr(tag: str):if hit is None:pytest.skip(f"{tag}: functional missing")_, src = hit- assert has_def(src, "get_ptr", "func") or "get_ptr" in src, (+ assert is_bound(src, "get_ptr"), (f"{tag}: bnb.functional.get_ptr missing; "f"unsloth/kernels/utils.py module-top ImportError")@@ -166,7 +166,7 @@ def test_bnb_quantstate_from_dict(tag: str):pytest.skip(f"{tag}: functional missing")_, src = hitassert has_def(src, "QuantState", "class"), f"{tag}: bnb.functional.QuantState missing"- assert "from_dict" in src, (+ assert function_params(src, "from_dict", cls = "QuantState") is not None, (f"{tag}: QuantState.from_dict missing; " f"unsloth-zoo monkey-patch silently no-ops")@@ -188,7 +188,7 @@ def test_bnb_nn_linear8bitlt(tag: str):]for p in candidates:src = fetch_text("bitsandbytes-foundation/bitsandbytes", tag, p)- if src and (has_def(src, "Linear8bitLt", "class") or "Linear8bitLt" in src):+ if src and is_bound(src, "Linear8bitLt"):returnpytest.fail(f"{tag}: bnb.nn.Linear8bitLt missing in {candidates}; " f"legacy load_in_8bit path breaks"@@ -215,7 +215,7 @@ def test_bnb_utils_pack_unpack(tag: str):if src is None:pytest.skip(f"{tag}: bitsandbytes/utils.py missing")for name in ("pack_dict_to_tensor", "unpack_tensor_to_dict"):- assert has_def(src, name, "func") or name in src, f"{tag}: bnb.utils.{name} missing"+ assert is_bound(src, name), f"{tag}: bnb.utils.{name} missing"def test_bnb_cextension_rocm_warp_size_optional(tag: str):@@ -236,7 +236,7 @@ def test_bnb_autograd_functions_matmul_4bit(tag: str):)if src is None:pytest.skip(f"{tag}: bitsandbytes/autograd/_functions.py missing")- assert "matmul_4bit" in src, f"{tag}: bnb.autograd._functions.matmul_4bit missing"+ assert is_bound(src, "matmul_4bit"), f"{tag}: bnb.autograd._functions.matmul_4bit missing"def test_bnb_version_parseable(tag: str):diff --git a/tests/version_compat/test_peft_pinned_symbols.py b/tests/version_compat/test_peft_pinned_symbols.pyindex ebd0e5af9b4..60ef23d3025 100644--- a/tests/version_compat/test_peft_pinned_symbols.py+++ b/tests/version_compat/test_peft_pinned_symbols.py@@ -12,7 +12,7 @@ import reimport pytest-from tests.version_compat._fetch import fetch_text, first_match, has_def+from tests.version_compat._fetch import fetch_text, first_match, has_def, is_bound# pyproject pin: peft>=0.18.0. Test the floor + each minor since. `main` catches breakage before a release lands.@@ -38,7 +38,7 @@ def test_peft_top_level_exports(tag: str):"get_peft_model","PeftModel",)- missing = [n for n in needed if n not in src]+ missing = [n for n in needed if not is_bound(src, n)]assert not missing, (f"{tag}: peft top-level missing {missing}; "f"unsloth.models.sentence_transformer:1948 + unsloth-zoo saving_utils "@@ -137,7 +137,7 @@ def test_peft_variant_kwarg_keys_const(tag: str):src = fetch_text("huggingface/peft", tag, "src/peft/tuners/lora/layer.py")if src is None:pytest.skip(f"{tag}: src/peft/tuners/lora/layer.py missing")- if "VARIANT_KWARG_KEYS" not in src:+ if not is_bound(src, "VARIANT_KWARG_KEYS"):pytest.fail(f"{tag}: peft.tuners.lora.layer.VARIANT_KWARG_KEYS missing; "f"unsloth_zoo/compiler.py:2645 import injection breaks (unsloth-zoo#430)"@@ -200,9 +200,7 @@ def test_peft_transformers_weight_conversion_module(tag: str):if hit is None:pytest.skip(f"{tag}: transformers_weight_conversion not present (legacy peft)")_, src = hit- assert (- has_def(src, "build_peft_weight_mapping", "func") or "build_peft_weight_mapping" in src- ), (+ assert is_bound(src, "build_peft_weight_mapping"), (f"{tag}: build_peft_weight_mapping missing in transformers_weight_conversion; "f"unsloth/import_fixes.py:1375-1456 wrap breaks (unsloth#5167)")@@ -217,7 +215,7 @@ def test_peft_integrations_dequantize_module_weight(tag: str):hit = first_match("huggingface/peft", tag, candidates)assert hit is not None, f"{tag}: src/peft/utils/integrations[.py|/__init__.py] both missing"_, src = hit- assert has_def(src, "dequantize_module_weight", "func") or "dequantize_module_weight" in src, (+ assert is_bound(src, "dequantize_module_weight"), (f"{tag}: peft.utils.integrations.dequantize_module_weight missing; "f"unsloth-zoo vllm_utils.py:2701, unsloth/_utils.py:1550, "f"saving_utils.py:270 ImportError"diff --git a/tests/version_compat/test_sentence_transformers_pinned_symbols.py b/tests/version_compat/test_sentence_transformers_pinned_symbols.pyindex e062eb4f722..4a67dfce076 100644--- a/tests/version_compat/test_sentence_transformers_pinned_symbols.py+++ b/tests/version_compat/test_sentence_transformers_pinned_symbols.py@@ -9,7 +9,7 @@ import reimport pytest-from tests.version_compat._fetch import fetch_text, first_match, has_def+from tests.version_compat._fetch import fetch_text, first_match, has_def, is_bound# ST is unpinned in pyproject.toml; track the last few minors plus main.@@ -33,7 +33,7 @@ def test_st_top_level_exports(tag: str):src = fetch_text("UKPLab/sentence-transformers", tag, "sentence_transformers/__init__.py")assert src is not None, f"{tag}: sentence_transformers/__init__.py missing"needed = ("SentenceTransformer", "SentenceTransformerTrainer")- missing = [n for n in needed if n not in src]+ missing = [n for n in needed if not is_bound(src, n)]assert not missing, (f"{tag}: sentence_transformers top-level missing {missing}; "f"unsloth.models.sentence_transformer:1467,2154 will ImportError"@@ -53,7 +53,7 @@ def test_st_models_re_exports(tag: str):needed = ("Transformer", "Pooling", "Normalize")if legacy_hit is not None:_path, src = legacy_hit- missing = [n for n in needed if n not in src]+ missing = [n for n in needed if not is_bound(src, n)]assert not missing, (f"{tag}: legacy sentence_transformers/models layout missing "f"{missing}; unsloth.models.sentence_transformer:1016,1206,1467 "@@ -171,9 +171,7 @@ def test_st_util_helpers(tag: str):assert hit is not None, f"{tag}: sentence_transformers/util[.py|/__init__.py] both missing"_path, src = hitfor fn in ("import_from_string", "load_dir_path"):- defined_here = has_def(src, fn, "func")- reexported = bool(re.search(rf"\b{re.escape(fn)}\b", src))- if not (defined_here or reexported):+ if not is_bound(src, fn):subpaths = ["sentence_transformers/util/import_utils.py","sentence_transformers/util/file_utils.py",@@ -183,7 +181,7 @@ def test_st_util_helpers(tag: str):found = Falsefor sp in subpaths:sub = fetch_text("UKPLab/sentence-transformers", tag, sp)- if sub and (has_def(sub, fn, "func") or fn in sub):+ if sub and is_bound(sub, fn):found = Truebreakassert found, (diff --git a/tests/version_compat/test_transformers_pinned_symbols.py b/tests/version_compat/test_transformers_pinned_symbols.pyindex 41e16637d60..3f6d63e3a9a 100644--- a/tests/version_compat/test_transformers_pinned_symbols.py+++ b/tests/version_compat/test_transformers_pinned_symbols.py@@ -17,10 +17,17 @@ import tempfileimport urllib.errorimport urllib.requestfrom pathlib import Path+from typing import NamedTupleimport pytest-from tests.version_compat._fetch import fetch_text, first_match, has_def+from tests.version_compat._fetch import (+ fetch_text,+ first_match,+ function_params,+ has_def,+ is_bound,+)# Read from pyproject: a hardcoded floor went stale and silently dropped 4.52-4.56.@@ -230,10 +237,10 @@ def test_trainer_compute_loss_num_items_in_batch_param(tag: str):hit = first_match("huggingface/transformers", tag, candidates)assert hit is not None_, src = hit- m = re.search(r"^\s*def compute_loss\(([^)]*)\)", src, re.MULTILINE | re.DOTALL)- if m is None:+ params = function_params(src, "compute_loss", cls = "Trainer")+ if params is None:pytest.fail(f"{tag}: Trainer.compute_loss not found in source")- assert "num_items_in_batch" in m.group(1), (+ assert "num_items_in_batch" in params, (f"{tag}: Trainer.compute_loss signature missing num_items_in_batch param; "f"unsloth grad-accum patches assume this kwarg present")@@ -359,10 +366,9 @@ def test_fp8linear_init_param_names(tag: str):pytest.skip(f"{tag}: integrations/finegrained_fp8.py missing")if not has_def(src, "FP8Linear", "class"):pytest.skip(f"{tag}: FP8Linear not yet defined")- has_bias_kw = re.search(r"def __init__\([^)]*\bbias\b", src) is not None- has_has_bias_kw = re.search(r"def __init__\([^)]*\bhas_bias\b", src) is not None+ params = function_params(src, "__init__", cls = "FP8Linear") or ()assert (- has_bias_kw or has_has_bias_kw+ "bias" in params or "has_bias" in params), f"{tag}: FP8Linear.__init__ has neither `bias` nor `has_bias` param"@@ -371,36 +377,149 @@ def test_processing_utils_unpack_importable(tag: str):src = fetch_text("huggingface/transformers", tag, "src/transformers/processing_utils.py")if src is None:pytest.skip(f"{tag}: processing_utils.py missing")- has_unpack = bool(re.search(r"^Unpack\b\s*=", src, re.MULTILINE) or "Unpack" in src)- assert has_unpack, (+ assert is_bound(src, "Unpack"), (f"{tag}: transformers.processing_utils.Unpack missing; "f"unsloth-zoo#583/584 import guard breaks")-# Models: gemma3, gpt_oss forward signature drift.---def test_gemma3_attention_forward_present(tag: str):- src = fetch_text(- "huggingface/transformers",- tag,- "src/transformers/models/gemma3/modeling_gemma3.py",- )- if src is None:- pytest.skip(f"{tag}: modeling_gemma3.py missing")- assert has_def(src, "Gemma3Attention", "class"), f"{tag}: class Gemma3Attention missing"+# NamedTuple, not dataclass: test_transformers_cap_sites.py exec_module()s this file outside sys.modules.+class ModelingContract(NamedTuple):+ name: str+ path: str+ classes: tuple[str, ...]+ rope_name: str | None = None+ extra_bindings: tuple[str, ...] = ()+ patch_site: str = ""+++MODELING_CONTRACTS: tuple[ModelingContract, ...] = (+ ModelingContract(+ name = "llama",+ path = "src/transformers/models/llama/modeling_llama.py",+ classes = ("LlamaAttention", "LlamaDecoderLayer", "LlamaModel", "LlamaForCausalLM"),+ rope_name = "LlamaRotaryEmbedding",+ # LlamaLinearScalingRotaryEmbedding is Unsloth's own shim, not an upstream rebind.+ patch_site = "unsloth/models/llama.py:FastLlamaModel.pre_patch",+ ),+ ModelingContract(+ name = "qwen2",+ path = "src/transformers/models/qwen2/modeling_qwen2.py",+ classes = ("Qwen2Attention", "Qwen2DecoderLayer", "Qwen2Model", "Qwen2ForCausalLM"),+ rope_name = "Qwen2RotaryEmbedding",+ patch_site = "unsloth/models/qwen2.py:FastQwen2Model.pre_patch",+ ),+ ModelingContract(+ name = "qwen3",+ path = "src/transformers/models/qwen3/modeling_qwen3.py",+ classes = ("Qwen3Attention", "Qwen3DecoderLayer", "Qwen3Model", "Qwen3ForCausalLM"),+ rope_name = "Qwen3RotaryEmbedding",+ patch_site = "unsloth/models/qwen3.py:FastQwen3Model.pre_patch",+ ),+ ModelingContract(+ name = "qwen3_moe",+ path = "src/transformers/models/qwen3_moe/modeling_qwen3_moe.py",+ classes = (+ "Qwen3MoeAttention",+ "Qwen3MoeSparseMoeBlock",+ "Qwen3MoeMLP",+ "Qwen3MoeDecoderLayer",+ "Qwen3MoeModel",+ "Qwen3MoeForCausalLM",+ ),+ rope_name = "Qwen3MoeRotaryEmbedding",+ patch_site = "unsloth/models/qwen3_moe.py:FastQwen3MoeModel.pre_patch",+ ),+ ModelingContract(+ name = "gemma",+ path = "src/transformers/models/gemma/modeling_gemma.py",+ classes = ("GemmaAttention", "GemmaDecoderLayer", "GemmaModel", "GemmaForCausalLM"),+ rope_name = "GemmaRotaryEmbedding",+ patch_site = "unsloth/models/gemma.py:FastGemmaModel.pre_patch",+ ),+ ModelingContract(+ name = "gemma2",+ path = "src/transformers/models/gemma2/modeling_gemma2.py",+ classes = ("Gemma2Attention", "Gemma2DecoderLayer", "Gemma2Model", "Gemma2ForCausalLM"),+ rope_name = "Gemma2RotaryEmbedding",+ patch_site = "unsloth/models/gemma2.py:FastGemma2Model.pre_patch",+ ),+ ModelingContract(+ name = "gemma3",+ path = "src/transformers/models/gemma3/modeling_gemma3.py",+ classes = ("Gemma3Attention",),+ patch_site = "early-warning: gemma3 modeling still present upstream",+ ),+ ModelingContract(+ name = "gpt_oss",+ path = "src/transformers/models/gpt_oss/modeling_gpt_oss.py",+ classes = ("GptOssModel",),+ patch_site = "early-warning: gpt_oss modeling still present upstream",+ ),+ ModelingContract(+ name = "mistral",+ path = "src/transformers/models/mistral/modeling_mistral.py",+ classes = (+ "MistralAttention",+ "MistralDecoderLayer",+ "MistralModel",+ "MistralForCausalLM",+ ),+ rope_name = "MistralRotaryEmbedding",+ patch_site = "unsloth/models/mistral.py:FastMistralModel.pre_patch",+ ),+ ModelingContract(+ name = "cohere",+ path = "src/transformers/models/cohere/modeling_cohere.py",+ classes = ("CohereAttention", "CohereDecoderLayer", "CohereModel", "CohereForCausalLM"),+ rope_name = "CohereRotaryEmbedding",+ patch_site = "unsloth/models/cohere.py:FastCohereModel.pre_patch",+ ),+ ModelingContract(+ name = "granite",+ path = "src/transformers/models/granite/modeling_granite.py",+ classes = (+ "GraniteAttention",+ "GraniteDecoderLayer",+ "GraniteModel",+ "GraniteForCausalLM",+ ),+ rope_name = "GraniteRotaryEmbedding",+ patch_site = "unsloth/models/granite.py:FastGraniteModel.pre_patch",+ ),+ ModelingContract(+ name = "falcon_h1",+ path = "src/transformers/models/falcon_h1/modeling_falcon_h1.py",+ classes = (+ "FalconH1Attention",+ "FalconH1DecoderLayer",+ "FalconH1Model",+ "FalconH1ForCausalLM",+ ),+ rope_name = "FalconH1RotaryEmbedding",+ extra_bindings = ("FalconH1RMSNorm",),+ patch_site = "unsloth/models/falcon_h1.py:FastFalconH1Model.pre_patch",+ ),+ # glm4_moe omitted: upstream renamed Glm4MoeLiteNaiveMoe -> Glm4MoeLiteExperts; add with the Unsloth-side fix.+)-def test_gpt_oss_model_forward_present(tag: str):- src = fetch_text(- "huggingface/transformers",- tag,- "src/transformers/models/gpt_oss/modeling_gpt_oss.py",- )[email protected]("contract", MODELING_CONTRACTS, ids = lambda c: c.name)+def test_modeling_contract(tag: str, contract: ModelingContract):+ src = fetch_text("huggingface/transformers", tag, contract.path)if src is None:- pytest.skip(f"{tag}: modeling_gpt_oss.py missing (legacy)")- assert has_def(src, "GptOssModel", "class"), f"{tag}: class GptOssModel missing"+ pytest.skip(f"{tag}/{contract.name}: {contract.path} missing")+ for cls in contract.classes:+ assert has_def(+ src, cls, "class"+ ), f"{tag}/{contract.name}: class {cls} missing: {contract.patch_site}"+ if contract.rope_name is not None:+ assert is_bound(src, contract.rope_name), (+ f"{tag}/{contract.name}: {contract.rope_name} missing; "+ f"{contract.patch_site} RoPE rebind silently no-ops"+ )+ for n in contract.extra_bindings:+ assert is_bound(src, n), f"{tag}/{contract.name}: {n} missing: {contract.patch_site}"# auto_factory: unsloth#5155 _LazyAutoMapping private API.@@ -471,9 +590,8 @@ def test_modeling_attn_mask_utils_symbols(tag: str):if src is None:pytest.skip(f"{tag}: modeling_attn_mask_utils.py missing")assert has_def(src, "AttentionMaskConverter", "class"), f"{tag}: AttentionMaskConverter missing"- assert (- has_def(src, "_prepare_4d_attention_mask_for_sdpa", "func")- or "_prepare_4d_attention_mask_for_sdpa" in src+ assert is_bound(+ src, "_prepare_4d_attention_mask_for_sdpa"), f"{tag}: _prepare_4d_attention_mask_for_sdpa missing"@@ -491,7 +609,7 @@ def test_training_args_parallel_mode_importable(tag: str):src = fetch_text("huggingface/transformers", tag, "src/transformers/training_args.py")if src is None:pytest.skip(f"{tag}: training_args.py missing")- assert "ParallelMode" in src, (+ assert is_bound(src, "ParallelMode"), (f"{tag}: transformers.training_args.ParallelMode missing; "f"unsloth-zoo loss_utils.py:232 ImportError")diff --git a/tests/version_compat/test_trl_grpo_pinned_symbols.py b/tests/version_compat/test_trl_grpo_pinned_symbols.pyindex b34da374f4c..90793e16b96 100644--- a/tests/version_compat/test_trl_grpo_pinned_symbols.py+++ b/tests/version_compat/test_trl_grpo_pinned_symbols.py@@ -16,7 +16,7 @@ import reimport pytest-from tests.version_compat._fetch import fetch_text, first_match, has_def+from tests.version_compat._fetch import fetch_text, first_match, has_def, is_boundfrom tests.version_compat._rl_anchors import _reject_aux_loss_opt_in, _rl_py_constant@@ -85,7 +85,7 @@ def test_trl_top_level_grpo_sft(tag: str):src = fetch_text("huggingface/trl", tag, "trl/__init__.py")assert src is not None, f"trl/__init__.py missing in {tag}"for name in ("GRPOTrainer", "GRPOConfig", "SFTTrainer", "SFTConfig"):- assert name in src, (+ assert is_bound(src, name), (f"{tag}: `from trl import {name}` will fail; "f"unsloth/trainer.py + unsloth/models/rl.py rely on this re-export")@@ -123,9 +123,9 @@ def test_data_collator_for_preference_resolvable(tag: str):new_path = fetch_text("huggingface/trl", tag, "trl/trainer/dpo_trainer.py")old_path = fetch_text("huggingface/trl", tag, "trl/trainer/utils.py")have = []- if new_path is not None and "DataCollatorForPreference" in new_path:+ if new_path is not None and is_bound(new_path, "DataCollatorForPreference"):have.append("trl.trainer.dpo_trainer")- if old_path is not None and "DataCollatorForPreference" in old_path:+ if old_path is not None and is_bound(old_path, "DataCollatorForPreference"):have.append("trl.trainer.utils")assert have, (f"{tag}: DataCollatorForPreference defined in NEITHER "@@ -141,7 +141,7 @@ def test_trl_trainer_utils_pad(tag: str):# Some TRL versions split utils into a package.src = fetch_text("huggingface/trl", tag, "trl/trainer/utils/__init__.py")assert src is not None, f"{tag}: trl/trainer/utils[.py|/__init__.py] both missing"- assert has_def(src, "pad", "func") or "def pad(" in src, (+ assert is_bound(src, "pad"), (f"{tag}: trl.trainer.utils.pad missing — "f"unsloth/models/rl_replacements.py:326 emits `from trl.trainer.utils "f"import pad as _unsloth_trl_pad` into the GRPO compile cell"@@ -160,7 +160,7 @@ def test_unwrap_model_for_generation_either_path(tag: str):src = fetch_text("huggingface/trl", tag, path)if src is None:continue- if "unwrap_model_for_generation" in src:+ if is_bound(src, "unwrap_model_for_generation"):returnpytest.fail(f"{tag}: trl.unwrap_model_for_generation not in any known path "@@ -331,8 +331,8 @@ def test_trl_openenv_utils_generators(tag: str):src = fetch_text("huggingface/trl", tag, "trl/experimental/openenv/utils.py")if src is None:pytest.skip(f"{tag}: openenv.utils not present (gated optional)")- legacy = "generate_rollout_completions" in src- new = "_generate_rollout_completions_colocate" in src+ legacy = is_bound(src, "generate_rollout_completions")+ new = is_bound(src, "_generate_rollout_completions_colocate")assert legacy or new, (f"{tag}: openenv.utils has neither `generate_rollout_completions` "f"nor `_generate_rollout_completions_colocate`; "