Skip to content

Fix circular import when the architecture adapter factory is imported first - #1805

Merged
jlarson4 merged 2 commits into
TransformerLensOrg:devfrom
YHC66:fix-factory-circular-import
Sep 24, 2026
Merged

jlarson4 merged 2 commits into
TransformerLensOrg:devfrom
YHC66:fix-factory-circular-import

Conversation

@YHC66

@YHC66 YHC66 commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

Description

Importing transformer_lens.factories.architecture_adapter_factory before transformer_lens.model_bridge has been loaded fails with a circular import. This is the first line of the external adapter registration guide, and it also fails after a plain import transformer_lens, since the top-level package loads model_bridge lazily. Reproduced on dev and on the released 4.0.0 wheel:

$ python -c "from transformer_lens.factories.architecture_adapter_factory import ArchitectureAdapterFactory"
ImportError: cannot import name 'ArchitectureAdapterFactory' from partially initialized module
'transformer_lens.factories.architecture_adapter_factory' (most likely due to a circular import)

The cycle is architecture_adapter_factory -> model_bridge/__init__ (via model_bridge.architecture_adapter) -> model_bridge.sources.transformers -> sources/__init__ -> sources._bridge_builder -> from ...architecture_adapter_factory import ArchitectureAdapterFactory, while the factory module is still only partially initialized. It also means test files that import the factory before anything else fail to collect when run on their own (e.g. pytest tests/unit/test_verify_models_phase_defaults.py); in a full run they pass only because an earlier module already loaded model_bridge.

The fix changes the five modules under model_bridge/sources that import the factory to import the module (from transformer_lens.factories import architecture_adapter_factory) and look up ArchitectureAdapterFactory / SUPPORTED_ARCHITECTURES when they are called. All uses were already inside functions, so behavior is unchanged. I did not make factories/__init__.py import model_bridge first, since import transformer_lens imports factories and that would make model_bridge load eagerly.

Three test helpers patched select_architecture_adapter through the old module-level name (<source module>.ArchitectureAdapterFactory.select_architecture_adapter); I only updated those patch paths to go through the module attribute.

The regression test runs the import in a subprocess, because the test session has already imported model_bridge. It fails on dev and passes with this change. tests/unit passes locally (6559 passed, same warning count as dev), and black, isort, pycln and mypy are clean on the changed files.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have not rewritten tests relating to key interfaces which would affect backward compatibility

… first

Importing transformer_lens.factories.architecture_adapter_factory before
transformer_lens.model_bridge fails: the factory imports model_bridge,
whose __init__ imports model_bridge.sources, which imports names from the
still partially initialized factory module.

Import the factory module in model_bridge.sources and look the names up
at call time instead.

@koriyoshi2041 koriyoshi2041 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The follow-up now patches the exact module attribute used by production code, so the deferred import remains intact while the dtype-forwarding tests exercise the intended seam. I reviewed exact head f9a1c26; the previously failing compatibility jobs are green across Python 3.10, 3.11, and 3.12, including the full coverage run.

@jlarson4

Copy link
Copy Markdown
Collaborator

Looks great @YHC66, thanks for putting this together!

@jlarson4
jlarson4 merged commit 4636804 into TransformerLensOrg:dev Sep 24, 2026
26 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.

3 participants