A straightforward method for training your LLM, from downloading data to generating text.
Transformer Architecture
FareedKhan-dev65779ddc1599
diff --git a/src/__init__.py b/src/__init__.pynew file mode 100644index 0000000..e69de29diff --git a/src/models/__init__.py b/src/models/__init__.pynew file mode 100644index 0000000..dab85fd--- /dev/null+++ b/src/models/__init__.py@@ -0,0 +1,5 @@+# This file makes the 'src.models' directory a Python package.+from .mlp import MLP+from .attention import Head, MultiHeadAttention+from .transformer_block import Block+from .transformer import Transformer\ No newline at end of filediff --git a/src/models/attention.py b/src/models/attention.pynew file mode 100644index 0000000..608c42a--- /dev/null+++ b/src/models/attention.py@@ -0,0 +1,110 @@+import torch+import torch.nn as nn+import torch.nn.functional as F+import math++class Head(nn.Module):+ """+ A single attention head.++ This module calculates attention scores and applies them to the values.+ It includes key, query, and value projections, and uses causal masking+ to prevent attending to future tokens.++ Args:+ head_size (int): The dimensionality of the key, query, and value projections.+ n_embed (int): The dimensionality of the input embedding.+ context_length (int): The maximum length of the input sequence, used for causal masking.+ """+ def __init__(self, head_size, n_embed, context_length):+ """+ Initializes the attention head.++ Args:+ head_size (int): The dimensionality of the key, query, and value projections.+ n_embed (int): The dimensionality of the input embedding.+ context_length (int): The maximum length of the input sequence.+ """+ super().__init__()+ self.key = nn.Linear(n_embed, head_size, bias=False) # Key projection+ self.query = nn.Linear(n_embed, head_size, bias=False) # Query projection+ self.value = nn.Linear(n_embed, head_size, bias=False) # Value projection+ # Lower triangular matrix for causal masking+ self.register_buffer('tril', torch.tril(torch.ones(context_length, context_length)))++ def forward(self, x):+ """+ Forward pass through the attention head.++ Args:+ x (torch.Tensor): Input tensor of shape (B, T, C).++ Returns:+ torch.Tensor: Output tensor after applying attention.+ """+ B, T, C = x.shape+ k = self.key(x) # (B, T, head_size)+ q = self.query(x) # (B, T, head_size)+ scale_factor = 1 / math.sqrt(C)+ # Calculate attention weights: (B, T, head_size) @ (B, head_size, T) -> (B, T, T)+ attn_weights = q @ k.transpose(-2, -1) * scale_factor+ # Apply causal masking+ attn_weights = attn_weights.masked_fill(self.tril[:T, :T] == 0, float('-inf'))+ attn_weights = F.softmax(attn_weights, dim=-1)+ v = self.value(x) # (B, T, head_size)+ # Apply attention weights to values+ out = attn_weights @ v # (B, T, T) @ (B, T, head_size) -> (B, T, head_size)+ return out++class MultiHeadAttention(nn.Module):+ """+ Multi-Head Attention module.++ This module combines multiple attention heads in parallel. The outputs of each head+ are concatenated to form the final output.++ Args:+ n_head (int): The number of parallel attention heads.+ n_embed (int): The dimensionality of the input embedding.+ context_length (int): The maximum length of the input sequence.+ """+ def __init__(self, n_head, n_embed, context_length):+ """+ Initializes the multi-head attention module.++ Args:+ n_head (int): The number of parallel attention heads.+ n_embed (int): The dimensionality of the input embedding.+ context_length (int): The maximum length of the input sequence.+ """+ super().__init__()+ self.heads = nn.ModuleList([Head(n_embed // n_head, n_embed, context_length) for _ in range(n_head)])++ def forward(self, x):+ """+ Forward pass through the multi-head attention.++ Args:+ x (torch.Tensor): Input tensor of shape (B, T, C).++ Returns:+ torch.Tensor: Output tensor after concatenating the outputs of all heads.+ """+ # Concatenate the output of each head along the last dimension (C)+ x = torch.cat([h(x) for h in self.heads], dim=-1)+ return x++if __name__ == '__main__':+ # Example Usage (optional, for testing the module independently)+ batch_size = 2+ sequence_length = 5+ embedding_dim = 32+ num_heads = 4+ context_len = 5+ input_tensor = torch.randn(batch_size, sequence_length, embedding_dim)++ multihead_attn = MultiHeadAttention(n_head=num_heads, n_embed=embedding_dim, context_length=context_len)+ output_tensor = multihead_attn(input_tensor)++ print("MultiHeadAttention Input Shape:", input_tensor.shape)+ print("MultiHeadAttention Output Shape:", output_tensor.shape)\ No newline at end of filediff --git a/src/models/mlp.py b/src/models/mlp.pynew file mode 100644index 0000000..c45f89a--- /dev/null+++ b/src/models/mlp.py@@ -0,0 +1,79 @@+import torch+import torch.nn as nn++class MLP(nn.Module):+ """+ A simple Multi-Layer Perceptron with one hidden layer.++ This module is used within the Transformer block for feed-forward processing.+ It expands the input embedding size, applies a ReLU activation, and then projects it back+ to the original embedding size.++ Args:+ n_embed (int): The dimensionality of the input embedding.+ """+ def __init__(self, n_embed):+ """+ Initializes the MLP module.++ Args:+ n_embed (int): The dimensionality of the input embedding.+ """+ super().__init__()+ self.hidden = nn.Linear(n_embed, 4 * n_embed) # Linear layer to expand embedding size+ self.relu = nn.ReLU() # ReLU activation function+ self.proj = nn.Linear(4 * n_embed, n_embed) # Linear layer to project back to original size++ def forward(self, x):+ """+ Forward pass through the MLP.++ Args:+ x (torch.Tensor): Input tensor of shape (B, T, C), where B is batch size,+ T is sequence length, and C is embedding size.++ Returns:+ torch.Tensor: Output tensor of the same shape as the input.+ """+ x = self.forward_embedding(x)+ x = self.project_embedding(x)+ return x++ def forward_embedding(self, x):+ """+ Applies the hidden linear layer followed by ReLU activation.++ Args:+ x (torch.Tensor): Input tensor.++ Returns:+ torch.Tensor: Output after the hidden layer and ReLU.+ """+ x = self.relu(self.hidden(x))+ return x++ def project_embedding(self, x):+ """+ Applies the projection linear layer.++ Args:+ x (torch.Tensor): Input tensor.++ Returns:+ torch.Tensor: Output after the projection layer.+ """+ x = self.proj(x)+ return x++if __name__ == '__main__':+ # Example Usage (optional, for testing the module independently)+ batch_size = 2+ sequence_length = 3+ embedding_dim = 16+ input_tensor = torch.randn(batch_size, sequence_length, embedding_dim)++ mlp_module = MLP(n_embed=embedding_dim)+ output_tensor = mlp_module(input_tensor)++ print("MLP Input Shape:", input_tensor.shape)+ print("MLP Output Shape:", output_tensor.shape)\ No newline at end of filediff --git a/src/models/transformer.py b/src/models/transformer.pynew file mode 100644index 0000000..64d1515--- /dev/null+++ b/src/models/transformer.py@@ -0,0 +1,136 @@+import torch+import torch.nn as nn+import torch.nn.functional as F+from src.models.transformer_block import Block++class Transformer(nn.Module):+ """+ The main Transformer model.++ This class combines token and position embeddings with a sequence of Transformer blocks+ and a final linear layer for language modeling.++ Args:+ n_head (int): The number of attention heads in each transformer block.+ n_embed (int): The dimensionality of the embedding space.+ context_length (int): The maximum length of the input sequence.+ vocab_size (int): The size of the vocabulary.+ N_BLOCKS (int): The number of transformer blocks in the model.+ """+ def __init__(self, n_head, n_embed, context_length, vocab_size, N_BLOCKS):+ """+ Initializes the Transformer model.++ Args:+ n_head (int): Number of attention heads.+ n_embed (int): Embedding dimension.+ context_length (int): Maximum sequence length.+ vocab_size (int): Size of the vocabulary.+ N_BLOCKS (int): Number of transformer blocks.+ """+ super().__init__()+ self.context_length = context_length+ self.N_BLOCKS = N_BLOCKS+ self.token_embed = nn.Embedding(vocab_size, n_embed)+ self.position_embed = nn.Embedding(context_length, n_embed)+ self.attn_blocks = nn.ModuleList([Block(n_head, n_embed, context_length) for _ in range(N_BLOCKS)])+ self.layer_norm = nn.LayerNorm(n_embed)+ self.lm_head = nn.Linear(n_embed, vocab_size)+ self.register_buffer('pos_idxs', torch.arange(context_length))++ def _pre_attn_pass(self, idx):+ """+ Combines token and position embeddings.++ Args:+ idx (torch.Tensor): Input token indices.++ Returns:+ torch.Tensor: Sum of token and position embeddings.+ """+ B, T = idx.shape+ tok_embedding = self.token_embed(idx)+ pos_embedding = self.position_embed(self.pos_idxs[:T])+ return tok_embedding + pos_embedding++ def forward(self, idx, targets=None):+ """+ Forward pass through the Transformer.++ Args:+ idx (torch.Tensor): Input token indices.+ targets (torch.Tensor, optional): Target token indices for loss calculation. Defaults to None.++ Returns:+ tuple: Logits and loss (if targets are provided).+ """+ x = self._pre_attn_pass(idx)+ for block in self.attn_blocks:+ x = block(x)+ x = self.layer_norm(x)+ logits = self.lm_head(x)+ loss = None+ if targets is not None:+ B, T, C = logits.shape+ flat_logits = logits.view(B * T, C)+ targets = targets.view(B * T).long()+ loss = F.cross_entropy(flat_logits, targets)+ return logits, loss++ def forward_embedding(self, idx):+ """+ Forward pass focusing on the embedding and attention blocks.++ Args:+ idx (torch.Tensor): Input token indices.++ Returns:+ tuple: Output after attention blocks and the residual.+ """+ x = self._pre_attn_pass(idx)+ residual = x+ for block in self.attn_blocks:+ x, residual = block.forward_embedding(x)+ return x, residual++ def generate(self, idx, max_new_tokens):+ """+ Generates new tokens given a starting sequence.++ Args:+ idx (torch.Tensor): Initial sequence of token indices.+ max_new_tokens (int): Number of tokens to generate.++ Returns:+ torch.Tensor: The extended sequence of tokens.+ """+ for _ in range(max_new_tokens):+ idx_cond = idx[:, -self.context_length:]+ logits, _ = self(idx_cond)+ logits = logits[:, -1, :]+ probs = F.softmax(logits, dim=-1)+ idx_next = torch.multinomial(probs, num_samples=1)+ idx = torch.cat((idx, idx_next), dim=1)+ return idx++if __name__ == '__main__':+ # Example Usage (optional, for testing the module independently)+ batch_size = 2+ sequence_length = 5+ vocab_size = 100+ embedding_dim = 32+ num_heads = 4+ num_blocks = 2+ context_len = 5+ input_indices = torch.randint(0, vocab_size, (batch_size, sequence_length))++ transformer_model = Transformer(n_head=num_heads, n_embed=embedding_dim, context_length=context_len, vocab_size=vocab_size, N_BLOCKS=num_blocks)+ logits, loss = transformer_model(input_indices, targets=input_indices) # Using input as target for simplicity++ print("Transformer Logits Shape:", logits.shape)+ print("Transformer Loss:", loss)++ # Example of generating tokens+ start_indices = input_indices[:, :1] # Take the first token of each sequence as start+ generated_tokens = transformer_model.generate(start_indices, max_new_tokens=5)+ print("Generated Tokens Shape:", generated_tokens.shape)\ No newline at end of filediff --git a/src/models/transformer_block.py b/src/models/transformer_block.pynew file mode 100644index 0000000..de00b74--- /dev/null+++ b/src/models/transformer_block.py@@ -0,0 +1,76 @@+import torch+import torch.nn as nn+from src.models.attention import MultiHeadAttention+from src.models.mlp import MLP++class Block(nn.Module):+ """+ A single Transformer block.++ This block consists of a multi-head attention layer followed by an MLP,+ with layer normalization and residual connections.++ Args:+ n_head (int): The number of attention heads in the multi-head attention layer.+ n_embed (int): The dimensionality of the input embedding.+ context_length (int): The maximum length of the input sequence.+ """+ def __init__(self, n_head, n_embed, context_length):+ """+ Initializes the Transformer block.++ Args:+ n_head (int): The number of attention heads.+ n_embed (int): The dimensionality of the embedding space.+ context_length (int): The maximum sequence length.+ """+ super().__init__()+ self.ln1 = nn.LayerNorm(n_embed)+ self.attn = MultiHeadAttention(n_head, n_embed, context_length)+ self.ln2 = nn.LayerNorm(n_embed)+ self.mlp = MLP(n_embed)++ def forward(self, x):+ """+ Forward pass through the Transformer block.++ Args:+ x (torch.Tensor): Input tensor.++ Returns:+ torch.Tensor: Output tensor after the block.+ """+ # Apply multi-head attention with residual connection+ x = x + self.attn(self.ln1(x))+ # Apply MLP with residual connection+ x = x + self.mlp(self.ln2(x))+ return x++ def forward_embedding(self, x):+ """+ Forward pass focusing on the embedding and attention parts.++ Args:+ x (torch.Tensor): Input tensor.++ Returns:+ tuple: A tuple containing the output after MLP embedding and the residual.+ """+ res = x + self.attn(self.ln1(x))+ x = self.mlp.forward_embedding(self.ln2(res))+ return x, res++if __name__ == '__main__':+ # Example Usage (optional, for testing the module independently)+ batch_size = 2+ sequence_length = 5+ embedding_dim = 32+ num_heads = 4+ context_len = 5+ input_tensor = torch.randn(batch_size, sequence_length, embedding_dim)++ transformer_block = Block(n_head=num_heads, n_embed=embedding_dim, context_length=context_len)+ output_tensor = transformer_block(input_tensor)++ print("Transformer Block Input Shape:", input_tensor.shape)+ print("Transformer Block Output Shape:", output_tensor.shape)\ No newline at end of file