Skip to content

Backfill the conversion symbols peft imports and transformers 5 dropped - #8051

Merged
danielhanchen merged 33 commits into
unslothai:mainfrom
danielhanchen:fix/peft-conversion-symbol-backfill
Aug 9, 2026
Merged

danielhanchen merged 33 commits into
unslothai:mainfrom
danielhanchen:fix/peft-conversion-symbol-backfill

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Ministral_3_(3B)_Reinforcement_Learning_Sudoku_Game dies on transformers 5.0.0.dev0 with peft 0.19.1:

ImportError: cannot import name '_MODEL_TO_CONVERSION_PATTERN' from 'transformers.conversion_mapping'

import_fixes.py already stubs transformers.conversion_mapping and transformers.core_model_loading for peft, but only when the module is absent. Here the module is present and only the name is gone, so _peft_stub_module_importable answered True, patched_any stayed 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:

  • a transformers that still exports them sees no change at all (the backfill reports False and does nothing)
  • the real module keeps its identity and every other symbol it exports
  • it is idempotent

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:

REPRO: cannot import name '_MODEL_TO_CONVERSION_PATTERN' from 'transformers.conversion_mapping'
backfilled: True
symbol back: True
AFTER FIX: peft converter imports cleanly

tests/version_compat/test_peft_conversion_symbol_backfill.py adds 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.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 6, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 6, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 6342744002

ℹ️ 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".

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@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: 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".

Comment on lines +200 to +201
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef, ast.Import)):
continue

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 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
pre-commit-ci Bot and others added 14 commits August 8, 2026 03:23
…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.
@danielhanchen
danielhanchen force-pushed the fix/peft-conversion-symbol-backfill branch from 160f80b to d1eaf5b Compare August 8, 2026 03:24
@danielhanchen

Copy link
Copy Markdown
Member Author

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 _backfill_missing_peft_symbols, which walks _PEFT_REQUIRED_SYMBOLS and donates whatever a real submodule lacks. The symbol table is the same two submodules this PR names. The difference is what gets donated for _MODEL_TO_CONVERSION_PATTERN:

  • the general pass donates an inert {}, which answers every lookup with a silent None
  • this pass installs a MoE-aware stand-in, which refuses a fused-MoE lookup it cannot answer

Both are hasattr-gated, so whichever runs first decides what the symbol IS. If the inert donor wins, a fused MoE adapter loads with its LoRA targets unconverted and nothing reports it. So the rebase keeps both, this one first, and two tests pin that:

  • test_the_moe_aware_map_wins_over_the_inert_donor asserts the call order in the source
  • test_the_general_pass_does_not_overwrite_an_installed_stand_in asserts the general pass still gates on absence, so a second pass cannot undo the first

One thing I got wrong in the first resolution and am flagging rather than burying: those two tests reference inspect, and their import came from a hunk main had already rewritten, so they failed with NameError after the rebase. Fixed in the follow-up commit. tests/version_compat/test_peft_conversion_symbol_backfill.py is 65 passed.

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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 0008229c94

ℹ️ 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".

@danielhanchen
danielhanchen merged commit f48eeb2 into unslothai:main Aug 9, 2026
39 of 50 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant