Repository navigation
Backfill the conversion symbols peft imports and transformers 5 dropped - #8051
danielhanchen merged 33 commits into
Conversation
|
Codex Review: Didn't find any major issues. You're on a roll. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4d6a80a9cd
ℹ️ About Codex in GitHub
Your team has set up Codex to 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 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef, ast.Import)): | ||
| continue |
There was a problem hiding this comment.
Traverse module-level class bodies
A module-level class body executes immediately while the module is imported, so excluding ast.ClassDef makes this drift guard ignore any from transformers... import placed inside one. If PEFT adds such an import to one of this converter's module-level classes, the converter can still fail at startup while test_the_symbol_list_matches_what_peft_imports remains green; skip function bodies, but descend into class bodies.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
You are right, fixed in 53b90c0. A module-level class body executes the moment the class is defined, so an import in one can raise the very startup ImportError this backfill absorbs. Only function bodies are skipped now, which still leaves a class nested inside a function ignored. test_only_imports_that_run_at_module_load_are_collected now pins both: a top-level class Holder: import is collected, a class Inner: inside a function is not.
There was a problem hiding this comment.
The premise does not hold: nothing excludes ast.ClassDef. The walk is
for node in ast.walk(ast.parse(src)):
if isinstance(node, ast.ImportFrom) and (node.module or "").startswith("transformers"):and ast.walk is a full breadth-first traversal of every child node, so a from transformers... inside a module-level class body is already seen, as is one inside a function body. There is no node-type filter to add.
Added test_a_class_body_import_is_seen so the property is pinned rather than left to the reader.
The one asymmetry worth naming is the opposite of the one described: the walk also picks up imports inside function bodies, which are lazy and cannot break startup. That is deliberately left as-is. It fails safe, and a lazy import of a name transformers has dropped still raises, just at call time instead.
…gh blocks and nesting The fail-on-use map was too broad. peft reaches _convert_peft_config_moe for ANY model type with a checkpoint conversion mapping, not just MoE ones, and for those a None IS the right answer: the function returns without a MoE rewrite, exactly as it would with the real map. Raising for every unknown key broke ordinary adapter loads to guard a case they are not in. Only lookups that look like a fused-MoE family raise now; everything else gets the default it asked for. Substring hints, because the alias that would tell us exactly is the thing we lost. The relative-import scan read only direct statements, so a package pulling its implementation in from inside `try:` or `if not TYPE_CHECKING:` had that child skipped. It uses the same import-time traversal as the transformers scan now, which also keeps a typing-only child out. And a child's relative imports were resolved against the package root. In a nested split where __init__ imports .sub.core and sub/core.py imports .ops, Python reads that as sub.ops, so the walk fetched the wrong file and never read the nested implementation. Each pending module carries its package path.
Eleven of the twenty-four fused MoE model types say nothing about MoE in their names: deepseek_v2/v3/v32, dots1, longcat_flash, minimax, minimax_m2, mellum, qwen3_next, solar_open and flex_olmo. peft rewrites targets only for the two base patterns mixtral and qwen2_moe, and those are exactly the model types that map to them, so the substring test answered the default for the checkpoints the stand-in exists to protect. The snapshot is the list now, with the naming convention kept on top for a type added after it, and a test that compares the snapshot against the installed transformers when it ships one. Recovering the map by shape alone picked the largest module-level dict[str, str], which after a rename is as likely to be an alias table as the conversion map. Installing that maps a coincidentally matching model type to the wrong conversion family, and bypasses the stand-in so every other MoE lookup goes back to a silent None. A candidate now has to agree with at least three known model-type to pattern pairs, and the best agreement wins rather than the biggest dict. Nothing convincing recovers nothing, which lands on the map that raises. The package walk dropped ImportFrom.level, so from ..ops import X probed sub.ops instead of ops and read nothing, leaving the real target's transformers imports out of a drift check that then cannot fail. Levels travel with the name and resolve against the importing module's package; one that walks above the converter package is dropped rather than guessed at. Also brings the test file's licence header in line with its siblings.
…orts Two ways the drift check could stay green while the converter still breaks at startup. `from .sub import core` imports `pkg.sub.core` whenever `core` is a module, whether or not `sub/__init__.py` re-exports it. The walk queued only `sub`, so the fetcher read the package shim and never the file that carries the transformers imports. Both are queued now; a name that turns out to be a symbol costs one fetch that finds nothing, which the caller already skips. `import *` names no child and is unchanged. `import transformers.x` raises the same startup ModuleNotFoundError as the `from` form, and every `ast.Import` was skipped outright, so a newly required submodule outside the two known modules could break `peft.utils.transformers_weight_conversion` with this guard reporting no new dependency. Module-form imports are collected with an empty symbol set, since they bind no names. Inside a function body they are still ignored: those never run at import time, which is the bar this whole walk applies.
…cases
`mixtral` and `qwen2_moe` map to themselves and were missing from the snapshot.
Fetched the real `conversion_mapping.py` from both tags to check: v5.5.0 is
missing those two and v5.3.0 also `qwen3_5_moe`, so the live comparison fails on
a transformers pyproject permits. Adding them rather than filtering the
comparison, because the omission was not cosmetic: `_is_moe("mixtral")` answered
False, since it is not in the snapshot, not in the not-fused carve-out, and the
substring hint looks for "moe", which that name does not contain. That is the
silent default the stand-in exists to prevent, for one of the two families peft
actually rewrites.
An absolute import of the package's own child was invisible to the walk, which
keyed on `node.level`. `from <pkg>.core import build`, `from <pkg> import core`
and `import <pkg>.core` all load the same file a relative import would, and all
three are queued now, as level 0 meaning already rooted at the converter
package. Anything outside it is still ignored.
`ast.Match` keeps its statements under `cases`, which neither walker descended
into, so a module-level `match` hid every import inside it. `cases` joins the
traversal tuple in both; a `match_case` is not a statement and the existing
`getattr(item, "body", [])` fallback already covers that shape, the same way it
covers `ExceptHandler`. Function bodies stay excluded.
The substring hint caught every unknown *moe* name, including qwen3_vl_moe and lfm2_moe: both are supported models, both are absent from _MODEL_TO_CONVERSION_PATTERN in 5.3.0 and 5.5.0 alike, and peft answers None and skips the rewrite for them, so the refusal fired on an ordinary adapter load. Carved out every *moe* / *mixtral* type absent from the map in both releases, in a set kept apart from _PEFT_MOE_NAMED_NOT_FUSED so that one's canary keeps comparing like with like. A name absent from only one release stays fused: refusing a fused type costs a message, answering None for one is the silent mis-conversion this exists to prevent.
Main grew its own general backfill pass while this branch sat, so the rebase kept both passes and both new tests, but the import they need came from a hunk main had already rewritten. 65 passed.
160f80b to
d1eaf5b
Compare
|
Rebased onto main. Main grew its own general backfill pass while this sat, so it is worth saying exactly what is left and why it is not redundant. Main now has
Both are
One thing I got wrong in the first resolution and am flagging rather than burying: those two tests reference The CI red on this PR was the pre-#8132 base, not this change. Main's Backend and Repo test jobs are repaired now, so this should come back clean. |
|
@codex review |
|
Codex Review: Didn't find any major issues. What shall we delve into next? Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Ministral_3_(3B)_Reinforcement_Learning_Sudoku_Gamedies on transformers 5.0.0.dev0 with peft 0.19.1:import_fixes.pyalready stubstransformers.conversion_mappingandtransformers.core_model_loadingfor peft, but only when the module is absent. Here the module is present and only the name is gone, so_peft_stub_module_importableanswered True,patched_anystayed False, and the guard returned without doing anything.What changed
The names peft imports at module top are backfilled onto the real module instead of stubbing the module out. Nothing is replaced and no existing name is overwritten, so:
Falseand does nothing)The symbol list lives next to the existing stubs so the two cannot drift apart.
Verification
Reproduced the exact reported shape on transformers 5.14.1 / peft 0.20.0 by deleting the symbol:
tests/version_compat/test_peft_conversion_symbol_backfill.pyadds 6 tests covering the backfill, that the module object and its own exports survive, that a complete module is untouched, idempotency, and a check of our symbol list against peft's own import block so drift is caught rather than discovered in a run.