Spaces:
Running
Running
File size: 6,322 Bytes
9e582c5 |
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 |
import torch
import torch.nn as nn
import torch.nn.functional as F
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
encoder_block_size = 33
decoder_block_size = 30
class Head(nn.Module):
""" one self-attention head """
def __init__(self, n_embd, d_k, dropout, mask=0): # d_k is dimention of key , nomaly d_k = n_embd / 4
super().__init__()
self.mask = mask
self.key = nn.Linear(n_embd, d_k, bias=False, device=device)
self.query = nn.Linear(n_embd, d_k, bias=False, device=device)
self.value = nn.Linear(n_embd, d_k, bias=False, device=device)
if mask:
self.register_buffer('tril', torch.tril(torch.ones(encoder_block_size, encoder_block_size, device=device)))
self.dropout = nn.Dropout(dropout)
def forward(self, x, encoder_output = None):
B,T,C = x.shape
if encoder_output is not None:
k = self.key(encoder_output)
Be, Te, Ce = encoder_output.shape
else:
k = self.key(x) # (B,T,d_k)
q = self.query(x) # (B,T,d_k)
# compute attention scores
wei = q @ k.transpose(-2, -1) * C**-0.5 # (B,T,T)
if self.mask:
if encoder_output is not None:
wei = wei.masked_fill(self.tril[:T, :Te] == 0, float('-inf')) # (B,T,T)
else:
wei = wei.masked_fill(self.tril[:T, :T] == 0, float('-inf')) # (B,T,T)
wei = F.softmax(wei, dim=-1)
wei = self.dropout(wei)
# perform weighted aggregation of values
if encoder_output is not None:
v = self.value(encoder_output)
else:
v = self.value(x)
out = wei @ v # (B,T,C)
return out
class MultiHeadAttention(nn.Module):
""" multiple self attention heads in parallel """
def __init__(self, n_embd, num_head, d_k, dropout, mask=0):
super().__init__()
self.heads = nn.ModuleList([Head(n_embd, d_k, dropout, mask) for _ in range(num_head)])
self.proj = nn.Linear(n_embd, n_embd)
self.dropout = nn.Dropout(dropout)
def forward(self, x, encoder_output=None):
out = torch.cat([h(x, encoder_output) for h in self.heads], dim=-1)
out = self.dropout(self.proj(out))
return out
class FeedForward(nn.Module):
""" multiple self attention heads in parallel """
def __init__(self, n_embd, dropout):
super().__init__()
self.net = nn.Sequential(
nn.Linear(n_embd, 4 * n_embd),
nn.ReLU(),
nn.Linear(4 * n_embd, n_embd),
nn.Dropout(dropout)
)
def forward(self, x):
return self.net(x)
class encoderBlock(nn.Module):
""" Tranformer encoder block : communication followed by computation """
def __init__(self, n_embd, n_head, dropout):
super().__init__()
d_k = n_embd // n_head
self.sa = MultiHeadAttention(n_embd, n_head, d_k, dropout)
self.ffwd = FeedForward(n_embd, dropout)
self.ln1 = nn.LayerNorm(n_embd)
self.ln2 = nn.LayerNorm(n_embd)
def forward(self, x, encoder_output=None):
x = x + self.sa(self.ln1(x), encoder_output)
x = x + self.ffwd(self.ln2(x))
return x
class Encoder(nn.Module):
def __init__(self, n_embd, n_head, n_layers, dropout):
super().__init__()
self.token_embedding_table = nn.Embedding(input_vocab_size, n_embd) # n_embd: input embedding dimension
self.position_embedding_table = nn.Embedding(encoder_block_size, n_embd)
self.blocks = nn.Sequential(*[encoderBlock(n_embd, n_head, dropout) for _ in range(n_layers)])
self.ln_f = nn.LayerNorm(n_embd) # final layer norm
def forward(self, idx):
B, T = idx.shape
tok_emb = self.token_embedding_table(idx) # (B,T,n_embd)
pos_emb = self.position_embedding_table(torch.arange(T, device=device)) # (T,n_embd)
x = tok_emb + pos_emb # (B,T,n_embd)
x = self.blocks(x) # apply one attention layer (B,T,C)
x = self.ln_f(x) # (B,T,C)
return x
class decoderBlock(nn.Module):
""" Tranformer decoder block : self communication then cross communication followed by computation """
def __init__(self, n_embd, n_head, dropout):
super().__init__()
d_k = n_embd // n_head
self.sa = MultiHeadAttention(n_embd, n_head, d_k, dropout, mask = 1)
self.ca = MultiHeadAttention(n_embd, n_head, d_k, dropout, mask = 1)
self.ffwd = FeedForward(n_embd, dropout)
self.ln1 = nn.LayerNorm(n_embd, device=device)
self.ln2 = nn.LayerNorm(n_embd, device=device)
self.ln3 = nn.LayerNorm(n_embd, device=device)
def forward(self, x_encoder_output):
x = x_encoder_output[0]
encoder_output = x_encoder_output[1]
x = x + self.sa(self.ln1(x))
x = x + self.ca(self.ln2(x), encoder_output)
x = x + self.ffwd(self.ln3(x))
return (x,encoder_output)
class Decoder(nn.Module):
def __init__(self, n_embd, n_head, n_layers, dropout):
super().__init__()
self.token_embedding_table = nn.Embedding(output_vocab_size, n_embd) # n_embd: input embedding dimension
self.position_embedding_table = nn.Embedding(decoder_block_size, n_embd)
self.blocks = nn.Sequential(*[decoderBlock(n_embd, n_head=n_head, dropout=dropout) for _ in range(n_layers)])
self.ln_f = nn.LayerNorm(n_embd) # final layer norm
self.lm_head = nn.Linear(n_embd, output_vocab_size)
def forward(self, idx, encoder_output, targets=None):
B, T = idx.shape
tok_emb = self.token_embedding_table(idx) # (B,T,n_embd)
pos_emb = self.position_embedding_table(torch.arange(T, device=device)) # (T,n_embd)
x = tok_emb + pos_emb # (B,T,n_embd)
x =self.blocks((x, encoder_output))
x = self.ln_f(x[0]) # (B,T,C)
logits = self.lm_head(x) # (B,T,output_vocab_size)
if targets is None:
loss = None
else:
B, T, C = logits.shape
temp_logits = logits.view(B*T, C)
targets = targets.reshape(B*T)
loss = F.cross_entropy(temp_logits, targets.long())
# print(logits)
# out = torch.argmax(logits)
return logits, loss
|