-
Notifications
You must be signed in to change notification settings - Fork 0
355 lines (328 loc) · 15.8 KB
/
Copy pathtrain.yml
File metadata and controls
355 lines (328 loc) · 15.8 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
name: AI Training Pipeline
# ─────────────────────────────────────────────────────────────────────────────
# Triggers
# ─────────────────────────────────────────────────────────────────────────────
on:
# Run every day at 02:00 UTC (uses defaults below).
schedule:
- cron: "25 16 * * *"
# Fully configurable manual run.
workflow_dispatch:
inputs:
games:
description: "Number of games to play"
required: false
default: "5"
type: string
llm:
description: "LLM provider (AI student)"
required: false
default: "gemini"
type: choice
options:
- gemini
- openai
- claude
model:
description: "Model name override (leave blank for provider default)"
required: false
default: ""
type: string
skill:
description: "Starting Stockfish skill level (0 = weakest ... 20 = Grandmaster)"
required: false
default: "5"
type: string
best_of_n:
description: "LLM samples per move for best-of-N selection"
required: false
default: "3"
type: string
time_control:
description: "Time-control preset for Stockfish"
required: false
default: "rapid"
type: choice
options:
- classic
- rapid
- lightning
all_moves:
description: "Include both colours in fine-tuning dataset (default: Black only)"
required: false
default: "false"
type: choice
options:
- "false"
- "true"
# ─────────────────────────────────────────────────────────────────────────────
# Concurrency — only one training run per branch at a time.
# ─────────────────────────────────────────────────────────────────────────────
concurrency:
group: training-${{ github.ref_name }}
cancel-in-progress: false
# ─────────────────────────────────────────────────────────────────────────────
# Job
# ─────────────────────────────────────────────────────────────────────────────
jobs:
train:
name: >-
Train (${{ inputs.llm || 'gemini' }},
${{ inputs.games || '5' }} games,
skill ${{ inputs.skill || '5' }})
runs-on: ubuntu-latest
permissions:
contents: read
steps:
# ── 1. Source code ───────────────────────────────────────────────────
- name: Checkout repository
uses: actions/checkout@v4
# ── 2. Python + pip cache ────────────────────────────────────────────
- name: Set up Python 3.11
uses: actions/setup-python@v5
with:
python-version: "3.11"
cache: "pip"
- name: Install Python dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
# ── 3. Stockfish ─────────────────────────────────────────────────────
- name: Install Stockfish
run: |
sudo apt-get update -qq
sudo apt-get install -y stockfish
SF_PATH="$(which stockfish)"
echo "STOCKFISH_PATH=${SF_PATH}" >> "$GITHUB_ENV"
echo "Stockfish installed at: ${SF_PATH}"
stockfish --version || true
# ── 4. Restore persisted training state ──────────────────────────────
- name: Restore training state
id: restore-state
uses: actions/cache/restore@v4
with:
path: |
elo_history.json
adaptive_progress.json
training_data.pgn
# Unique key for this run; restore-keys falls back to most-recent.
key: training-state-${{ github.ref_name }}-${{ github.run_id }}
restore-keys: |
training-state-${{ github.ref_name }}-
- name: Report restored state
run: |
{
echo "## Prior training state"
if [ -f elo_history.json ]; then
python - <<'PYEOF'
import json, sys
try:
with open("elo_history.json") as f:
d = json.load(f)
print(f"- **Current Elo:** {d['current_elo']:.0f}")
print(f"- **Total games on record:** {len(d['history'])}")
except Exception as e:
print(f"Could not parse elo_history.json: {e}", file=sys.stderr)
PYEOF
else
echo "No previous state found — starting fresh."
fi
echo ""
} >> "$GITHUB_STEP_SUMMARY"
# ── 5. Validate that the required API key secret is set ───────────────
- name: Validate API key availability
id: api-key-check
env:
SELECTED_LLM: ${{ inputs.llm || 'gemini' }}
GOOGLE_API_KEY: ${{ secrets.GOOGLE_API_KEY }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: |
missing_reason=""
case "$SELECTED_LLM" in
gemini)
if [ -z "$GOOGLE_API_KEY" ]; then
missing_reason="GOOGLE_API_KEY secret is not set. Add it in Settings → Secrets."
fi
;;
openai)
if [ -z "$OPENAI_API_KEY" ]; then
missing_reason="OPENAI_API_KEY secret is not set. Add it in Settings → Secrets."
fi
;;
claude)
if [ -z "$ANTHROPIC_API_KEY" ]; then
missing_reason="ANTHROPIC_API_KEY secret is not set. Add it in Settings → Secrets."
fi
;;
esac
if [ -n "$missing_reason" ]; then
echo "skip_training=true" >> "$GITHUB_OUTPUT"
echo "::warning::$missing_reason Skipping training run."
{
echo "## Training skipped"
echo "- Provider: $SELECTED_LLM"
echo "- Reason: $missing_reason"
echo ""
} >> "$GITHUB_STEP_SUMMARY"
exit 0
fi
echo "skip_training=false" >> "$GITHUB_OUTPUT"
echo "API key check passed for provider: $SELECTED_LLM"
# ── 6. Run training games ─────────────────────────────────────────────
- name: Run training games
if: steps.api-key-check.outputs.skip_training != 'true'
env:
GOOGLE_API_KEY: ${{ secrets.GOOGLE_API_KEY }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: |
TRAIN_ARGS=(
"--games" "${{ inputs.games || '5' }}"
"--llm" "${{ inputs.llm || 'gemini' }}"
"--skill" "${{ inputs.skill || '5' }}"
"--best-of-n" "${{ inputs.best_of_n || '3' }}"
"--time-control" "${{ inputs.time_control || 'rapid' }}"
)
if [ -n "${{ inputs.model }}" ]; then
TRAIN_ARGS+=("--model" "${{ inputs.model }}")
fi
echo "Running: python cyberchess.py ${TRAIN_ARGS[*]}"
python cyberchess.py "${TRAIN_ARGS[@]}"
# ── 7. Generate fine-tuning dataset ───────────────────────────────────
- name: Generate fine-tuning dataset
if: steps.api-key-check.outputs.skip_training != 'true'
run: |
FT_ARGS=(
"--input" "training_data.pgn"
"--output" "finetune_data.jsonl"
"--metadata"
)
if [ "${{ inputs.all_moves }}" = "true" ]; then
FT_ARGS+=("--all-moves")
fi
echo "Running: python finetune_pipeline.py ${FT_ARGS[*]}"
python finetune_pipeline.py "${FT_ARGS[@]}"
# ── 8. Write GitHub Step Summary ──────────────────────────────────────
- name: Write training summary
if: always()
run: |
python - <<'PYEOF'
import json, os, sys
summary_path = os.environ.get("GITHUB_STEP_SUMMARY", "/dev/stdout")
lines = []
lines.append("## Training Run Summary\n")
# ── Parameters table ──────────────────────────────────────────────
lines.append("### Run parameters\n")
lines.append("| Parameter | Value |")
lines.append("|-----------|-------|")
params = [
("LLM provider", "${{ inputs.llm || 'gemini' }}"),
("Model override", "${{ inputs.model || '(default)' }}"),
("Games played", "${{ inputs.games || '5' }}"),
("Stockfish skill", "${{ inputs.skill || '5' }}"),
("Best-of-N", "${{ inputs.best_of_n || '3' }}"),
("Time control", "${{ inputs.time_control || 'rapid' }}"),
("All moves", "${{ inputs.all_moves || 'false' }}"),
]
for name, val in params:
lines.append(f"| {name} | {val} |")
lines.append("")
# ── Elo summary ───────────────────────────────────────────────────
try:
with open("elo_history.json") as f:
d = json.load(f)
history = d.get("history", [])
wins = sum(1 for g in history if
(g["result"] == "0-1" and g["ai_color"] == "black") or
(g["result"] == "1-0" and g["ai_color"] == "white"))
draws = sum(1 for g in history if g["result"] == "1/2-1/2")
losses = len(history) - wins - draws
lines.append("### Elo rating\n")
lines.append(f"**Current Elo:** {d['current_elo']:.0f} ")
lines.append(f"**Total games:** {len(history)} "
f"(W {wins} / D {draws} / L {losses})\n")
shown = history[-10:]
if shown:
lines.append("**Last 10 games:**\n")
lines.append("| # | Result | vs Skill | Elo before | Elo after | Δ Elo |")
lines.append("|---|--------|----------|------------|-----------|-------|")
for g in shown:
sign = "+" if g["delta"] >= 0 else ""
lines.append(
f"| {g['game']} | {g['result']} | {g['stockfish_skill']} "
f"| {g['elo_before']:.0f} | {g['elo_after']:.0f} "
f"| {sign}{g['delta']:.0f} |"
)
lines.append("")
except FileNotFoundError:
lines.append("*elo_history.json not found — Elo data unavailable.*\n")
except Exception as e:
lines.append(f"*Could not read elo_history.json: {e}*\n")
# ── Adaptive plan ─────────────────────────────────────────────────
try:
with open("adaptive_progress.json") as f:
ap = json.load(f)
ap_history = ap.get("history", [])
if ap_history:
last = ap_history[-1]
lines.append("### Adaptive curriculum (last plan)\n")
lines.append("| Field | Value |")
lines.append("|-------|-------|")
lines.append(f"| Regime | {last.get('regime', '—')} |")
lines.append(f"| Stockfish skill | {last.get('stockfish_skill', '—')} |")
lines.append(f"| Stockfish time | {last.get('stockfish_time', '—')} s |")
lines.append(f"| Best-of-N | {last.get('best_of_n', '—')} |")
lines.append(f"| Recent score (smoothed) | {last.get('recent_score', '—')} |")
lines.append("")
except FileNotFoundError:
pass
except Exception as e:
lines.append(f"*Could not read adaptive_progress.json: {e}*\n")
# ── Dataset stats ─────────────────────────────────────────────────
try:
sys.path.insert(0, ".")
from finetune_pipeline import pgn_to_training_examples
examples = pgn_to_training_examples("training_data.pgn", black_only=False)
if examples:
games_set = {e["metadata"]["game"] for e in examples}
black_ex = [e for e in examples if e["metadata"]["color"] == "black"]
white_ex = [e for e in examples if e["metadata"]["color"] == "white"]
lines.append("### Fine-tuning dataset\n")
lines.append("| Metric | Value |")
lines.append("|--------|-------|")
lines.append(f"| Total examples | {len(examples)} |")
lines.append(f"| Unique games | {len(games_set)} |")
lines.append(f"| Black examples | {len(black_ex)} |")
lines.append(f"| White examples | {len(white_ex)} |")
lines.append("")
except FileNotFoundError:
lines.append("*training_data.pgn not found — dataset stats unavailable.*\n")
except Exception as e:
lines.append(f"*Could not generate dataset stats: {e}*\n")
with open(summary_path, "a") as fh:
fh.write("\n".join(lines) + "\n")
PYEOF
# ── 9. Save updated training state to cache ───────────────────────────
- name: Save training state
uses: actions/cache/save@v4
if: always() && steps.api-key-check.outputs.skip_training != 'true'
with:
path: |
elo_history.json
adaptive_progress.json
training_data.pgn
key: training-state-${{ github.ref_name }}-${{ github.run_id }}
# ── 10. Upload all training artefacts ─────────────────────────────────
- name: Upload training artefacts
uses: actions/upload-artifact@v4
if: always() && steps.api-key-check.outputs.skip_training != 'true'
with:
name: training-run-${{ github.run_number }}
path: |
training_data.pgn
finetune_data.jsonl
elo_history.json
adaptive_progress.json
if-no-files-found: warn
retention-days: 90