File size: 5,652 Bytes
a1d579a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/sh
# 44: the queue after the privacy-filter LoRA run, shortest first (Arthur, 2026-09-25: "Adjust the steps ... to be the ones
# that are shorter to longer, so we cross off stuff first"): 40_round2.sh's cases and 43_toss.sh's, reordered; the case
# descriptions are below (40) and in 43_toss.sh. Waits for 36_pf_lora.sh. Usage: sh claude_run/NEW/scripts/44_queue.sh <device> <logfile>
# Round 2 (Arthur, 2026-09-24: "try whatever we have, keeping only the two best of each ... check how legalbert and
# contractbert were post-trained, so maybe ... we can do it with modernbert and then we will have huge contexts").
# Every case trains as 37/38/39 do (annotated gold in ws_train_n60_annot, 3 epochs, 60 documents), infers on the 100
# documents in ws_sot, is mixed with the rules and scored by 81_after_run.sh (the 40 held-out documents), and then
# 15_keep_best.py keeps the two best checkpoints of each model. Cases, in order:
#   modernbert e3_l2048    windows of 2,048 tokens (stride 512) instead of 512: ModernBERT reads up to 8,192
#   modernbert e3_l8192    windows of 8,192 (stride 2,048); may not fit in memory on MPS: a failure is recorded, the rest go on
#   legalbert  e3_nlr      "~" line breaks + the rules' cuts marked (case B on the best LegalBERT)
#   legalbert  e3_nlrc     + page chrome blanked (case C)
#   contractsbert e3_nl_t  "~" line breaks: tells the marker character ("¶" hurt ContractsBERT) from the model
#   dapt       16_dapt_modernbert.py (EDGAR 2024-Q2 contracts, 150 minutes), then modernbert on it at the better of
#              512 / 2,048 windows (e3_dapt or e3_l2048_dapt)
# Waits for 36_pf_lora.sh (and 38, 39 before it). Usage: sh claude_run/NEW/scripts/40_round2.sh <device> <logfile>
set -u
DEVICE="$1"; LOG="$2"
cd "$(dirname "$0")/../../.." || exit 1
N=claude_run/NEW; n=60; W=$N/ws_train_n${n}_annot; T=$N/train_annot/n$n; EPOCHS=3
WHO="source of truth segments_with_original_text_2026-09-22.jsonl (LLM segmentation + judge) with Arthur's viewer annotations (sot_annotated.jsonl); imported by Claude"
FAILED=0
run() { echo "\$ $*"; START=$(date +%s); if "$@"; then echo "took $(( $(date +%s) - START ))s"; return 0; else RC=$?; echo "FAILED (exit $RC) after $(( $(date +%s) - START ))s"; return "$RC"; fi; }
case_run() {  # case_run MODEL TAG [--set k=v ...]
  M="$1"; TAG="$2"; shift 2; m=$(printf '%s' "$M" | tr 'A-Z' 'a-z')
  SETS="--set gold_run=gold --set forbidden_families=$T/forbidden_families.json --set declarations=true --set device=$DEVICE --set epochs=$EPOCHS $*"
  RID="t-$m-$TAG"; PID="m_${m}_n${n}annot_$TAG"
  echo "######## $M $TAG ($(date))"
  if [ ! -f "$W/runs/$RID/manifest.json" ]; then
    rm -f "$T/receipts/$RID.json"
    # shellcheck disable=SC2086
    run uv run route.py approve --ablation "T_$M" --workspace "$W" --from-run gold $SETS --reviewer "$WHO" --output "$T/receipts/$RID.json" --attest || { FAILED=$((FAILED + 1)); return; }
    # shellcheck disable=SC2086
    run uv run route.py run --ablation "T_$M" --workspace "$W" --from-run gold --run-id "$RID" --non-interactive $SETS --approval "$T/receipts/$RID.json" || { FAILED=$((FAILED + 1)); return; }
  fi
  CK="$W/runs/$RID/artifacts/model"
  if [ ! -f "$N/ws_sot/runs/$PID/manifest.json" ]; then
    run uv run route.py run --ablation "M_$M" --workspace $N/ws_sot --from-run a0 --run-id "$PID" --non-interactive --set "checkpoint=$CK" --set allow_training_overlap=true --set device="$DEVICE" \
      && uv run python $N/scripts/02_extract.py $N/ws_sot "$PID" || { FAILED=$((FAILED + 1)); return; }
  fi
  case "$M" in CONTRACTSBERT) HF=models--nlpaueb--bert-base-uncased-contracts;; LEGALBERT) HF=models--nlpaueb--legal-bert-base-uncased;; *) HF=-;; esac
  [ -f "$N/predictions/$PID.jsonl.gz" ] && { sh $N/scripts/81_after_run.sh "$PID" "$CK" "$HF" $N/logs/81_after_run.log || FAILED=$((FAILED + 1)); }
  grep -E "^(m_|x_intersection_rules_)${m}_n${n}annot_$TAG " $N/results/n60__annot.txt | grep -F "|"
  uv run python $N/scripts/15_keep_best.py
}
score_of() { grep -E "^(m_|x_intersection_rules_)modernbert_n${n}annot_$1 " $N/results/n60__annot.txt | grep -F "|" | awk '{print $4}' | sort -r | head -1; }
{
echo "######## waiting for 36_pf_lora.sh ($(date))"
while pgrep -f "[3]6_pf_lora[.]sh" >/dev/null; do sleep 60; done
G="$PWD/$N/sot_annotated.jsonl"
case_run LEGALBERT e3_nlr --set newline_marker=auto --set rule_marker=auto
case_run LEGALBERT e3_nlrc --set newline_marker=auto --set rule_marker=auto --set blank_chrome=true
case_run CONTRACTSBERT e3_nl_t "--set newline_marker=~"
case_run LEGALBERT e3_nl_toss --set newline_marker=auto "--set garbage_gold=$G"
case_run CONTRACTSBERT e3_toss "--set garbage_gold=$G"
case_run MODERNBERT e3_l2048 --set max_length=2048 --set stride=512
case_run MODERNBERT e3_l8192 --set max_length=8192 --set stride=2048
echo "######## dapt ($(date))"
D=$N/dapt/modernbert-ex10-2024q2
[ -f "$D/dapt_manifest.json" ] || run uv run --no-project --python 3.12 --with "transformers>=4.48,<5" --with torch --with polars --with huggingface_hub \
  python $N/scripts/16_dapt_modernbert.py --shards 0,1 --max-minutes 150 --device "$DEVICE" --out "$D" || FAILED=$((FAILED + 1))
if [ -f "$D/dapt_manifest.json" ]; then
  S512=$(score_of e3); S2048=$(score_of e3_l2048); echo "modernbert 512: ${S512:-none}, 2048: ${S2048:-none}"
  if [ -n "$S2048" ] && [ "$(printf '%s\n%s\n' "${S512:-0}" "$S2048" | sort -r | head -1)" = "$S2048" ]; then
    case_run MODERNBERT e3_l2048_dapt --set max_length=2048 --set stride=512 "--set base=$PWD/$D"
  else case_run MODERNBERT e3_dapt "--set base=$PWD/$D"; fi
fi
echo "44 done: $FAILED failed ($(date))"
} >> "$LOG" 2>&1
[ "$FAILED" -eq 0 ]