Skip to content

Fix KeyError in pipelines.from_pretrained when it is the first touch of the module - #174

Closed
xocialize wants to merge 1 commit into
microsoft:mainfrom
xocialize:fix/from-pretrained-lazy-import
Closed

Fix KeyError in pipelines.from_pretrained when it is the first touch of the module#174
xocialize wants to merge 1 commit into
microsoft:mainfrom
xocialize:fix/from-pretrained-lazy-import

Conversation

@xocialize

Copy link
Copy Markdown

Repro

from trellis2 import pipelines
pipelines.from_pretrained("microsoft/TRELLIS.2-image-to-3D-4B")
# KeyError: 'Trellis2ImageTo3DPipeline'

Fails whenever from_pretrained is the first touch of the module. trellis2/pipelines/__init__.py uses a lazy __getattr__ that populates globals() on first attribute access, but from_pretrained looks the class up with globals()[config['name']] — which bypasses __getattr__ entirely. It only works if something else (e.g. an earlier pipelines.Trellis2ImageTo3DPipeline reference) already triggered the lazy import.

Fix

Route the lookup through the module's own lazy loader:

return getattr(sys.modules[__name__], config['name']).from_pretrained(path)

getattr on the module object hits __getattr__, triggering the intended lazy import. No behavior change when the class is already loaded.

🤖 Generated with Claude Code

…tattr__

globals()[config['name']] bypasses the module-level lazy loader, so it
raises KeyError: 'Trellis2ImageTo3DPipeline' whenever from_pretrained is
the first touch of the module (nothing has populated globals() yet).
getattr(sys.modules[__name__], ...) hits __getattr__ and triggers the
lazy import as intended.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Igor Shaposhnikov (Jourloy) added a commit to Jourloy/TRELLIS.2 that referenced this pull request Jul 31, 2026
Apply the fixes reported in upstream PRs microsoft#173 and microsoft#174 while preserving the Apple Silicon backend adaptations.
@xocialize

Copy link
Copy Markdown
Author

Closing in favor of #175, which has folded this fix in (from_pretrained now routes the class lookup through getattr(sys.modules[__name__], config['name']) instead of globals()[...], so the lazy __getattr__ fires on first module touch). No reason to carry a duplicate that would only conflict at merge — tracking it there instead. Thanks!

@xocialize xocialize closed this Aug 5, 2026
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.

2 participants