File size: 8,074 Bytes
51e8dee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))

import torch
import torch.nn as nn
from torch.nn import functional as F
from src.model.config import MiniGPTConfig
from src.model.blocks import Block

class MiniGPT(nn.Module):
    """ The full GPT Language Model, with a context size of config.seq_len. """
    
    def __init__(self, config: MiniGPTConfig):
        """
        Initializes the MiniGPT model.

        Args:
            config (MiniGPTConfig): Configuration object containing model parameters.
        """
        super().__init__()
        assert config.vocab_size is not None
        assert config.seq_len is not None
        self.config = config

        # The core Transformer module dictionary
        self.transformer = nn.ModuleDict(dict(
            # Token Embedding (Vocabulary Size x Hidden Dimension)
            wte = nn.Embedding(config.vocab_size, config.d_model),
            
            # Positional Embedding (Sequence Length x Hidden Dimension)
            wpe = nn.Embedding(config.seq_len, config.d_model),
            
            # Dropout attached to the embeddings
            drop = nn.Dropout(config.dropout),
            
            # The core stack of Transformer blocks
            h = nn.ModuleList([Block(config) for _ in range(config.n_layers)]),
            
            # The final layer normalization
            ln_f = nn.LayerNorm(config.d_model),
        ))
        
        # The Language Modeling Head
        self.lm_head = nn.Linear(config.d_model, config.vocab_size, bias=False)
        
        # Weight tying:
        # GPT-2 shares the weights between the token embedding and the final projection layer.
        # This massively reduces parameters and generally improves performance because
        # tokens map to the same mathematical feature space symmetrically.
        self.transformer.wte.weight = self.lm_head.weight

        # This initializes weights to a scaled normal distribution
        self.apply(self._init_weights)

    def _init_weights(self, module):
        """
        Initialize the weights of the model.
        GPT-2 style initialization.
        """
        if isinstance(module, nn.Linear):
            torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
            if module.bias is not None:
                torch.nn.init.zeros_(module.bias)
        elif isinstance(module, nn.Embedding):
            torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)

    def forward(self, idx: torch.Tensor, targets: torch.Tensor = None):
        """
        Perform a forward pass through the model.
        
        Args:
            idx (torch.Tensor): LongTensor of shape (B, T) containing token indices.
            targets (torch.Tensor, optional): LongTensor of shape (B, T) containing target token indices.
            
        Returns:
            logits (torch.Tensor): Tensor of shape (B, T, vocab_size) with word scores.
            loss (torch.Tensor, optional): Scalar CrossEntropy loss if targets are provided.
        """
        device = idx.device
        b, t = idx.size()
        
        # Ensure sequence length is valid
        assert t <= self.config.seq_len, f"Cannot forward sequence of length {t}, block size is only {self.config.seq_len}"

        # 1. Generate position ids: [0, 1, 2, ..., T-1], shape (T)
        pos = torch.arange(0, t, dtype=torch.long, device=device)
        
        # 2. Extract Token Embeddings: shape (B, T, d_model)
        tok_emb = self.transformer.wte(idx) 
        
        # 3. Extract Positional Embeddings: shape (T, d_model) -> broadcasting handles batch B
        pos_emb = self.transformer.wpe(pos) 
        
        # 4. Add them together and apply dropout
        x = self.transformer.drop(tok_emb + pos_emb)
        
        # 5. Pass through the sequence of Transformer blocks
        for block in self.transformer.h:
            x = block(x)
            
        # 6. Apply final LayerNorm
        x = self.transformer.ln_f(x)
        
        # 7. Project back to vocabulary size
        logits = self.lm_head(x) # (B, T, vocab_size)
        
        loss = None
        if targets is not None:
            # PyTorch cross_entropy expects (B*T, C) shape for 2D inputs
            B, T, C = logits.shape
            logits_flat = logits.view(B*T, C)
            targets_flat = targets.view(B*T)
            loss = F.cross_entropy(logits_flat, targets_flat)
            
        return logits, loss

    @torch.no_grad()
    def generate(self, idx: torch.Tensor, max_new_tokens: int, temperature: float = 1.0, top_k: int = None) -> torch.Tensor:
        """
        Take a conditioning sequence of indices idx (LongTensor of shape (B, T)) and complete
        the sequence max_new_tokens times, feeding the predictions back into the model each time.
        
        Args:
            idx (torch.Tensor): Seed sequence of shape (B, T).
            max_new_tokens (int): Number of tokens to append.
            temperature (float): Scaling factor for the logits (lower = more deterministic).
            top_k (int, optional): If set, only sample from the top k most likely tokens.
            
        Returns:
            torch.Tensor: The sequence with generated tokens appended, shape (B, T + max_new_tokens).
        """
        for _ in range(max_new_tokens):
            # if the sequence context is growing too long we must crop it at seq_len
            idx_cond = idx if idx.size(1) <= self.config.seq_len else idx[:, -self.config.seq_len:]
            # forward the model to get the logits for the index in the sequence
            logits, _ = self(idx_cond)
            # pluck the logits at the final step and scale by desired temperature
            logits = logits[:, -1, :] / temperature # becomes (B, C)
            
            # optionally crop the logits to only the top k options
            if top_k is not None:
                v, _ = torch.topk(logits, min(top_k, logits.size(-1)))
                logits[logits < v[:, [-1]]] = -float('Inf')
                
            # apply softmax to convert logits to (normalized) probabilities
            probs = F.softmax(logits, dim=-1)
            # sample from the distribution
            idx_next = torch.multinomial(probs, num_samples=1) # (B, 1)
            # append sampled index to the running sequence and continue
            idx = torch.cat((idx, idx_next), dim=1) # (B, T+1)
        
        return idx

if __name__ == "__main__":
    # Small test script
    print("Testing the Full MiniGPT Model...")
    
    # 1. Load the tiny config
    config = MiniGPTConfig.tiny()
    print(f"Config loaded: Vocab={config.vocab_size}, SeqLen={config.seq_len}, D_model={config.d_model}, Layers={config.n_layers}")
    
    # 2. Instantiate the blank model
    model = MiniGPT(config)
    print(f"Total Parameters: {sum(p.numel() for p in model.parameters())/1e6:.2f} M")
    
    # 3. Create a dummy sequence shaped (Batch, Time)
    B, T = 2, 16 
    # Create random integers simulating token IDs
    dummy_input = torch.randint(0, config.vocab_size, (B, T)) 
    dummy_targets = torch.randint(0, config.vocab_size, (B, T)) 
    
    print(f"Input shape: {dummy_input.shape}")
    
    # 4. Forward pass
    logits, loss = model(dummy_input, targets=dummy_targets)
    
    print(f"Logits shape: {logits.shape}")
    print(f"Expected shape: ({B}, {T}, {config.vocab_size})")
    print(f"Calculated Loss: {loss.item():.4f}")
    
    print("\nTesting Generation Loop...")
    # Give it a 4-token prompt context
    prompt = torch.randint(0, config.vocab_size, (1, 4))
    print(f"Prompt shape: {prompt.shape}")
    
    # Generate 20 new tokens with temperature and top_k
    generated = model.generate(prompt, max_new_tokens=20, temperature=0.8, top_k=50)
    print(f"Generated shape: {generated.shape}")
    print(f"Expected shape: (1, 24)")
    
    print("\nSuccess! The Full Transformer successfully generated logits, calculated loss, and generated text autoregressively.")