Spaces:
Sleeping
Sleeping
File size: 8,867 Bytes
f6a4b47 |
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 |
import math
import torch
import torch.nn as nn
from torch.nn import functional as F
from utils import DEVICE
class RMSNorm(torch.nn.Module):
def __init__(self, dim: int, eps: float = 1e-6):
super().__init__()
self.eps = eps
self.weight = nn.Parameter(torch.ones(dim))
def _norm(self, x):
return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
def forward(self, x):
output = self._norm(x.float()).type_as(x)
return output * self.weight
class Attention(nn.Module):
"""
Multi-head Self-Attention with RoPE
"""
def __init__(self, num_heads, head_size, num_embed, dropout):
super().__init__()
self.num_heads = num_heads
self.head_size = head_size
self.wq = nn.Linear(num_embed, num_heads * head_size, bias = False)
self.wk = nn.Linear(num_embed, num_heads * head_size, bias = False)
self.wv = nn.Linear(num_embed, num_heads * head_size, bias = False)
self.wo = nn.Linear(num_heads * head_size, num_embed, bias = False)
inv_freq = 1 / (500000 ** (torch.arange(0, head_size, 2)[: (head_size // 2)].float() / head_size))
self.register_buffer('inv_freq', inv_freq)
self.dropout = nn.Dropout(dropout)
def reshape_for_broadcast(self, freq_cis, x):
ndim = x.ndim
shape = [1] * (ndim - 2) + list(freq_cis.shape)
return freq_cis.view(*shape)
def apply_rope(self, x, position, freq):
t = torch.arange(position, device=freq.device, dtype=torch.float32)
freq = torch.outer(t, freq)
freq_cis = torch.polar(torch.ones_like(freq), freq)
x_ = torch.view_as_complex(x.float().reshape(*x.shape[:-1], -1, 2))
freq_cis = self.reshape_for_broadcast(freq_cis, x)
x_out = torch.view_as_real(x_ * freq_cis).flatten(3)
return x_out.type_as(x)
def forward(self, x):
B, T, C = x.shape
mask = torch.triu(torch.full((T, T), float("-inf"), device=x.device), diagonal=1)
xq, xk, xv = self.wq(x), self.wk(x), self.wv(x)
xq = xq.view(B, T, self.num_heads, self.head_size)
xk = xk.view(B, T, self.num_heads, self.head_size)
xv = xv.view(B, T, self.num_heads, self.head_size)
xq = xq.transpose(1, 2)
xk = xk.transpose(1, 2)
xv = xv.transpose(1, 2)
xq = self.apply_rope(xq, T, self.inv_freq)
xk = self.apply_rope(xk, T, self.inv_freq)
attn_weights = torch.matmul(xq, xk.transpose(2, 3)) / math.sqrt(self.head_size)
attn_weights += mask
attn_weights = F.softmax(attn_weights.float(), dim=-1).type_as(xq)
output = torch.matmul(attn_weights, xv)
output = output.transpose(1, 2).contiguous().view(B, T, C)
return self.dropout(self.wo(output))
class MLP(nn.Module):
"""
Implementation of a Multi-Layer Perceptron (MLP) sub-module.
This module is a simple feed-forward network with two hidden layers
used in various Transformer components like the Mixture of Experts layer.
"""
def __init__(self, num_embed, dropout):
"""
Constructor for the MLP.
Args:
num_embed (int): The number of embedding dimensions.
"""
super().__init__()
hidden = int(4 * num_embed * 2 / 3)
# Define linear layers for the MLP
self.w1 = nn.Linear(num_embed, hidden, bias=False)
self.w2 = nn.Linear(hidden, num_embed, bias=False)
self.dropout = nn.Dropout(dropout)
def forward(self, x):
"""
Forward pass of the MLP.
Args:
x (torch.Tensor): Input tensor of shape (batch_size, seq_len, num_embed).
Returns:
torch.Tensor: Output tensor after passing through the MLP (shape: batch_size, seq_len, num_embed).
"""
return self.dropout(self.w2(F.silu(self.w1(x))))
class TransformerBlock(nn.Module):
"""
This calss will group together MultiHead Attention and
MLP, so that we can copy it in Transformer
"""
def __init__(self, num_heads, head_size, num_embed, dropout):
super().__init__()
self.mha = Attention(
num_heads=num_heads,
head_size=head_size,
num_embed=num_embed,
dropout=dropout
)
self.mlp = MLP(num_embed = num_embed, dropout = dropout)
# add the layer normalization
self.norm1 = RMSNorm(num_embed)
self.norm2 = RMSNorm(num_embed)
def forward(self, x):
"""
Decodes the input sequence.
Args:
x (torch.Tensor): A tensor of shape (batch_size, sequence_length, embedding_dim).
memory (torch.Tensor): A tensor of shape (batch_size, memory_length, embedding_dim).
Returns:
torch.Tensor: A tensor of shape (batch_size, sequence_length, embedding_dim).
"""
#print(x.shape)
x = x + self.mha(self.norm1(x))
x = x + self.mlp(self.norm2(x))
return x
class Transformer(nn.Module):
def __init__(self, **kwargs):
super().__init__()
# a simple lookup table that stores embeddings of a fixed dictionary and size
# each token directly reads off the logits for the next token from a lookup table
# see more: https://pytorch.org/docs/stable/generated/torch.nn.Embedding.html
self.model_type = 'Prome'
self.vocab_size = kwargs.get("vocab_size", 100)
self.num_embed = kwargs.get("num_embed", 32)
self.block_size = kwargs.get("block_size", 8)
self.num_heads = kwargs.get("num_heads", 4)
self.head_size = kwargs.get("head_size", 128)
self.num_layers = kwargs.get("num_layers", 4)
self.dropout = kwargs.get("dropout", 0.2)
self.max_seq_len = kwargs.get("max_sqe_len", 1024)
# each token reads the logits for the next token from a lookup table
self.token_embedding_table = nn.Embedding(self.vocab_size, self.num_embed)
# each position from 0 to block_size-1 will get its embedding
#self.position_embedding_table = nn.Embedding(self.max_seq_len, self.num_embed)
self.decoder = nn.Sequential(
*[
TransformerBlock(
num_heads=self.num_heads,
head_size=self.head_size,
num_embed=self.num_embed,
dropout=self.dropout,
)
for _ in range(self.num_layers)
]
)
self.lm_head = nn.Linear(self.num_embed, self.vocab_size)
def forward(self, idx, targets=None):
B, T = idx.shape
# idx and targets are (B,T) tensor of integers
# the token_emb is (B, T, C), C = NUM_EMBED
x = self.token_embedding_table(idx)
# (T, C)
#posit_emb = self.position_embedding_table(torch.arange(T, device=DEVICE))
#x = token_emb + posit_emb
x = self.decoder(x)
# (B, T, vocab_size)
logits = self.lm_head(x)
# Compute the loss
if targets != None:
# cross_entropy accepts inputs in a (batch_size, num_classes)
# so we need to reformat our logits dimensions to
# (batch_size * time, dim_vocabulary), time = block_size
#logits = logits.to(dtype=torch.float32)
loss = F.cross_entropy(logits.view(-1, logits.size(-1)), targets.view(-1), ignore_index=-1)
else:
loss = None
return logits, loss
def generate(self, idx: torch.Tensor, max_new_tokens: int, temperature: float = 0.6, top_p: float = 0.9):
for _ in range(max_new_tokens):
idx_crop = idx[:, -self.max_seq_len:]
logits, loss = self.forward(idx_crop)
logits = logits[:, -1, :]
if temperature > 0:
probs = F.softmax(logits / temperature, dim=-1)
idx_next = self.sample_top_p(probs, top_p)
else:
probs = F.softmax(logits, dim=-1)
idx_next = torch.multinomial(probs, num_samples=1)
idx = torch.cat((idx, idx_next), dim=1) # (B, T+1)
return idx
def sample_top_p(self, probs: torch.Tensor, top_p: float) -> torch.Tensor:
sorted_probs, sorted_indices = torch.sort(probs, descending=True, dim=-1)
cumulative_probs = torch.cumsum(sorted_probs, dim=-1)
# Create a mask for top-p filtering
top_p_mask = cumulative_probs <= top_p
top_p_mask[..., 1:] = top_p_mask[..., :-1].clone()
top_p_mask[..., 0] = 1
filtered_probs = sorted_probs * top_p_mask
filtered_probs /= filtered_probs.sum(dim=-1, keepdim=True) # Normalize filtered probabilities
next_token = torch.multinomial(filtered_probs, num_samples=1)
return torch.gather(sorted_indices, -1, next_token) |