Skip to content

[CLI][fix] lazy torch import in __init__.py to unblock CLI-only installs#3086

Merged
ApostaC merged 4 commits intoLMCache:devfrom
deng451e:fix_cli_wheel
Apr 21, 2026
Merged

[CLI][fix] lazy torch import in __init__.py to unblock CLI-only installs#3086
ApostaC merged 4 commits intoLMCache:devfrom
deng451e:fix_cli_wheel

Conversation

@deng451e
Copy link
Copy Markdown
Collaborator

@deng451e deng451e commented Apr 20, 2026

lmcache/init.py imported torch at the top level, causing any import of a lmcache subpackage — including lmcache.cli — to
unconditionally require torch. This broke the lightweight lmcache-cli wheel, which ships without torch.

Changes

  • Move import torch inside _get_backend() so it only loads when a CUDA backend is being selected.
  • Wrap the _get_backend() call in try/except ImportError so CLI-only environments (no torch/numba) import cleanly; the compute
    backend is simply skipped.
  • Make the cache-simulator subcommand optional: skip registration if its extra dependencies (sortedcontainers, matplotlib) aren't
    installed.
  • Add a CI smoke test that installs the CLI wheel without torch and verifies import lmcache and lmcache --help succeed.

Signed-off-by: deng451e <838677410@qq.com>
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 33581b0. Configure here.

Comment thread lmcache/__init__.py Outdated
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 modifies lmcache/init.py to defer the torch import and handle potential ImportError or ModuleNotFoundError during backend initialization. This change allows the package to be used in environments without torch installed, such as for CLI-only operations. I have no feedback to provide.

Signed-off-by: deng451e <838677410@qq.com>
Signed-off-by: deng451e <838677410@qq.com>
Signed-off-by: deng451e <838677410@qq.com>
@deng451e deng451e requested a review from hickeyma as a code owner April 20, 2026 21:22
Copy link
Copy Markdown
Contributor

@ApostaC ApostaC left a comment

Choose a reason for hiding this comment

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

LGTM!

@ApostaC ApostaC enabled auto-merge (squash) April 20, 2026 22:42
@github-actions github-actions Bot added the full Run comprehensive tests on this PR label Apr 20, 2026
Copy link
Copy Markdown
Contributor

@sammshen sammshen left a comment

Choose a reason for hiding this comment

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

LGTM!

@ApostaC ApostaC merged commit 18f98ea into LMCache:dev Apr 21, 2026
67 of 76 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

full Run comprehensive tests on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants