identityvoid commited on
Commit
b48f0ec
·
1 Parent(s): 0b060ae

Fix llama.cpp CUDA runtime loading

Browse files
Files changed (1) hide show
  1. app.py +42 -1
app.py CHANGED
@@ -12,6 +12,38 @@ MMPROJ_PATH = os.path.join("/tmp", MMPROJ_FILE)
12
 
13
  server_process = None
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  def download_models():
17
  from huggingface_hub import hf_hub_download
@@ -35,11 +67,12 @@ def download_models():
35
  def generate(prompt, max_tokens, temperature):
36
  global server_process
37
 
 
38
  download_models()
39
 
40
  if server_process is None or server_process.poll() is not None:
41
  cmd = [
42
- "llama-server",
43
  "-m", MODEL_PATH,
44
  "--mmproj", MMPROJ_PATH,
45
  "--host", "127.0.0.1",
@@ -50,11 +83,19 @@ def generate(prompt, max_tokens, temperature):
50
  "-t", "2",
51
  ]
52
 
 
 
 
 
 
 
 
53
  server_process = subprocess.Popen(
54
  cmd,
55
  stdout=subprocess.PIPE,
56
  stderr=subprocess.STDOUT,
57
  text=True,
 
58
  )
59
 
60
  import time
 
12
 
13
  server_process = None
14
 
15
+ LLAMA_DIR = "/tmp/llama"
16
+ LLAMA_SERVER = os.path.join(LLAMA_DIR, "llama-server")
17
+ LLAMA_RELEASE = "b10182"
18
+ LLAMA_ARCHIVE = f"llama.cpp-b{LLAMA_RELEASE}-cuda-12.8-amd64.tar.gz"
19
+ LLAMA_URL = f"https://github.com/ai-dock/llama.cpp-cuda/releases/download/b{LLAMA_RELEASE}/{LLAMA_ARCHIVE}"
20
+
21
+
22
+ def download_llama():
23
+ if os.path.exists(LLAMA_SERVER):
24
+ return
25
+
26
+ os.makedirs(LLAMA_DIR, exist_ok=True)
27
+
28
+ archive = os.path.join("/tmp", LLAMA_ARCHIVE)
29
+
30
+ subprocess.run(
31
+ ["wget", "-q", "--show-progress", "-O", archive, LLAMA_URL],
32
+ check=True,
33
+ )
34
+
35
+ subprocess.run(
36
+ ["tar", "-xzf", archive, "-C", LLAMA_DIR, "--strip-components=1"],
37
+ check=True,
38
+ )
39
+
40
+ if not os.path.exists(LLAMA_SERVER):
41
+ raise RuntimeError(
42
+ f"llama-server was not found after extraction: {LLAMA_SERVER}"
43
+ )
44
+
45
+ os.chmod(LLAMA_SERVER, 0o755)
46
+
47
 
48
  def download_models():
49
  from huggingface_hub import hf_hub_download
 
67
  def generate(prompt, max_tokens, temperature):
68
  global server_process
69
 
70
+ download_llama()
71
  download_models()
72
 
73
  if server_process is None or server_process.poll() is not None:
74
  cmd = [
75
+ LLAMA_SERVER,
76
  "-m", MODEL_PATH,
77
  "--mmproj", MMPROJ_PATH,
78
  "--host", "127.0.0.1",
 
83
  "-t", "2",
84
  ]
85
 
86
+ server_env = os.environ.copy()
87
+ server_env["LD_LIBRARY_PATH"] = (
88
+ LLAMA_DIR
89
+ + ":"
90
+ + server_env.get("LD_LIBRARY_PATH", "")
91
+ )
92
+
93
  server_process = subprocess.Popen(
94
  cmd,
95
  stdout=subprocess.PIPE,
96
  stderr=subprocess.STDOUT,
97
  text=True,
98
+ env=server_env,
99
  )
100
 
101
  import time