-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
93 lines (73 loc) · 2.02 KB
/
Copy pathmodel.py
File metadata and controls
93 lines (73 loc) · 2.02 KB
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
"""
model.py
Builds a minimal Decoder-Only Transformer Language Model by stacking
multiple Transformer Decoder Blocks.
Pipeline:
Token IDs
↓
Token Embedding + Positional Encoding
↓
N × Decoder Blocks
↓
Final Layer Normalization
↓
Linear Projection
↓
Vocabulary Logits
"""
import torch
import torch.nn as nn
from embeddings import TokenEmbedding
from decoder_block import DecoderBlock
from config import (
VOCAB_SIZE,
EMBED_DIM,
NUM_HEADS,
FF_HIDDEN_DIM,
NUM_LAYERS,
MAX_SEQ_LEN,
)
class MiniDecoderLM(nn.Module):
"""
A minimal Decoder-Only Transformer Language Model.
This project is designed for learning the internal architecture
of modern decoder-only Large Language Models (LLMs) such as
GPT, Llama, Mistral, Gemma, Qwen, Phi, and DeepSeek.
"""
def __init__(self):
super().__init__()
# Token Embedding + Positional Encoding
self.embedding = TokenEmbedding(
vocab_size=VOCAB_SIZE,
embed_dim=EMBED_DIM,
max_len=MAX_SEQ_LEN,
)
# Stack of Decoder Blocks
self.decoder_blocks = nn.ModuleList(
[
DecoderBlock(
embed_dim=EMBED_DIM,
num_heads=NUM_HEADS,
ff_hidden_dim=FF_HIDDEN_DIM,
)
for _ in range(NUM_LAYERS)
]
)
# Final Layer Normalization
self.final_norm = nn.LayerNorm(EMBED_DIM)
# Output Projection
self.output_projection = nn.Linear(
EMBED_DIM,
VOCAB_SIZE,
)
def forward(self, token_ids: torch.Tensor) -> torch.Tensor:
# Convert token IDs into embeddings
x = self.embedding(token_ids)
# Pass through all Decoder Blocks
for decoder in self.decoder_blocks:
x = decoder(x)
# Final normalization
x = self.final_norm(x)
# Project to vocabulary
logits = self.output_projection(x)
return logits