File size: 2,749 Bytes
cf8c8d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env bash
# Data-efficient fine-tuning pipeline (Linux). See docs/finetuning_recipes.md for the evidence behind each stage.
#   sieve -> balance -> sft -> hard -> selfplay -> grpo -> eval (alpha sweep)
# Prerequisite: canonical data from `python -m common.data_prep --sources configs/sources.yaml --formats hf`.
# Usage:
#   MODEL=data/models/OvisOCR3 bash recipes/data_efficient.sh
#   STAGES="selfplay grpo eval" bash recipes/data_efficient.sh   # only some stages (they reuse earlier outputs)
# Settings below fit an 8 GB GPU (LoRA, fp32 on pre-Ampere cards, <= 0.59 MP images, short sequences).
source "$(dirname "$0")/../common/env.sh"
MODEL=${MODEL:-$MODELS/OvisOCR3}
STAGES=${STAGES:-"sieve balance sft hard selfplay grpo eval"}
NAME=$(basename "$MODEL")
DATA=$PREPARED/canonical
SEL=$PREPARED/selected
OUT=$RUNS/recipe-$NAME
SMALL=(--max-pixels 589824 --gradient-checkpointing)
PROMPT=()
[[ -f "$MODEL/prompts/document_markdown.txt" ]] && PROMPT=(--prompt-file "$MODEL/prompts/document_markdown.txt")
has() { [[ " $STAGES " == *" $1 "* ]]; }
mkdir -p "$SEL" "$OUT"

has sieve   && $PY -m common.select_data sieve --input "$DATA/train.jsonl" --out "$SEL/clean.jsonl"
has balance && $PY -m common.select_data balance --input "$SEL/clean.jsonl" --per-source 20000 --out "$SEL/balanced.jsonl"
if has sft; then
  $PY -m common.vlm_sft --model "$MODEL" "${PROMPT[@]}" --train "$SEL/balanced.jsonl" --output "$OUT/sft" \
    --lora 16 --lr 1e-4 --epochs 2 --grad-accum 8 --max-length 2048 "${SMALL[@]}"
fi
if has hard; then
  $PY -m common.vlm_infer --model "$MODEL" --adapter "$OUT/sft" "${PROMPT[@]}" --input "$SEL/balanced.jsonl" \
    --max-pixels 589824 --out "$OUT/train_preds.jsonl"
  $PY -m common.select_data hard --input "$SEL/balanced.jsonl" --preds "$OUT/train_preds.jsonl" --keep 0.3 --out "$SEL/hard.jsonl"
fi
if has selfplay; then
  $PY -m common.selfplay --model "$MODEL" --adapter "$OUT/sft" "${PROMPT[@]}" --train "$SEL/hard.jsonl" --output "$OUT/selfplay" \
    --rounds 3 --max-new 1024 "${SMALL[@]}"
fi
if has grpo; then
  $PY -m common.select_data entropy --input "$SEL/hard.jsonl" --model "$MODEL" --adapter "$OUT/selfplay/round3" \
    "${PROMPT[@]}" --keep 0.5 --max-pixels 589824 --out "$SEL/grpo.jsonl"
  $PY -m common.grpo --model "$MODEL" --adapter "$OUT/selfplay/round3" "${PROMPT[@]}" --train "$SEL/grpo.jsonl" --output "$OUT/grpo" \
    --group 4 --kl 0.02 --max-new 1024 "${SMALL[@]}"
fi
if has eval; then
  for SCALE in 1.0 0.8 0.6; do
    $PY -m common.vlm_infer --model "$MODEL" --adapter "$OUT/grpo/final" --adapter-scale "$SCALE" "${PROMPT[@]}" \
      --input "$DATA/eval.jsonl" --max-pixels 589824 --out "$OUT/eval_scale$SCALE.jsonl"
    $PY -m common.score "$OUT/eval_scale$SCALE.jsonl"
  done
fi