Spaces:
Running on Zero
Running on Zero
Build llama.cpp from source instead of pre-built binaries
Browse files
app.py
CHANGED
|
@@ -416,82 +416,47 @@ _LLAMA_CPP_BIN = None # path to downloaded llama-cli binary
|
|
| 416 |
|
| 417 |
|
| 418 |
def _ensure_llama_cpp():
|
| 419 |
-
"""
|
| 420 |
global _LLAMA_CPP_BIN
|
| 421 |
if _LLAMA_CPP_BIN and os.path.isfile(_LLAMA_CPP_BIN):
|
| 422 |
return _LLAMA_CPP_BIN
|
| 423 |
|
| 424 |
import subprocess
|
| 425 |
-
import zipfile
|
| 426 |
-
import urllib.request
|
| 427 |
|
| 428 |
os.makedirs(_BENCHMARK_CACHE, exist_ok=True)
|
| 429 |
-
|
| 430 |
-
os.
|
| 431 |
-
|
| 432 |
llama_cli = os.path.join(bin_dir, "llama-cli")
|
| 433 |
-
|
| 434 |
-
if os.path.isfile(llama_cli)
|
| 435 |
_LLAMA_CPP_BIN = llama_cli
|
| 436 |
return llama_cli
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
|
|
|
|
|
|
| 440 |
import shutil as _shutil
|
| 441 |
-
_shutil.rmtree(
|
| 442 |
-
os.makedirs(bin_dir, exist_ok=True)
|
| 443 |
-
|
| 444 |
-
_log("Benchmark: downloading llama.cpp binary...")
|
| 445 |
-
# Use a known stable release
|
| 446 |
-
release = "b5440"
|
| 447 |
-
url = f"https://github.com/ggml-org/llama.cpp/releases/download/{release}/llama-{release}-bin-ubuntu-x64.zip"
|
| 448 |
-
_log(f"Benchmark: URL: {url}")
|
| 449 |
-
zip_path = os.path.join(bin_dir, "llama.zip")
|
| 450 |
-
urllib.request.urlretrieve(url, zip_path)
|
| 451 |
-
_log(f"Benchmark: downloaded {os.path.getsize(zip_path)} bytes")
|
| 452 |
-
|
| 453 |
-
_log("Benchmark: extracting llama.cpp...")
|
| 454 |
-
with zipfile.ZipFile(zip_path) as zf:
|
| 455 |
-
_log(f"Benchmark: zip has {len(zf.namelist())} files")
|
| 456 |
-
# Extract ALL files (libs + binaries) to bin_dir
|
| 457 |
-
zf.extractall(bin_dir)
|
| 458 |
-
os.unlink(zip_path)
|
| 459 |
-
|
| 460 |
-
# The files are under build/bin/ - move them up and fix permissions
|
| 461 |
-
build_bin = os.path.join(bin_dir, "build", "bin")
|
| 462 |
-
if os.path.isdir(build_bin):
|
| 463 |
-
for fname in os.listdir(build_bin):
|
| 464 |
-
src = os.path.join(build_bin, fname)
|
| 465 |
-
dst = os.path.join(bin_dir, fname)
|
| 466 |
-
os.rename(src, dst)
|
| 467 |
-
os.rmdir(os.path.join(bin_dir, "build", "bin"))
|
| 468 |
-
os.rmdir(os.path.join(bin_dir, "build"))
|
| 469 |
-
|
| 470 |
-
# Make all files executable
|
| 471 |
-
for fname in os.listdir(bin_dir):
|
| 472 |
-
fpath = os.path.join(bin_dir, fname)
|
| 473 |
-
if os.path.isfile(fpath):
|
| 474 |
-
os.chmod(fpath, 0o755)
|
| 475 |
-
|
| 476 |
-
_log(f"Benchmark: extracted to {bin_dir}")
|
| 477 |
-
_log(f"Benchmark: files: {os.listdir(bin_dir)}")
|
| 478 |
-
_log(f"Benchmark: libllama.so exists: {os.path.isfile(os.path.join(bin_dir, 'libllama.so'))}")
|
| 479 |
-
_log(f"Benchmark: libllama.so size: {os.path.getsize(os.path.join(bin_dir, 'libllama.so')) if os.path.isfile(os.path.join(bin_dir, 'libllama.so')) else 'N/A'}")
|
| 480 |
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 495 |
|
| 496 |
_LLAMA_CPP_BIN = llama_cli
|
| 497 |
return llama_cli
|
|
|
|
| 416 |
|
| 417 |
|
| 418 |
def _ensure_llama_cpp():
|
| 419 |
+
"""Build or download llama.cpp binary if not cached."""
|
| 420 |
global _LLAMA_CPP_BIN
|
| 421 |
if _LLAMA_CPP_BIN and os.path.isfile(_LLAMA_CPP_BIN):
|
| 422 |
return _LLAMA_CPP_BIN
|
| 423 |
|
| 424 |
import subprocess
|
|
|
|
|
|
|
| 425 |
|
| 426 |
os.makedirs(_BENCHMARK_CACHE, exist_ok=True)
|
| 427 |
+
build_dir = os.path.join(_BENCHMARK_CACHE, "llama_build")
|
| 428 |
+
bin_dir = os.path.join(build_dir, "build", "bin")
|
|
|
|
| 429 |
llama_cli = os.path.join(bin_dir, "llama-cli")
|
| 430 |
+
|
| 431 |
+
if os.path.isfile(llama_cli):
|
| 432 |
_LLAMA_CPP_BIN = llama_cli
|
| 433 |
return llama_cli
|
| 434 |
+
|
| 435 |
+
# Clone and build llama.cpp from source
|
| 436 |
+
_log("Benchmark: cloning llama.cpp...")
|
| 437 |
+
src_dir = os.path.join(_BENCHMARK_CACHE, "llama_src")
|
| 438 |
+
if os.path.isdir(src_dir):
|
| 439 |
import shutil as _shutil
|
| 440 |
+
_shutil.rmtree(src_dir, ignore_errors=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 441 |
|
| 442 |
+
subprocess.check_call(
|
| 443 |
+
["git", "clone", "--depth=1", "https://github.com/ggml-org/llama.cpp.git", src_dir],
|
| 444 |
+
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
| 445 |
+
)
|
| 446 |
+
_log("Benchmark: building llama.cpp (this takes 2-5 minutes)...")
|
| 447 |
+
n_threads = _parse_cpu_count() or 8
|
| 448 |
+
os.makedirs(build_dir, exist_ok=True)
|
| 449 |
+
subprocess.check_call(
|
| 450 |
+
["cmake", "-S", src_dir, "-B", build_dir,
|
| 451 |
+
"-DLLAMA_NATIVE=ON", "-DBUILD_SHARED_LIBS=ON"],
|
| 452 |
+
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
| 453 |
+
)
|
| 454 |
+
subprocess.check_call(
|
| 455 |
+
["cmake", "--build", build_dir, "-j", str(n_threads), "--target", "llama-cli"],
|
| 456 |
+
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
| 457 |
+
)
|
| 458 |
+
_log(f"Benchmark: llama.cpp built at {llama_cli}")
|
| 459 |
+
_log(f"Benchmark: files: {os.listdir(bin_dir)}")
|
| 460 |
|
| 461 |
_LLAMA_CPP_BIN = llama_cli
|
| 462 |
return llama_cli
|