transformers - ✅(Solved) Fix Lazy loading is not working properly [3 pull requests, 2 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
huggingface/transformers#44273Fetched 2026-04-08 00:29:25
View on GitHub
Comments
2
Participants
2
Timeline
10
Reactions
0
Timeline (top)
cross-referenced ×3commented ×2mentioned ×2subscribed ×2

Fix Action

Fixed

PR fix notes

PR #44275: [Fix] Restore lazy loading to improve import performance (#44273)

Description (problem / solution / changelog)

What does this PR do?

This PR addresses the performance regression where import transformers takes ~3.5s. The issue was caused by eager imports of heavy backend libraries (like torch/numpy) during the initial module load.

By moving these imports into functional scopes, the import time is restored to the expected <0.5s.

Fixes #44273

Summary of Changes

  • Refactored top-level imports in the affected modules to use lazy loading.
  • Verified that import transformers execution time is reduced.
  • Ensured TYPE_CHECKING blocks remain intact for IDE support.

Before submitting

  • This PR fixes a performance issue.
  • I have read the contributor guidelines.
  • I have verified the fix locally.

Who can review?

@CyrilVallez @ArthurZucker

Changed files


PR #5208: Refactor CLI [7/N]: Move patching to compat and import transformers conditionally

Description (problem / solution / changelog)

Move patching to _compat and import transformers conditionally:

Note that currently, importing transformers unconditionally, increases latency even if the patch is not necessary. See upstream issue:

This PR refactors the way compatibility with older versions of the transformers and accelerate libraries is handled, specifically regarding the ParallelismConfig symbol. The patch moves the compatibility workaround for ParallelismConfig from trl/scripts/utils.py to a more appropriate location in trl/_compat.py, and improves its implementation by adding version checks and error handling.

Transformers/Accelerate Compatibility Improvements:

  • Added a new function _patch_transformers_parallelism_config in trl/_compat.py to ensure transformers.training_args.ParallelismConfig is defined only when both transformers<4.57.0 and accelerate<1.10.1 are installed, preventing NameError during type hint resolution. The function includes error handling and a detailed docstring explaining its purpose and upstream context.
  • Removed the previous unconditional workaround function _ensure_transformers_parallelism_config from trl/scripts/utils.py, as this logic is now handled (with improvements) in trl/_compat.py.

Changed files

  • trl/_compat.py (modified, +30/-0)
  • trl/scripts/utils.py (modified, +0/-20)

PR #5210: Refactor CLI [9/N]: Replace HfArgumentParser from transformers with local

Description (problem / solution / changelog)

Replace HfArgumentParser from transformers with local.

This PR updates the import statements in trl/scripts/utils.py to use local versions of argument parsing utilities instead of those from the external transformers library. This change helps improving latency within the codebase.

The latency of import trl.scripts.utils changes from 7.5s to <0.5s after the merge of:

  • #5208
  • #5209
  • #5210

See upstream issue:

Dependency management:

  • Replaced imports of HfArgumentParser, DataClass, and DataClassType from the transformers library with imports from the local trl.scripts._hf_argparser module.
<!-- CURSOR_SUMMARY -->

[!NOTE] Medium Risk Although intended as a drop-in copy, vendoring core CLI argument parsing code can subtly change parsing behavior and may drift from upstream fixes, impacting script configuration/flag handling.

Overview Introduces a vendored copy of transformershf_argparser as trl/scripts/_hf_argparser.py, with yaml imported lazily in parse_yaml_file to avoid the upstream import-time latency.

Updates trl/scripts/utils.py to import HfArgumentParser/DataClass/DataClassType from the local module instead of transformers, keeping CLI parsing behavior local and reducing startup overhead.

<sup>Written by Cursor Bugbot for commit 2e15b40ff9ca0ac6fce4f2c41304c0466bbcebc1. This will update automatically on new commits. Configure here.</sup>

<!-- /CURSOR_SUMMARY -->

Changed files

  • trl/scripts/_hf_argparser.py (added, +432/-0)
  • trl/scripts/utils.py (modified, +4/-1)

Code Example

time python -c "import transformers"

---

real	0m3.669s
user	0m4.789s
sys	0m0.311s
RAW_BUFFERClick to expand / collapse

Problem

Lazy loading is not working properly:

  • importing transformers takes ~3.5s

Who can help?

No response

Information

  • The official example scripts
  • My own modified scripts

Tasks

  • An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
  • My own task or dataset (give details below)

Reproduction

time python -c "import transformers"
real	0m3.669s
user	0m4.789s
sys	0m0.311s

Expected behavior

It should take less <0.5s

extent analysis

Problem Summary

Lazy loading issue with transformers import taking ~3.5s

Root Cause Analysis

The root cause is likely due to the way the transformers library is being imported, which is causing a significant delay.

Fix Plan

1. Update transformers library to the latest version

pip install --upgrade transformers

2. Use lazy loading with importlib.util

import importlib.util

def lazy_import(module_name):
    spec = importlib.util.find_spec(module_name)
    if spec is None:
        raise ImportError(f"Module {module_name} not found")
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    return module

import transformers = lazy_import("transformers")

3. Use a caching mechanism (optional)

You can use a caching library like functools.lru_cache to cache the import result.

import functools

@functools.lru_cache(maxsize=None)
def lazy_import(module_name):
    # import logic here
    return module

Verification

Run the time python -c "import transformers" command again to verify the fix.

Extra Tips

  • Make sure to clean up any cached results when updating the transformers library.
  • Consider using a more robust lazy loading mechanism like importlib.machinery.SourceFileLoader.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

It should take less <0.5s

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

transformers - ✅(Solved) Fix Lazy loading is not working properly [3 pull requests, 2 comments, 2 participants]