Create llama_bootstrap.py
Browse files- llama_bootstrap.py +118 -0
llama_bootstrap.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
llama-cpp-python bootstrap β MUST import before gradio or anything else.
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
from __future__ import annotations
|
| 6 |
+
|
| 7 |
+
import os
|
| 8 |
+
import shutil
|
| 9 |
+
import subprocess
|
| 10 |
+
import sys
|
| 11 |
+
|
| 12 |
+
CUDA_VARIANTS = (
|
| 13 |
+
os.environ.get("LLAMA_CPP_CUDA", "cu124"),
|
| 14 |
+
"cu124",
|
| 15 |
+
"cu121",
|
| 16 |
+
)
|
| 17 |
+
PIP_BASE = [sys.executable, "-m", "pip"]
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def has_nvidia_gpu() -> bool:
|
| 21 |
+
if os.environ.get("LLAMA_CPP_FORCE_CPU", "").strip().lower() in ("1", "true", "yes"):
|
| 22 |
+
print("[LLAMA] LLAMA_CPP_FORCE_CPU=1 β skipping GPU", flush=True)
|
| 23 |
+
return False
|
| 24 |
+
if "CUDA_VISIBLE_DEVICES" in os.environ and os.environ["CUDA_VISIBLE_DEVICES"].strip() == "":
|
| 25 |
+
print("[LLAMA] CUDA_VISIBLE_DEVICES='' β no GPU visible", flush=True)
|
| 26 |
+
return False
|
| 27 |
+
if shutil.which("nvidia-smi") is None:
|
| 28 |
+
return False
|
| 29 |
+
try:
|
| 30 |
+
result = subprocess.run(
|
| 31 |
+
["nvidia-smi", "-L"],
|
| 32 |
+
capture_output=True,
|
| 33 |
+
text=True,
|
| 34 |
+
timeout=10,
|
| 35 |
+
)
|
| 36 |
+
ok = result.returncode == 0 and bool(result.stdout.strip())
|
| 37 |
+
if ok:
|
| 38 |
+
print(f"[LLAMA] GPU detected:\n{result.stdout.strip()}", flush=True)
|
| 39 |
+
return ok
|
| 40 |
+
except Exception as e:
|
| 41 |
+
print(f"[LLAMA] nvidia-smi check failed: {e}", flush=True)
|
| 42 |
+
return False
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def _pip(args: list) -> None:
|
| 46 |
+
cmd = [*PIP_BASE, *args]
|
| 47 |
+
print(f"[LLAMA] $ {' '.join(cmd)}", flush=True)
|
| 48 |
+
env = os.environ.copy()
|
| 49 |
+
env["PIP_DISABLE_PIP_VERSION_CHECK"] = "1"
|
| 50 |
+
env["PIP_ROOT_USER_ACTION"] = "ignore"
|
| 51 |
+
subprocess.check_call(cmd, env=env)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def pip_uninstall_llama() -> None:
|
| 55 |
+
try:
|
| 56 |
+
_pip(["uninstall", "-y", "llama-cpp-python"])
|
| 57 |
+
except Exception:
|
| 58 |
+
pass
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def pip_install_cpu() -> None:
|
| 62 |
+
print("[LLAMA] Installing CPU wheel (no CUDA)...", flush=True)
|
| 63 |
+
_pip(["install", "--no-cache-dir", "llama-cpp-python"])
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def pip_install_cuda(variant: str) -> None:
|
| 67 |
+
index = f"https://abetlen.github.io/llama-cpp-python/whl/{variant}"
|
| 68 |
+
print(f"[LLAMA] Installing CUDA wheel ({variant})...", flush=True)
|
| 69 |
+
_pip([
|
| 70 |
+
"install",
|
| 71 |
+
"--no-cache-dir",
|
| 72 |
+
"llama-cpp-python",
|
| 73 |
+
"--extra-index-url",
|
| 74 |
+
index,
|
| 75 |
+
])
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def try_import_llama():
|
| 79 |
+
import llama_cpp # noqa: F401
|
| 80 |
+
from llama_cpp import Llama # noqa: F401
|
| 81 |
+
return llama_cpp
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def bootstrap_llama_cpp():
|
| 85 |
+
try:
|
| 86 |
+
mod = try_import_llama()
|
| 87 |
+
print(f"[LLAMA] Ready (gpu={has_nvidia_gpu()})", flush=True)
|
| 88 |
+
return mod
|
| 89 |
+
except ImportError:
|
| 90 |
+
print("[LLAMA] Not installed β bootstrapping...", flush=True)
|
| 91 |
+
except (RuntimeError, OSError) as exc:
|
| 92 |
+
print(f"[LLAMA] Broken install ({exc}) β reinstalling...", flush=True)
|
| 93 |
+
pip_uninstall_llama()
|
| 94 |
+
|
| 95 |
+
if has_nvidia_gpu():
|
| 96 |
+
last_err = None
|
| 97 |
+
for variant in CUDA_VARIANTS:
|
| 98 |
+
try:
|
| 99 |
+
pip_uninstall_llama()
|
| 100 |
+
pip_install_cuda(variant)
|
| 101 |
+
mod = try_import_llama()
|
| 102 |
+
os.environ.setdefault("DAVIDAU_N_GPU_LAYERS", "-1")
|
| 103 |
+
print(f"[LLAMA] CUDA ready ({variant}, n_gpu_layers=-1)", flush=True)
|
| 104 |
+
return mod
|
| 105 |
+
except Exception as exc:
|
| 106 |
+
last_err = exc
|
| 107 |
+
print(f"[LLAMA] CUDA {variant} failed: {exc}", flush=True)
|
| 108 |
+
print(f"[LLAMA] All CUDA variants failed ({last_err}) β CPU fallback", flush=True)
|
| 109 |
+
pip_uninstall_llama()
|
| 110 |
+
|
| 111 |
+
pip_install_cpu()
|
| 112 |
+
os.environ.setdefault("DAVIDAU_N_GPU_LAYERS", "0")
|
| 113 |
+
mod = try_import_llama()
|
| 114 |
+
print("[LLAMA] CPU ready (n_gpu_layers=0)", flush=True)
|
| 115 |
+
return mod
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
bootstrap_llama_cpp()
|