Skip to content

fix(image/ocr): import torchvision lazily in deepseek_ocr (#5189) - #5191

Merged
qinxuye merged 1 commit into
xorbitsai:mainfrom
Anai-Guo:fix-5189-lazy-torchvision
Jul 17, 2026
Merged

fix(image/ocr): import torchvision lazily in deepseek_ocr (#5189)#5191
qinxuye merged 1 commit into
xorbitsai:mainfrom
Anai-Guo:fix-5189-lazy-torchvision

Conversation

@Anai-Guo

Copy link
Copy Markdown
Contributor

Problem

xinference/model/__init__._install() imports every model family at actor-creation
time. That chain reaches model/image/__init__.py.ocrocr/__init__.py
deepseek_ocr.py, which does a module-level from 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:

file import site
model/llm/transformers/multimodal/intern_vl.py inside method (line ~113)
model/llm/transformers/multimodal/ovis2.py inside method (line ~99)
model/video/diffusers.py inside method (line ~393)
model/image/ocr/deepseek_ocr.py module level ← this PR

So 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_env ordering — that reorder touches the
launch 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 exist failure variant for
audio models that never use torchvision at all.

🤖 Generated with Claude Code

…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.
@XprobeBot XprobeBot added the bug Something isn't working label Jul 17, 2026
@XprobeBot XprobeBot added this to the v2.x milestone Jul 17, 2026

@gemini-code-assist gemini-code-assist Bot 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.

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.

Comment thread xinference/model/image/ocr/deepseek_ocr.py

@qinxuye qinxuye 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.

LGTM

@qinxuye
qinxuye merged commit 2eb1dec into xorbitsai:main Jul 17, 2026
12 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants