Spaces:
Runtime error
Runtime error
Márk Vince Varga
commited on
SDAFOASDFSAKFSDA
Browse files
app.py
CHANGED
|
@@ -11,6 +11,7 @@ def ensure_cuda_runtime():
|
|
| 11 |
and restarts the script with the correct LD_LIBRARY_PATH.
|
| 12 |
"""
|
| 13 |
missing_libs = []
|
|
|
|
| 14 |
|
| 15 |
# 1. Check for libcudart (CUDA Runtime)
|
| 16 |
try:
|
|
@@ -26,17 +27,47 @@ def ensure_cuda_runtime():
|
|
| 26 |
print("libcublas.so.12 not found.")
|
| 27 |
missing_libs.append("nvidia-cublas-cu12")
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# 4. Find library paths
|
| 38 |
import site
|
| 39 |
-
lib_paths = []
|
| 40 |
for sp in site.getsitepackages():
|
| 41 |
# Runtime libs
|
| 42 |
rt_path = os.path.join(sp, "nvidia", "cuda_runtime", "lib")
|
|
@@ -48,7 +79,7 @@ def ensure_cuda_runtime():
|
|
| 48 |
if os.path.isdir(cublas_path):
|
| 49 |
lib_paths.append(cublas_path)
|
| 50 |
|
| 51 |
-
if not lib_paths:
|
| 52 |
print("Warning: Could not find nvidia lib paths after installation.")
|
| 53 |
return
|
| 54 |
|
|
@@ -68,8 +99,12 @@ def ensure_cuda_runtime():
|
|
| 68 |
new_ld = f"{new_ld}{os.pathsep}{current_ld}"
|
| 69 |
|
| 70 |
os.environ["LD_LIBRARY_PATH"] = new_ld
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
# Re-execute the current script with the new environment
|
| 72 |
-
os.
|
| 73 |
|
| 74 |
ensure_cuda_runtime()
|
| 75 |
# --- CUDA DEPENDENCY FIX END ---
|
|
@@ -84,14 +119,24 @@ try:
|
|
| 84 |
print("llama-cpp-python is correctly installed.")
|
| 85 |
except (ImportError, RuntimeError, OSError) as e:
|
| 86 |
print(f"llama-cpp-python needs installation or repair: {e}")
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
print("Installation complete.")
|
| 96 |
|
| 97 |
# --- IMPORTS AFTER INSTALL ---
|
|
|
|
| 11 |
and restarts the script with the correct LD_LIBRARY_PATH.
|
| 12 |
"""
|
| 13 |
missing_libs = []
|
| 14 |
+
lib_paths = []
|
| 15 |
|
| 16 |
# 1. Check for libcudart (CUDA Runtime)
|
| 17 |
try:
|
|
|
|
| 27 |
print("libcublas.so.12 not found.")
|
| 28 |
missing_libs.append("nvidia-cublas-cu12")
|
| 29 |
|
| 30 |
+
# 3. Check for libcuda.so.1 (NVIDIA Driver)
|
| 31 |
+
# This CANNOT be installed via pip. It must be mounted from the host.
|
| 32 |
+
# If missing, we must fall back to CPU mode.
|
| 33 |
+
driver_found = False
|
| 34 |
+
try:
|
| 35 |
+
ctypes.CDLL("libcuda.so.1")
|
| 36 |
+
driver_found = True
|
| 37 |
+
except OSError:
|
| 38 |
+
print("libcuda.so.1 (NVIDIA Driver) not found via dlopen.")
|
| 39 |
+
# Search common paths
|
| 40 |
+
search_paths = [
|
| 41 |
+
"/usr/lib/x86_64-linux-gnu",
|
| 42 |
+
"/usr/lib64",
|
| 43 |
+
"/usr/lib",
|
| 44 |
+
"/usr/local/cuda/lib64",
|
| 45 |
+
"/usr/lib/wsl/lib"
|
| 46 |
+
]
|
| 47 |
+
for path in search_paths:
|
| 48 |
+
if os.path.exists(os.path.join(path, "libcuda.so.1")):
|
| 49 |
+
print(f"Found libcuda.so.1 manually at {path}")
|
| 50 |
+
lib_paths.append(path)
|
| 51 |
+
driver_found = True
|
| 52 |
+
break
|
| 53 |
|
| 54 |
+
if not driver_found and not missing_libs:
|
| 55 |
+
# If we have libs but no driver, we can't run GPU.
|
| 56 |
+
# We will set an env var to force CPU install later.
|
| 57 |
+
print("CRITICAL: NVIDIA Driver (libcuda.so.1) not found. GPU acceleration will fail.")
|
| 58 |
+
print("Switching to CPU-only mode for this run.")
|
| 59 |
+
os.environ["FORCE_CPU_MODE"] = "1"
|
| 60 |
+
|
| 61 |
+
if not missing_libs and driver_found:
|
| 62 |
+
return # All libraries and driver found
|
| 63 |
+
|
| 64 |
+
if missing_libs:
|
| 65 |
+
print(f"Missing CUDA libraries. Installing: {', '.join(missing_libs)}...")
|
| 66 |
+
# 3. Install missing packages
|
| 67 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install"] + missing_libs)
|
| 68 |
|
| 69 |
# 4. Find library paths
|
| 70 |
import site
|
|
|
|
| 71 |
for sp in site.getsitepackages():
|
| 72 |
# Runtime libs
|
| 73 |
rt_path = os.path.join(sp, "nvidia", "cuda_runtime", "lib")
|
|
|
|
| 79 |
if os.path.isdir(cublas_path):
|
| 80 |
lib_paths.append(cublas_path)
|
| 81 |
|
| 82 |
+
if not lib_paths and missing_libs:
|
| 83 |
print("Warning: Could not find nvidia lib paths after installation.")
|
| 84 |
return
|
| 85 |
|
|
|
|
| 99 |
new_ld = f"{new_ld}{os.pathsep}{current_ld}"
|
| 100 |
|
| 101 |
os.environ["LD_LIBRARY_PATH"] = new_ld
|
| 102 |
+
|
| 103 |
+
# Pass the FORCE_CPU_MODE flag to the restarted process if set
|
| 104 |
+
env = os.environ.copy()
|
| 105 |
+
|
| 106 |
# Re-execute the current script with the new environment
|
| 107 |
+
os.execvpe(sys.executable, [sys.executable] + sys.argv, env)
|
| 108 |
|
| 109 |
ensure_cuda_runtime()
|
| 110 |
# --- CUDA DEPENDENCY FIX END ---
|
|
|
|
| 119 |
print("llama-cpp-python is correctly installed.")
|
| 120 |
except (ImportError, RuntimeError, OSError) as e:
|
| 121 |
print(f"llama-cpp-python needs installation or repair: {e}")
|
| 122 |
+
|
| 123 |
+
if os.environ.get("FORCE_CPU_MODE") == "1":
|
| 124 |
+
print("Installing CPU-only llama-cpp-python (Fallback)...")
|
| 125 |
+
subprocess.check_call([
|
| 126 |
+
sys.executable, "-m", "pip", "install",
|
| 127 |
+
"llama-cpp-python",
|
| 128 |
+
"--prefer-binary",
|
| 129 |
+
"--force-reinstall"
|
| 130 |
+
])
|
| 131 |
+
else:
|
| 132 |
+
print("Installing llama-cpp-python from pre-built wheel index (CUDA)...")
|
| 133 |
+
subprocess.check_call([
|
| 134 |
+
sys.executable, "-m", "pip", "install",
|
| 135 |
+
"llama-cpp-python",
|
| 136 |
+
"--extra-index-url", "https://abetlen.github.io/llama-cpp-python/whl/cu121",
|
| 137 |
+
"--prefer-binary",
|
| 138 |
+
"--force-reinstall"
|
| 139 |
+
])
|
| 140 |
print("Installation complete.")
|
| 141 |
|
| 142 |
# --- IMPORTS AFTER INSTALL ---
|