kirikir13 commited on
Commit
3a23c77
Β·
verified Β·
1 Parent(s): e2ed8fd

Update llama_bootstrap.py

Browse files
Files changed (1) hide show
  1. llama_bootstrap.py +18 -83
llama_bootstrap.py CHANGED
@@ -1,49 +1,25 @@
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"
@@ -51,67 +27,26 @@ def _pip(args: list) -> None:
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
 
 
1
  """
2
  llama-cpp-python bootstrap β€” MUST import before gradio or anything else.
3
+
4
+ 2026-07-27: simplified after the great wheel hunt.
5
+ - PyPI = source-only (compiles 20+ min β€” the old "runs and runs" bug).
6
+ - abetlen CUDA indexes = abandoned (newest cu121 wheel is v0.2.59). Do NOT attempt.
7
+ - abetlen CPU index = current prebuilt wheels. That is the only fast path.
8
+ GPU speed returns via the Level-2 llama-server route (official llama.cpp CUDA
9
+ binaries + MTP flags) β€” separate spec, not this file.
10
  """
11
 
12
  from __future__ import annotations
13
 
14
  import os
 
15
  import subprocess
16
  import sys
17
 
18
+ CPU_WHEEL_INDEX = "https://abetlen.github.io/llama-cpp-python/whl/cpu"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
 
21
  def _pip(args: list) -> None:
22
+ cmd = [sys.executable, "-m", "pip", *args]
23
  print(f"[LLAMA] $ {' '.join(cmd)}", flush=True)
24
  env = os.environ.copy()
25
  env["PIP_DISABLE_PIP_VERSION_CHECK"] = "1"
 
27
  subprocess.check_call(cmd, env=env)
28
 
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  def try_import_llama():
31
  import llama_cpp # noqa: F401
 
32
  return llama_cpp
33
 
34
 
35
  def bootstrap_llama_cpp():
36
  try:
37
  mod = try_import_llama()
38
+ print("[LLAMA] Ready (prebuilt wheel already installed)", flush=True)
 
39
  except ImportError:
40
+ print("[LLAMA] Not installed β€” installing CPU wheel (fast path)...", flush=True)
41
+ _pip([
42
+ "install", "--no-cache-dir", "llama-cpp-python==0.3.34",
43
+ "--extra-index-url", CPU_WHEEL_INDEX,
44
+ ])
45
+ mod = try_import_llama()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
+ # CPU wheel => CPU inference. Be explicit so nothing pretends otherwise.
48
  os.environ.setdefault("DAVIDAU_N_GPU_LAYERS", "0")
49
+ print("[LLAMA] CPU mode (n_gpu_layers=0) β€” GPU speed is the Level-2 llama-server job", flush=True)
 
50
  return mod
51
 
52