Upload src/tokenizer.py with huggingface_hub
Browse files- src/tokenizer.py +79 -0
src/tokenizer.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Milestone 1: Character-level tokenizer and data loading.
|
| 3 |
+
|
| 4 |
+
Reads tiny Shakespeare, builds a vocab from all unique characters,
|
| 5 |
+
provides encode/decode, and a get_batch() function for training.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import torch
|
| 9 |
+
|
| 10 |
+
# ββ Config ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 11 |
+
DATA_PATH = "data/input.txt"
|
| 12 |
+
BLOCK_SIZE = 256 # context length (tokens per sample)
|
| 13 |
+
BATCH_SIZE = 64 # samples per batch
|
| 14 |
+
if torch.cuda.is_available():
|
| 15 |
+
DEVICE = "cuda"
|
| 16 |
+
elif torch.backends.mps.is_available():
|
| 17 |
+
DEVICE = "mps"
|
| 18 |
+
else:
|
| 19 |
+
DEVICE = "cpu"
|
| 20 |
+
|
| 21 |
+
# ββ Load data βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 22 |
+
with open(DATA_PATH, "r") as f:
|
| 23 |
+
text = f.read()
|
| 24 |
+
|
| 25 |
+
# ββ Build vocab βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 26 |
+
chars = sorted(set(text))
|
| 27 |
+
VOCAB_SIZE = len(chars)
|
| 28 |
+
|
| 29 |
+
stoi = {ch: i for i, ch in enumerate(chars)} # char -> int
|
| 30 |
+
itos = {i: ch for i, ch in enumerate(chars)} # int -> char
|
| 31 |
+
|
| 32 |
+
def encode(s: str) -> list[int]:
|
| 33 |
+
return [stoi[c] for c in s]
|
| 34 |
+
|
| 35 |
+
def decode(ids: list[int]) -> str:
|
| 36 |
+
return "".join(itos[i] for i in ids)
|
| 37 |
+
|
| 38 |
+
# ββ Train / val split βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 39 |
+
data = torch.tensor(encode(text), dtype=torch.long)
|
| 40 |
+
n = int(0.9 * len(data))
|
| 41 |
+
train_data = data[:n]
|
| 42 |
+
val_data = data[n:]
|
| 43 |
+
|
| 44 |
+
# ββ Batch sampler βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 45 |
+
def get_batch(split: str):
|
| 46 |
+
"""Return a random batch of (x, y) pairs.
|
| 47 |
+
|
| 48 |
+
x: (BATCH_SIZE, BLOCK_SIZE) input token ids
|
| 49 |
+
y: (BATCH_SIZE, BLOCK_SIZE) target token ids (x shifted right by 1)
|
| 50 |
+
"""
|
| 51 |
+
src = train_data if split == "train" else val_data
|
| 52 |
+
ix = torch.randint(len(src) - BLOCK_SIZE, (BATCH_SIZE,))
|
| 53 |
+
x = torch.stack([src[i : i + BLOCK_SIZE ] for i in ix])
|
| 54 |
+
y = torch.stack([src[i + 1 : i + BLOCK_SIZE + 1] for i in ix])
|
| 55 |
+
return x.to(DEVICE), y.to(DEVICE)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
# ββ Quick sanity check ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 59 |
+
if __name__ == "__main__":
|
| 60 |
+
print(f"Dataset length : {len(text):,} characters")
|
| 61 |
+
print(f"Vocab size : {VOCAB_SIZE} unique chars")
|
| 62 |
+
print(f"Train tokens : {len(train_data):,}")
|
| 63 |
+
print(f"Val tokens : {len(val_data):,}")
|
| 64 |
+
print(f"Device : {DEVICE}")
|
| 65 |
+
print()
|
| 66 |
+
|
| 67 |
+
sample = text[:100]
|
| 68 |
+
encoded = encode(sample)
|
| 69 |
+
decoded = decode(encoded)
|
| 70 |
+
print(f"Sample text : {repr(sample[:40])}")
|
| 71 |
+
print(f"Encoded[:10] : {encoded[:10]}")
|
| 72 |
+
print(f"Round-trip OK : {decoded == sample}")
|
| 73 |
+
print()
|
| 74 |
+
|
| 75 |
+
x, y = get_batch("train")
|
| 76 |
+
print(f"Batch x shape : {x.shape} (on {x.device})")
|
| 77 |
+
print(f"Batch y shape : {y.shape} (on {y.device})")
|
| 78 |
+
print(f"x[0,:8] : {x[0,:8].tolist()}")
|
| 79 |
+
print(f"y[0,:8] : {y[0,:8].tolist()} (x shifted by 1)")
|