Repository navigation
Fix circular import when the architecture adapter factory is imported first - #1805
Merged
jlarson4 merged 2 commits intoSep 24, 2026
Merged
Conversation
… 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
approved these changes
Sep 24, 2026
koriyoshi2041
left a comment
Contributor
There was a problem hiding this comment.
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.
Collaborator
|
Looks great @YHC66, thanks for putting this together! |
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.
Description
Importing
transformer_lens.factories.architecture_adapter_factorybeforetransformer_lens.model_bridgehas been loaded fails with a circular import. This is the first line of the external adapter registration guide, and it also fails after a plainimport transformer_lens, since the top-level package loadsmodel_bridgelazily. Reproduced ondevand on the released 4.0.0 wheel:The cycle is
architecture_adapter_factory->model_bridge/__init__(viamodel_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 loadedmodel_bridge.The fix changes the five modules under
model_bridge/sourcesthat import the factory to import the module (from transformer_lens.factories import architecture_adapter_factory) and look upArchitectureAdapterFactory/SUPPORTED_ARCHITECTURESwhen they are called. All uses were already inside functions, so behavior is unchanged. I did not makefactories/__init__.pyimportmodel_bridgefirst, sinceimport transformer_lensimportsfactoriesand that would makemodel_bridgeload eagerly.Three test helpers patched
select_architecture_adapterthrough 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 ondevand passes with this change.tests/unitpasses locally (6559 passed, same warning count asdev), andblack,isort,pyclnandmypyare clean on the changed files.Type of change
Checklist: