LLM Architecture
- https://huggingface.co/learn/llm-course/chapter1/4
- https://huggingface.co/learn/llm-course/chapter1/6
Transformers
- Based on paper: https://en.wikipedia.org/wiki/Attention_Is_All_You_Need
- Deep learning architecture based on “Attention” algorithm
- Used in LLM, Image and Video Generation, Speech recognition
- Transformers Arch: https://huggingface.co/learn/llm-course/chapter1/4#general-transformer-architecture
Transformer Types
Encoders
- Takes text (or other data) as input and outputs a dense representation (or embedding) of that text
- Example: BERT from Google
- Use Cases: Text classification, semantic search, Named Entity Recognition
- Typical Size: Millions of parameters
Decoders
- Focuses on generating new tokens to complete a sequence, one token at a time
- Example: Llama from Meta
- Use Cases: Text generation, chatbots, code generation
- Typical Size: Billions of parameters
Seq2Seq (Encoder–Decoder)
- Combines an encoder and a decoder
- Encoder first processes the input sequence into a context representation, then the decoder generates an output sequence.
- Example: T5, BART, Whisper
- Use Cases: Translation, Summarization, Paraphrasing
- Typical Size: Millions of parameters
Context Length
- Context length refers to the maximum number of tokens the model can consider at once when generating a response
- It represents the maximum attention span it has
gemma4:26Bhas context length of 256K tokens
Sliding Window Size
- Maximum number of historical tokens a model’s local attention layer can look back on at any given moment
gemma4:26Bhas sliding window size of 1024 tokens
Vocabulary Size
- Total number of unique words or tokens your model knows
gemma4:26Bhas vocabulary size of 262K
Embedding Size
- Number of dimensions used to mathematically represent each token
embeddinggemmahas 768 embedding length
Parameters
- The total count of weights and biases in the neural network, representing the model’s overall memory capacity
gemma3:4bhas 4.3B parameters
Layers
- A layer is a component of a neural network that performs a particular computation
- Model depth generally refers to the number of Hidden Layers
- Generally Hidden layers refer to Transformer blocks
- Types of Layers
- Embedding Layer
- Common Identifier:
embed_tokens - Converts token IDs into learned vectors (embeddings)
- Common Identifier:
- Hidden Layer
- aka Transformer block
- Common Identifier:
model.layers.N - Processes hidden representations using attention and feed-forward networks
- Normalization Layer
- Common Identifier:
norm - Normalizes activations
- Common Identifier:
- Output Projection layer
- Common Identifier:
lm_head - Projects hidden representations to vocabulary logits for next-token prediction
- Common Identifier:
- Specialized layers
- For example, vision encoders in multimodal models
- Embedding Layer
- Examples
SmolLM3-3B(36 Layers)- https://huggingface.co/HuggingFaceTB/SmolLM3-3B/blob/main/model.safetensors.index.json
- Embedding + Output Projection Layer (1)
- shared weight matrix due to weight tying enabled
- Hidden Layers (36)
- Normalization Layer (1)
gemma-4-26B-A4B-it(30 Layers)- https://huggingface.co/google/gemma-4-26B-A4B-it/blob/main/model.safetensors.index.json
- Embedding + Output Projection Layer (1)
- shared weight matrix due to weight tying enabled
- Hidden Layers (30)
- Normalization Layer (1)
- Vision Layers
- Embedding Layer (1)
- Hidden Layer (27)
- Other Layers (4)
Logit
- https://en.wikipedia.org/wiki/Logit
- https://datascience.stackexchange.com/questions/31041/what-does-logits-in-machine-learning-mean
- Logits are the unnormalized predictions (or outputs) of a model
- Logits are passed into activation functions based on the classification type to obtain probabilities
Binary Classification
- use logistic function
- It is a type of sigmoid function
- logistic function is inverse of logit function
- https://en.wikipedia.org/wiki/Logistic_function
- is logit for binary classification, then
Multi-Class Classification
- use softmax function
- https://en.wikipedia.org/wiki/Softmax_function
- used when 3 or more mutually exclusive classes
- In an LLM, each class corresponds to a possible token in the model’s vocabulary
- All the probabilites obtained by applying softmax function add up to 1
- be logits for classes
Key-Value Cache
- aka KV Cache
- Stores information about previously processed tokens
- During generation:
- First pass: Processes all input tokens and stores their information in the cache
- Next passes: Processes only the newly generated token instead of resending all previous tokens
- It speeds up text generation by avoiding redundant computation
- KV cache vs. session:
- KV cache stores temporary computation information for a sequence
- A session maintains conversation state, such as chat history
- The KV cache is typically reused within a generation sequence, but may also be retained or reused across requests by some inference systems
Attention
- It is process of identifying the most relevant words to predict the next token
- Intuition: Query and key determine where to pay attention; values determine what information is retrieved
- — Query
- Represents what the current token is looking for in other tokens
- — Key
- Represents what each token offers for matching against a query
- Retrieved from the KV cache
- — Value
- Represents the information contributed by each token when it receives attention
- Retrieved from the KV cache
- — Attention scores
- Measures how strongly each query matches each key
- — Key dimension
- The number of dimensions in each key vector
- — Scaling factor
- Prevents attention scores from becoming excessively large.
- Softmax: Converts scores into attention weights that sum to 1 across the attended positions
- Multiplication by : Computes a weighted combination of value vectors, producing the attention output
- — Query
Mixture of Experts (MoE)
- https://www.reddit.com/r/LocalLLaMA/comments/174f42z/can_anyone_explain_moe_like_im_25/
- Model consists of many smaller sub-networks called experts
- In standard dense network model, all parameters are used
- Every weight is used for every token
- In MoE, a routing mechanism only triggers a fraction of these experts
- meaning the active parameter count is much lower than the total
- Only few weights are used for every token
- In
gemma4:26b- https://ollama.com/library/gemma4
- Expert Count: 8 active / 128 total and 1 shared
- Active Parameters: 3.8B
- Total Parameters: 25.2B
LLM Model Contents
- Example:
- Architecture and Weights
config.json: number of layers, attention heads, hidden layers, base model type.safetensors: weights of model.safetensors.index.json: map of each layer to weights file, used for big models split into multiple weights file
- Tokenizer
tokenizer.json: Vocabulary of model mapping each word/syllable/character to unique IDtokenizer_config.json: Tells library how to handle special tokens and rules like max sequence length etc.special_tokens_map.json: explicitly defines which IDs represent structural markers like the start, end, or padding of text
- Chat
chat_template.jinja: Template to structure text into raw prompt strings
- Output Control
generation_config.json: default behavior when the model generates text liketemperature,top_p,top_k
- Multimodal
preprocessor_config.json: Feature extractor containing mathematical arrays for image resizing, cropping, or audio sample normalization.- contains processor class
processor_config.json: links tokenization with image/video/audio processing- contains processor class
- LoRA
adapter_config.json: tells loading library how to attach the plugin to the base modeladapter_model.safetensors: adapter weights
Running Raw LLM Model
- Example Model: https://huggingface.co/HuggingFaceTB/SmolLM2-360M
- https://github.com/huggingface/transformers/blob/536ecc007387a50e77603bb5d92100e9b07514cc/src/transformers/models/llama/modeling_llama.py#L356
- https://github.com/huggingface/transformers/blob/main/src/transformers/modeling_outputs.py
- https://github.com/huggingface/transformers/blob/09281b8d56b08199ce9f397c694f38853cc45278/src/transformers/modeling_utils.py#L3823
- For inference
model.generate()is called which internally callsforward()method generate()repeatedly calls the model’sforward()method to obtain next-token predictionsforward()method performs one pass over all the layers in LLM Neural Network- Input
input_ids: Tensor- Integer IDs representing the input tokens
inputs_embeds: Tensor- Vector representations of input tokens
- Pass either
input_idsorinput_embeds
attention_mask: Tensor- consist of 0s and 1s
- Indicates which input positions are valid (1) and which should be masked (0), commonly to exclude padding tokens
position_ids: Tensor- Specifies the position index of each token in the sequence
- If KV caching is disabled, positions typically start at
0for the input sequence. - If KV caching is enabled, positions for newly processed tokens typically continue from the number of previously cached tokens.
use_cache: bool- Controls whether the model uses the key-value cache
past_key_values: Cache- KV cache
- Stores previously computed attention keys and values
so they can be reused instead of recomputed during generation
- Output
logits- Logit score for each token in vocabulary
past_key_values- The updated key-value cache containing attention information
for tokens processed so far - This is passed into the next forward call to speed up generation
- The updated key-value cache containing attention information
- Input
- In essence, LLM does following:
- Take input tokens from its vocabulary
- Converts them into vector representations using embedding layer
- Processes them through multiple neural network layers using attention and feed-forward operations
- Each pass produces logits representing the scores of possible next tokens
- During text generation, these logits are converted into probabilities
- Using those probabilities and LLM config, a next token is selected
- This next token is fed back into the model to predict the next token
- With KV caching enabled, the model typically processes only the new token on subsequent steps
- Process repeats until a stopping condition is met
from transformers import AutoTokenizer, AutoModelForCausalLM
model_name = "HuggingFaceTB/SmolLM2-360M-Instruct"
# Load tokenizer and model
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
# 49152 ~ 49k tokens
print("Tokenizer vocabulary:", len(tokenizer))
# model_type: llama
# dtype: bfloat16
# num_hidden_layers: 32
# hidden_size: 960 (embedding size of each vector)
# num_attention_heads: 15
print("Model config:", model.config.to_json_string())
# Prepare a conversation
messages = [
{"role": "user", "content": "Explain branches of mathematics."}
]
# Convert conversation to model input
inputs = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
tokenize=True,
return_tensors="pt",
)
# torch.Size([1, 35])
# 1 conversation with 35 tokens
print(inputs["input_ids"].shape)
# torch.Size([1, 35])
# consist of 0s and 1s for each input token
print(inputs["attention_mask"].shape)
# Generate text
# generate internally calls `forward()`
outputs = model.generate(
**inputs,
max_new_tokens=200,
)
# include input tokens (35) + newly generated tokens (200)
print(outputs.shape)
# Decode only the newly generated tokens
new_tokens = outputs[0][inputs["input_ids"].shape[-1]:]
response = tokenizer.decode(new_tokens, skip_special_tokens=True)
print(response)- Model Config
{
"architectures": [
"LlamaForCausalLM"
],
"attention_bias": false,
"attention_dropout": 0.0,
"bos_token_id": 1,
"dtype": "bfloat16",
"eos_token_id": 2,
"head_dim": 64,
"hidden_act": "silu",
"hidden_size": 960,
"initializer_range": 0.02,
"intermediate_size": 2560,
"is_llama_config": true,
"max_position_embeddings": 8192,
"mlp_bias": false,
"model_type": "llama",
"num_attention_heads": 15,
"num_hidden_layers": 32,
"num_key_value_heads": 5,
"pad_token_id": 2,
"pretraining_tp": 1,
"rms_norm_eps": 1e-05,
"rope_interleaved": false,
"rope_parameters": {
"rope_theta": 100000,
"rope_type": "default"
},
"tie_word_embeddings": true,
"transformers.js_config": {
"kv_cache_dtype": {
"fp16": "float16",
"q4f16": "float16"
}
},
"transformers_version": "5.8.1",
"use_cache": true,
"vocab_size": 49152
}