Skip to content

fix(huggingface): resolve the config class for eval() explicitly (#2703) - #2704

Open
Anai-Guo wants to merge 1 commit into
pytorch:mainfrom
Anai-Guo:fix-hf-config-eval-namespace
Open

fix(huggingface): resolve the config class for eval() explicitly (#2703)#2704
Anai-Guo wants to merge 1 commit into
pytorch:mainfrom
Anai-Guo:fix-hf-config-eval-namespace

Conversation

@Anai-Guo

@Anai-Guo Anai-Guo commented Sep 3, 2026

Copy link
Copy Markdown

Fixes #2703.

Problem

download_model() imports the config class with exec() and then builds it with eval():

# torchbenchmark/util/framework/huggingface/basic_configs.py:302-304
config_cls_name = _extract_config_cls_name(HUGGINGFACE_MODELS[model_name][2])
exec(f"from transformers import {config_cls_name}")
config = eval(HUGGINGFACE_MODELS[model_name][2])

As of Python 3.13, PEP 667 makes locals() in an optimized (function) scope return an independent snapshot, so exec() writes the imported name into a dict the following eval() never reads. Every entry in HUGGINGFACE_MODELS then fails:

NameError: name 'AutoConfig' is not defined

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 with getattr(transformers, ...) — the same lookup the very next line already uses for the model class:

config_ns = {config_cls_name: getattr(transformers, config_cls_name)}
config = eval(HUGGINGFACE_MODELS[model_name][2], config_ns)

transformers is already imported at module level, and all seven config classes referenced by the table (AutoConfig, BertConfig, BigBirdConfig, LlamaConfig, PhiConfig, ReformerConfig, WhisperConfig) are top-level transformers exports. This also stops the import leaking into the caller's scope.

Verification

The real download_model was AST-extracted from this file and replayed over all 31 entries of HUGGINGFACE_MODELS against a stub transformers module, on both interpreters:

Python 3.13.13 Python 3.12.10
before 0 / 31 ok — all NameError: name 'AutoConfig' is not defined 31 / 31 ok
after 31 / 31 ok 31 / 31 ok

So the failure reproduces exactly as reported on 3.13, the fix clears it, and 3.12 behaviour is unchanged.

black --line-length 88 reports the file unchanged.

🤖 Generated with Claude Code

`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>
@meta-cla meta-cla Bot added the cla signed label Sep 3, 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.

[huggingface] NameError: name 'AutoConfig' is not defined in download_model()

1 participant