File size: 3,305 Bytes
d9a31e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
"""Launch parallel European HPLT filtering workers.

The filter itself is intentionally single-process and shard-safe. This launcher
splits the actual HF train parquet file list across workers and starts each
worker in a detached screen session with its own output directory and log.
"""

from __future__ import annotations

import argparse
import subprocess
from pathlib import Path

from huggingface_hub import list_repo_files


def parse_args() -> argparse.Namespace:
    ap = argparse.ArgumentParser()
    ap.add_argument("--repo-id", default="ashtok897/european-hplt-v1")
    ap.add_argument("--workers", type=int, default=4)
    ap.add_argument("--out-dir", type=Path, default=Path("data/european_hplt_pl_parallel"))
    ap.add_argument("--log-dir", type=Path, default=Path("logs"))
    ap.add_argument("--screen-prefix", default="hplt")
    ap.add_argument("--target-total-tokens", type=int, default=500_000_000)
    ap.add_argument("--max-records-per-worker", type=int, default=200_000)
    ap.add_argument("--shard-records", type=int, default=25_000)
    ap.add_argument("--shard-tokens", type=int, default=25_000_000)
    ap.add_argument("--progress-every", type=int, default=100_000)
    ap.add_argument("--dry-run", action="store_true")
    return ap.parse_args()


def main() -> None:
    args = parse_args()
    if args.workers < 1:
        raise SystemExit("--workers must be >= 1")

    files = sorted(
        path
        for path in list_repo_files(args.repo_id, repo_type="dataset")
        if path.startswith("data/train/") and path.endswith(".parquet")
    )
    if not files:
        raise SystemExit(f"No train parquet files found in {args.repo_id}")

    args.out_dir.mkdir(parents=True, exist_ok=True)
    args.log_dir.mkdir(parents=True, exist_ok=True)
    target_tokens = max(1, args.target_total_tokens // args.workers)

    chunks = [files[idx:: args.workers] for idx in range(args.workers)]
    print(f"repo={args.repo_id} train_files={len(files)} workers={args.workers}")
    for worker, chunk in enumerate(chunks):
        if not chunk:
            continue
        out_dir = args.out_dir / f"worker_{worker}"
        out_dir.mkdir(parents=True, exist_ok=True)
        log_path = args.log_dir / f"filter_european_hplt_{args.screen_prefix}_w{worker}.log"
        data_files = ",".join(chunk)
        cmd = (
            "python3 src/filter_european_hplt.py "
            f"--repo-id {args.repo_id} "
            f"--data-files '{data_files}' "
            f"--out-dir '{out_dir}' "
            f"--source-name european_hplt_pl_w{worker} "
            f"--max-records {args.max_records_per_worker} "
            f"--max-tokens {target_tokens} "
            f"--shard-records {args.shard_records} "
            f"--shard-tokens {args.shard_tokens} "
            f"--progress-every {args.progress_every} "
            f"> '{log_path}' 2>&1"
        )
        print(
            f"worker_{worker}: files={len(chunk)} target_tokens={target_tokens:,} "
            f"first={chunk[0]} last={chunk[-1]} log={log_path}"
        )
        if not args.dry_run:
            subprocess.run(
                ["screen", "-dmS", f"{args.screen_prefix}_w{worker}", "zsh", "-lc", cmd],
                check=True,
            )


if __name__ == "__main__":
    main()