File size: 10,806 Bytes
7f974df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
"""
finetune/prepare_data.py

Downloads teknium/OpenHermes-2.5 from HuggingFace, formats conversations
as ChatML, tokenizes with our custom tokenizer + 2 new special tokens,
and saves train_sft.pt / val_sft.pt to finetune/data/.

Also saves the tokenizer (with special tokens baked in) to finetune/data/
so sft_train.py and chat.py can load it without re-adding tokens.

Usage:
    python finetune/prepare_data.py
    python finetune/prepare_data.py --n_samples 50000

Dataset structure (OpenHermes-2.5):
    Each row has a "conversations" key:
    [
        {"from": "system",  "value": "..."},   # optional
        {"from": "human",   "value": "..."},
        {"from": "gpt",     "value": "..."},
        ...                                     # may have more turns
    ]
"""

import os
import sys
import json
import random
import argparse
from pathlib import Path

import torch
from transformers import PreTrainedTokenizerFast
from datasets import load_dataset
from tqdm import tqdm

# ------------------------------------------------------------------ #
#  Paths  (relative to project root, not this script)
# ------------------------------------------------------------------ #

SCRIPT_DIR   = Path(__file__).resolve().parent
PROJECT_ROOT = SCRIPT_DIR.parent

sys.path.insert(0, str(PROJECT_ROOT))

TOKENIZER_DIR = PROJECT_ROOT / "tokenizer" / "fineweb_edu_tokenizer"

# The two new tokens that define ChatML structure
SPECIAL_TOKENS = ["<|im_start|>", "<|im_end|>"]

MAX_LENGTH = 1024   # model context_length β€” truncate anything longer

# Map OpenHermes role names β†’ ChatML role names
ROLE_MAP = {
    "system":    "system",
    "human":     "user",
    "gpt":       "assistant",
    "user":      "user",
    "assistant": "assistant",
}


# ------------------------------------------------------------------ #
#  TOKENIZER
# ------------------------------------------------------------------ #

def load_and_extend_tokenizer() -> PreTrainedTokenizerFast:
    """
    Loads our pretrained BPE tokenizer and adds the two ChatML tokens.
    Returns the extended tokenizer (vocab 32,000 β†’ 32,002).
    """
    tokenizer = PreTrainedTokenizerFast.from_pretrained(str(TOKENIZER_DIR))

    new_tokens = [t for t in SPECIAL_TOKENS if t not in tokenizer.get_vocab()]
    if new_tokens:
        added = tokenizer.add_special_tokens({"additional_special_tokens": new_tokens})
        print(f"  Added {added} special token(s): {new_tokens}")
    else:
        print("  Special tokens already present β€” skipping add.")

    print(f"  Final vocab size: {len(tokenizer):,}")
    return tokenizer


# ------------------------------------------------------------------ #
#  FORMAT + TOKENIZE ONE CONVERSATION
# ------------------------------------------------------------------ #

def format_and_tokenize(
    conversations: list[dict],
    tokenizer:     PreTrainedTokenizerFast,
) -> tuple[list[int], list[int]] | None:
    """
    Converts a list of chat turns into (input_ids, labels).

    ChatML format per turn:
        <|im_start|>{role}\\n{content}<|im_end|>\\n

    Labels:
        - User / system turns  β†’ all -100  (not learned)
        - Assistant turns      β†’ header (-100) + content (actual token ids)
          i.e. we learn the response but not the "<|im_start|>assistant\\n" prefix

    Returns None for:
        - Conversations with no assistant turns (nothing to learn)
        - Conversations that tokenize to fewer than 8 tokens
    """
    input_ids: list[int] = []
    labels:    list[int] = []

    for turn in conversations:
        role_raw = turn.get("from", turn.get("role", "")).strip().lower()
        content  = turn.get("value", turn.get("content", "")).strip()
        role     = ROLE_MAP.get(role_raw, role_raw)

        if not content or not role:
            continue

        # ---- header: <|im_start|>role\n  β€” never labeled ----------- #
        header_text = f"<|im_start|>{role}\n"
        header_ids  = tokenizer.encode(header_text, add_special_tokens=False)

        # ---- body: content<|im_end|>\n ------------------------------ #
        body_text = f"{content}<|im_end|>\n"
        body_ids  = tokenizer.encode(body_text, add_special_tokens=False)

        turn_input  = header_ids + body_ids

        if role == "assistant":
            # Teach the model the body (response + im_end), not the header
            turn_labels = [-100] * len(header_ids) + body_ids
        else:
            # User / system: no learning signal
            turn_labels = [-100] * len(turn_input)

        input_ids.extend(turn_input)
        labels.extend(turn_labels)

    # Must have at least one labeled token to be a valid training example
    if not any(l != -100 for l in labels):
        return None

    # Truncate to context window
    input_ids = input_ids[:MAX_LENGTH]
    labels    = labels[:MAX_LENGTH]

    # Skip micro-sequences (likely malformed)
    if len(input_ids) < 8:
        return None

    return input_ids, labels


# ------------------------------------------------------------------ #
#  ARG PARSING
# ------------------------------------------------------------------ #

def parse_args():
    p = argparse.ArgumentParser(description="Prepare SFT data from OpenHermes-2.5")
    p.add_argument("--n_samples",   type=int,   default=80_000,
                   help="Number of conversations to sample (default: 80000)")
    p.add_argument("--val_ratio",   type=float, default=0.05,
                   help="Fraction held out for validation (default: 0.05)")
    p.add_argument("--output_dir",  type=str,   default=str(SCRIPT_DIR / "data"),
                   help="Where to save train_sft.pt, val_sft.pt, and tokenizer")
    p.add_argument("--seed",        type=int,   default=42)
    return p.parse_args()


