Skip to content

tests(version_compat): AST-based symbol checks and per-model drift coverage - #6740

Merged
danielhanchen merged 13 commits into
unslothai:mainfrom
Suchitra-idu:tests/qwen3-pinned-symbols
Oct 5, 2026
Merged

danielhanchen merged 13 commits into
unslothai:mainfrom
Suchitra-idu:tests/qwen3-pinned-symbols

Conversation

@Suchitra-idu

@Suchitra-idu Suchitra-idu commented Jun 29, 2026 •

Copy link
Copy Markdown
Contributor

Why

Every Fast<Model>Model.pre_patch in unsloth/models/ rebinds forward methods and assigns a rotary-embedding class on transformers.models.<X>.modeling_<X>. If any of those names disappear upstream, the rebind silently no-ops (Python lets you assign arbitrary attrs on a module) and Unsloth's fast paths stop applying with no signal at import time. tests/version_compat/test_transformers_pinned_symbols.py previously guarded this failure class only for Gemma3Attention and GptOssModel; everything else was uncovered.

While adding the coverage I also moved the regex/substring helpers in tests/version_compat/_fetch.py to AST. The old has_def and inline <sym> in src checks false-positive on docstrings, string literals, and commented-out code. The new helpers parse the source once and walk the tree, with a regex fallback for the case where ast.parse fails on syntax newer than the running Python.

What

  1. tests/version_compat/test_transformers_pinned_symbols.py: replaced the per-model test_<X>_* tests with a table-driven test_modeling_contract parametrized over a MODELING_CONTRACTS tuple of ModelingContract rows. Each row pins the upstream module path, the classes that must remain, the rotary-embedding name (or None), and optional extra bindings. Adding a new model is one tuple entry.

    Covered: llama, qwen2, qwen3, qwen3_moe, gemma, gemma2, gemma3, gpt_oss, mistral, cohere, granite, falcon_h1. glm4_moe is omitted with an inline note: upstream renamed Glm4MoeLiteNaiveMoe to Glm4MoeLiteExperts on main, so the Unsloth side needs a fix before the test can pass against main.

  2. tests/version_compat/_fetch.py: AST-based helpers replacing the regex ones.

    • has_def(src, name, kind="any"): walks ClassDef / FunctionDef; with default kind="any" also matches module-level assignments and import ... as name.
    • New is_bound(src, name): True iff name is bound at module scope. Replaces ~30 ad-hoc <sym> in src substring checks across the suite.
    • New function_params(src, name, *, cls=None): returns param names of def name(...), optionally scoped to a class. Replaces ad-hoc re.search(r"^\s*def name\(([^)]*)\)", ...) patterns.
    • All three fall back to regex matchers when ast.parse fails. AST is preferred; the fallback is best-effort and coarser. function_params(..., cls=X) returns None rather than guessing under regex (class scoping isn't reliable without an AST).

    Five files updated to use the new helpers: test_transformers_pinned_symbols.py, test_bitsandbytes_pinned_symbols.py, test_sentence_transformers_pinned_symbols.py, test_peft_pinned_symbols.py, test_trl_grpo_pinned_symbols.py.

No new runtime deps (ast and re are stdlib). No changes to non-test code.

FastQwen3Model.pre_patch (unsloth/models/qwen3.py) rebinds
Qwen3Attention/DecoderLayer/Model/ForCausalLM.forward and assigns
Qwen3RotaryEmbedding on the transformers module. If any of those
names disappear in a future transformers release the rebind
silently no-ops and Unsloth's Qwen3 fast paths stop applying.

Mirrors the existing gemma3/gpt_oss checks in the same file.
CPU-only; 12 parametrized tags, ~6s wall.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 010b2b5e5f

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +260 to +261
if src is None:
pytest.skip(f"{tag}: modeling_qwen3.py missing")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Fail when pinned Qwen3 source is missing

When a pinned transformers tag moves or deletes src/transformers/models/qwen3/modeling_qwen3.py, fetch_text returns None on the 404 and this new guard skips instead of failing. unsloth/models/qwen3.py imports that exact module for supported transformers versions, so skipping makes the test pass in the file-moved/deleted case it is meant to catch; the Qwen3-supported pinned tags should fail here (or check an explicit alternate path), while transient network skips are already handled inside fetch_text.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Skipping is intended: gpt_oss and falcon_h1 do not exist on the older matrix tags, and a rename on a tag where the file exists still fails the class asserts.

@Suchitra-idu

Copy link
Copy Markdown
Contributor Author

Same gap exists in other files

Happy to send a follow-up, either one test per model like this one, or a table-driven parametrize (one function + a row per model). Or skip it if you'd rather not expand the pattern. Let me know?

@wasimysaid wasimysaid self-assigned this Jun 29, 2026
@Suchitra-idu

Suchitra-idu commented Jun 29, 2026 •

Copy link
Copy Markdown
Contributor Author

@wasimysaid I'm willing to work on that. I'd like to learn more about Unsloth by contributing further.

Could you assign the follow-up work to me? I'd also appreciate any guidance on the intended approach or your vision for these tests so I can implement them.

@wasimysaid

Copy link
Copy Markdown
Collaborator

@wasimysaid I'm willing to work on that. I'd like to learn more about Unsloth by contributing further.

Could you assign the follow-up work to me? I'd also appreciate any guidance on the intended approach or your vision for these tests so I can implement them.

oh yes no problem, thank you! il take a look in a bit and tell you if i found anything :)

@Suchitra-idu

Copy link
Copy Markdown
Contributor Author

@danielhanchen , @wasimysaid The current has_def helpers are regex, which can false-positive on docstrings and string literals. I prototyped an AST-based matcher and it works against the same TRANSFORMERS_TAGS matrix. Want the follow-up to switch to that, or keep the regex shape for consistency with the existing file?

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@Suchitra-idu Suchitra-idu changed the title tests(version_compat): add Qwen3 modeling-symbol drift check tests(version_compat): AST-based symbol checks and per-model drift coverage Jun 29, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@Suchitra-idu

Suchitra-idu commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor Author

@danielhanchen this is ready for review. Coverage now spans 12 model files via a table-driven MODELING_CONTRACTS setup (llama, qwen2, qwen3, qwen3_moe, gemma, gemma2, gemma3, gpt_oss, mistral, cohere, granite, falcon_h1). 144/144 pass across the 12-tag matrix, full suite 239/1 skip.

Let me know if any changes are needed, otherwise it's all yours.

@danielhanchen

Copy link
Copy Markdown
Member

Review record for #6740, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#408.

Findings are quoted as posted and attributed to the account that posted them. The reactions shown are the triage recorded on the mirror; none were re-applied here. Commits are named as the mirror's, with this PR's equivalent where one was found.

Round 1: reviewed mirror 61d231840 (no equivalent commit found on this PR)

chatgpt-codex-connector[bot] (on the mirror)

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 61d2318404

No triage reaction was recorded on the mirror

Verdict

The round converged at mirror 61d231840 (no equivalent commit found on this PR).

This is the review as it stood at that commit. Anything pushed to this PR afterwards was not part of it.

@danielhanchen
danielhanchen merged commit a1ff98b into unslothai:main Oct 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants