fix(python): import converters and specs lazily so inference does not load torch (memory reduction) #2080
All Vouch review confidence checks passed
Residual risk 0.20 = Change Risk (0.45) × (1 − Review Confidence (0.57)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.45 · 435 lines diff across 5 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.364
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #2080"
general
weight: 0.35
jordimas
Fixes #2078 (#2079 is a duplicate of the same report).
Problem
import ctranslate2unconditionally imports torch, transformers and huggingface_hub when they are present in the environment, even for inference-only use (Translator/Generator/Whisper).python/ctranslate2/__init__.pyends with:converters/__init__.pyimportsconverters.transformers, which doestry: import huggingface_hub, torch, transformers.specs/model_spec.pydoestry: import torchto settorch_is_available.Both are only needed for model conversion, never for inference — so every inference-only user with torch installed pays the import cost. As the reporter notes, this also forces users who mix CTranslate2 inference with onnxruntime in one process to shell out to a subprocess just to keep torch out of it.
Change
Import
convertersandspecson first attribute access using a module-level__getattr__(PEP 562), plus a matching__dir__.modelsstays eager — it only imports from the compiled_ext, so it costs nothing.Compatibility
All existing access patterns still work, verified against an installed 4.7.1 with the patch applied:
| form | result |
|---|---|
|
import ctranslate2; ctranslate2.converters.TransformersConverter| ok (triggers lazy import) ||
import ctranslate2; ctranslate2.specs.TransformerSpec| ok ||
from ctranslate2 import converters| ok ||
from ctranslate2 import specs| ok ||
import ctranslate2.converters| ok ||
from ctranslate2.converters import TransformersConverter| ok ||
from ctranslate2.specs import TransformerSpec| ok ||
from ctranslate2.specs import model_spec→torch_is_available|True||
'converters' in dir(ctranslate2)/'specs' in dir(...)|True||
ctranslate2.does_not_exist| raisesAttributeError|All 6
ct2-*-converterconsole_scripts entry points still resolve (ctranslate2.converters.{transformers,opennmt_py,fairseq,marian,opus_mt,openai_gpt2}:main— each imports and exposes a callablemain).Measurements
Windows, Python 3.12.10, torch 2.5.1+cu121, ctranslate2 4.7.1:
| |
import ctranslate2(subprocess, min of 3) | package subtree (-X importtime) ||---|---|---|
| before | 3.04 s | 2.73 s |
| after | 0.15 s | 0.11 s |
Test
Added
test_import_does_not_load_conversion_dependenciestopython/tests/test_misc.py, parametrized overtorchandtransformers. It runsimport ctranslate2in a subprocess (the test session itself imports both, so an in-process check would be meaningless).Against the installed package:
black,isort(profile=black, lines_between_types=1) andflake8(max-line-length=100) are clean on both changed files.---
AI disclosure, per CONTRIBUTING.md: this change was written with AI assistance (Claude Code). The reported import chain was verified by reading the actual source on
master, and every claim above — the compatibility matrix, entry points, timings, and before/after test results — was produced by running it locally against an installed CTranslate2, not inferred.🤖 Generated with Claude Code