File size: 13,007 Bytes
5022842 | 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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | from collections import Counter
from tqdm import tqdm
import json
class BPETokenizer:
def __init__(self):
# state: self.merges (dict mapping pair -> new id) and self.vocab (dict mapping id -> bytes)
self.merges = {}
# will map pair-of-token-IDs -> new token ID
# example: {(116, 104): 256} means "the pair (t, h) becomes token 256"
self.vocab = {i: bytes([i]) for i in range(256)}
# the initial 256 tokens are just the 256 possible byte values
# i.e., vocab[97] == b"a", vocab[98] == b"b", etc.
def train(self, text: str, vocab_size: int) -> None:
# ============================================================
# OPTIMIZED BPE TRAINING
# Per-merge cost: O(occurrences of merged pair), not O(total tokens)
# ============================================================
# --- STEP 1: Initialize tokens + linked list pointers ---
tokens = list(text.encode("utf-8"))
n = len(tokens)
# Doubly-linked list pointers so we can splice elements in O(1)
prev = [i - 1 for i in range(n)] # prev[0] = -1 (sentinel: no prev)
next_ = [i + 1 for i in range(n)] # next_[n-1] = n (sentinel: no next)
is_dead = [False] * n # tracks spliced-out positions
# --- STEP 2: One-time O(n) pass to populate pair counts + positions ---
pair_counts = {} # pair -> count
pair_positions = {} # pair -> SET of positions (left index)
for i in range(n - 1):
pair = (tokens[i], tokens[i+1])
pair_counts[pair] = pair_counts.get(pair, 0) + 1
pair_positions.setdefault(pair, set()).add(i)
num_merges = vocab_size - 256
print(f"Initial corpus: {n:,} tokens, {len(pair_counts):,} unique pairs")
print(f"Training {num_merges:,} merges...")
# --- STEP 3: The merge loop ---
for merge_index in tqdm(range(num_merges), desc="Training BPE"):
# 3a. Find max pair (and bail if no pairs left)
if not pair_counts:
break
(a, b) = max(pair_counts, key=pair_counts.get)
if pair_counts[(a, b)] <= 0:
break
new_id = 256 + merge_index
# 3b. Snapshot positions in sorted order, then process each
positions_to_process = sorted(pair_positions[(a, b)])
for pos in positions_to_process:
# 3c. Validity checks — skip dead or stale positions
if is_dead[pos]:
continue
j = next_[pos] # position of the right half of the pair
if j >= n or is_dead[j]:
continue
if tokens[pos] != a or tokens[j] != b:
continue
# 3d. Find neighbors
left_idx = prev[pos] # -1 if no left
right_idx = next_[j] # n if no right
# 3e. Update LEFT neighbor pair counts
# Before: (tokens[left_idx], a) -> After: (tokens[left_idx], new_id)
if left_idx >= 0:
old_left = (tokens[left_idx], a)
new_left = (tokens[left_idx], new_id)
pair_counts[old_left] -= 1
pair_positions[old_left].discard(left_idx)
pair_counts[new_left] = pair_counts.get(new_left, 0) + 1
pair_positions.setdefault(new_left, set()).add(left_idx)
# 3f. Update RIGHT neighbor pair counts
# Before: (b, tokens[right_idx]) at position j
# After: (new_id, tokens[right_idx]) at position pos
if right_idx < n:
old_right = (b, tokens[right_idx])
new_right = (new_id, tokens[right_idx])
pair_counts[old_right] -= 1
pair_positions[old_right].discard(j)
pair_counts[new_right] = pair_counts.get(new_right, 0) + 1
pair_positions.setdefault(new_right, set()).add(pos)
# 3g. Decrement the merged pair's count for THIS position
pair_counts[(a, b)] -= 1
# 3h. Splice — replace pos with new_id, remove j from the list
tokens[pos] = new_id
next_[pos] = right_idx
if right_idx < n:
prev[right_idx] = pos
is_dead[j] = True
# 3i. Clean up: clear the merged pair's position set
pair_positions[(a, b)] = set()
pair_counts[(a, b)] = 0
# 3j. Record the merge in tokenizer state
self.merges[(a, b)] = new_id
self.vocab[new_id] = self.vocab[a] + self.vocab[b]
print(f"Done! Final vocab size: {len(self.vocab):,}")
# All done. tokens, prev, next_, is_dead, pair_counts, pair_positions can be discarded.
def train_naive(self, text: str, vocab_size: int) -> None:
"""Original naive O(n) per merge implementation. Kept for benchmarking."""
# Reset state (so we can call this on a fresh tokenizer)
self.merges = {}
self.vocab = {i: bytes([i]) for i in range(256)}
tokens = list(text.encode("utf-8"))
num_merges = vocab_size - 256
for i in tqdm(range(num_merges), desc="Training BPE (naive)"):
pair_counts = Counter(zip(tokens, tokens[1:]))
if not pair_counts:
break
most_common_pair = pair_counts.most_common(1)[0][0]
new_id = 256 + i
tokens = self._replace_pair(tokens, most_common_pair, new_id)
self.merges[most_common_pair] = new_id
self.vocab[new_id] = self.vocab[most_common_pair[0]] + self.vocab[most_common_pair[1]]
def encode(self, text: str) -> list[int]:
import heapq
# Convert to bytes
tokens = list(text.encode("utf-8"))
n = len(tokens)
if n < 2:
return tokens
# Linked list state (same pattern as train)
prev = [i - 1 for i in range(n)]
next_ = [i + 1 for i in range(n)]
is_dead = [False] * n
# Build initial heap: for every position with an applicable merge,
# push (rank, position) where rank = the merge ID (= merge order learned)
heap = []
for i in range(n-1):
pair = (tokens[i], tokens[i+1])
if pair in self.merges:
heapq.heappush(heap, (self.merges[pair], i))
# Process merges in order of rank
while heap:
rank, pos = heapq.heappop(heap)
# === Validity checks ===
if is_dead[pos]:
continue
j = next_[pos]
if j >= n or is_dead[j]:
continue
# Check the pair is STILL what we expected (might have changed since being pushed)
current_pair = (tokens[pos], tokens[j])
if current_pair not in self.merges or self.merges[current_pair] != rank:
continue
new_id = rank # rank IS the merge's new token ID
# === Splice (same as training) ===
right_idx = next_[j]
tokens[pos] = new_id
next_[pos] = right_idx
if right_idx < n:
prev[right_idx] = pos
is_dead[j] = True
# === Push new pairs created by this merge ===
# Left neighbor + this position
if prev[pos] >= 0:
left_pair = (tokens[prev[pos]], new_id)
if left_pair in self.merges:
heapq.heappush(heap, (self.merges[left_pair], prev[pos]))
# This position + right neighbor
if right_idx < n:
right_pair = (new_id, tokens[right_idx])
if right_pair in self.merges:
heapq.heappush(heap, (self.merges[right_pair], pos))
# === Walk the linked list to extract live tokens in order ===
result = []
i = 0
while i < n:
result.append(tokens[i])
i = next_[i]
return result
def decode(self, ids: list[int]) -> str:
# ids → bytes → utf-8 string
# Look up each ID's bytes, concatenate, decode as UTF-8
all_bytes = b"".join(self.vocab[id] for id in ids)
return all_bytes.decode("utf-8", errors="replace")
# errors="replace" is important — if a model generates a weird
# token sequence that doesn't decode as valid UTF-8, we get a
# replacement character (�) instead of crashing
def save(self, path: str) -> None:
# Convert self.merges (which has tuple keys, not JSON-friendly)
# into a list of plain records that preserve the training order
# Note: dicts preserve insertion order in Python 3.7+,
# so iterating gives merges in the order they were learned
merges_list = [
{"pair": [a, b], "id": new_id}
for (a, b), new_id in self.merges.items()
]
data = {"merges": merges_list}
with open(path, "w") as f:
json.dump(data, f)
def load(self, path: str) -> None:
with open(path) as f:
data = json.load(f)
# Reset state to fresh
self.merges = {}
self.vocab = {i: bytes([i]) for i in range(256)}
# Replay each merge in training order, rebuilding both merges and vocab
for entry in data["merges"]:
a, b = entry["pair"]
new_id = entry["id"]
pair = (a, b)
self.merges[pair] = new_id
# Vocab entry = concatenation of the two halves' bytes
self.vocab[new_id] = self.vocab[a] + self.vocab[b]
def _replace_pair(self, tokens, pair, new_id):
# Walks the tokens list, replacing every occurrence of `pair` with `new_id`.
# Returns a new list (doesn't mutate the input).
result = []
i = 0
while i < len(tokens):
if i < len(tokens) - 1 and tokens[i] == pair[0] and tokens[i+1] == pair[1]:
result.append(new_id)
i = i + 2 # skip past both elements we just merged
else:
result.append(tokens[i])
i = i + 1
return result
if __name__ == "__main__":
import urllib.request
import os
import time
# Download Tiny Shakespeare (cache locally so we only download once)
cache_path = "/tmp/tinyshakespeare.txt"
if not os.path.exists(cache_path):
print("Downloading Tiny Shakespeare...")
url = "https://raw.githubusercontent.com/karpathy/char-rnn/master/data/tinyshakespeare/input.txt"
urllib.request.urlretrieve(url, cache_path)
with open(cache_path) as f:
text = f.read()
print(f"Benchmark text: {len(text):,} chars\n")
vocab_size = 1000 # 744 merges
# === NEW algorithm ===
tok_new = BPETokenizer()
start = time.time()
tok_new.train(text, vocab_size=vocab_size)
new_time = time.time() - start
print(f"\nNEW algorithm: {new_time:.2f}s\n")
# === OLD (naive) algorithm ===
tok_old = BPETokenizer()
start = time.time()
tok_old.train_naive(text, vocab_size=vocab_size)
old_time = time.time() - start
print(f"\nOLD algorithm: {old_time:.2f}s\n")
# === RESULTS ===
print(f"=== Speedup: {old_time / new_time:.1f}x ===")
# Quick correctness check
sample = "To be or not to be"
enc_new = tok_new.encode(sample)
enc_old = tok_old.encode(sample)
print(f"New algorithm encodes '{sample}' as: {enc_new}")
print(f"Old algorithm encodes '{sample}' as: {enc_old}")
print(f"(Different token IDs are expected due to tie-breaking — both are valid)")
text = "the quick brown fox jumps over the lazy dog. " * 100
tok = BPETokenizer()
tok.train(text, vocab_size=300)
encoded = tok.encode("the quick fox")
decoded = tok.decode(encoded)
print(f"Encoded: {encoded}")
print(f"Decoded: '{decoded}'")
print(f"Match: {decoded == 'the quick fox'}")
# Speed test on a bigger chunk
big_text = "the quick brown fox " * 50000 # ~1MB
start = time.time()
encoded = tok.encode(big_text)
elapsed = time.time() - start
print(f"Encoded {len(big_text):,} chars in {elapsed:.2f}s "
f"({len(big_text)/elapsed:,.0f} chars/sec)") |