Repository navigation
Expand file tree
/
Copy pathcli.sh
More file actions
executable file
·66 lines (61 loc) · 1.95 KB
/
Copy pathcli.sh
File metadata and controls
executable file
·66 lines (61 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env bash
set -euo pipefail
# Resolve the requested backend in precedence order:
# 1) LLAMA_CPP_BACKEND env var (per-invocation override)
# 2) snap config (snap set llama-cpp backend=…)
# 3) default: auto
resolve_request() {
if [ -n "${LLAMA_CPP_BACKEND:-}" ]; then
echo "$LLAMA_CPP_BACKEND"; return
fi
local cfg
cfg="$(snapctl get backend 2>/dev/null || true)"
if [ -n "$cfg" ]; then
echo "$cfg"; return
fi
echo "auto"
}
choose_backend() {
local req; req="$(resolve_request)"
case "$req" in
cpu)
echo ""; return ;;
hip|cuda|vulkan|opencl)
if [ ! -f "$SNAP_COMPONENTS/$req/lib/ggml/backends/libggml-$req.so" ]; then
echo "llama-cpp: backend='$req' selected but the $req component is not installed." >&2
echo " Install it: sudo snap install --devmode llama-cpp+$req" >&2
echo " Or change: sudo snap set llama-cpp backend=auto" >&2
exit 2
fi
echo "$req"; return ;;
auto)
for c in hip cuda vulkan opencl; do
if [ -f "$SNAP_COMPONENTS/$c/lib/ggml/backends/libggml-$c.so" ]; then
echo "$c"; return
fi
done
echo ""; return ;;
*)
echo "llama-cpp: invalid backend='$req' (use cpu|hip|cuda|vulkan|opencl|auto)" >&2
exit 2 ;;
esac
}
backend="$(choose_backend)"
if [ -n "$backend" ]; then
export GGML_BACKEND_PATH="$SNAP_COMPONENTS/$backend/lib/ggml/backends/libggml-$backend.so"
export LD_LIBRARY_PATH="$SNAP_COMPONENTS/$backend/lib:${LD_LIBRARY_PATH:-}"
fi
if [ $# -eq 0 ]; then
echo "Usage: llama-cpp <subcommand> [args...]"
echo "Example: llama-cpp cli --help"
echo
echo "Available subcommands:"
ls "$SNAP/bin/" | sed -n 's/^llama-/ /p'
echo
echo "Backend: ${backend:-cpu}"
echo " Configure persistently: sudo snap set llama-cpp backend={cpu,hip,cuda,vulkan,opencl,auto}"
echo " Override per-run: LLAMA_CPP_BACKEND=... llama-cpp ..."
exit 1
fi
cmd="$1"; shift
exec "$SNAP/bin/llama-$cmd" "$@"