File size: 4,988 Bytes
4f7e31d |
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 |
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers import PreTrainedModel, PretrainedConfig
from transformers.modeling_outputs import CausalLMOutput
# =========================
# Config
# =========================
class TinyWayConfig(PretrainedConfig):
model_type = "tinyway"
def __init__(
self,
vocab_size=50257,
n_positions=256,
n_embd=512,
n_layer=10,
n_head=8,
dropout=0.1,
**kwargs
):
super().__init__(**kwargs)
self.vocab_size = vocab_size
self.n_positions = n_positions
self.n_embd = n_embd
self.n_layer = n_layer
self.n_head = n_head
self.dropout = dropout
# 🔥 HuggingFace-required aliases
self.hidden_size = n_embd
self.num_hidden_layers = n_layer
self.num_attention_heads = n_head
self.max_position_embeddings = n_positions
# =========================
# Causal Self-Attention
# =========================
class CausalSelfAttention(nn.Module):
def __init__(self, config):
super().__init__()
assert config.n_embd % config.n_head == 0
self.n_head = config.n_head
self.head_dim = config.n_embd // config.n_head
self.qkv = nn.Linear(config.n_embd, 3 * config.n_embd)
self.proj = nn.Linear(config.n_embd, config.n_embd)
self.attn_dropout = nn.Dropout(config.dropout)
self.proj_dropout = nn.Dropout(config.dropout)
self.register_buffer(
"mask",
torch.tril(
torch.ones(
config.n_positions,
config.n_positions,
dtype=torch.bool
)
)
)
self.last_attn = None
def forward(self, x):
B, T, C = x.shape
qkv = self.qkv(x)
q, k, v = qkv.chunk(3, dim=-1)
q = q.view(B, T, self.n_head, self.head_dim).transpose(1, 2)
k = k.view(B, T, self.n_head, self.head_dim).transpose(1, 2)
v = v.view(B, T, self.n_head, self.head_dim).transpose(1, 2)
att = (q @ k.transpose(-2, -1)) / math.sqrt(self.head_dim)
att = att.masked_fill(
~self.mask[:T, :T],
torch.finfo(att.dtype).min
)
att = F.softmax(att, dim=-1)
self.last_attn = att.detach()
att = self.attn_dropout(att)
out = att @ v
out = out.transpose(1, 2).contiguous().view(B, T, C)
out = self.proj(out)
out = self.proj_dropout(out)
return out
# =========================
# Transformer Block
# =========================
class Block(nn.Module):
def __init__(self, config):
super().__init__()
self.ln1 = nn.LayerNorm(config.n_embd)
self.attn = CausalSelfAttention(config)
self.ln2 = nn.LayerNorm(config.n_embd)
# 🔥 FFN EXACTLY MATCHES TRAINING
self.ffn = nn.Sequential(
nn.Linear(config.n_embd, 4 * config.n_embd),
nn.GELU(),
nn.Linear(4 * config.n_embd, config.n_embd),
nn.Dropout(config.dropout),
)
def forward(self, x):
x = x + self.attn(self.ln1(x))
x = x + self.ffn(self.ln2(x))
return x
# =========================
# TinyWay Language Model
# =========================
class TinyWayForCausalLM(PreTrainedModel):
config_class = TinyWayConfig
def __init__(self, config):
super().__init__(config)
self.token_emb = nn.Embedding(config.vocab_size, config.n_embd)
self.pos_emb = nn.Embedding(config.n_positions, config.n_embd)
self.blocks = nn.ModuleList([
Block(config) for _ in range(config.n_layer)
])
self.ln = nn.LayerNorm(config.n_embd)
self.head = nn.Linear(
config.n_embd,
config.vocab_size,
bias=False
)
# weight tying
self.head.weight = self.token_emb.weight
self.dropout = nn.Dropout(config.dropout)
self.post_init()
def forward(
self,
input_ids,
labels=None,
attention_mask=None, # intentionally unused (causal LM)
**kwargs # 🔥 accept return_dict, use_cache, etc.
):
B, T = input_ids.shape
pos = torch.arange(T, device=input_ids.device)
x = self.token_emb(input_ids) + self.pos_emb(pos)
x = self.dropout(x)
for block in self.blocks:
x = block(x)
x = self.ln(x)
logits = self.head(x)
loss = None
if labels is not None:
loss = F.cross_entropy(
logits.view(-1, logits.size(-1)),
labels.view(-1)
)
return CausalLMOutput(
loss=loss,
logits=logits
)
def prepare_inputs_for_generation(self, input_ids, **kwargs):
return {"input_ids": input_ids}
|