File size: 6,671 Bytes
f4e346e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# Data Module

This module handles all data preprocessing, tokenization, and preparation for training.

## Overview

The data pipeline converts raw text into binary token files optimized for training:
- **Raw text collection** from multiple sources
- **Tokenization** using BPE tokenizer
- **Binary serialization** for efficient loading
- **Train/validation splitting**

## Directory Structure

```
data/
β”œβ”€β”€ raw/                    # Raw text sources
β”‚   β”œβ”€β”€ books/             # Book corpus
β”‚   β”œβ”€β”€ wikipedia/         # Wikipedia dumps
β”‚   β”œβ”€β”€ fineweb/           # Web crawl data
β”‚   └── merged_text/
β”‚       └── corpus.txt     # Combined corpus
β”œβ”€β”€ bin/                   # Tokenized binary files
β”‚   β”œβ”€β”€ train.bin         # Training data (uint16)
β”‚   └── val.bin           # Validation data (uint16)
└── prepare_data.py       # Tokenization script
```

## Data Processing Pipeline

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ 1. Raw Text Sources                         β”‚
β”‚    - Books: 15 files                        β”‚
β”‚    - Wikipedia: 3 dumps                     β”‚
β”‚    - FineWeb: 1 crawl                       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                   β”‚
                   β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ 2. Merge & Clean                            β”‚
β”‚    β†’ corpus.txt (all text combined)         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                   β”‚
                   β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ 3. Tokenize (prepare_data.py)              β”‚
β”‚    - Load BPE tokenizer                     β”‚
β”‚    - Process line-by-line                   β”‚
β”‚    - Append EOS tokens                      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                   β”‚
                   β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ 4. Convert to NumPy (uint16)               β”‚
β”‚    - Vocab size: 32,000 fits in uint16     β”‚
β”‚    - Memory efficient (2 bytes/token)       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                   β”‚
                   β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ 5. Train/Val Split (90/10)                 β”‚
β”‚    - train.bin: 325M tokens                 β”‚
β”‚    - val.bin: 36M tokens                    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

## Data Preparation Script

**File**: `prepare_data.py`

```python
import numpy as np
from transformers import AutoTokenizer
from tqdm import tqdm

# 1. Load tokenizer
tokenizer = AutoTokenizer.from_pretrained("Tokenizer/BPE")
eos_id = tokenizer.eos_token_id

# 2. Read corpus
with open("data/raw/merged_text/corpus.txt") as f:
    lines = f.readlines()

# 3. Tokenize
all_tokens = []
for line in tqdm(lines):
    tokens = tokenizer.encode(line.strip())
    tokens.append(eos_id)  # Mark end of line
    all_tokens.extend(tokens)

# 4. Convert to uint16
ids = np.array(all_tokens, dtype=np.uint16)

# 5. Split
val_count = int(len(ids) * 0.1)
train_ids = ids[:-val_count]
val_ids = ids[-val_count:]

# 6. Save
train_ids.tofile("data/bin/train.bin")
val_ids.tofile("data/bin/val.bin")
```

## Example: Text β†’ Tokens

**Input Text** (`corpus.txt`):
```
The quick brown fox jumps over the lazy dog.
Machine learning is transforming the world.
```

**Tokenization Process**:

```
Line 1: "The quick brown fox jumps over the lazy dog."
  Tokens: [1, 334, 3855, 288, 267, 2959, 354, 267, 12397, 8885, 2]
          [<s>, The, quick, brown, fox, jumps, over, the, lazy, dog, </s>]

Line 2: "Machine learning is transforming the world."
  Tokens: [1, 5234, 1234, 456, 7890, 267, 9876, 2]
          [<s>, Machine, learning, is, transforming, the, world, </s>]

Combined: [1, 334, 3855, ..., 2, 1, 5234, ..., 2]
```

**Binary Format**:

```
train.bin structure:
  Byte 0-1:   Token 0 (uint16)
  Byte 2-3:   Token 1 (uint16)
  Byte 4-5:   Token 2 (uint16)
  ...
  Byte N-2:N  Token N/2 (uint16)

Total size: 325,004,796 tokens Γ— 2 bytes = ~650 MB
```

## Dataset Statistics

### Corpus Size

```
Raw Text:
  - Total files: 19
  - Total size: ~1.4 GB
  - Total lines: ~5.2M

Tokenized:
  - Total tokens: 361,116,440
  - Train tokens: 325,004,796 (90%)
  - Val tokens: 36,111,644 (10%)
```

## Usage

### Prepare Data

```bash
# Tokenize corpus
python data/prepare_data.py
```

**Output:**
```
Loading tokenizer from Tokenizer/BPE...
Vocab size: 32000
EOS ID: 2
Reading data/raw/merged_text/corpus.txt...
Total lines: 5,234,567
Tokenizing...
100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 5.2M/5.2M [02:34<00:00]
Total tokens: 361,116,440
Train tokens: 325,004,796
Val tokens:   36,111,644
βœ… Saved binary files to data/bin/
```

### Load in Training

```python
from train.dataloader import DataLoader

loader = DataLoader("data/bin", batch_size=16, block_size=512, split="train")
x, y = loader.get_batch(device="cuda")

# x: [16, 512] input tokens
# y: [16, 512] target tokens (shifted by 1)
```

## Memory-Mapped Loading

The binary files are loaded using `np.memmap` for efficiency:

```python
# Traditional loading (BAD)
data = np.fromfile("train.bin", dtype=np.uint16)  # Loads 650MB into RAM!

# Memory-mapped loading (GOOD)
data = np.memmap("train.bin", dtype=np.uint16, mode='r')  # OS handles paging
```

**Benefits:**
- **No RAM overhead**: File stays on disk
- **Fast random access**: OS caches hot pages
- **Scalable**: Works with TB-scale datasets

## References

- [The Pile: An 800GB Dataset](https://arxiv.org/abs/2101.00027)
- [Data Quality for Language Models](https://arxiv.org/abs/2201.06009)
- [Efficient Data Loading](https://pytorch.org/docs/stable/data.html)