Files
alzeo/install-embeddings.sh
T

337 lines
20 KiB
Bash
Executable File

#!/usr/bin/env bash
# Install the embeddings lane: Perplexity's pplx-embed-v1-0.6b (texts, 1,024 dimensions) on the CPU, on a port of its own
# (30028), answering /v1/embeddings in the OpenAI shape, which the keepalive proxy relays to it when this lane serves
# (decisions 1323 and 1494 of Hasan, 2026-11; the promise of discussion #69), by this repo's own server since v1.61.0
# (decision 1644, 2026-12-01).
#
# ./install-embeddings.sh first install, or update in place
# ./install-embeddings.sh --no-smoke skip the proof at the end
# ./install-embeddings.sh --uninstall remove the unit and its code (the model and the image kept, said)
# ./install-embeddings.sh --units-only the unit and its code alone: no image, no model (install.sh, on demand)
# ./install-embeddings.sh --prepare its image and its model, with no sudo: the cockpit's
# first Load runs it (./prepare-lane.sh embeddings),
# idempotent (what the cache holds is not fetched again)
# ./install-embeddings.sh --fetch its model alone, into the Hugging Face cache, with no sudo
# ./install-embeddings.sh --stage FILE the unit for the model in the cache, written to FILE
# with no sudo: the tests read what a Load will start
#
# WHY IT IS A LANE BESIDE EVERY LANE. Its engine never touches the GPU, so it runs beside whichever lane serves, like the
# speech lane: no Conflicts= in its unit, and its Load from the cockpit stops nothing. The need it serves is an indexing
# service (discussion #69: one embedding at the start of a task, the queries of an agent beside it), for which the CPU's
# throughput is enough, and a GPU engine would contend with generation (docs/embed-lane.md, "The GPU path: not yet").
#
# WHY OUR OWN SERVER, AND NOT TEI. Hugging Face's text-embeddings-inference, which the model's card documents, served it in
# v1.60.0: its ORT backend ran the repository's ONNX export at 381 tokens/s on these cores, and neither of its backends uses
# their bf16 instructions. pplx_model.py is this repo's own module, written from the model's configuration and its weights'
# names and shapes, and pplx-server.py its server, on the routes, the batches and the turns of the Clipstral lane's
# (vectors_server.py). Their vectors are those of the repository's ONNX export, which wrote pplx-reference.json: the proof at
# the end of this script asks the lane for the same texts, on every box. The repository's Python is not fetched nor run.
#
# WHY THE 27B LANE'S IMAGE. Its torch (with oneDNN and Arm's Compute Library on the CPU), its tokenizers and safetensors are
# all the server imports, and it is the image of the lane a new box boots by default and of the Clipstral lane: nothing more
# to download on a box that has it.
#
# WHY NO KEY AND LOOPBACK ONLY. The only door to it from the network is the proxy, which checks the box's key on
# /v1/embeddings as on every client route. Programs on the box reach it directly on its port, with no key, as they do the
# speech lane's routes.
set -euo pipefail
# One spelling of the home, without a trailing slash, as install.sh says why: the cockpit's
# exact-argv sudoers is rendered from it and has to match the cockpit's own calls (2026-10-22).
while [ -n "${HOME:-}" ] && [ "$HOME" != / ] && [ "${HOME%/}" != "$HOME" ]; do HOME="${HOME%/}"; done; export HOME
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
UNIT=qwen38-embed.service
INSTALLED="${LANES_UNIT_DIR:-/etc/systemd/system}/$UNIT"
CONTAINER=qwen38-embed
die(){ printf '\n\033[1;31mERROR:\033[0m %s\n' "$*" >&2; exit 1; }
step(){ printf '\n\033[1;36m── %s\033[0m\n' "$*"; }
# Under sudo every path below moves to /root: the cache and the config the unit reads.
# install.sh refuses the same way, for the same reason.
if [ "$(id -u)" = "0" ] && [ "${ALLOW_ROOT:-0}" != "1" ]; then
die "run this as the user who will use the box, not as root (everything it installs is addressed from \$HOME)."
fi
SMOKE=1; ACTION=install; MODE=full
for a in "$@"; do case "$a" in
--no-smoke) SMOKE=0 ;;
--uninstall) ACTION=uninstall ;;
# The two halves of a lane installed on demand, as for the other lanes (install-speech.sh's
# shape): the unit first, the image and the model at the lane's first Load.
--units-only) [ "$MODE" = full ] || die "one mode at a time"; MODE=units ;;
--prepare) [ "$MODE" = full ] || die "one mode at a time"; MODE=prepare ;;
--fetch) [ "$MODE" = full ] || die "one mode at a time"; MODE=fetch ;;
--stage) [ "$MODE" = full ] || die "one mode at a time"; MODE=stage ;;
-h|--help) awk 'NR > 1 && !/^#/ { exit } NR > 1' "$0"; exit 0 ;;
*) if [ "$MODE" = stage ] && [ -z "${STAGE_OUT:-}" ]; then STAGE_OUT="$a"; else die "unknown option: $a"; fi ;;
esac; done
if [ "$MODE" = stage ] && [ -z "${STAGE_OUT:-}" ]; then die "--stage takes the file to write the unit to"; fi
CONFIG_DIR="$HOME/.config/qwen38"
# shellcheck source=lanes.sh
. "$HERE/lanes.sh"
# A 64 GB DGX Spark cannot hold this lane as measured (boxmem.py): no run of it beside Bolt's share of a 64 GB box, the
# rule of every lane never run there. Refused before anything is fetched; --uninstall still takes away what was put there.
[ "$ACTION" = uninstall ] || [ "$(box_mem_class)" != 64 ] || lane_fits embeddings || die "The pplx-embed v1 0.6B lane needs a 128 GB DGX Spark, and this box is a 64 GB one."
# A terminal opened before the account joined the docker group: the run goes on with it (lanes.sh).
docker_session bash "$HERE/${BASH_SOURCE[0]##*/}" "$@"
# The pins, at column 0 like install.sh's. check-pins.sh and the cockpit read them here.
# Perplexity's pplx-embed-v1-0.6b (MIT), a bidirectional Qwen3 0.6B in float32: 1,024-dimension vectors, mean pooling, no
# query or document prefix. The weights by their sha256 and the tokenizer by its sha256, read from the Hub's API at the
# pinned revision and from the files themselves. The repository's Python and its ONNX export are not fetched.
EMBED_MODEL="perplexity-ai/pplx-embed-v1-0.6b"
EMBED_REV="2c4d510dd4a732063c31a0f70193e35067b51fd8"
EMBED_FILE="model.safetensors=2c8d2f64f8268ccd5383b7f9bea8e660349aa6a151bd68a5a47f4c129f2a4974:2384233112"
EMBED_TOKENIZER="tokenizer.json=c6fb5c5bbba5fa5f8332edfb6d8aa67bd7fb3d75365b1765f108201698eaebf5:11422837"
# The 27B lane's SGLang image (install.sh's IMAGE, by its digest), for its torch on the CPU.
EMBED_IMAGE="lmsysorg/sglang@sha256:e3a4f06001a29542fc4a8638536d9ffc5eb9c9942b08603c09b19a56eae4a1ae"
# EMBED_MEMORY: the container's cgroup cap, --memory and --memory-swap equal: its own pages at the most, 1000 MiB (a text of 8,024
# tokens, 2026-12-02), and the 2274 MiB of its model's file, which the kernel may take back, plus 2 GiB, rounded up to
# 512 MiB.
# EMBED_CPUS, EMBED_THREADS: the ten A725 cores (2.8 GHz, lscpu: 0-4 and 10-14), a thread on each, never the X925
# the text engines' busy threads run on.
# EMBED_PRODUCTS: how the matrices are multiplied, bf16 on the cores' bf16 instructions or fp32 (docs/embed-lane.md has
# both, measured).
EMBED_MEMORY=5632m
# EMBED_ROOM_GIB: what the box has to have available before the lane starts: its cap, rounded up, and the 3 GiB under which
# the cockpit and the proxy stop the generations that run (MEM_FLOOR_GIB). Beside a lane that leaves the box less, a start
# would take what that lane's long prompts live on, and a GB10 whose memory runs out freezes whole: the unit's head refuses
# it, with the reason (found by the proof of v1.60.0).
EMBED_ROOM_GIB=9
# EMBED_AGAIN_GIB: what a start of systemd's own asks, after a death: the 1 GiB the lane holds (docs/embed-lane.md) and the 3.
EMBED_AGAIN_GIB=4
EMBED_CPUS=0-4,10-14
EMBED_THREADS=10
EMBED_PRODUCTS=bf16
# The model whole, 2.40 GB, rounded up: the room its fetch asks.
EMBED_GB=3
# The room the image's pull asks at its peak, in GiB, where the box lacks it (install-lfm25.sh's figure for the same image).
IMAGE_GB=48
# The lane's port, read back as install-transcribe.sh reads its (the unit is this lane's only
# record; the cockpit's Load re-renders, it does not invent). The proxy reads it there too,
# the --port of the installed unit, so a EMBED_PORT taken here is followed without telling it.
# No bind variable: without a key, this lane never listens beyond 127.0.0.1 (see WHY NO KEY).
installed_flag(){ # $1 the flag (--port): the value the installed unit gives it, its last word
# shellcheck disable=SC2086
{ grep -hoE -- "$1 [^ \\\\']+" "$INSTALLED" 2>/dev/null || true; } | tail -1 | awk '{print $2}'
}
PORT="${EMBED_PORT:-$(installed_flag --port)}"; PORT="${PORT:-30028}"
case "$PORT" in ''|*[!0-9]*) die "EMBED_PORT takes a number, got: $PORT" ;; esac
# The Hugging Face cache the unit mounts, read the way the lanes beside it read theirs
# (vllm-lane.sh's vlane_text_cache): this lane's own unit first, then the text lanes', so a box
# whose cache lives on another disk (docs/external-disk.md, HF_CACHE=...) keeps the model there.
TEXT_UNITS="${TEXT_UNITS:-$LANES_UNIT_DIR/qwen38-sglang.service $CONFIG_DIR/launch-flash.sh}"
HF_CACHE="${HF_CACHE:-$(unit_hf_cache "$INSTALLED" $TEXT_UNITS 2>/dev/null || true)}"; HF_CACHE="${HF_CACHE:-$HOME/.cache/huggingface}"
model_files(){ printf '%s\n' "$EMBED_FILE" "$EMBED_TOKENIZER"; }
# The lane's code, a copy of this repo's two files in a folder of its own named by their content: the unit names that
# folder, so an update that waits for the lane's Load leaves the code the unit in place runs as it was.
CODE_FILES=(pplx-server.py pplx_model.py vectors_server.py cpu_layers.py)
CODE_DIR="$CONFIG_DIR/embed/$(cd "$HERE" && cat "${CODE_FILES[@]}" | sha256sum | cut -c1-12)"
put_code(){
local f
mkdir -p "$CODE_DIR"
for f in "${CODE_FILES[@]}"; do cmp -s "$HERE/$f" "$CODE_DIR/$f" || install -m 644 "$HERE/$f" "$CODE_DIR/$f"; done
}
drop_old_code(){ # the folders neither the unit in place nor the one that waits names
local d
for d in "$CONFIG_DIR"/embed/*/; do
d="${d%/}"; [ -d "$d" ] && [ "$d" != "$CODE_DIR" ] || continue
grep -qsF "$d," "$INSTALLED" "$CONFIG_DIR/pending/$UNIT" || rm -rf "$d"
done
}
missing_gb(){
# fsize and miss, never size or need: lanes.sh's own size-of and need are arrays there, and a
# scalar of the same name in this file is SC2178's whole story (the review of 2026-11-19).
local f blob fsize miss=0 repo="$HF_CACHE/hub/models--${EMBED_MODEL//\//--}"
while read -r f; do
blob="$repo/blobs/${f##*=}"; blob="${blob%:*}"; fsize="${f##*:}"
if [ ! -f "$blob" ] || [ "$(stat -c %s "$blob" 2>/dev/null || echo 0)" != "$fsize" ]; then
miss=$(( miss + fsize ))
fi
done < <(model_files)
echo $(( (miss + 999999999) / 1000000000 ))
}
fetch_model(){
mkdir -p "$HF_CACHE"
[ -z "${HF_TOKEN:-}" ] || export HF_TOKEN
local files=()
mapfile -t files < <(model_files)
HF_HUB_DOWNLOAD_TIMEOUT=30 python3 -I "$HERE/gguf-fetch.py" "$HF_CACHE" "$EMBED_MODEL" "$EMBED_REV" \
"${files[@]}" \
|| die "the model of $EMBED_MODEL could not be fetched (the line above says why). Causes: no internet, Hugging Face throttling an unauthenticated download (set HF_TOKEN=<your token>), a pinned revision removed upstream, or a permission error in $HF_CACHE. Re-running resumes."
}
if [ "$ACTION" = uninstall ]; then
step "Removing the embeddings lane"
# The unit, and the boot with it: this lane never took the boot from the text lane (it runs
# beside it), so there is nothing to give back; the text lane keeps the boot it has.
if [ -f "$INSTALLED" ]; then
sudo systemctl disable --now "$UNIT" 2>/dev/null || true
sudo rm -f "$INSTALLED"; sudo systemctl daemon-reload
echo "unit removed"
fi
unit_pending_clear "$INSTALLED" # an update that waited for its Load: the cockpit went on saying so
docker rm -f "$CONTAINER" >/dev/null 2>&1 || true
pin_untag embeddings
rm -rf "${CONFIG_DIR:?}/embed"
echo "the image stays until a docker image prune (docker rmi it yourself if nothing else uses it)"
d="$HF_CACHE/hub/models--${EMBED_MODEL//\//--}"
[ -d "$d" ] && echo "the model is kept in $d ($(du -sh "$d" 2>/dev/null | cut -f1)); delete it yourself if you want the space"
exit 0
fi
for f in "${CODE_FILES[@]}" vectors-check.py pplx-reference.json "$UNIT.template"; do
[ -f "$HERE/$f" ] || die "$f is missing next to this script (a partial copy?): run it from a whole clone of the repository."
done
# The unit, rendered for this box: written to $1.
render_unit(){
sed -e "s|__USER__|$(id -un)|g" -e "s|__GROUP__|$(id -gn "$(id -un)")|g" -e "s|__HOME__|$HOME|g" \
-e "s|__IMAGE__|$EMBED_IMAGE|g" -e "s|__HF_CACHE__|$HF_CACHE|g" \
-e "s|__EMBED_MODEL__|$EMBED_MODEL|g" -e "s|__EMBED_MODEL_DASH__|${EMBED_MODEL//\//--}|g" \
-e "s|__EMBED_REV__|$EMBED_REV|g" -e "s|__EMBED_PORT__|$PORT|g" \
-e "s|__EMBED_MEMORY__|$EMBED_MEMORY|g" -e "s|__EMBED_ROOM_GIB__|$EMBED_ROOM_GIB|g" -e "s|__EMBED_AGAIN_GIB__|$EMBED_AGAIN_GIB|g" -e "s|__EMBED_CPUS__|$EMBED_CPUS|g" \
-e "s|__EMBED_THREADS__|$EMBED_THREADS|g" -e "s|__EMBED_PRODUCTS__|$EMBED_PRODUCTS|g" \
-e "s|__EMBED_CODE__|$CODE_DIR|g" \
"$HERE/$UNIT.template" > "$1"
if grep -q '__[A-Z][A-Z0-9_]*__' "$1"; then die "the unit template still holds an unsubstituted placeholder: $(grep -o '__[A-Z][A-Z0-9_]*__' "$1" | sort -u | tr '\n' ' ')"; fi
}
# The one part that needs root: the unit. A lane loaded on demand takes the new unit whenever
# it changes, as the transcribe lane does (lanes.sh's unit_put keeps the note for a serving lane).
install_service(){
local was; was="$(installed_flag --port)"
step "Service"
RENDER="$(mktemp)"; trap 'rm -f "$RENDER"' EXIT
put_code
render_unit "$RENDER"
put="$(unit_put "$RENDER" "$INSTALLED")"
case "$put" in
pending) echo "its new unit waits for its Load, the one in place kept: it lacks $(unit_pending_says "$INSTALLED") (decision 1408)" ;;
same) echo "unit unchanged" ;;
deps) sudo systemctl daemon-reload
echo "unit updated: only the lines naming the other lanes changed, which the lane does not read" ;;
*) sudo systemctl daemon-reload
echo "unit installed at $INSTALLED" ;;
esac
[ "$put" = pending ] || beside_port_moved "$UNIT" "$was" "$PORT"
drop_old_code
}
# --stage: the unit for the model in the cache, with no sudo.
if [ "$MODE" = stage ]; then
render_unit "$STAGE_OUT"
exit 0
fi
# The room the model needs, checked before its first byte, and the fetch itself: what the
# --fetch mode runs, and what --prepare runs after the image, so the cockpit's first Load
# poses the model the unit's preflight will ask for (the blind review of 2026-11-20: a Load
# that posed the image alone left the start to die at 78 on a box without the model).
fetch_model_with_room(){
need_gb="$(missing_gb)"
if [ "$need_gb" -gt 0 ]; then
need_gib=$(( (need_gb * 1000000000 + 1073741823) / 1073741824 )); free_gib="$(free_gb_at "$HF_CACHE")"
[ "${free_gib:-0}" -ge "$need_gib" ] \
|| die "${free_gib:-?} GiB free under HF_CACHE=$HF_CACHE, the model needs about $need_gib GiB more. Free some space first: nothing was downloaded."
fi
fetch_model
}
# --fetch: the model, for a terminal, with no sudo, the room checked first.
if [ "$MODE" = fetch ]; then
fetch_model_with_room
exit 0
fi
step "1/4 Preflight ($EMBED_MODEL @ ${EMBED_REV:0:12})"
case "$(uname -m)" in aarch64|arm64) : ;; *) die "this lane is verified on the DGX Spark's ARM64 GB10 only (this box is $(uname -m))." ;; esac
if [ "$MODE" = units ]; then
echo "OK: $(uname -m); the image and the model come with the lane's first Load"
install_service
step "Done: the embeddings lane is installed; its first Load prepares it"
echo " the cockpit: Load on pplx-embed v1 0.6B in the Lanes view pulls its image (the 27B lane's, already"
echo " there on a box that has it) and fetches its model (about $EMBED_GB GB), then enables it at"
echo " boot and starts it"
echo " a terminal : ./install-embeddings.sh --prepare (the image and the model, no sudo)"
exit 0
fi
step "2/4 Image (${EMBED_IMAGE:0:40}...)"
if docker image inspect "$EMBED_IMAGE" >/dev/null 2>&1; then
echo "already here"
else
# The room the pull needs, checked before its first byte, the way the model's own fetch checks
# its own (vllm-lane.sh's vlane_room): Docker's disk, not the cache's, and an image already
# here needs none (the review of 2026-11-19: this check was missing).
DOCKER_ROOT="$(docker info --format '{{.DockerRootDir}}' 2>/dev/null || echo /var/lib/docker)"
free_gib="$(free_gb_at "$DOCKER_ROOT")"
[ "${free_gib:-0}" -ge "$IMAGE_GB" ] \
|| die "${free_gib:-?} GiB free under Docker's root ($DOCKER_ROOT), the image needs about $IMAGE_GB GiB. Free some space first: nothing was pulled."
docker pull "$EMBED_IMAGE" \
|| die "docker pull of $EMBED_IMAGE failed. Causes: no internet, a Docker Hub rate limit (retry in a few minutes, or docker login), or the digest removed upstream."
fi
pin_tag embeddings "$EMBED_IMAGE"
unpin_stale embeddings "$EMBED_IMAGE" || true
if [ "$MODE" = prepare ]; then
step "3/4 Model ($EMBED_MODEL @ ${EMBED_REV:0:12}, resumable)"
fetch_model_with_room
step "Done: the embeddings lane's image and model are ready; its Load starts it"
exit 0
fi
step "3/4 Model ($EMBED_MODEL @ ${EMBED_REV:0:12}, resumable)"
fetch_model_with_room
install_service
if [ "$SMOKE" -eq 0 ]; then step "Done (proof skipped)"; exit 0; fi
step "4/4 Proving it embeds (the vectors of the model's ONNX export, then the lane as it was)"
# This lane runs beside whichever lane serves: nothing is stopped for the proof, and the lane
# is left as it was found.
WAS_ACTIVE=0; systemctl is-active --quiet "$UNIT" 2>/dev/null && WAS_ACTIVE=1
KEEP=$WAS_ACTIVE
cleanup(){ rm -f "${RENDER:-}"; [ "$KEEP" -eq 1 ] || sudo systemctl stop "$UNIT" 2>/dev/null || true; }
trap cleanup EXIT
# A lane that serves older files than the ones this run put in place restarts for the proof (install-embeddings.sh says
# why). Its files: the unit, its code, and the model's as its snapshot names them.
SNAP="$HF_CACHE/hub/models--${EMBED_MODEL//\//--}/snapshots/$EMBED_REV"
PROVE_FILES=("$INSTALLED" "${CODE_FILES[@]/#/$CODE_DIR/}")
while read -r f; do PROVE_FILES+=("$SNAP/${f%%=*}"); done < <(model_files)
if [ "$WAS_ACTIVE" -eq 1 ] && lane_serves_older "$UNIT" "${PROVE_FILES[@]}"; then
echo "the lane runs older files than the ones installed now (its unit, its code or its model): it restarts to prove these"
sudo systemctl restart "$UNIT"
elif [ "$WAS_ACTIVE" -eq 0 ]; then
sudo systemctl start "$UNIT"
fi
SINCE="$(date '+%Y-%m-%d %H:%M:%S')"
r0="$(systemctl show -p NRestarts --value "$UNIT" 2>/dev/null | tr -dc '0-9')"
ready=0
for i in $(seq 1 45); do
curl -fsS -m 3 "http://127.0.0.1:$PORT/health" >/dev/null 2>&1 && { ready=1; break; }
if [ "$(systemctl is-active "$UNIT" || true)" = failed ] \
|| [ "$(systemctl show -p NRestarts --value "$UNIT" 2>/dev/null | tr -dc '0-9')" -gt "${r0:-0}" ]; then
{ sudo journalctl -u "$UNIT" --since "$SINCE" --no-pager || true; } | tail -40
die "the lane died while starting; the journal above says why."
fi
[ $((i % 5)) -eq 0 ] && echo " still loading... ($((i * 4)) s)"
sleep 4
done
[ "$ready" -eq 1 ] || { { sudo journalctl -u "$UNIT" --since "$SINCE" --no-pager || true; } | tail -40; die "the lane did not answer /health within 3 min. The journal above says why."; }
echo "up after ~$(( (i - 1) * 4 )) s"
python3 -I "$HERE/vectors-check.py" "http://127.0.0.1:$PORT" "$HERE/pplx-reference.json" \
|| { { sudo journalctl -u "$UNIT" --since "$SINCE" --no-pager || true; } | tail -20; die "what came back is not what the published model gives (the lines above): the lane is installed, and not proved."; }
step "Done: the embeddings lane is installed and proved it embeds, on 127.0.0.1:$PORT"
echo " through the proxy: POST /v1/embeddings on :30001 with the box's key (docs/embed-lane.md)"
echo " on this box : curl http://127.0.0.1:$PORT/v1/embeddings -d '{\"input\":[\"Hello\"],\"model\":\"pplx-embed-v1-0.6b\"}'"
if [ "$KEEP" -eq 1 ]; then
echo " it is running, and stays running"
else
echo " start/stop : sudo systemctl start $UNIT (and stop; its Load from the cockpit starts it and enables it at boot)"
fi