File size: 3,168 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
# Data-efficient fine-tuning pipeline (Windows). 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:
#   $env:MODEL = "data/models/OvisOCR3"; powershell -ExecutionPolicy Bypass -File recipes\data_efficient.ps1
#   $env:STAGES = "selfplay grpo eval"   to run 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).
. (Join-Path $PSScriptRoot "..\common\env.ps1")
$model  = if ($env:MODEL)  { $env:MODEL }  else { "$MODELS/OvisOCR3" }
$stages = if ($env:STAGES) { $env:STAGES -split "\s+" } else { @("sieve", "balance", "sft", "hard", "selfplay", "grpo", "eval") }
$name   = Split-Path $model -Leaf
$data   = "$PREPARED/canonical"
$sel    = "$PREPARED/selected"
$out    = "$RUNS/recipe-$name"
$small  = @("--max-pixels", "589824", "--gradient-checkpointing")
$promptArgs = @()
if (Test-Path "$model/prompts/document_markdown.txt") { $promptArgs = @("--prompt-file", "$model/prompts/document_markdown.txt") }
New-Item -ItemType Directory -Force $sel, $out | Out-Null

if ($stages -contains "sieve")   { Invoke-Step $PY -m common.select_data sieve --input "$data/train.jsonl" --out "$sel/clean.jsonl" }
if ($stages -contains "balance") { Invoke-Step $PY -m common.select_data balance --input "$sel/clean.jsonl" --per-source 20000 --out "$sel/balanced.jsonl" }
if ($stages -contains "sft") {
  Invoke-Step $PY -m common.vlm_sft --model $model @promptArgs --train "$sel/balanced.jsonl" --output "$out/sft" `
    --lora 16 --lr 1e-4 --epochs 2 --grad-accum 8 --max-length 2048 @small
}
if ($stages -contains "hard") {
  Invoke-Step $PY -m common.vlm_infer --model $model --adapter "$out/sft" @promptArgs --input "$sel/balanced.jsonl" `
    --max-pixels 589824 --out "$out/train_preds.jsonl"
  Invoke-Step $PY -m common.select_data hard --input "$sel/balanced.jsonl" --preds "$out/train_preds.jsonl" --keep 0.3 --out "$sel/hard.jsonl"
}
if ($stages -contains "selfplay") {
  Invoke-Step $PY -m common.selfplay --model $model --adapter "$out/sft" @promptArgs --train "$sel/hard.jsonl" --output "$out/selfplay" `
    --rounds 3 --max-new 1024 @small
}
if ($stages -contains "grpo") {
  Invoke-Step $PY -m common.select_data entropy --input "$sel/hard.jsonl" --model $model --adapter "$out/selfplay/round3" `
    @promptArgs --keep 0.5 --max-pixels 589824 --out "$sel/grpo.jsonl"
  Invoke-Step $PY -m common.grpo --model $model --adapter "$out/selfplay/round3" @promptArgs --train "$sel/grpo.jsonl" --output "$out/grpo" `
    --group 4 --kl 0.02 --max-new 1024 @small
}
if ($stages -contains "eval") {
  foreach ($scale in "1.0", "0.8", "0.6") {
    Invoke-Step $PY -m common.vlm_infer --model $model --adapter "$out/grpo/final" --adapter-scale $scale @promptArgs `
      --input "$data/eval.jsonl" --max-pixels 589824 --out "$out/eval_scale$scale.jsonl"
    Invoke-Step $PY -m common.score "$out/eval_scale$scale.jsonl"
  }
}