fix(huggingface): resolve the config class for eval() explicitly (#2703) - #2704
Open
Anai-Guo wants to merge 1 commit into
Open
fix(huggingface): resolve the config class for eval() explicitly (#2703)#2704Anai-Guo wants to merge 1 commit into
Anai-Guo wants to merge 1 commit into
Conversation
`download_model` imports the config class with `exec()` and then builds it
with `eval()`:
exec(f"from transformers import {config_cls_name}")
config = eval(HUGGINGFACE_MODELS[model_name][2])
As of Python 3.13 (PEP 667), `locals()` in a function scope returns an
independent snapshot, so `exec()` writes the imported name into a snapshot
that the following `eval()` never sees. Every HuggingFace model in
HUGGINGFACE_MODELS then fails with
NameError: name 'AutoConfig' is not defined
On 3.12 and earlier both calls shared one locals dict, which is why this
went unnoticed.
Drop the `exec()` and evaluate the constructor against an explicit
namespace built with `getattr(transformers, ...)` -- the same lookup the
next line already uses for the model class. Verified by replaying the real
`download_model` over all 31 entries of HUGGINGFACE_MODELS: on 3.13 all 31
raise the NameError before this change and all 31 succeed after, and on
3.12 all 31 succeed both before and after.
Fixes pytorch#2703
Signed-off-by: Anai-Guo <antai12232931@outlook.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2703.
Problem
download_model()imports the config class withexec()and then builds it witheval():As of Python 3.13, PEP 667 makes
locals()in an optimized (function) scope return an independent snapshot, soexec()writes the imported name into a dict the followingeval()never reads. Every entry inHUGGINGFACE_MODELSthen fails:On 3.12 and earlier, both calls shared a single locals dict, so the name happened to be visible — which is why this has gone unnoticed.
Fix
Drop the
exec()and evaluate the constructor against an explicit namespace built withgetattr(transformers, ...)— the same lookup the very next line already uses for the model class:transformersis already imported at module level, and all seven config classes referenced by the table (AutoConfig,BertConfig,BigBirdConfig,LlamaConfig,PhiConfig,ReformerConfig,WhisperConfig) are top-leveltransformersexports. This also stops the import leaking into the caller's scope.Verification
The real
download_modelwas AST-extracted from this file and replayed over all 31 entries ofHUGGINGFACE_MODELSagainst a stubtransformersmodule, on both interpreters:NameError: name 'AutoConfig' is not definedSo the failure reproduces exactly as reported on 3.13, the fix clears it, and 3.12 behaviour is unchanged.
black --line-length 88reports the file unchanged.🤖 Generated with Claude Code