# ------------------------------------------------------------------ #
#  MAIN
# ------------------------------------------------------------------ #

def main():
    args = parse_args()
    random.seed(args.seed)
    os.makedirs(args.output_dir, exist_ok=True)

    print("\n" + "=" * 60)
    print("  SLLM-150M SFT β€” Data Preparation")
    print("=" * 60)

    # ---------------------------------------------------------------- #
    # 1. Tokenizer
    # ---------------------------------------------------------------- #
    print("\n[1/4] Loading tokenizer + adding ChatML special tokens...")
    tokenizer = load_and_extend_tokenizer()

    # Save the extended tokenizer to data dir so training/chat can load it
    tokenizer.save_pretrained(args.output_dir)
    print(f"  Extended tokenizer saved β†’ {args.output_dir}/")

    # ---------------------------------------------------------------- #
    # 2. Dataset download
    # ---------------------------------------------------------------- #
    print(f"\n[2/4] Loading teknium/OpenHermes-2.5 from HuggingFace...")
    ds = load_dataset("teknium/OpenHermes-2.5")
    full = ds["train"]   # only split in this dataset
    print(f"  Full dataset size: {len(full):,} examples")

    # Sample a subset
    n = min(args.n_samples, len(full))
    indices = random.sample(range(len(full)), n)
    subset  = full.select(indices)
    print(f"  Sampled: {n:,} examples (seed={args.seed})")

    # ---------------------------------------------------------------- #
    # 3. Tokenize
    # ---------------------------------------------------------------- #
    print(f"\n[3/4] Formatting and tokenizing conversations...")

    all_input_ids: list[torch.Tensor] = []
    all_labels:    list[torch.Tensor] = []
    skipped = 0

    for example in tqdm(subset, desc="Tokenizing", unit="conv"):
        conversations = example.get("conversations", [])
        result = format_and_tokenize(conversations, tokenizer)

        if result is None:
            skipped += 1
            continue

        ids, lbls = result
        all_input_ids.append(torch.tensor(ids,  dtype=torch.long))
        all_labels.append(   torch.tensor(lbls, dtype=torch.long))

    total = len(all_input_ids)
    print(f"\n  Kept   : {total:,}")
    print(f"  Skipped: {skipped:,}  (no assistant turn or too short)")

    if total == 0:
        raise RuntimeError("No valid examples produced β€” check dataset structure.")

    # Print a sample so we can visually verify
    print("\n  ── Sample (first conversation, first 400 chars) ──")
    sample_decoded = tokenizer.decode(all_input_ids[0].tolist(), skip_special_tokens=False)
    print("  " + sample_decoded[:400].replace("\n", "\n  "))
    print()

    # ---------------------------------------------------------------- #
    # 4. Split + save
    # ---------------------------------------------------------------- #
    print(f"[4/4] Splitting and saving...")

    perm    = list(range(total))
    random.shuffle(perm)
    val_n   = max(1, int(total * args.val_ratio))
    train_n = total - val_n

    train_ids = [all_input_ids[i] for i in perm[:train_n]]
    train_lbl = [all_labels[i]    for i in perm[:train_n]]
    val_ids   = [all_input_ids[i] for i in perm[train_n:]]
    val_lbl   = [all_labels[i]    for i in perm[train_n:]]

    train_path = os.path.join(args.output_dir, "train_sft.pt")
    val_path   = os.path.join(args.output_dir, "val_sft.pt")

    torch.save({"input_ids": train_ids, "labels": train_lbl}, train_path)
    torch.save({"input_ids": val_ids,   "labels": val_lbl},   val_path)

    # Stats
    lengths       = [len(x) for x in all_input_ids]
    label_ratios  = [(t != -100).float().mean().item() for t in all_labels]
    avg_len       = sum(lengths) / len(lengths)
    avg_lbl_ratio = sum(label_ratios) / len(label_ratios)

    print(f"\n  train_sft.pt  : {train_n:,} examples")
    print(f"  val_sft.pt    : {val_n:,}   examples")
    print(f"\n  Avg seq length         : {avg_len:.0f} tokens  (max={max(lengths)})")
    print(f"  Avg assistant ratio    : {avg_lbl_ratio:.1%}  of tokens are labeled")

    # Save metadata for reference
    meta = {
        "dataset":        "teknium/OpenHermes-2.5",
        "n_sampled":      n,
        "n_train":        train_n,
        "n_val":          val_n,
        "vocab_size":     len(tokenizer),
        "special_tokens": SPECIAL_TOKENS,
        "max_length":     MAX_LENGTH,
        "seed":           args.seed,
    }
    with open(os.path.join(args.output_dir, "meta.json"), "w") as f:
        json.dump(meta, f, indent=2)
    print(f"\n  meta.json saved β†’ {args.output_dir}/meta.json")

    print("\n" + "=" * 60)
    print("  Data preparation complete!")
    print("=" * 60)
    print(f"""
Next step:
    python finetune/sft_train.py \\
        --base_ckpt runs/sllm_150m/ckpt_0011500.pt \\
        --run_dir   runs/sllm_150m_chat \\
        --max_steps 2000 \\
        --batch_size 4 --grad_accum 8 \\
        --grad_checkpoint
""")


if __name__ == "__main__":
    main()