Skip to content

Instantly share code, notes, and snippets.

@cjams
cjams / bpe-tokenize.py
Created February 21, 2026 00:53
bpe-tokenizer
for s in data_trn[:nr_trn]:
cur_tokens += list(s)
init_token_count = len(cur_tokens)
nr_actual_tokens = len(stoi)
nr_desired_tokens = 5000
print(f"Initial token count: {init_token_count}")
while nr_actual_tokens < nr_desired_tokens:
@cjams
cjams / layernorm.py
Created January 8, 2026 02:37
LayerNorm
class LayerNorm():
def __init__(self, device, num_features):
self.out = None
self.gamma = torch.ones(num_features, device=device)
self.bias = torch.zeros(num_features, device=device)
def __call__(self, x: torch.Tensor):
assert x.ndim == 2
H = x.shape[1]
@cjams
cjams / bengio-train-with-grads.py
Created January 2, 2026 21:45
Training loop with grads
%matplotlib inline
def plot_loss(trn_loss, val_loss=None, title="Loss Curves"):
plt.figure(figsize=(10, 6))
plt.xticks(fontsize=12)
plt.yticks(fontsize=12)
plt.title(title)
legends = []
@cjams
cjams / bengio-deep.py
Created January 2, 2026 21:41
Bengio deep
model = [
Embedding(device=device, num_embeddings=vocab_size, embedding_dim=embed_dim),
Flatten(input_dim1=ctx_window, input_dim2=embed_dim),
Linear(device=device, in_features=ctx_window*embed_dim, out_features=hidden_size, bias=True),
Tanh(),
Linear(device=device, in_features=hidden_size, out_features=hidden_size, bias=True),
Tanh(),
Linear(device=device, in_features=hidden_size, out_features=hidden_size, bias=True),
Tanh(),
Linear(device=device, in_features=hidden_size, out_features=hidden_size, bias=True),
@cjams
cjams / bengio-sample.py
Created December 31, 2025 19:46
Bengio sampling
story = ‘’
ctx = [0] * ctx_window # start with context full of “special” characters
while True:
x = torch.tensor([ctx], device=device)
for layer in model:
x = layer(x)
counts = x.exp()
@cjams
cjams / bengio-perplexity.py
Created December 31, 2025 19:45
Bengio perplexity
# Computing perplexity
with torch.no_grad():
test_strs = data_val[21800:]
total_nll = 0.0
total_tokens = 0
for test_str in test_strs:
seq_nll = 0.0
ctx = [0] * ctx_window
@cjams
cjams / bengio-train.py
Created December 31, 2025 19:43
Bengio train
def plot_loss(trn_loss, val_loss=None, title=”Loss Curves”):
plt.figure(figsize=(10, 6))
plt.xticks(fontsize=12)
plt.yticks(fontsize=12)
plt.title(title)
legends = []
assert len(trn_loss) % 1000 == 0
plt.plot(torch.tensor(trn_loss).view(-1, 1000).mean(dim=1))
@cjams
cjams / basic-bengio.py
Created December 31, 2025 19:41
Bengio basic model
model = [
Embedding(device=device, num_embeddings=vocab_size, embedding_dim=embed_dim),
Flatten(device=device, input_dim1=ctx_window, input_dim2=embed_dim),
Linear(device=device, in_features=ctx_window*embed_dim, out_features=hidden_size, bias=True),
Tanh(device=device),
Linear(device=device, in_features=hidden_size, out_features=vocab_size, bias=False)
]
params = [p for layer in model for p in layer.params()]
@cjams
cjams / hyperparams.py
Created December 31, 2025 19:39
Bengio hyperparams
ctx_window = 8
max_step = 100000
batch_size = 64
embed_dim = 32
hidden_size = 256
lr = 1e-3
vocab_size = len(stoi)
device = torch.device(”cuda” if torch.cuda.is_available() else “cpu”)
@cjams
cjams / layers.py
Created December 31, 2025 19:38
Bengio layers
class Embedding():
def __init__(self, device, num_embeddings, embedding_dim):
self.out = None
self.weight = torch.randn(num_embeddings, embedding_dim, device=device)
def __call__(self, x):
self.out = F.embedding(x, self.weight)
return self.out
def params(self):