Skip to content

[Misc][Build] Lazy load cv2 in nemotron_parse.py#33189

Merged
mgoin merged 2 commits intovllm-project:mainfrom
kiersten-stokes:lazy-load-cv2
Jan 29, 2026
Merged

[Misc][Build] Lazy load cv2 in nemotron_parse.py#33189
mgoin merged 2 commits intovllm-project:mainfrom
kiersten-stokes:lazy-load-cv2

Conversation

@kiersten-stokes
Copy link
Copy Markdown
Contributor

@kiersten-stokes kiersten-stokes commented Jan 27, 2026

Purpose

Lazy loads the cv2 module to avoid import errors in certain envs when not needed

Error trace
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3.12/multiprocessing/spawn.py", line 122, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "/usr/lib/python3.12/multiprocessing/spawn.py", line 131, in _main
    prepare(preparation_data)
  File "/usr/lib/python3.12/multiprocessing/spawn.py", line 246, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "/usr/lib/python3.12/multiprocessing/spawn.py", line 297, in _fixup_main_from_path
    main_content = runpy.run_path(main_path,
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen runpy>", line 280, in run_path
  File "<frozen runpy>", line 92, in _run_module_code
  File "<frozen runpy>", line 86, in _run_code
  File "/opt/venv/bin/vllm", line 5, in <module>
    from vllm.scripts import main
  File "/opt/venv/lib/python3.12/site-packages/vllm/__init__.py", line 3, in <module>
    from vllm.engine.arg_utils import AsyncEngineArgs, EngineArgs
  File "/opt/venv/lib/python3.12/site-packages/vllm/engine/__init__.py", line 1, in <module>
    from vllm.engine.async_llm_engine import AsyncLLMEngine
  File "/opt/venv/lib/python3.12/site-packages/vllm/engine/async_llm_engine.py", line 11, in <module>
    from vllm.executor.executor_base import ExecutorBase
  File "/opt/venv/lib/python3.12/site-packages/vllm/executor/__init__.py", line 1, in <module>
    from vllm.executor.executor_base import ExecutorBase, ExecutorAsyncBase
  File "/opt/venv/lib/python3.12/site-packages/vllm/executor/executor_base.py", line 7, in <module>
    from vllm.lora.request import LoRARequest
  File "/opt/venv/lib/python3.12/site-packages/vllm/lora/__init__.py", line 1, in <module>
    from vllm.lora.layers import (
  File "/opt/venv/lib/python3.12/site-packages/vllm/lora/layers.py", line 14, in <module>
    from vllm.model_executor.layers.linear import (
  File "/opt/venv/lib/python3.12/site-packages/vllm/model_executor/__init__.py", line 1, in <module>
    from vllm.model_executor.models import ModelRegistry
  File "/opt/venv/lib/python3.12/site-packages/vllm/model_executor/models/__init__.py", line 9, in <module>
    _MODELS = {
              ^
  File "/opt/venv/lib/python3.12/site-packages/vllm/model_executor/models/__init__.py", line 127, in <dictcomp>
    model: _get_model_architecture(model) for model in _MODELS
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.12/site-packages/vllm/model_executor/models/__init__.py", line 127, in <dictcomp>
    model: _get_model_architecture(model) for model in _MODELS
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.12/site-packages/vllm/model_executor/models/__init__.py", line 35, in _get_model_architecture
    mod = importlib.import_module(
          ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/opt/venv/lib/python3.12/site-packages/vllm/model_executor/models/nemotron_parse.py", line 14, in <module>
    import cv2
  File "/opt/venv/lib/python3.12/site-packages/cv2/__init__.py", line 181, in <module>
    bootstrap()
  File "/opt/venv/lib/python3.12/site-packages/cv2/__init__.py", line 153, in bootstrap
    native_module = importlib.import_module("cv2")
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.12/site-packages/cv2/typing/__init__.py", line 62, in <module>
    import cv2.mat_wrapper
ImportError: libxcb.so.1: cannot open shared object file: No such file or directory

Test Plan

Build locally in problem env and test for above error

Test Result

Local build succeeded as expected without above error

Essential Elements of an Effective PR Description Checklist
  • The purpose of the PR, such as "Fix some issue (link existing issues this PR will resolve)".
  • The test plan, such as providing test command.
  • The test results, such as pasting the results comparison before and after, or e2e results
  • (Optional) The necessary documentation update, such as updating supported_models.md and examples for a new model.
  • (Optional) Release notes update. If your change is user facing, please update the release notes draft in the Google Doc.

Signed-off-by: kiersten-stokes <kierstenstokes@gmail.com>
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

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 aims to lazy-load the cv2 module in nemotron_parse.py to prevent import errors in environments where opencv-python is not installed but the model is used for text-only tasks. The change correctly moves the import cv2 statement from the top of the file into the methods that use it.

My review identifies an opportunity to make the lazy loading even more effective. Currently, cv2 is imported during the initialization of NemotronParseImageProcessor, which may still be too early. I've suggested a change to defer the import until an image is actually processed, fully aligning with the PR's goal.

@kiersten-stokes kiersten-stokes changed the title Lazy load cv2 in nemotron_parse.py [Misc][Build] Lazy load cv2 in nemotron_parse.py Jan 27, 2026
@mergify
Copy link
Copy Markdown

mergify bot commented Jan 27, 2026

Hi @kiersten-stokes, the pre-commit checks have failed. Please run:

uv pip install pre-commit
pre-commit install
pre-commit run --all-files

Then, commit the changes and push to your branch.

For future commits, pre-commit will run automatically on changed files before each commit.

Tip

Is mypy or markdownlint failing?
mypy and markdownlint are run differently in CI. If the failure is related to either of these checks, please use the following commands to run them locally:
# For mypy (substitute "3.10" with the failing version if needed)
pre-commit run --hook-stage manual mypy-3.10
# For markdownlint
pre-commit run --hook-stage manual markdownlint

Signed-off-by: kiersten-stokes <kierstenstokes@gmail.com>
Copy link
Copy Markdown
Member

@mgoin mgoin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follows the pattern in the result of the codebase, thanks!

@mgoin mgoin added bug Something isn't working ready ONLY add when PR is ready to merge/full CI is needed labels Jan 27, 2026
@mgoin mgoin enabled auto-merge (squash) January 27, 2026 22:13
@mgoin mgoin merged commit 9e138cb into vllm-project:main Jan 29, 2026
59 of 60 checks passed
apd10 pushed a commit to apd10/vllm that referenced this pull request Jan 31, 2026
Signed-off-by: kiersten-stokes <kierstenstokes@gmail.com>
@mgoin mgoin added this to the v0.15.1 Hotfix milestone Feb 2, 2026
khluu pushed a commit that referenced this pull request Feb 3, 2026
Signed-off-by: kiersten-stokes <kierstenstokes@gmail.com>
(cherry picked from commit 9e138cb)
PiratePai pushed a commit to PiratePai/epd_shm that referenced this pull request Feb 3, 2026
Signed-off-by: kiersten-stokes <kierstenstokes@gmail.com>
Signed-off-by: PiratePai <416932041@qq.com>
Signed-off-by: Pai <416932041@qq.com>
ItzDEXX pushed a commit to ItzDEXX/vllm that referenced this pull request Feb 19, 2026
Signed-off-by: kiersten-stokes <kierstenstokes@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ready ONLY add when PR is ready to merge/full CI is needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants