Spaces:
Running on Zero
Running on Zero
Upload app_deepseek.py
Browse files- app_deepseek.py +46 -12
app_deepseek.py
CHANGED
|
@@ -401,7 +401,10 @@ GGUF_REPO = os.environ.get("GIJL_GGUF_REPO", "unsloth/DeepSeek-V4-Flash-0731-GGU
|
|
| 401 |
# GIJL_GGUF_QUANT to override explicitly.
|
| 402 |
GGUF_QUANT = os.environ.get("GIJL_GGUF_QUANT", "UD-IQ3_XXS")
|
| 403 |
LLAMA_CTX = int(os.environ.get("GIJL_CTX", "12288")) # tokens; KV cache is ~2-4GB at this size
|
| 404 |
-
LLAMA_LOAD_TIMEOUT_S =
|
|
|
|
|
|
|
|
|
|
| 405 |
LLAMA_BIN_DIR = os.path.join(WORK_DIR, "bin") # CUDA build cache (synced to the hub, see CACHE_LLAMA_BINARY)
|
| 406 |
LLAMA_CPU_DIR = os.path.join(WORK_DIR, "llama_cpu") # prebuilt CPU release (not synced: quick to re-download)
|
| 407 |
LLAMA_BUILD_ROOT = os.environ.get("GIJL_LLAMA_BUILD_DIR", "/tmp/gijl_llama_build")
|
|
@@ -1424,20 +1427,37 @@ def build_llama_cuda(archs: list):
|
|
| 1424 |
|
| 1425 |
def download_llama_cpu_release():
|
| 1426 |
"""Prebuilt CPU llama-server from GitHub releases. The 'latest' release only carries nightly-tag.txt, which
|
| 1427 |
-
names the tag holding the real binaries (llama-<tag>-bin-ubuntu-x64.tar.gz).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1428 |
import requests
|
| 1429 |
arch = "arm64" if platform.machine().lower() in ("aarch64", "arm64") else "x64"
|
| 1430 |
base = "https://github.com/ggml-org/llama.cpp/releases"
|
| 1431 |
hdr = {"User-Agent": _CRAWLER_UA}
|
| 1432 |
-
|
| 1433 |
-
|
| 1434 |
-
|
| 1435 |
-
|
| 1436 |
-
|
| 1437 |
-
|
| 1438 |
-
|
| 1439 |
-
|
| 1440 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1441 |
last_err = "no candidate tag"
|
| 1442 |
for tag in tags:
|
| 1443 |
dest = os.path.join(LLAMA_CPU_DIR, tag)
|
|
@@ -1591,7 +1611,21 @@ class LlamaServer:
|
|
| 1591 |
t0, last = time.time(), time.time()
|
| 1592 |
while time.time() - t0 < LLAMA_LOAD_TIMEOUT_S:
|
| 1593 |
if self.proc.poll() is not None:
|
| 1594 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1595 |
try:
|
| 1596 |
r = requests.get(self.base_url + "/health", timeout=5)
|
| 1597 |
if r.status_code == 200 and r.json().get("status") == "ok":
|
|
|
|
| 401 |
# GIJL_GGUF_QUANT to override explicitly.
|
| 402 |
GGUF_QUANT = os.environ.get("GIJL_GGUF_QUANT", "UD-IQ3_XXS")
|
| 403 |
LLAMA_CTX = int(os.environ.get("GIJL_CTX", "12288")) # tokens; KV cache is ~2-4GB at this size
|
| 404 |
+
LLAMA_LOAD_TIMEOUT_S = 3600 # was 1800s; a ~100GB GGUF loading on CPU-only (mmap + page faults, no GPU to offload
|
| 405 |
+
# onto) can legitimately take well past 30 minutes depending on storage throughput --
|
| 406 |
+
# this is not an error state (see the "[llama] loading model ... Ns" progress log),
|
| 407 |
+
# just slow I/O, so the budget needs real headroom instead of racing it.
|
| 408 |
LLAMA_BIN_DIR = os.path.join(WORK_DIR, "bin") # CUDA build cache (synced to the hub, see CACHE_LLAMA_BINARY)
|
| 409 |
LLAMA_CPU_DIR = os.path.join(WORK_DIR, "llama_cpu") # prebuilt CPU release (not synced: quick to re-download)
|
| 410 |
LLAMA_BUILD_ROOT = os.environ.get("GIJL_LLAMA_BUILD_DIR", "/tmp/gijl_llama_build")
|
|
|
|
| 1427 |
|
| 1428 |
def download_llama_cpu_release():
|
| 1429 |
"""Prebuilt CPU llama-server from GitHub releases. The 'latest' release only carries nightly-tag.txt, which
|
| 1430 |
+
names the tag holding the real binaries (llama-<tag>-bin-ubuntu-x64.tar.gz).
|
| 1431 |
+
GIJL_LLAMA_CPU_TAG pins a specific release tag (e.g. "b10800") instead of always grabbing whatever is newest.
|
| 1432 |
+
This exists because of a confirmed, UNFIXED upstream llama.cpp bug: certain large/MoE GGUF models crash
|
| 1433 |
+
CPU-only inference with GGML_ASSERT(*cur_backend_id != -1) in ggml_backend_sched_split_graph during context
|
| 1434 |
+
creation -- reproduced against the build this pipeline downloads (b11429) and reported against several other
|
| 1435 |
+
recent builds too (ggml-org/llama.cpp issues #18099, #19397), closed upstream as "not planned" with no fix and
|
| 1436 |
+
no confirmed workaround other than attaching a GPU. -fit off does NOT fix it (tested in both issues and in
|
| 1437 |
+
this pipeline's own run logs). If you hit that exact crash, there is nothing further this Python script can
|
| 1438 |
+
patch around -- it is a C++ scheduler bug, not a flag the server exposes. Two real options: (1) run with a GPU
|
| 1439 |
+
accelerator (this pipeline already warns for that at startup), or (2) set GIJL_LLAMA_CPU_TAG to an older
|
| 1440 |
+
release tag that predates whatever regression introduced this, and see whether that specific build is
|
| 1441 |
+
unaffected for your model/quant -- there is no single tag known in advance to be safe, so this is trial and
|
| 1442 |
+
error, not a guaranteed fix."""
|
| 1443 |
import requests
|
| 1444 |
arch = "arm64" if platform.machine().lower() in ("aarch64", "arm64") else "x64"
|
| 1445 |
base = "https://github.com/ggml-org/llama.cpp/releases"
|
| 1446 |
hdr = {"User-Agent": _CRAWLER_UA}
|
| 1447 |
+
pinned = os.environ.get("GIJL_LLAMA_CPU_TAG", "").strip()
|
| 1448 |
+
if pinned:
|
| 1449 |
+
print(f"[llama] GIJL_LLAMA_CPU_TAG set -> using pinned CPU build {pinned} instead of latest", flush=True)
|
| 1450 |
+
tags = [pinned]
|
| 1451 |
+
else:
|
| 1452 |
+
tags = []
|
| 1453 |
+
try:
|
| 1454 |
+
latest = requests.get(f"{base}/latest", timeout=30, headers=hdr).url.rstrip("/").rsplit("/", 1)[-1]
|
| 1455 |
+
nt = requests.get(f"{base}/download/{latest}/nightly-tag.txt", timeout=30, headers=hdr)
|
| 1456 |
+
if nt.status_code == 200 and nt.text.strip():
|
| 1457 |
+
tags.append(nt.text.strip().split()[0])
|
| 1458 |
+
tags.append(latest)
|
| 1459 |
+
except requests.RequestException as e:
|
| 1460 |
+
raise RuntimeError(f"cannot reach GitHub releases: {_redact(str(e))[:200]}")
|
| 1461 |
last_err = "no candidate tag"
|
| 1462 |
for tag in tags:
|
| 1463 |
dest = os.path.join(LLAMA_CPU_DIR, tag)
|
|
|
|
| 1611 |
t0, last = time.time(), time.time()
|
| 1612 |
while time.time() - t0 < LLAMA_LOAD_TIMEOUT_S:
|
| 1613 |
if self.proc.poll() is not None:
|
| 1614 |
+
tail = self.tail(40)
|
| 1615 |
+
if self.kind == "cpu" and "cur_backend_id != -1" in tail:
|
| 1616 |
+
# Confirmed unfixed upstream llama.cpp bug, not something this script's flags control -- see the
|
| 1617 |
+
# long comment on download_llama_cpu_release(). Spell this out instead of just dumping the
|
| 1618 |
+
# C++ backtrace, since "-fit off" (tried automatically above) is already known NOT to fix it.
|
| 1619 |
+
raise RuntimeError(
|
| 1620 |
+
"llama-server crashed during model load with GGML_ASSERT(*cur_backend_id != -1) failed "
|
| 1621 |
+
"(ggml_backend_sched_split_graph). This is a confirmed, UNFIXED upstream llama.cpp bug on "
|
| 1622 |
+
"CPU-only inference of some large/MoE GGUF models (ggml-org/llama.cpp issues #18099, "
|
| 1623 |
+
"#19397 -- closed as 'not planned', no fix released). --fit off does not fix it. There is "
|
| 1624 |
+
"no flag this script can add to work around it. Real options: (1) run on a GPU accelerator "
|
| 1625 |
+
"instead of CPU, or (2) set GIJL_LLAMA_CPU_TAG to try a different/older llama-server CPU "
|
| 1626 |
+
f"release build (current: {os.path.basename(os.path.dirname(self.binary)) or '?'}), with no "
|
| 1627 |
+
f"guarantee any particular tag is unaffected. Log tail:\n{tail}")
|
| 1628 |
+
raise RuntimeError(f"llama-server exited during startup (code {self.proc.returncode}). Log tail:\n{tail}")
|
| 1629 |
try:
|
| 1630 |
r = requests.get(self.base_url + "/health", timeout=5)
|
| 1631 |
if r.status_code == 200 and r.json().get("status") == "ok":
|