From 0cb886acf7cf402bea7b3271d9b159fd5c62ff29 Mon Sep 17 00:00:00 2001 From: alkatrazz Date: Fri, 19 Jun 2026 20:44:25 +0200 Subject: [PATCH] =?UTF-8?q?feat(llama=5Fserver):=20watchdog=20auto-r=C3=A9?= =?UTF-8?q?paration=20du=20wedge=20ROCm?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Service systemd local sur gpu-01 qui sonde une vraie génération sur :1234 (pas juste /health, qui ment quand le slot d'inférence est figé). Sur N échecs consécutifs → systemctl restart llama-server en local (root, sans SSH/sudo distant). Gère le 503 "Loading model" post-restart sans le compter comme échec. Complète le llm-heartbeat (rôle litellm) : le heartbeat empêche la péremption par inactivité de la connexion ; le watchdog répare le figeage du serveur lui-même. Co-Authored-By: Claude Opus 4.8 --- admin/incidents-llm-gpu.md | 28 ++++++++-- ansible/roles/llama_server/README.md | 22 ++++++++ ansible/roles/llama_server/defaults/main.yml | 12 +++++ ansible/roles/llama_server/handlers/main.yml | 5 ++ ansible/roles/llama_server/tasks/main.yml | 28 ++++++++++ .../templates/llama-watchdog.service.j2 | 15 ++++++ .../templates/llama-watchdog.sh.j2 | 52 +++++++++++++++++++ 7 files changed, 157 insertions(+), 5 deletions(-) create mode 100644 ansible/roles/llama_server/templates/llama-watchdog.service.j2 create mode 100644 ansible/roles/llama_server/templates/llama-watchdog.sh.j2 diff --git a/admin/incidents-llm-gpu.md b/admin/incidents-llm-gpu.md index 77b10d8..c1b2260 100644 --- a/admin/incidents-llm-gpu.md +++ b/admin/incidents-llm-gpu.md @@ -144,9 +144,27 @@ systemctl status llm-heartbeat ; journalctl -u llm-heartbeat -f # v ⚠️ Le heartbeat appelle `hermes-default` en continu → si on bascule cet alias sur **Claude** (API facturée), mettre `llm_heartbeat_enabled: false`. -### Reste à faire (durci, mais pas zéro-risque) +### Fix racine #2 — watchdog gpu-01 (déployé) -Le heartbeat empêche la *péremption par inactivité*. Si `llama-server` **se fige** carrément -(wedge ROCm), le heartbeat le **détecte** (échecs loggués) mais ne le **répare pas** encore. -Évolution possible : sur N échecs consécutifs du heartbeat → restart auto de `llama-server` -**et** `litellm` (watchdog actif). Pour l'instant : détection + alerte manuelle. +Le heartbeat empêche la *péremption par inactivité*, mais si `llama-server` **se fige** +carrément (wedge ROCm), il le **détecte** sans le **réparer**. C'est désormais couvert par le +`llama-watchdog` (rôle `llama_server`) : un service systemd **local sur gpu-01** qui sonde une +**vraie génération** sur `:1234` toutes les `llama_watchdog_interval` s (défaut 20 s, +`max_tokens:1`, `/no_think`). Sur `llama_watchdog_failures` échecs consécutifs (défaut 3) → +`systemctl restart llama-server` **en local** (tourne en root, ni SSH ni sudo distant), puis +cooldown jusqu'à ce que la génération remarche (gère le `503 "Loading model"` du rechargement +VRAM sans le compter comme échec). + +```bash +ansible-playbook -i inventory.yml playbooks/gpu-01.yml --tags llama_server # déploie le watchdog +systemctl status llama-watchdog ; journalctl -u llama-watchdog -f # vérifier +``` + +Pourquoi sur gpu-01 (et pas piloté depuis storage-01 via le heartbeat) : le restart est +**local** → pas de SSH/sudo inter-hôtes, répare même si LiteLLM ou le réseau cluster est en +vrac, et sonde au plus près du serveur figé. + +**Défense en profondeur (3 couches)** : +1. `llm-heartbeat` (storage-01, rôle `litellm`) — garde la connexion chaude (anti-péremption). +2. `request_timeout: 20` + `num_retries: 2` (LiteLLM) + retry STT-server — bornent le blip. +3. `llama-watchdog` (gpu-01, rôle `llama_server`) — répare le wedge ROCm à la source. diff --git a/ansible/roles/llama_server/README.md b/ansible/roles/llama_server/README.md index 8eb2993..c4f1066 100644 --- a/ansible/roles/llama_server/README.md +++ b/ansible/roles/llama_server/README.md @@ -12,6 +12,28 @@ Compile llama.cpp (backend HIP/ROCm) et déploie le service **llama-server GPU** `llama-embed` servant un modèle d'embedding spécialisé (ex. `nomic-embed-text`) sur `:1238`, GPU. Télécharge le GGUF depuis `llama_embed_model_url` si absent. Partage le binaire et le GPU avec `:1234` (modèle ~300 Mo VRAM). +- **Watchdog** (optionnel, `llama_watchdog_enabled`, défaut `true`) : service systemd + `llama-watchdog` qui sonde une **vraie génération** sur `:1234` toutes les + `llama_watchdog_interval` s. Sur `llama_watchdog_failures` échecs consécutifs → + `systemctl restart llama-server` **en local** (tourne en root, pas de SSH). Répare + automatiquement le wedge ROCm où `/health` reste vert mais l'inférence est morte + (cf. `admin/incidents-llm-gpu.md`). Logs : `journalctl -u llama-watchdog`. + +## Watchdog — variables + +| Variable | Défaut | +|---|---| +| `llama_watchdog_enabled` | `true` | +| `llama_watchdog_port` | `{{ llama_server_port }}` (1234) | +| `llama_watchdog_model` | `{{ llama_model_alias }}` (`qwen3-8b`) | +| `llama_watchdog_interval` | `20` (s entre deux sondes) | +| `llama_watchdog_timeout` | `15` (s max par sonde) | +| `llama_watchdog_failures` | `3` (échecs consécutifs avant restart) | + +> Complémentaire au `llm-heartbeat` du rôle `litellm` : le heartbeat empêche la +> *péremption par inactivité* de la connexion LiteLLM↔llama-server ; le watchdog répare le +> *figeage* du llama-server lui-même. Le watchdog gère le `503 "Loading model"` post-restart +> (rechargement VRAM) sans le compter comme échec. ## Variables principales diff --git a/ansible/roles/llama_server/defaults/main.yml b/ansible/roles/llama_server/defaults/main.yml index 2a85ded..91b8464 100644 --- a/ansible/roles/llama_server/defaults/main.yml +++ b/ansible/roles/llama_server/defaults/main.yml @@ -25,3 +25,15 @@ llama_embed_model_alias: "nomic-embed-text" llama_embed_ctx_size: 2048 llama_embed_n_gpu_layers: 99 llama_embed_pooling: "mean" + +# Watchdog — auto-réparation du llama-server figé (wedge ROCm gfx1031). +# Sonde une VRAIE génération sur :1234 (pas seulement /health, qui ment quand le +# slot d'inférence est bloqué — cf. admin/incidents-llm-gpu.md). Sur N échecs +# consécutifs → `systemctl restart llama-server` en local (tourne en root). +llama_watchdog_enabled: true +llama_watchdog_port: "{{ llama_server_port }}" +llama_watchdog_model: "{{ llama_model_alias }}" +llama_watchdog_service: "llama-server" +llama_watchdog_interval: 20 # s entre deux sondes +llama_watchdog_timeout: 15 # s max par sonde (génération triviale) +llama_watchdog_failures: 3 # échecs consécutifs avant restart diff --git a/ansible/roles/llama_server/handlers/main.yml b/ansible/roles/llama_server/handlers/main.yml index bb35846..a7f7609 100644 --- a/ansible/roles/llama_server/handlers/main.yml +++ b/ansible/roles/llama_server/handlers/main.yml @@ -12,3 +12,8 @@ ansible.builtin.systemd: name: llama-embed state: restarted + +- name: Restart llama-watchdog + ansible.builtin.systemd: + name: llama-watchdog + state: restarted diff --git a/ansible/roles/llama_server/tasks/main.yml b/ansible/roles/llama_server/tasks/main.yml index 125c7b9..120e825 100644 --- a/ansible/roles/llama_server/tasks/main.yml +++ b/ansible/roles/llama_server/tasks/main.yml @@ -123,3 +123,31 @@ state: enabled immediate: true when: llama_embed_enabled + +# --- Watchdog auto-réparation (wedge ROCm) — optionnel ------------------------ + +- name: Deploy llama-watchdog script + ansible.builtin.template: + src: llama-watchdog.sh.j2 + dest: /usr/local/bin/llama-watchdog + mode: '0755' + notify: Restart llama-watchdog + when: llama_watchdog_enabled + +- name: Deploy llama-watchdog service + ansible.builtin.template: + src: llama-watchdog.service.j2 + dest: /etc/systemd/system/llama-watchdog.service + mode: '0644' + notify: + - Reload systemd + - Restart llama-watchdog + when: llama_watchdog_enabled + +- name: Enable llama-watchdog service + ansible.builtin.systemd: + name: llama-watchdog + enabled: true + daemon_reload: true + state: started + when: llama_watchdog_enabled diff --git a/ansible/roles/llama_server/templates/llama-watchdog.service.j2 b/ansible/roles/llama_server/templates/llama-watchdog.service.j2 new file mode 100644 index 0000000..7de567b --- /dev/null +++ b/ansible/roles/llama_server/templates/llama-watchdog.service.j2 @@ -0,0 +1,15 @@ +[Unit] +Description=llama-watchdog (auto-réparation du llama-server figé — ROCm gfx1031) +After=llama-server.service +Wants=llama-server.service + +[Service] +Type=simple +User=root +ExecStart=/usr/local/bin/llama-watchdog +Restart=always +RestartSec=10 +SyslogIdentifier=llama-watchdog + +[Install] +WantedBy=multi-user.target diff --git a/ansible/roles/llama_server/templates/llama-watchdog.sh.j2 b/ansible/roles/llama_server/templates/llama-watchdog.sh.j2 new file mode 100644 index 0000000..0858b37 --- /dev/null +++ b/ansible/roles/llama_server/templates/llama-watchdog.sh.j2 @@ -0,0 +1,52 @@ +#!/usr/bin/env bash +# llama-watchdog — auto-réparation du llama-server figé (instabilité ROCm gfx1031). +# +# Le piège (cf. admin/incidents-llm-gpu.md) : un llama-server wedgé répond encore à +# /health et /v1/models, mais la GÉNÉRATION est morte. On sonde donc par une VRAIE +# génération (max_tokens:1, /no_think). Sur N échecs consécutifs → restart local. +# +# Tourne en root (service systemd) → peut `systemctl restart` en local, sans SSH ni sudo +# distant. Déployé/géré par le rôle Ansible `llama_server`. +set -u + +URL="http://127.0.0.1:{{ llama_watchdog_port }}/v1/chat/completions" +MODEL="{{ llama_watchdog_model }}" +SERVICE="{{ llama_watchdog_service }}" +INTERVAL="{{ llama_watchdog_interval }}" +TIMEOUT="{{ llama_watchdog_timeout }}" +THRESHOLD="{{ llama_watchdog_failures }}" + +payload="{\"model\":\"${MODEL}\",\"messages\":[{\"role\":\"user\",\"content\":\"ping /no_think\"}],\"max_tokens\":1}" + +probe() { # echo le code HTTP ; "000" si pas de réponse (timeout/refus) + curl -s -m "$TIMEOUT" -o /tmp/llama-watchdog.body -w '%{http_code}' \ + "$URL" -H 'Content-Type: application/json' -d "$payload" 2>/dev/null || echo "000" +} + +fails=0 +while true; do + code=$(probe) + + if [ "$code" = "200" ]; then + fails=0 + elif [ "$code" = "503" ] && grep -q "Loading model" /tmp/llama-watchdog.body 2>/dev/null; then + # Rechargement VRAM (juste après un restart) → ni échec ni succès : on patiente. + : + else + fails=$((fails + 1)) + echo "watchdog: probe KO (HTTP ${code}) — ${fails}/${THRESHOLD}" + if [ "$fails" -ge "$THRESHOLD" ]; then + echo "watchdog: ${SERVICE} figé (${THRESHOLD} échecs consécutifs) → restart" + systemctl restart "$SERVICE" + echo "watchdog: restart émis ; attente du rechargement du modèle…" + # Cooldown : attendre que la génération remarche (couvre mort de l'instance + load VRAM). + for _ in $(seq 1 30); do + sleep 5 + [ "$(probe)" = "200" ] && { echo "watchdog: ${SERVICE} de nouveau réactif"; break; } + done + fails=0 + fi + fi + + sleep "$INTERVAL" +done