File size: 2,247 Bytes
fbe8d49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Single-source baselines of the same size, for comparison with the mix.

python baselines.py 10M   -> out/base_tinystories_10M, out/base_fineweb_edu_10M
Light cleaning only (ftfy + whitespace), no mix, no complexity or dedup filters:
this is what someone would get by just taking the dataset.
"""
import os
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent
_env = ROOT / ".env"
if _env.exists():
    for _l in _env.read_text(encoding="utf-8").splitlines():
        if "=" in _l and not _l.lstrip().startswith("#"):
            _k, _v = _l.split("=", 1)
            os.environ.setdefault(_k.strip(), _v.strip().strip("'\""))

import ftfy
from datasets import load_dataset
from huggingface_hub import HfFileSystem

EOT = "<|endoftext|>"


def tinystories():
    buf = []
    with HfFileSystem().open("datasets/roneneldan/TinyStories/TinyStoriesV2-GPT4-train.txt", "r",
                             encoding="utf-8", errors="replace") as f:
        for line in f:
            if EOT in line:
                if buf:
                    yield " ".join(buf)
                buf = []
            else:
                buf.append(line.strip())


def fineweb_edu():
    for r in load_dataset("HuggingFaceFW/fineweb-edu", "sample-10BT", split="train", streaming=True):
        if r["int_score"] >= 4:
            yield r["text"]


SOURCES = {"tinystories": tinystories, "fineweb_edu": fineweb_edu}


def main():
    size = sys.argv[1]
    target = {"10M": 10e6, "100M": 100e6}[size]
    for name, gen in SOURCES.items():
        if len(sys.argv) > 2 and name not in sys.argv[2:]:
            continue
        out = ROOT / "out" / f"base_{name}_{size}"
        out.mkdir(parents=True, exist_ok=True)
        words, n = 0, 0
        with (out / "train.txt").open("w", encoding="utf-8") as f:
            for text in gen():
                text = ftfy.fix_text(text).strip()
                if not text:
                    continue
                f.write(text + f"\n{EOT}\n")
                words += len(text.split())
                n += 1
                if words >= target:
                    break
        print(f"{name} {size}: {words/1e6:.2f}M words, {n} docs", flush=True)


if __name__ == "__main__":
    main()