File size: 2,042 Bytes
2054ea3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Build a mixed JSONL by sampling each source down to a token budget.

Token shares are what the packed dataset actually sees, so integer `repeat`
factors on whole files are too coarse (LongAlign alone is 265M tokens). This
samples rows per source until each budget is met, then shuffles.

    python3 build_mix.py --out mix_sft.jsonl \
        --source identity.jsonl:1000000 --source train_0.jsonl:28000000 \
        --source longalign_sft.jsonl:9000000 --source niah_sft.jsonl:55000000
"""
import argparse
import json
import random

from tokenmaker import Tokenizer


def parse_args():
    p = argparse.ArgumentParser()
    p.add_argument('--out', required=True)
    p.add_argument('--source', action='append', required=True,
                   metavar='PATH:TOKENS', help='repeatable; tokens is a budget')
    p.add_argument('--seed', type=int, default=0)
    return p.parse_args()


def main():
    args = parse_args()
    rng = random.Random(args.seed)
    rows, stats = [], []
    for spec in args.source:
        path, _, budget = spec.rpartition(':')
        budget = int(budget)
        used = 0
        n = 0
        with open(path) as f:
            for line in f:
                line = line.strip()
                if not line:
                    continue
                d = json.loads(line)
                msgs = d.get('data') or []
                used += sum(len(Tokenizer.encode(m)) + 6 for m in msgs)
                rows.append(d)
                n += 1
                if used >= budget:
                    break
        stats.append((path, n, used))

    rng.shuffle(rows)
    with open(args.out, 'w') as f:
        for d in rows:
            f.write(json.dumps(d, ensure_ascii=False) + '\n')

    total = sum(u for _, _, u in stats)
    for path, n, used in stats:
        print(f'  {path:28s} {n:7d} rows {used/1e6:6.1f}M tokens '
              f'({100*used/total:4.1f}%)')
    print(f'wrote {len(rows)} rows, {total/1e6:.1f}M tokens -> {args.out}')


if __name__ == '__main__':
    main()