Dellboy commited on
Commit
312640e
·
verified ·
1 Parent(s): 89b4e16

Real fix: preload nvidia-*-cu12 pip packages' shared libraries via ctypes before llama_cpp import (libcudart.so.12 not found on ZeroGPU worker, confirmed via real runtime logs)

Browse files
Files changed (2) hide show
  1. app.py +32 -0
  2. requirements.txt +2 -0
app.py CHANGED
@@ -61,7 +61,39 @@ def _generate_tokens(
61
  across the function boundary, so tokens are collected here rather than streamed token-by-token
62
  -- the client sees compute-then-deliver rather than true live latency, a real trade-off of the
63
  ZeroGPU model.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  from llama_cpp import Llama
66
 
67
  llm = Llama(
 
61
  across the function boundary, so tokens are collected here rather than streamed token-by-token
62
  -- the client sees compute-then-deliver rather than true live latency, a real trade-off of the
63
  ZeroGPU model.
64
+
65
+ Real fix (found + fixed 2026-07-23): the CUDA-built llama-cpp-python wheel failed to load on
66
+ the real ZeroGPU worker with `OSError: libcudart.so.12: cannot open shared object file` --
67
+ confirmed via the Space's actual runtime logs (fetch_space_logs), not guessed. The
68
+ `nvidia-cuda-runtime-cu12`/`nvidia-cublas-cu12` pip packages bundle the needed .so files (real
69
+ fix pattern confirmed via github.com/abetlen/llama-cpp-python#1460), but llama-cpp-python loads
70
+ its library via a raw `ctypes.CDLL()` call that only searches the system's standard library
71
+ path / LD_LIBRARY_PATH -- it doesn't know to look inside a pip package's install directory
72
+ (unlike PyTorch's own wheels, which have special-cased loader logic for exactly this). The real
73
+ subpath is `<package>.__path__[0]/lib/*.so*` (a `lib/` subdirectory, not a Python submodule).
74
+ Pre-loading every nvidia-*-cu12 package's .so files explicitly via ctypes here, before
75
+ llama_cpp's own import triggers its CDLL() call, makes it resolve to the already-loaded
76
+ libraries instead of searching (and failing) on its own -- done generically across every
77
+ installed `nvidia.*` package rather than hardcoding exact library names, since llama.cpp's
78
+ CUDA backend may need more than just cudart+cublas depending on the exact build.
79
  """
80
+ import ctypes
81
+ import glob
82
+ import importlib
83
+ import pkgutil
84
+
85
+ try:
86
+ import nvidia
87
+ for _finder, _pkg_name, _ in pkgutil.iter_modules(nvidia.__path__):
88
+ try:
89
+ _pkg = importlib.import_module(f"nvidia.{_pkg_name}")
90
+ for _so in glob.glob(f"{_pkg.__path__[0]}/lib/*.so*"):
91
+ ctypes.CDLL(_so, mode=ctypes.RTLD_GLOBAL)
92
+ except Exception:
93
+ continue
94
+ except Exception:
95
+ pass # fall through -- if this fails, llama_cpp's own import error will surface as before
96
+
97
  from llama_cpp import Llama
98
 
99
  llm = Llama(
requirements.txt CHANGED
@@ -1,5 +1,7 @@
1
  --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cu121
2
  llama-cpp-python
 
 
3
  gradio>=4.0
4
  spaces
5
  huggingface_hub
 
1
  --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cu121
2
  llama-cpp-python
3
+ nvidia-cuda-runtime-cu12
4
+ nvidia-cublas-cu12
5
  gradio>=4.0
6
  spaces
7
  huggingface_hub