Lets quickly recap we I have discussed in previous blog about Attention Mechanism. Regarding Attention Mechanism, we have seen what is self-attention, drawbacks of legacy systems, Self-attention without trainable weights, Self-attention with trainable weights, Causal attention, Masked multi-headed attention.
Inside Self-Attention, we have discussed about :
- Attention Scores
- Attention Weights
- Context Vector
Importance of context vector is, it is going to maintain the semantics of all the tokens(not just about a single token).
Self-attention with trainable weights :
- Inputs
- Weight, Query, Value matrix (Wq, Wk, Wv) and cal Q, K, V
- Query = Input @ Wq
- Key = Input @ Wk
- Value = Input @ Wv
- Above Query, Key, Value matrix are initially random values and they will be adjusted during training
- Calculate Attention score
- Query matrix @ Transpose of Key matrix
- Attention Weights
- Scaling = Square root of dimensions of Key matrix
- Normalization using softmax
- We are transforming data from Attention Scores to Attention Weights because we want to make interpretability easy.
- Context vector
- Attention Weights @ Value vector
Causal Attention :
- Inputs
- Weight, Query, Value matrix (Wq, Wk, Wv)
- Query = Input @ Wq
- Key = Input @ Wk
- Value = Input @ Wv
- Attention Scores
- Query matrix @ Key Transpose
- Attention Weights
- Scaling
- Masking (Future Values i.e. -infinity)
- Softmax
- Optional dropout
- Context Vector
- Attention Weights @ Value matrix
- Same as Causal Attention but this have multi heads
- This is to increase the parallelism
- For GPT-2 small model, it has 12 layers & 12 heads per layer (Total heads = 144)
LLM Architecture
Above image represents each and every single step involved in implementing a GPT model from scratch. For GPT-2 model, we have 12 layers and in each layer there will be 12 heads. So, as shown in the above images Transformer Block will be repeated 12 times for each layer. By the end this blog, we will know in detail about all the blocks shown in the above image.Also see , another Skip/Residual Connection started after first Dropout after '+' and connected to next '+' at the end of transformer layers.
This entire grey colour box which internally contain a Transformer block is called a GPT model. We will program this entire architecture in this blog. That's the goal for this blog.
GPT-2 small model (124 million parameters) :
We need below configuration for implementing GPT model.
- Vocabulary size, vocab_size = 50,257
- In above image, during tokenization we need this vocabulary
- Token Embedding
- embedding size, emb_dim = 768
- each word/token will be represented in a 768 dimensional space
- Context Length
- context_length = 1024
- means at a time, we will consider 1024 tokens for progressing
- Heads
- n_heads = 12
- Layers
- n_layers = 12
- Dropout
- drop_rate = 0.1
Thank you for reading this blog !
Arun Mathe
Comments
Post a Comment