import torch import torch.nn as nn import torch.nn.functional as F class SelfAttentionHead(nn.Module): def __init__(self, head_size, n_embed, block_size, dropout=0.2) -> None: super().__init__() self.head_size = head_size self.key = nn.Linear(n_embed, head_size, bias=False) self.query = nn.Linear(n_embed, head_size, bias=False) self.value = nn.Linear(n_embed, head_size, bias=False) self.register_buffer("tril", torch.tril(torch.ones(block_size, block_size))) self.dropout = nn.Dropout(dropout) def forward(self, x): B, T, C = x.shape k = self.key(x) # (B, T, C) q = self.query(x) # (B, T, C) wei = q @ k.transpose(-2, -1) * (C ** -0.5) # (B, T, C) @ (B, C, T) -> (B, T, T) wei = wei.masked_fill(self.tril[:T, :T] == 0, float('-inf')) # (B, T, T) wei = F.softmax(wei, dim=-1) # (B, T, T) wei = self.dropout(wei) v = self.value(x) # (B, T, C) out = wei @ v # (B, T, T) @ (B, T, C) -> (B, T, C) return out class MultiHeadAttention(nn.Module): def __init__(self, num_heads, head_size, n_embed, block_size, dropout=0.2) -> None: super().__init__() self.heads = nn.ModuleList([SelfAttentionHead(head_size, n_embed, block_size) for _ in range(num_heads)]) # self.projection = nn.Linear(num_heads * head_size, n_embed) self.projection = nn.Linear(n_embed, n_embed) self.dropout = nn.Dropout(dropout) def forward(self, x): out = torch.cat([h(x) for h in self.heads], dim=-1) out = self.dropout(self.projection(out)) return out class FeedForwardNet(nn.Module): def __init__(self, n_embed, dropout=0.2) -> None: super().__init__() self.net = nn.Sequential( nn.Linear(n_embed, 4 * n_embed), nn.ReLU(), nn.Linear(4 * n_embed, n_embed), nn.Dropout(dropout) ) def forward(self, x): return self.net(x) class DecoderBlock(nn.Module): def __init__(self, n_embed, num_heads, block_size) -> None: super().__init__() head_size = n_embed // num_heads self.sa_head = MultiHeadAttention(num_heads, head_size, n_embed, block_size) self.ffn = FeedForwardNet(n_embed) self.ln1 = nn.LayerNorm(n_embed) self.ln2 = nn.LayerNorm(n_embed) def forward(self, x): x = x + self.sa_head(self.ln1(x)) x = x + self.ffn(self.ln2(x)) return x