mini-beatrix-2.5s / embedding.py
AbstractPhil's picture
mini-beatrix-2.5s: the 2s core (bit-identical) packaged with its arm library β€” 13 mountable rows over 11 weight files, a self-contained arm runtime verified bit-exact against amoe-lora, and an automodel that loads under current transformers
fc9a663 verified
Raw
History Blame Contribute Delete
1.57 kB
"""Embeddings.
TrigramByteEmbedding β€” the validated composed byte embedding:
e_t = E0[x_t] + E1[x_{t-1}] + E2[x_{t-2}] + P[t]
with the PAD LAW built in permanently: the shift tables carry a dedicated
pad row (index 256). Padding trigram shifts with a legal byte conflates
real history with sequence starts and starves address consumption
(measured +.05..+.11 on repair) β€” the fix ships on, not opt-in.
TokenEmbedding β€” plain table + positions for BPE crafts.
"""
from __future__ import annotations
import torch
import torch.nn as nn
import torch.nn.functional as F
BYTE_VOCAB = 256
PAD_ROW = 256 # dedicated pad index in the shift tables (size 257)
class TrigramByteEmbedding(nn.Module):
def __init__(self, d: int, context: int):
super().__init__()
self.emb0 = nn.Embedding(BYTE_VOCAB, d)
self.emb1 = nn.Embedding(BYTE_VOCAB + 1, d) # + pad row
self.emb2 = nn.Embedding(BYTE_VOCAB + 1, d)
self.pos = nn.Parameter(0.01 * torch.randn(1, context, d))
def forward(self, idx):
x = self.emb0(idx) \
+ self.emb1(F.pad(idx, (1, 0), value=PAD_ROW)[:, :-1]) \
+ self.emb2(F.pad(idx, (2, 0), value=PAD_ROW)[:, :-2])
return x + self.pos[:, : idx.shape[1]]
class TokenEmbedding(nn.Module):
def __init__(self, vocab: int, d: int, context: int):
super().__init__()
self.emb = nn.Embedding(vocab, d)
self.pos = nn.Parameter(0.01 * torch.randn(1, context, d))
def forward(self, idx):
return self.emb(idx) + self.pos[:, : idx.shape[1]]