Skip to content

Commit 33581b0

Browse files
committed
lazy import torch for lmcache cli
Signed-off-by: deng451e <838677410@qq.com>
1 parent 10fd636 commit 33581b0

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

lmcache/__init__.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
import importlib
66
import sys
77

8-
# Third Party
9-
import torch
10-
118
# First Party
129
from lmcache.logging import init_logger
1310

@@ -21,6 +18,9 @@ def _get_backend() -> Any:
2118
"""
2219
Try backends in order, first successful import wins.
2320
"""
21+
# Third Party
22+
import torch
23+
2424
backend_candidates = [
2525
(
2626
"lmcache.c_ops",
@@ -70,6 +70,8 @@ def _get_backend() -> Any:
7070
# --------------------------
7171
# Backend instance
7272
# --------------------------
73-
_ops = _get_backend()
74-
75-
sys.modules["lmcache.c_ops"] = _ops
73+
try:
74+
_ops = _get_backend()
75+
sys.modules["lmcache.c_ops"] = _ops
76+
except (ImportError, ModuleNotFoundError):
77+
pass # torch not installed; CLI-only usage does not need CUDA ops

0 commit comments

Comments
 (0)