#!/usr/bin/env bash # One-command Apple Silicon Mac installer & launcher for Looped DeepSeek-V4.1-Flash-1017 (sushi STQ). # # Usage: # curl -fsSL https://raw.githubusercontent.com/jasontitus/dsv41-loop-eval/main/install_mac.sh | bash # # Options (via env vars): # QUANT=auto|1.75bpw|2.7bpw STQ pack tier (default: auto — reuses existing pack if found, else 1.75bpw) # STQ_PACK_DIR=/path Custom directory for STQ weights (e.g. external Thunderbolt/USB4 SSD) # INSTALL_DIR=/path Install location for adapter + sushi binary (default: ~/.dsv41-loop) # SSD_BUDGET_GB=auto| Expert-streaming RAM budget (default: auto — uses sushi's live free-RAM planner) # PORT=11434 Local OpenAI + Anthropic + Browser Chat port (NO_SERVE=1 to skip launching) set -euo pipefail if [ "$(uname -s)" != "Darwin" ] || [ "$(uname -m)" != "arm64" ]; then echo "ERROR: install_mac.sh requires an Apple Silicon Mac (macOS arm64)." >&2 echo " For NVIDIA CUDA / vLLM, run:" >&2 echo " curl -fsSL https://raw.githubusercontent.com/jasontitus/dsv41-loop-eval/main/install_cuda.sh | bash" >&2 exit 1 fi RAM_BYTES=$(sysctl -n hw.memsize 2>/dev/null || echo 68719476736) RAM_GB=$(( (RAM_BYTES + 536870912) / 1073741824 )) MACOS_VER=$(sw_vers -productVersion 2>/dev/null || echo "0.0") MACOS_MAJOR="${MACOS_VER%%.*}" MACOS_REST="${MACOS_VER#*.}" MACOS_MINOR="${MACOS_REST%%.*}" [[ "$MACOS_MINOR" =~ ^[0-9]+$ ]] || MACOS_MINOR=0 # 1. Fail fast BEFORE downloading weights if macOS < 26.2 (unless user brings a custom SUSHI_BIN) if [ -z "${SUSHI_BIN:-}" ]; then if [ "${MACOS_MAJOR:-0}" -lt 26 ] || { [ "${MACOS_MAJOR:-0}" -eq 26 ] && [ "${MACOS_MINOR:-0}" -lt 2 ]; }; then echo "ERROR: Detected macOS ${MACOS_VER}. The bundled sushi 1.8.1 Metal NAX binaries require macOS 26.2+." >&2 echo " Upgrade to macOS 26.2+, or build https://github.com/beamivalice/sushi/pull/280 from source" >&2 echo " and re-run with SUSHI_BIN=/path/to/sushi." >&2 exit 1 fi fi if [ "$RAM_GB" -lt 48 ]; then echo "ERROR: Detected ${RAM_GB} GB unified memory. Looped DeepSeek-V4.1 (552B) via sushi NVMe expert" >&2 echo " streaming requires 64 GB+ recommended (or an adventurous 48 GB Mac with apps closed)." >&2 exit 1 fi INSTALL_DIR="${INSTALL_DIR:-$HOME/.dsv41-loop}" BIN_LINK_DIR="${BIN_LINK_DIR:-$HOME/.local/bin}" KIT_URL="https://github.com/jasontitus/dsv41-loop-eval/releases/latest/download/dsv41-loop-friend-eval-kit.tar.gz" RAW_EVAL_URL="https://raw.githubusercontent.com/jasontitus/dsv41-loop-eval/main/run_friend_eval" RAW_GRADER_URL="https://raw.githubusercontent.com/jasontitus/dsv41-loop-eval/main/bench_data/lcb_eval.py" has_pack_files() { local d="$1" [ -n "$d" ] && [ -d "$d" ] && { [ -f "$d/config.json" ] || [ -f "$d/model.safetensors.index.json" ]; } } find_existing_pack() { local repo_short="$1" local cand for cand in \ "$HOME/.sushi/models/beamster/$repo_short" \ "$HOME/.sushi/models/$repo_short" \ "$HOME/.cache/huggingface/hub/models--beamster--${repo_short}/snapshots"/*; do if has_pack_files "$cand"; then echo "$cand" return 0 fi done return 1 } # 2. Resolve existing STQ weights FIRST across ~/.sushi/models and ~/.cache/huggingface so 0 GB is re-downloaded! QUANT="${QUANT:-auto}" STQ_PACK="${STQ_PACK_DIR:-}" FOUND_175="$(find_existing_pack "DeepSeek-V4.1-Flash-1017-Sushi-STQ-1.75bpw" || true)" FOUND_270="$(find_existing_pack "DeepSeek-V4.1-Flash-1017-Sushi-STQ-2.7bpw" || true)" if has_pack_files "$STQ_PACK"; then echo "[dsv41-loop] Reusing existing STQ pack at $STQ_PACK (0 GB re-downloaded!)" STQ_REPO="" elif [ "$QUANT" = "2.7bpw" ] || [ "$QUANT" = "2.7" ]; then if [ "$RAM_GB" -lt 128 ] && [ "${FORCE_QUANT:-0}" != "1" ]; then echo "ERROR: STQ-2.7bpw is a 256 GB tier pack (~465 GB on disk) and detected RAM is ${RAM_GB} GB." >&2 echo " Use default 1.75bpw (omit QUANT), or set FORCE_QUANT=1 to override." >&2 exit 1 fi STQ_REPO="beamster/DeepSeek-V4.1-Flash-1017-Sushi-STQ-2.7bpw" STQ_PACK="${STQ_PACK:-${FOUND_270:-$HOME/.sushi/models/beamster/DeepSeek-V4.1-Flash-1017-Sushi-STQ-2.7bpw}}" elif [ "$QUANT" = "1.75bpw" ] || [ "$QUANT" = "1.75" ]; then STQ_REPO="beamster/DeepSeek-V4.1-Flash-1017-Sushi-STQ-1.75bpw" STQ_PACK="${STQ_PACK:-${FOUND_175:-$HOME/.sushi/models/beamster/DeepSeek-V4.1-Flash-1017-Sushi-STQ-1.75bpw}}" elif [ -n "$FOUND_270" ] && [ "$RAM_GB" -ge 128 ] && [ -z "$STQ_PACK" ]; then echo "[dsv41-loop] Found existing 2.7-bpw STQ pack at $FOUND_270 (0 GB re-downloaded!)" STQ_REPO="beamster/DeepSeek-V4.1-Flash-1017-Sushi-STQ-2.7bpw" STQ_PACK="$FOUND_270" elif [ -n "$FOUND_175" ] && [ -z "$STQ_PACK" ]; then echo "[dsv41-loop] Found existing 1.75-bpw STQ pack at $FOUND_175 (0 GB re-downloaded!)" STQ_REPO="beamster/DeepSeek-V4.1-Flash-1017-Sushi-STQ-1.75bpw" STQ_PACK="$FOUND_175" else STQ_REPO="beamster/DeepSeek-V4.1-Flash-1017-Sushi-STQ-1.75bpw" STQ_PACK="${STQ_PACK:-$HOME/.sushi/models/beamster/DeepSeek-V4.1-Flash-1017-Sushi-STQ-1.75bpw}" fi # 3. Auto-tune memory & context flags: # - On standard 48/64/96/128/192/256 GB Macs, sushi's built-in `ram_default` planner automatically selects # the largest GiB budget that fits BOTH Metal's GPU working set AND live free RAM at startup! # - On 512 GB M3 Ultra (or non-standard RAM sizes outside sushi's 48..256 GB table), set explicit safe GiB. DEFAULT_CTX=36864 DEFAULT_NO_VISION=0 DEFAULT_NO_MTP=0 case "$RAM_GB" in 48|64|96|128|192|256) DEFAULT_BUDGET="auto" ;; *) if [ "$RAM_GB" -ge 380 ]; then DEFAULT_BUDGET=180 [[ "${STQ_REPO}${STQ_PACK}" == *"2.7bpw"* ]] && DEFAULT_BUDGET=260 elif [ "$RAM_GB" -ge 180 ]; then DEFAULT_BUDGET=135 else DEFAULT_BUDGET=$(( RAM_GB / 2 )) fi ;; esac if [ "$RAM_GB" -lt 96 ]; then DEFAULT_NO_VISION=1 fi if [ "$RAM_GB" -le 52 ]; then DEFAULT_CTX=24576 echo "[dsv41-loop] 48 GB Mac mode: enabling --no-vision & 24k context so sushi reserves maximum RAM for expert cache." echo " (Tip: quit heavy browsers/IDEs before serving; pass LOW_MEM=1 to also disable DSpark MTP and save 3.85 GB.)" fi mkdir -p "$INSTALL_DIR" "$BIN_LINK_DIR" # 4. Download loop adapter + sushi bundle (~611 MiB) and verify binary runs BEFORE pulling 300+ GB of weights! KIT_ROOT="$INSTALL_DIR/dsv41-loop-friend-eval-kit" if [ ! -f "$KIT_ROOT/adapters/r5a-000300/loop.json" ]; then echo "[dsv41-loop] Downloading Looped DeepSeek-V4.1 adapter + sushi bundle to $INSTALL_DIR..." curl -fL "$KIT_URL" | tar xz -C "$INSTALL_DIR" fi xattr -dr com.apple.quarantine "$KIT_ROOT/bin/darwin-arm64" 2>/dev/null || true # Keep bundled run_friend_eval and lcb_eval.py synced with latest main script improvements if curl -fsSL "$RAW_EVAL_URL" -o "$KIT_ROOT/run_friend_eval.tmp" 2>/dev/null && [ -s "$KIT_ROOT/run_friend_eval.tmp" ]; then mv "$KIT_ROOT/run_friend_eval.tmp" "$KIT_ROOT/run_friend_eval" chmod +x "$KIT_ROOT/run_friend_eval" else rm -f "$KIT_ROOT/run_friend_eval.tmp" fi if [ -d "$KIT_ROOT/bench_data" ] && curl -fsSL "$RAW_GRADER_URL" -o "$KIT_ROOT/bench_data/lcb_eval.py.tmp" 2>/dev/null && [ -s "$KIT_ROOT/bench_data/lcb_eval.py.tmp" ]; then mv "$KIT_ROOT/bench_data/lcb_eval.py.tmp" "$KIT_ROOT/bench_data/lcb_eval.py" else rm -f "$KIT_ROOT/bench_data/lcb_eval.py.tmp" fi SBIN_CHECK="${SUSHI_BIN:-$KIT_ROOT/bin/darwin-arm64/sushi/sushi}" if ! "$SBIN_CHECK" --version >/dev/null 2>&1; then echo "ERROR: $SBIN_CHECK failed to start on macOS ${MACOS_VER}. Ensure macOS 26.2+ is installed." >&2 exit 1 fi # 5. Ensure STQ base pack is present (skip completely if already downloaded!) if has_pack_files "$STQ_PACK"; then echo "[dsv41-loop] Verified existing STQ weights at $STQ_PACK — skipping download!" else # Pre-flight free disk check: STQ-1.75bpw is ~309 GiB on disk (~120 GiB transformer + 189 GiB NVMe-streamed Engram tables) NEED_GIB=330 [[ "$STQ_REPO" == *"2.7bpw"* ]] && NEED_GIB=450 CHECK_DIR="$STQ_PACK" while [ ! -d "$CHECK_DIR" ] && [ "$CHECK_DIR" != "/" ]; do CHECK_DIR="$(dirname "$CHECK_DIR")" done FREE_KB=$(df -Pk "$CHECK_DIR" 2>/dev/null | awk 'NR==2 {print $4}') FREE_GIB=$(( ${FREE_KB:-0} / 1048576 )) if [ "$FREE_GIB" -lt "$NEED_GIB" ] && [ "${SKIP_DISK_CHECK:-0}" != "1" ]; then echo "ERROR: Target drive ($CHECK_DIR) has ${FREE_GIB} GiB free, but $STQ_REPO requires ~${NEED_GIB} GiB on SSD" >&2 echo " (~120 GiB 1.75-bit weights + 189 GiB NVMe-streamed Engram lookup tables, which use 0 GB of RAM)." >&2 echo "" >&2 echo " To store weights on an external Thunderbolt / USB4 NVMe SSD instead, re-run with:" >&2 echo " STQ_PACK_DIR=/Volumes//dsv41-stq curl -fsSL https://raw.githubusercontent.com/jasontitus/dsv41-loop-eval/main/install_mac.sh | bash" >&2 exit 1 fi DEFAULT_175="$HOME/.sushi/models/beamster/DeepSeek-V4.1-Flash-1017-Sushi-STQ-1.75bpw" DEFAULT_270="$HOME/.sushi/models/beamster/DeepSeek-V4.1-Flash-1017-Sushi-STQ-2.7bpw" if [ "$STQ_PACK" = "$DEFAULT_175" ] || [ "$STQ_PACK" = "$DEFAULT_270" ]; then echo "[dsv41-loop] Downloading $STQ_REPO into ~/.sushi/models via native sushi pull (resumable, no Python needed)..." "$SBIN_CHECK" --no-update-check pull "$STQ_REPO" /dev/null 2>&1; then # Avoid macOS Xcode CLT GUI popup on fresh Macs by installing standalone uv if ! xcode-select -p >/dev/null 2>&1 && [ ! -x /opt/homebrew/bin/python3 ]; then echo "[dsv41-loop] Installing standalone uv for fast Hugging Face Xet download..." curl -LsSf https://astral.sh/uv/install.sh | sh export PATH="$HOME/.local/bin:$PATH" fi fi if command -v uv >/dev/null 2>&1; then uv venv -q --python 3.12 "$VENV" && uv pip install -q --python "$VENV/bin/python" "huggingface_hub[hf_xet]" else python3 -m venv "$VENV" && "$VENV/bin/pip" install -q --upgrade pip "huggingface_hub[hf_xet]" fi fi mkdir -p "$STQ_PACK" "$VENV/bin/python" - "$STQ_REPO" "$STQ_PACK" <<'PY' import sys from huggingface_hub import snapshot_download snapshot_download(repo_id=sys.argv[1], local_dir=sys.argv[2], max_workers=8) PY fi fi MODEL_ID="$(basename "$STQ_PACK")" # 6. Write reusable `dsv41-loop-serve` CLI wrapper LAUNCHER="$BIN_LINK_DIR/dsv41-loop-serve" cat > "$LAUNCHER" <