|
""" |
|
Data preparation script for training nanoGPT on the flytech/python-codes-25k dataset. |
|
This script downloads the dataset, tokenizes it, and creates the binary files needed for training. |
|
""" |
|
|
|
import os |
|
import pickle |
|
import numpy as np |
|
from datasets import load_dataset |
|
from tqdm import tqdm |
|
|
|
def download_and_prepare_code_dataset(): |
|
"""Download and prepare the flytech/python-codes-25k dataset for nanoGPT training.""" |
|
|
|
print("Loading flytech/python-codes-25k dataset...") |
|
dataset = load_dataset("flytech/python-codes-25k") |
|
|
|
print(f"Dataset structure: {dataset}") |
|
print(f"Available splits: {list(dataset.keys())}") |
|
print(f"Train split size: {len(dataset['train'])}") |
|
|
|
|
|
print("\nFirst example structure:") |
|
first_example = dataset['train'][0] |
|
for key, value in first_example.items(): |
|
print(f" {key}: {repr(value[:200])}...") |
|
|
|
|
|
data_dir = os.path.join('data', 'python-codes-25k') |
|
os.makedirs(data_dir, exist_ok=True) |
|
|
|
|
|
print("Extracting code content...") |
|
train_texts = [] |
|
test_texts = [] |
|
|
|
|
|
for item in tqdm(dataset['train'], desc="Processing train split"): |
|
|
|
code = item.get('text', '') or item.get('output', '') or item.get('code', '') |
|
if code and isinstance(code, str) and len(code.strip()) > 0: |
|
train_texts.append(code) |
|
|
|
|
|
print("Splitting data into train and validation sets...") |
|
total_samples = len(train_texts) |
|
split_idx = int(0.9 * total_samples) |
|
|
|
train_texts_final = train_texts[:split_idx] |
|
test_texts = train_texts[split_idx:] |
|
|
|
print(f"Final train samples: {len(train_texts_final)}") |
|
print(f"Validation samples: {len(test_texts)}") |
|
|
|
print(f"Extracted {len(train_texts)} total samples") |
|
|
|
|
|
all_text = '\n'.join(train_texts_final + test_texts) |
|
print(f"Total characters: {len(all_text):,}") |
|
|
|
|
|
print("Creating vocabulary...") |
|
chars = sorted(list(set(all_text))) |
|
vocab_size = len(chars) |
|
print(f"Vocabulary size: {vocab_size}") |
|
|
|
|
|
stoi = {ch: i for i, ch in enumerate(chars)} |
|
itos = {i: ch for i, ch in enumerate(chars)} |
|
|
|
|
|
meta = { |
|
'vocab_size': vocab_size, |
|
'itos': itos, |
|
'stoi': stoi, |
|
} |
|
with open(os.path.join(data_dir, 'meta.pkl'), 'wb') as f: |
|
pickle.dump(meta, f) |
|
print(f"Saved vocabulary to {os.path.join(data_dir, 'meta.pkl')}") |
|
|
|
|
|
print("Tokenizing training data...") |
|
train_ids = [] |
|
for text in tqdm(train_texts_final, desc="Tokenizing train"): |
|
ids = [stoi[c] for c in text] |
|
train_ids.extend(ids) |
|
|
|
|
|
print("Tokenizing test data...") |
|
test_ids = [] |
|
for text in tqdm(test_texts, desc="Tokenizing test"): |
|
ids = [stoi[c] for c in text] |
|
test_ids.extend(ids) |
|
|
|
|
|
train_ids = np.array(train_ids, dtype=np.uint16) |
|
test_ids = np.array(test_ids, dtype=np.uint16) |
|
|
|
train_path = os.path.join(data_dir, 'train.bin') |
|
test_path = os.path.join(data_dir, 'val.bin') |
|
|
|
train_ids.tofile(train_path) |
|
test_ids.tofile(test_path) |
|
|
|
print(f"Saved training data to {train_path} ({len(train_ids):,} tokens)") |
|
print(f"Saved validation data to {test_path} ({len(test_ids):,} tokens)") |
|
|
|
|
|
print(f"\nDataset statistics:") |
|
print(f"Vocabulary size: {vocab_size}") |
|
print(f"Training tokens: {len(train_ids):,}") |
|
print(f"Validation tokens: {len(test_ids):,}") |
|
print(f"Total tokens: {len(train_ids) + len(test_ids):,}") |
|
|
|
|
|
print(f"\nFirst 100 characters in vocabulary:") |
|
print(''.join(chars[:100])) |
|
|
|
return data_dir |
|
|
|
if __name__ == '__main__': |
|
download_and_prepare_code_dataset() |