ModhiSathvik commited on
Commit
45d5b40
·
verified ·
1 Parent(s): 099951d

Add run_pipeline.sh

Browse files
Files changed (1) hide show
  1. run_pipeline.sh +364 -0
run_pipeline.sh ADDED
@@ -0,0 +1,364 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ # ========================================================================
3
+ # Automated multi-format synthetic data generation pipeline.
4
+ #
5
+ # For each format in the configured list:
6
+ # 1. Filter seeds (skip silently if format has no filter rule)
7
+ # 2. Run smoke test (50 samples)
8
+ # 3. Validate quality of the smoke test
9
+ # 4. If quality passes, run full generation
10
+ # 5. If anything fails, STOP and report the reason
11
+ #
12
+ # Usage:
13
+ # bash run_pipeline.sh javascript # default formats
14
+ # bash run_pipeline.sh javascript tier1 # autocomplete-focused only
15
+ # bash run_pipeline.sh python tier2 # broader coverage
16
+ #
17
+ # Resume: re-running the script picks up where it left off (each generator
18
+ # auto-resumes from existing output files).
19
+ #
20
+ # Stop conditions (script exits with error):
21
+ # - vLLM endpoint unreachable
22
+ # - Generator file missing
23
+ # - Smoke test produces 0 successful samples
24
+ # - Smoke test fail rate > 50%
25
+ # - Smoke test sample is empty or malformed JSON
26
+ #
27
+ # Logs:
28
+ # logs/<subject>_pipeline.log master pipeline log
29
+ # logs/<subject>_<format>.log per-format generation log
30
+ # ========================================================================
31
+
32
+ set -u # error on undefined vars (don't use -e; we want to handle errors explicitly)
33
+
34
+ # ===== Args =====
35
+ SUBJECT="${1:-python}"
36
+ TIER="${2:-tier1}"
37
+
38
+ # ===== Config =====
39
+ ROOT="/workspace/pluto_run"
40
+ VLLM_URL="http://localhost:8000/v1"
41
+ SMOKE_LIMIT=50
42
+ SMOKE_FAIL_THRESHOLD=50 # max % of failed samples in smoke test before we abort
43
+
44
+ # Format list per tier. Order matters: highest-value formats first so partial
45
+ # completions still leave you with the best data.
46
+ case "$TIER" in
47
+ tier1)
48
+ # Autocomplete-focused (~3 hours)
49
+ FORMATS=(
50
+ "infilling:128"
51
+ "code_exercise:128"
52
+ "infilling_long:96"
53
+ "infilling_class:128"
54
+ "infilling_real_libs:128"
55
+ )
56
+ ;;
57
+ tier2)
58
+ # Broader coverage (Tier 1 + conceptual)
59
+ FORMATS=(
60
+ "infilling:128"
61
+ "code_exercise:128"
62
+ "infilling_long:96"
63
+ "infilling_class:128"
64
+ "infilling_real_libs:128"
65
+ "textbook:96"
66
+ "worked_example:128"
67
+ "refinement:96"
68
+ )
69
+ ;;
70
+ all)
71
+ # Everything (~10 hours)
72
+ FORMATS=(
73
+ "infilling:128"
74
+ "code_exercise:128"
75
+ "infilling_long:96"
76
+ "infilling_class:128"
77
+ "infilling_real_libs:128"
78
+ "textbook:96"
79
+ "worked_example:128"
80
+ "refinement:96"
81
+ "debug_dialogue:96"
82
+ "instruction_reversal:128"
83
+ )
84
+ ;;
85
+ *)
86
+ echo "ERROR: unknown tier '$TIER'. Use: tier1 | tier2 | all"
87
+ exit 1
88
+ ;;
89
+ esac
90
+
91
+ # ===== Setup =====
92
+ cd "$ROOT" || { echo "ERROR: cannot cd to $ROOT"; exit 1; }
93
+ mkdir -p logs data/$SUBJECT
94
+
95
+ PIPELINE_LOG="logs/${SUBJECT}_pipeline.log"
96
+
97
+ log() {
98
+ local msg="$1"
99
+ echo "[$(date '+%Y-%m-%d %H:%M:%S')] $msg" | tee -a "$PIPELINE_LOG"
100
+ }
101
+
102
+ abort() {
103
+ local reason="$1"
104
+ log ""
105
+ log "================================================================"
106
+ log "PIPELINE ABORTED"
107
+ log "Reason: $reason"
108
+ log ""
109
+ log "Diagnose:"
110
+ log " - Master log: $PIPELINE_LOG"
111
+ log " - Format logs: logs/${SUBJECT}_*.log"
112
+ log " - Last format: ${LAST_FORMAT:-none}"
113
+ log "================================================================"
114
+ exit 1
115
+ }
116
+
117
+ # ===== Pre-flight checks =====
118
+ log "================================================================"
119
+ log "Pipeline starting: subject=$SUBJECT tier=$TIER"
120
+ log "Formats to run (${#FORMATS[@]}):"
121
+ for entry in "${FORMATS[@]}"; do
122
+ log " - $entry"
123
+ done
124
+ log "================================================================"
125
+
126
+ # 1. Seeds file exists
127
+ SEEDS_FILE="seeds/${SUBJECT}.json"
128
+ if [ ! -f "$SEEDS_FILE" ]; then
129
+ abort "Seeds file missing: $SEEDS_FILE. Run topic expansion first."
130
+ fi
131
+ SEED_COUNT=$(python -c "import json; print(len(json.load(open('$SEEDS_FILE'))))" 2>/dev/null) \
132
+ || abort "Cannot parse seeds file: $SEEDS_FILE"
133
+ log "Seeds file: $SEEDS_FILE ($SEED_COUNT topics)"
134
+
135
+ # 2. vLLM endpoint reachable
136
+ if ! curl -sf "$VLLM_URL/models" > /dev/null; then
137
+ abort "vLLM endpoint unreachable at $VLLM_URL. Start vLLM first."
138
+ fi
139
+ MODEL=$(curl -s "$VLLM_URL/models" | python -c "import json,sys; print(json.load(sys.stdin)['data'][0]['id'])" 2>/dev/null)
140
+ log "vLLM up: model=$MODEL"
141
+
142
+ # 3. Generators exist
143
+ log "Checking generator files..."
144
+ for entry in "${FORMATS[@]}"; do
145
+ fmt="${entry%%:*}"
146
+ gen="generators/generate_${fmt}.py"
147
+ if [ ! -f "$gen" ]; then
148
+ abort "Generator missing: $gen"
149
+ fi
150
+ done
151
+ log "All generators present."
152
+
153
+ # 4. Filter utility
154
+ if [ ! -f "utils/filter_seeds.py" ]; then
155
+ log "WARNING: utils/filter_seeds.py missing - per-format seed filtering will be skipped"
156
+ fi
157
+
158
+ # ===== Quality validator =====
159
+ # Reads smoke output JSONL, counts successful samples, checks for obvious problems
160
+ validate_smoke() {
161
+ local fmt="$1"
162
+ local out_file="data/${SUBJECT}/${fmt}.jsonl"
163
+
164
+ if [ ! -f "$out_file" ]; then
165
+ echo "MISSING_FILE"
166
+ return
167
+ fi
168
+
169
+ python << PYEOF
170
+ import json
171
+ import sys
172
+
173
+ samples = []
174
+ malformed = 0
175
+ with open("$out_file") as f:
176
+ for line in f:
177
+ try:
178
+ samples.append(json.loads(line))
179
+ except Exception:
180
+ malformed += 1
181
+
182
+ n = len(samples)
183
+ if n == 0:
184
+ print("EMPTY")
185
+ sys.exit()
186
+
187
+ # Check for content-level issues
188
+ empty_text = 0
189
+ for s in samples:
190
+ text = s.get("messages", [{}, {}, {}])[2].get("content", "")
191
+ if not text or len(text) < 50:
192
+ empty_text += 1
193
+
194
+ # Token range
195
+ tokens = [s.get("tokens", 0) for s in samples]
196
+ min_tok = min(tokens)
197
+ max_tok = max(tokens)
198
+ median_tok = sorted(tokens)[len(tokens) // 2]
199
+
200
+ # Output structured result for the bash caller
201
+ empty_pct = empty_text / n * 100
202
+ print(f"OK n={n} malformed={malformed} empty={empty_text} empty_pct={empty_pct:.1f} "
203
+ f"tokens_min={min_tok} tokens_median={median_tok} tokens_max={max_tok}")
204
+ PYEOF
205
+ }
206
+
207
+ # ===== Per-format pipeline =====
208
+ LAST_FORMAT=""
209
+ TOTAL_SAMPLES_BEFORE=$(find data/$SUBJECT -name "*.jsonl" 2>/dev/null | xargs -r wc -l 2>/dev/null | tail -1 | awk '{print $1}' || echo 0)
210
+ log ""
211
+ log "Starting samples on disk: ${TOTAL_SAMPLES_BEFORE:-0}"
212
+
213
+ for entry in "${FORMATS[@]}"; do
214
+ fmt="${entry%%:*}"
215
+ conc="${entry##*:}"
216
+ LAST_FORMAT="$fmt"
217
+
218
+ log ""
219
+ log "================================================================"
220
+ log "FORMAT: $fmt (concurrency=$conc)"
221
+ log "================================================================"
222
+
223
+ # ---- Step 1: Filter seeds (best-effort, may not apply) ----
224
+ if [ -f "utils/filter_seeds.py" ]; then
225
+ log "[1/4] Filtering seeds for $fmt..."
226
+ python utils/filter_seeds.py --subject "$SUBJECT" --format "$fmt" \
227
+ >> "$PIPELINE_LOG" 2>&1 \
228
+ || log " (filter step had warnings - format may use base seeds)"
229
+ fi
230
+
231
+ # ---- Step 2: Smoke test ----
232
+ log "[2/4] Smoke test ($SMOKE_LIMIT samples)..."
233
+
234
+ # Snapshot existing data so we can detect new samples cleanly
235
+ out_file="data/${SUBJECT}/${fmt}.jsonl"
236
+ before_count=0
237
+ [ -f "$out_file" ] && before_count=$(wc -l < "$out_file")
238
+
239
+ smoke_log="logs/${SUBJECT}_${fmt}_smoke.log"
240
+ python "generators/generate_${fmt}.py" \
241
+ --subject "$SUBJECT" \
242
+ --limit $SMOKE_LIMIT \
243
+ --concurrent "$conc" \
244
+ > "$smoke_log" 2>&1 &
245
+ smoke_pid=$!
246
+
247
+ # Wait for smoke test
248
+ wait $smoke_pid
249
+ smoke_exit=$?
250
+
251
+ if [ $smoke_exit -ne 0 ]; then
252
+ log " Smoke test exited with code $smoke_exit"
253
+ log " Last 20 lines of smoke log:"
254
+ tail -20 "$smoke_log" | sed 's/^/ /' | tee -a "$PIPELINE_LOG"
255
+ abort "Smoke test failed for $fmt (exit code $smoke_exit). See $smoke_log"
256
+ fi
257
+
258
+ # ---- Step 3: Validate smoke quality ----
259
+ log "[3/4] Validating smoke output..."
260
+ result=$(validate_smoke "$fmt")
261
+ log " $result"
262
+
263
+ case "$result" in
264
+ "MISSING_FILE")
265
+ abort "Smoke test for $fmt produced no output file"
266
+ ;;
267
+ "EMPTY")
268
+ abort "Smoke test for $fmt produced 0 samples (output file empty)"
269
+ ;;
270
+ OK*)
271
+ # Parse the metrics
272
+ after_count=$(wc -l < "$out_file")
273
+ new_samples=$((after_count - before_count))
274
+
275
+ if [ $new_samples -lt 5 ]; then
276
+ log " Last 30 lines of smoke log for diagnosis:"
277
+ tail -30 "$smoke_log" | sed 's/^/ /' | tee -a "$PIPELINE_LOG"
278
+ abort "Smoke test produced only $new_samples new samples (need at least 5). See $smoke_log"
279
+ fi
280
+
281
+ success_rate=$((new_samples * 100 / SMOKE_LIMIT))
282
+ log " Success rate: $new_samples/$SMOKE_LIMIT (${success_rate}%)"
283
+
284
+ if [ $success_rate -lt 30 ]; then
285
+ log " Last 30 lines of smoke log:"
286
+ tail -30 "$smoke_log" | sed 's/^/ /' | tee -a "$PIPELINE_LOG"
287
+ abort "Smoke success rate ${success_rate}% below 30% threshold for $fmt"
288
+ fi
289
+ log " Smoke test PASSED"
290
+ ;;
291
+ *)
292
+ abort "Unknown validation result: $result"
293
+ ;;
294
+ esac
295
+
296
+ # ---- Step 4: Full generation ----
297
+ log "[4/4] Full generation for $fmt..."
298
+ full_log="logs/${SUBJECT}_${fmt}.log"
299
+
300
+ python "generators/generate_${fmt}.py" \
301
+ --subject "$SUBJECT" \
302
+ --concurrent "$conc" \
303
+ > "$full_log" 2>&1 &
304
+ full_pid=$!
305
+ echo $full_pid > "logs/${SUBJECT}_${fmt}.pid"
306
+
307
+ # Show progress every minute while it runs
308
+ while kill -0 $full_pid 2>/dev/null; do
309
+ if [ -f "$out_file" ]; then
310
+ cur_count=$(wc -l < "$out_file")
311
+ tail_line=$(tail -1 "$full_log" 2>/dev/null | tr -d '\r' | tail -c 200)
312
+ log " in progress: $cur_count samples on disk | $tail_line"
313
+ fi
314
+ sleep 60
315
+ done
316
+
317
+ wait $full_pid
318
+ full_exit=$?
319
+
320
+ if [ $full_exit -ne 0 ]; then
321
+ log " Full generation exited with code $full_exit"
322
+ log " Last 20 lines of generation log:"
323
+ tail -20 "$full_log" | sed 's/^/ /' | tee -a "$PIPELINE_LOG"
324
+ abort "Full generation failed for $fmt (exit code $full_exit). See $full_log"
325
+ fi
326
+
327
+ # Final stats
328
+ final_count=$(wc -l < "$out_file")
329
+ final_tokens=$(python -c "
330
+ import json
331
+ total = 0
332
+ for line in open('$out_file'):
333
+ total += json.loads(line).get('tokens', 0)
334
+ print(f'{total:,}')
335
+ " 2>/dev/null || echo "?")
336
+
337
+ log " COMPLETED: $final_count samples, $final_tokens completion tokens"
338
+ done
339
+
340
+ # ===== Final summary =====
341
+ log ""
342
+ log "================================================================"
343
+ log "PIPELINE COMPLETE for subject=$SUBJECT tier=$TIER"
344
+ log "================================================================"
345
+ log ""
346
+ log "Final corpus state:"
347
+ for f in data/$SUBJECT/*.jsonl; do
348
+ if [ -f "$f" ]; then
349
+ fmt=$(basename "$f" .jsonl)
350
+ count=$(wc -l < "$f")
351
+ tokens=$(python -c "
352
+ import json
353
+ total = 0
354
+ for line in open('$f'):
355
+ total += json.loads(line).get('tokens', 0)
356
+ print(f'{total/1e6:.2f}M')
357
+ " 2>/dev/null || echo "?")
358
+ log " $(printf '%-25s' "$fmt") $(printf '%6d' "$count") samples $tokens tokens"
359
+ fi
360
+ done
361
+
362
+ log ""
363
+ log "Logs preserved in: logs/${SUBJECT}_*.log"
364
+ log "Done."