File size: 7,265 Bytes
45b798c
 
 
 
 
 
 
 
 
 
3898f6c
 
 
 
 
 
402c91a
45b798c
 
 
 
 
 
 
 
 
 
97f1921
 
 
 
 
 
45b798c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70f85ef
45b798c
 
70f85ef
 
 
45b798c
 
70f85ef
45b798c
 
 
70f85ef
45b798c
70f85ef
45b798c
 
 
70f85ef
de0ced6
 
 
 
402c91a
 
 
 
 
de0ced6
70f85ef
 
 
45b798c
 
 
402c91a
 
de0ced6
 
 
45b798c
70f85ef
45b798c
70f85ef
45b798c
 
70f85ef
 
45b798c
 
 
 
70f85ef
45b798c
70f85ef
 
 
45b798c
 
 
86b06f4
 
70f85ef
 
 
 
45b798c
 
3898f6c
45b798c
 
 
 
 
 
70f85ef
 
3898f6c
86b06f4
 
 
 
3898f6c
86b06f4
3898f6c
aeeba46
 
 
3898f6c
aeeba46
 
 
 
3898f6c
 
 
 
70f85ef
 
 
 
 
 
 
 
86b06f4
 
45b798c
86b06f4
3898f6c
 
 
70f85ef
 
 
 
 
 
45b798c
 
 
70f85ef
86b06f4
45b798c
 
 
70f85ef
 
 
 
45b798c
 
 
 
 
 
 
 
 
 
 
 
 
 
66ed84c
 
 
 
 
 
45b798c
 
66ed84c
45b798c
 
 
 
 
 
 
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/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