From e280e457d1697215b00ceca1b8d7fae8ccbfa29d Mon Sep 17 00:00:00 2001 From: Roland Tannous Date: Mon, 2 Mar 2026 04:04:41 +0000 Subject: [PATCH] Move llama.cpp clone/build from in-tree to ~/.unsloth/llama.cpp - setup.sh: builds at ~/.unsloth/llama.cpp instead of ./llama.cpp - setup.ps1: builds at %USERPROFILE%/.unsloth/llama.cpp - inference llama_cpp.py: searches ~/.unsloth/ first, in-tree as legacy - export.py: updated comments (unsloth-zoo handles path natively) --- setup.ps1 | 11 ++++--- setup.sh | 9 ++++-- studio/backend/core/export/export.py | 5 ++- studio/backend/core/inference/llama_cpp.py | 36 ++++++++++++---------- 4 files changed, 35 insertions(+), 26 deletions(-) diff --git a/setup.ps1 b/setup.ps1 index fd5447b124..8a2ec56033 100644 --- a/setup.ps1 +++ b/setup.ps1 @@ -739,13 +739,16 @@ if ($OpenSslRoot) { # ========================================================================== # PHASE 4: Build llama.cpp with CUDA for GGUF inference + export # ========================================================================== -# Builds in-tree at $REPO/llama.cpp/ (same as setup.sh on Linux). -# This directory is already in .gitignore. +# Builds at ~/.unsloth/llama.cpp — a single shared location under the user's +# home directory. This is used by both the inference server and the GGUF +# export pipeline (unsloth-zoo). # We build: -# - llama-server: for GGUF model inference (with HTTPS if vcpkg/curl available) +# - llama-server: for GGUF model inference (with HTTPS if OpenSSL available) # - llama-quantize: for GGUF export quantization # Prerequisites (git, cmake, VS Build Tools, CUDA Toolkit) already installed in Phase 1. -$LlamaCppDir = Join-Path $PSScriptRoot "llama.cpp" +$UnslothHome = Join-Path $env:USERPROFILE ".unsloth" +if (-not (Test-Path $UnslothHome)) { New-Item -ItemType Directory -Force $UnslothHome | Out-Null } +$LlamaCppDir = Join-Path $UnslothHome "llama.cpp" $BuildDir = Join-Path $LlamaCppDir "build" $LlamaServerBin = Join-Path $BuildDir "bin\Release\llama-server.exe" diff --git a/setup.sh b/setup.sh index 409df0b798..07ea131125 100755 --- a/setup.sh +++ b/setup.sh @@ -197,11 +197,14 @@ else fi # ── 8. Build llama.cpp binaries for GGUF inference + export ── -# Builds in-tree at $REPO/llama.cpp/. This directory is shared with -# unsloth-zoo's GGUF export pipeline. We build: +# Builds at ~/.unsloth/llama.cpp — a single shared location under the user's +# home directory. This is used by both the inference server and the GGUF +# export pipeline (unsloth-zoo). # - llama-server: for GGUF model inference # - llama-quantize: for GGUF export quantization (symlinked to root for check_llama_cpp()) -LLAMA_CPP_DIR="$SCRIPT_DIR/llama.cpp" +UNSLOTH_HOME="$HOME/.unsloth" +mkdir -p "$UNSLOTH_HOME" +LLAMA_CPP_DIR="$UNSLOTH_HOME/llama.cpp" LLAMA_SERVER_BIN="$LLAMA_CPP_DIR/build/bin/llama-server" rm -rf "$LLAMA_CPP_DIR" { diff --git a/studio/backend/core/export/export.py b/studio/backend/core/export/export.py index bc4e267f75..3d3db2d560 100644 --- a/studio/backend/core/export/export.py +++ b/studio/backend/core/export/export.py @@ -418,9 +418,8 @@ class ExportBackend: pre_existing_ggufs = set(glob.glob(os.path.join(cwd, "*.gguf"))) # Pass absolute path — no os.chdir needed. - # unsloth saves intermediate HF model files into model_save_path, - # while check_llama_cpp("llama.cpp") resolves against cwd (repo root) - # where setup.sh already built llama.cpp with quantizer. + # unsloth saves intermediate HF model files into model_save_path. + # unsloth-zoo's check_llama_cpp() uses ~/.unsloth/llama.cpp by default. model_save_path = os.path.join(abs_save_dir, "model") self.current_model.save_pretrained_gguf( model_save_path, diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.py index e0e2eebcf2..09f8a2fcc9 100644 --- a/studio/backend/core/inference/llama_cpp.py +++ b/studio/backend/core/inference/llama_cpp.py @@ -77,10 +77,11 @@ class LlamaCppBackend: Search order: 1. LLAMA_SERVER_PATH environment variable - 2. ./llama.cpp/build/bin/llama-server (built by setup.sh in-tree) - 3. ~/.unsloth/llama.cpp/build/bin/Release/llama-server (built by setup.ps1 on Windows) - 4. llama-server on PATH (system install) - 5. ./bin/llama-server (legacy: extracted binary) + 2. ~/.unsloth/llama.cpp/build/bin/llama-server (Linux, built by setup.sh) + 3. ~/.unsloth/llama.cpp/build/bin/Release/llama-server.exe (Windows, built by setup.ps1) + 4. ./llama.cpp/build/bin/llama-server (legacy: in-tree build) + 5. llama-server on PATH (system install) + 6. ./bin/llama-server (legacy: extracted binary) """ import os import sys @@ -92,31 +93,34 @@ class LlamaCppBackend: if env_path and Path(env_path).is_file(): return env_path - # Project root: llama_cpp.py → inference/ → core/ → backend/ → studio/ → root - project_root = Path(__file__).resolve().parents[4] + # 2. ~/.unsloth/llama.cpp (primary — setup.sh / setup.ps1 build here) + unsloth_home = Path.home() / ".unsloth" / "llama.cpp" + home_linux = unsloth_home / "build" / "bin" / binary_name + if home_linux.is_file(): + return str(home_linux) - # 2. In-tree llama.cpp build (setup.sh builds here on Linux) + # 3. Windows MSVC build has Release subdir + if sys.platform == "win32": + home_win = unsloth_home / "build" / "bin" / "Release" / binary_name + if home_win.is_file(): + return str(home_win) + + # 4. Legacy: in-tree build (older setup.sh / setup.ps1 versions) + project_root = Path(__file__).resolve().parents[4] build_path = project_root / "llama.cpp" / "build" / "bin" / binary_name if build_path.is_file(): return str(build_path) - - # 3. Windows MSVC build (Release config, in-tree — matches setup.ps1) if sys.platform == "win32": - # In-tree (primary — setup.ps1 now builds here) win_path = project_root / "llama.cpp" / "build" / "bin" / "Release" / binary_name if win_path.is_file(): return str(win_path) - # Legacy: ~/.unsloth (older setup.ps1 versions built here) - home_path = Path.home() / ".unsloth" / "llama.cpp" / "build" / "bin" / "Release" / binary_name - if home_path.is_file(): - return str(home_path) - # 4. System PATH + # 5. System PATH system_path = shutil.which("llama-server") if system_path: return system_path - # 5. Legacy: extracted to bin/ + # 6. Legacy: extracted to bin/ bin_path = project_root / "bin" / binary_name if bin_path.is_file(): return str(bin_path)