LEM-Eval / lem-eval.sh
Snider
feat: generative toxigen eval with silent exit detection
402c91a
#!/usr/bin/env bash
# lem-eval.sh — the worker service script
#
# Iterates the targets owned by this hostname (from targets.yaml), runs one
# canon-advance slice for each, and pushes results to both the target model
# repo and the lthn/LEM-benchmarks aggregator.
#
# Usage:
# ./lem-eval.sh once # one pass over all owned targets, exit
# ./lem-eval.sh maintain # git pull LEM-Eval + lem-benchmarks, no eval
# ./lem-eval.sh loop # once, sleep 10s, repeat (for systemd-style runs)
#
# Environment filters (comma-separated):
# LEM_TYPES=gguf # only run gguf targets (auto-detected if unset)
# LEM_NAMES=lemer # only run targets named "lemer"
# LEM_NAMES=lemer,lemma # run both lemer and lemma targets
# LEM_TASK=toxigen # override task (default: from targets.yaml)
#
# Designed for cron:
# */30 * * * * cd /home/x/LEM-Eval && flock -n .lock ./lem-eval.sh once
# 15 * * * * cd /home/x/LEM-Eval && ./lem-eval.sh maintain
set -euo pipefail
HERE="$(cd "$(dirname "$0")" && pwd)"
cd "$HERE"
# Cron runs with a minimal PATH — make sure uv + user-local bins are
# available regardless of how this script is invoked.
if [[ ":$PATH:" != *":$HOME/.local/bin:"* ]]; then
export PATH="$HOME/.local/bin:$PATH"
fi
MODE="${1:-once}"
HOST="$(hostname)"
LEM_BENCHMARKS_DIR="$HERE/lem-benchmarks"
WORKSPACES="$HERE/workspaces"
log() { printf "\033[1;32m[lem-eval %s]\033[0m %s\n" "$(date -u +%H:%M:%S)" "$*"; }
# --- maintain: pull LEM-Eval + lem-benchmarks clones ----------------------
maintain() {
log "git pull LEM-Eval"
git pull --ff-only || log "LEM-Eval pull failed (non-fatal)"
if [[ -d "$LEM_BENCHMARKS_DIR/.git" ]]; then
log "git pull lem-benchmarks"
(cd "$LEM_BENCHMARKS_DIR" && git pull --ff-only) || log "lem-benchmarks pull failed (non-fatal)"
fi
}
# --- run one slice for a single (name, type, quant) triple --------------
run_target() {
local name="$1" ttype="$2" quant="$3" repo="$4"
local workspace="$WORKSPACES/$repo"
local label="$name/$ttype/$quant"
if [[ ! -d "$workspace/.git" ]]; then
log "[$label] workspace $repo not cloned, skipping (run install.sh first)"
return 0
fi
log "[$label] git pull $repo"
(cd "$workspace" && git pull --ff-only) || {
log "[$label] pull failed, skipping"
return 0
}
log "[$label] running eval.py..."
# || true so a single target failure (missing model, probe crash,
# network hiccup) doesn't cascade via set -euo pipefail and kill
# the outer loop. Each target is independent — the next one in the
# rotation should still get its chance this pass.
local task_args=()
if [[ -n "${LEM_TASK:-}" ]]; then
task_args=(--task "$LEM_TASK")
fi
if ! uv run --script eval.py \
--target "$name" \
--type "$ttype" \
--quant "$quant" \
--eval-results-dir "$workspace/.eval_results" \
--lem-benchmarks-dir "$LEM_BENCHMARKS_DIR" \
--n-questions 1 \
--rounds 8 \
"${task_args[@]}"; then
log "[$label] eval.py failed — continuing with next target"
return 0
fi
# Commit + push to target model repo (canon advance for this quant)
if (cd "$workspace" && git status --short | grep -q .); then
log "[$label] committing canon update to $repo"
(cd "$workspace" \
&& git add .eval_results/ \
&& git commit -m "eval: ${name}/${ttype}/${quant} advance ($(date -u +%Y-%m-%dT%H:%M:%SZ))" \
&& git push) || log "[$label] model repo push failed (non-fatal)"
fi
# Commit + push to lem-benchmarks for the aggregator row
if (cd "$LEM_BENCHMARKS_DIR" && git status --short | grep -q .); then
log "[$label] committing canon update to lem-benchmarks"
(cd "$LEM_BENCHMARKS_DIR" \
&& git add "results/$name/" \
&& git commit -m "eval: ${name}/${ttype}/${quant} advance ($(date -u +%Y-%m-%dT%H:%M:%SZ))" \
&& git push) || log "[$label] lem-benchmarks push failed (non-fatal)"
fi
}
# --- one pass over all targets this worker can run -----------------------
#
# Iterates every (name, type, quant) triple whose type matches this
# worker's capability set — $LEM_TYPES env var overrides, otherwise
# capability probe (mlx on Apple Silicon with mlx_lm importable, gguf
# everywhere the openai client is importable).
once() {
log "host=$HOST types=${LEM_TYPES:-auto} names=${LEM_NAMES:-all} mode=once"
if [[ ! -d "$LEM_BENCHMARKS_DIR/.git" ]]; then
log "lem-benchmarks not cloned — run ./install.sh first"
exit 1
fi
# Emit one pipe-separated line per runnable target: name|type|quant|repo
local triples
triples=$(LEM_TYPES="${LEM_TYPES:-}" LEM_NAMES="${LEM_NAMES:-}" python3 - <<'PY'
import os, platform, yaml
types_env = os.environ.get("LEM_TYPES", "").strip()
if types_env:
allowed_types = set(t.strip() for t in types_env.split(","))
else:
allowed_types = set()
if platform.system() == "Darwin":
try:
import mlx_lm # noqa: F401
allowed_types.add("mlx")
except ImportError:
pass
if any(os.path.exists(os.path.join(p, "ollama"))
for p in os.environ.get("PATH", "").split(":")):
allowed_types.add("gguf")
names_env = os.environ.get("LEM_NAMES", "").strip()
allowed_names = set(n.strip() for n in names_env.split(",")) if names_env else None
def derive_repo_id(this_ref):
if this_ref.startswith("hf.co/"):
base = this_ref[len("hf.co/"):]
if ":" in base:
base = base.split(":", 1)[0]
return base
return this_ref
with open("targets.yaml") as f:
cfg = yaml.safe_load(f)
for t in cfg.get("targets", []):
if t.get("type") not in allowed_types:
continue
if allowed_names and t["name"] not in allowed_names:
continue
name = t["name"]
ttype = t["type"]
quant = t.get("quant", "")
repo = derive_repo_id(t["this"])
print(f"{name}|{ttype}|{quant}|{repo}")
PY
)
if [[ -z "$triples" ]]; then
log "no targets match this worker's types (set LEM_TYPES to override)"
exit 0
fi
while IFS='|' read -r name ttype quant repo; do
[[ -z "$name" ]] && continue
run_target "$name" "$ttype" "$quant" "$repo"
done <<< "$triples"
log "pass complete"
}
# --- main dispatch --------------------------------------------------------
case "$MODE" in
once)
once
;;
maintain)
maintain
;;
loop)
# Continuous worker: run one pass after another with no long sleep.
# Each pass iterates every runnable target on this host and commits
# + pushes after each target's eval, so the canon on HF stays in
# lockstep with the local state. A brief breather prevents a hot
# loop if something misconfigures — 10 seconds is enough to not
# thrash cron/logs but short enough that the machine stays busy.
while true; do
once
sleep 10
done
;;
*)
echo "usage: $0 {once|maintain|loop}" >&2
exit 1
;;
esac