Skip to content

fix: Add prefer_auto_map opt-in for registered-class vs auto_map conflicts - #45729

Open
sebastianvlad1 wants to merge 3 commits into
huggingface:mainfrom
sebastianvlad1:fix/local-checkpoint-precedence-45698
Open

sebastianvlad1 wants to merge 3 commits into
huggingface:mainfrom
sebastianvlad1:fix/local-checkpoint-precedence-45698

Conversation

@sebastianvlad1

@sebastianvlad1 sebastianvlad1 commented Apr 30, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

This PR keeps the default precedence introduced in #45094: explicitly registered local classes continue to override checkpoint auto_map code by default.

It adds an explicit prefer_auto_map=True opt-in to AutoConfig.from_pretrained() for cases where callers want the checkpoint's auto_map code to win instead. AutoModel.from_pretrained(..., prefer_auto_map=True) supports the same workflow when the config is auto-loaded, because the flag is forwarded into AutoConfig.from_pretrained().

The PR also preserves the local config-file-path fix from the earlier revision by normalizing file paths to their containing directory before calling get_class_from_dynamic_module().

Fixes #45698

Code Agent Policy

  • I confirm that this is not a pure code agent PR.

Before submitting

Validation

python -m pytest tests/models/auto/test_configuration_auto.py -k "dynamic_config_conflict or prefer_auto_map or saved_dynamic_config_conflict" -s -v
# passed

python -m pytest tests/models/auto/test_modeling_auto.py -k "dynamic_model_conflict or prefer_auto_map or saved_dynamic_model_conflict" -s -v
# passed

python utils/tests_fetcher.py
# ran

python utils/checkers.py ruff_check,ruff_format,init_isort,sort_auto_mappings --fix
# passed

Who can review?

@Cyrilvallez @Rocketknight1 @nurpax

@Qodo-Free-For-OSS

Copy link
Copy Markdown

Hi, AutoConfig.from_pretrained can now select dynamic loading for a local config file input, but it still passes the file path into get_class_from_dynamic_module, which only treats directories as local and will mis-handle the file path as a Hub repo id.

Severity: action required | Category: correctness

How to fix: Normalize file path to dir

Agent prompt to fix - you can give this to your LLM of choice:

Issue description

AutoConfig.from_pretrained can take a file path (e.g. .../config.json). With this PR’s new condition, the code can enter the dynamic-loading branch, but still passes the file path to get_class_from_dynamic_module(). That dynamic loader only recognizes directories as local (via os.path.isdir), so a file path is treated like a remote repo id and dynamic loading fails.

Issue Context

  • _local_checkpoint_contains_auto_map_module() already normalizes file paths to dirname, so the code can decide “local checkpoint contains module”.
  • The subsequent dynamic load call should use the same normalized directory.

Fix Focus Areas

  • src/transformers/models/auto/configuration_auto.py[91-109]
  • src/transformers/models/auto/configuration_auto.py[401-421]

Suggested change

When pretrained_model_name_or_path is a file, compute checkpoint_dir = os.path.dirname(os.fspath(pretrained_model_name_or_path)) and pass checkpoint_dir to get_class_from_dynamic_module() (and likely to config_class.from_pretrained() as well, for consistency with other local-dir loads).


Found by Qodo code review. FYI, Qodo is free for open-source.

@sebastianvlad1
sebastianvlad1 force-pushed the fix/local-checkpoint-precedence-45698 branch from 73824b3 to 412b2e3 Compare May 1, 2026 12:05
@sebastianvlad1

Copy link
Copy Markdown
Contributor Author

@Rocketknight1 would you mind taking a look when you get a chance?

@sebastianvlad1
sebastianvlad1 force-pushed the fix/local-checkpoint-precedence-45698 branch from 412b2e3 to d983c73 Compare May 29, 2026 11:12
@sebastianvlad1 sebastianvlad1 changed the title fix: AutoConfig reloads wrong class after save_pretrained + local register fix: Add prefer_auto_map opt-in for registered-class vs auto_map conflicts May 29, 2026
@github-actions

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: auto

@Rocketknight1

Copy link
Copy Markdown
Member

cc @hmellor since I haven't investigated this too deeply and you were commenting on the original issue, but if you want me to take it instead, just ping me!

@nurpax

nurpax commented Jun 2, 2026 •

Copy link
Copy Markdown
Contributor

Sorry for taking so long to review!

I read through the code but tbh, it would be easier for code review if code formatting was applied in a separate commit and the real beef of the change kept separate from that. Similarly, I would've dropped the commit that doesn't relate to prefer_auto_map fix, since that shouldn't be needed anymore, right?

Maybe HF has some policy on when is the right time to apply code formatting changes, maybe it's ok to keep them in the same commit.. but it increases the code review load a fair bit.

With these changes, how does the original repro from #45698 change behavior?

@sebastianvlad1

Copy link
Copy Markdown
Contributor Author

Thanks for the review @nurpax!

Yes, the intent here was to implement the direction suggested in #45698: expose the precedence explicitly with a per call prefer_auto_map option, while preserving the current default behavior from #45094.

So the behavior is:

This branch has not been deployed

No deployments
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.

from_pretrained loads wrong custom module after save_pretrained

4 participants