chore: remove stale scripts/build_fixer_replanner_iter2.py
Browse files
scripts/build_fixer_replanner_iter2.py
DELETED
|
@@ -1,102 +0,0 @@
|
|
| 1 |
-
"""Build fixer ORPO iter-2 're-planner' dataset.
|
| 2 |
-
|
| 3 |
-
Insight: the current fixer is too conservative — it changes planner_sql only 1.4%
|
| 4 |
-
of the time and rescues 0/533 hard questions on BIRD-dev. The fixer architecture
|
| 5 |
-
needs to be re-framed: instead of 'apply small critique-driven edit', train it as
|
| 6 |
-
a re-planner that produces a COMPLETE correct alternative when given a failed
|
| 7 |
-
attempt.
|
| 8 |
-
|
| 9 |
-
Data source: K=4 BIRD-train rollouts. For each question, find a (wrong-trajectory,
|
| 10 |
-
correct-trajectory) pair within the K=4 samples. Use:
|
| 11 |
-
- chosen = correct trajectory's planner_sql (the alternative that works)
|
| 12 |
-
- rejected = wrong trajectory's planner_sql or the fixer's mistaken output
|
| 13 |
-
- prompt = fixer's standard prompt with the wrong trajectory as the input
|
| 14 |
-
|
| 15 |
-
Output: data/llm_alignment/scaleup_iter2_v3/hf_fixer_replanner
|
| 16 |
-
"""
|
| 17 |
-
import json
|
| 18 |
-
import os
|
| 19 |
-
import random
|
| 20 |
-
import re
|
| 21 |
-
from datasets import Dataset, DatasetDict
|
| 22 |
-
|
| 23 |
-
OUT_DIR = "/home/datht/mats-sql-tist/data/llm_alignment/scaleup_iter2_v3/hf_fixer_replanner"
|
| 24 |
-
|
| 25 |
-
SRC_PATHS = [
|
| 26 |
-
"/home/datht/mats-sql-tist/data/rollouts/bird_train_3stage_K4.jsonl",
|
| 27 |
-
"/home/datht/mats-sql-tist/data/rollouts/scaleup_bird_train_3stage_K4.jsonl",
|
| 28 |
-
"/home/datht/mats-sql-tist/data/rollouts/iter2_bird_train_3stage_K8.jsonl",
|
| 29 |
-
"/home/datht/mats-sql-tist/data/rollouts/scaleup_bird_train_2stage_K4.jsonl",
|
| 30 |
-
]
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
def normalize_sql(sql):
|
| 34 |
-
return re.sub(r"\s+", " ", sql or "").lower().strip()
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
def main():
|
| 38 |
-
rng = random.Random(42)
|
| 39 |
-
pairs = []
|
| 40 |
-
seen_keys = set() # (question_hash, wrong_sql_hash) → dedup
|
| 41 |
-
|
| 42 |
-
for p in SRC_PATHS:
|
| 43 |
-
if not os.path.exists(p):
|
| 44 |
-
continue
|
| 45 |
-
with open(p) as f:
|
| 46 |
-
for line in f:
|
| 47 |
-
s = json.loads(line)
|
| 48 |
-
traj = s.get("trajectories", [])
|
| 49 |
-
if len(traj) < 2:
|
| 50 |
-
continue
|
| 51 |
-
correct_trajs = [t for t in traj if t.get("is_planner_correct")]
|
| 52 |
-
wrong_trajs = [t for t in traj if not t.get("is_planner_correct")]
|
| 53 |
-
if not correct_trajs or not wrong_trajs:
|
| 54 |
-
continue
|
| 55 |
-
# Build (wrong → correct) pairs within the K samples
|
| 56 |
-
for wt in wrong_trajs:
|
| 57 |
-
wsql = (wt.get("planner_sql") or "").strip()
|
| 58 |
-
if not wsql:
|
| 59 |
-
continue
|
| 60 |
-
# Pick the shortest correct planner_sql as the "preferred" alternative
|
| 61 |
-
correct_trajs_sorted = sorted(correct_trajs, key=lambda t: len(t.get("planner_sql") or ""))
|
| 62 |
-
csql = (correct_trajs_sorted[0].get("planner_sql") or "").strip()
|
| 63 |
-
if not csql or normalize_sql(csql) == normalize_sql(wsql):
|
| 64 |
-
continue
|
| 65 |
-
fixer_prompt = (wt.get("fixer_prompt") or "").strip()
|
| 66 |
-
if not fixer_prompt:
|
| 67 |
-
continue
|
| 68 |
-
key = (hash(s.get("question", "")), hash(normalize_sql(wsql)))
|
| 69 |
-
if key in seen_keys:
|
| 70 |
-
continue
|
| 71 |
-
seen_keys.add(key)
|
| 72 |
-
chosen_text = f"```sql\n{csql}\n```"
|
| 73 |
-
rejected_text = f"```sql\n{wsql}\n```"
|
| 74 |
-
pairs.append({
|
| 75 |
-
"prompt": fixer_prompt,
|
| 76 |
-
"chosen": chosen_text,
|
| 77 |
-
"rejected": rejected_text,
|
| 78 |
-
"db_path": s.get("db_path", ""),
|
| 79 |
-
"question": s.get("question", ""),
|
| 80 |
-
"db_id": s.get("db_id", ""),
|
| 81 |
-
})
|
| 82 |
-
|
| 83 |
-
rng.shuffle(pairs)
|
| 84 |
-
|
| 85 |
-
n_test = max(40, len(pairs) // 30)
|
| 86 |
-
test = pairs[:n_test]
|
| 87 |
-
train = pairs[n_test:]
|
| 88 |
-
|
| 89 |
-
dd = DatasetDict({
|
| 90 |
-
"train_dpo": Dataset.from_list(train),
|
| 91 |
-
"test_dpo": Dataset.from_list(test),
|
| 92 |
-
})
|
| 93 |
-
dd.save_to_disk(OUT_DIR)
|
| 94 |
-
|
| 95 |
-
print(f"=== Fixer ORPO iter-2 RE-PLANNER dataset ===")
|
| 96 |
-
print(f" total pairs: {len(pairs)}")
|
| 97 |
-
print(f" train: {len(train)}, test: {len(test)}")
|
| 98 |
-
print(f" Saved to {OUT_DIR}")
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
if __name__ == "__main__":
|
| 102 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|