fix(image/ocr): import torchvision lazily in deepseek_ocr (#5189) - #5191
Merged
Conversation
…5189) `xinference/model/__init__._install()` imports every model family at actor-creation time, so `model/image/ocr/__init__.py` -> `deepseek_ocr.py` pulled `from torchvision import transforms` into *every* model subprocess, including audio/LLM models that never touch torchvision. Every other torchvision user in the tree already imports it inside the function that needs it (`transformers/multimodal/intern_vl.py`, `multimodal/ovis2.py`, `video/diffusers.py`); `deepseek_ocr.py` was the only module-level import left. Move it into the two scopes that actually use it (`normalize_transform` and `BasicImageTransform.__init__`) to match that convention. This also removes one eager host-torchvision import from subprocesses whose virtualenv installs its own torchvision, which is a contributing factor in the version-collision reported in xorbitsai#5189.
Contributor
There was a problem hiding this comment.
Code Review
This pull request refactors the imports in deepseek_ocr.py by moving torchvision to local scopes for lazy loading. The reviewer suggested a minor optimization to place the lazy import after the initial None checks in normalize_transform to prevent unnecessary import overhead and potential ImportErrors when the library is not installed.
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.
Problem
xinference/model/__init__._install()imports every model family at actor-creationtime. That chain reaches
model/image/__init__.py→.ocr→ocr/__init__.py→deepseek_ocr.py, which does a module-levelfrom torchvision import transforms.Net effect: torchvision is imported into every model subprocess — audio models,
LLMs, anything — regardless of whether the model has anything to do with OCR.
Why this is the odd one out
Every other torchvision consumer in the tree already imports it lazily, inside the
function that needs it:
model/llm/transformers/multimodal/intern_vl.pymodel/llm/transformers/multimodal/ovis2.pymodel/video/diffusers.pymodel/image/ocr/deepseek_ocr.pySo this looks like a plain oversight rather than a deliberate choice.
Change
Move the import into the only two scopes that use
transforms:normalize_transform()BasicImageTransform.__init__()That is the whole diff — 5 lines, no behavior change for OCR users (the import
just happens on first use instead of at module import).
Relation to #5189
This is the "additionally" item called out at the end of #5189, filed as its own
minimal change. It does not claim to fix the root cause of #5189 (the
_create_subpool/_prepare_virtual_envordering — that reorder touches thelaunch flow and deserves a separate, maintainer-directed discussion).
What it does do is remove one eager host torchvision import from subprocesses
whose virtualenv installs its own pinned torchvision, which the issue identifies as
what aggravates the
operator torchvision::nms does not existfailure variant foraudio models that never use torchvision at all.
🤖 Generated with Claude Code