Understanding Next-Token Prediction in LLMs
How does an LLM actually generate text? Let's start from the basics.
Before understanding LLMs, I wanted to understand how machines generated text before modern LLMs.
How text was generated before LLMs#
Earlier NLP systems used approaches such as rule-based systems, statistical language models, and traditional machine learning.
For example, an N-gram language model predicts the next word based on the previous N-1 words.
I have also included an example in the image. The model predicts "Delhi" as the next token for the prompt about India's capital city. After generating "Delhi", it continues predicting the next token, which could be a full stop (.) or another word, depending on the context.
But these approaches had limitations:
- They depended heavily on local word sequences.
- They struggled with long-range context.
- They had difficulty capturing complex relationships across long sentences.
- They often required task-specific engineering.
From neural language models to the Transformer#
The evolution continued with neural language models, which introduced deep learning into language processing.
The next major breakthrough came in 2017, when Ashish Vaswani and his seven co-authors introduced the Transformer architecture in their research paper, "Attention Is All You Need."
This architecture introduced the Self-Attention mechanism, which allowed models to learn relationships between tokens more effectively and process input sequences in parallel during training.
This became the foundation of modern LLMs.
So, what exactly does an LLM do?#
At its core, an autoregressive LLM predicts the next token based on the tokens it has already received.
Let's break down the process:
- Tokenization
- Token embeddings
- Transformer processing
- Next-token prediction
This is the basic generation pipeline of an autoregressive LLM.
What is coming next in this series#
Next, I want to understand and explain each process step by step, including what happens internally, how the data is prepared, how tensors and their shapes change, and how prompts are processed.
I will cover positional encoding, self-attention, multi-head attention, Dense vs. MoE architectures, decoding strategies, layer normalization, quantization, base model training, and fine-tuning using LoRA.
My aim is to explain everything from scratch, so anyone can understand how LLMs actually work, rather than simply loading a model from Hugging Face and using it.
In my upcoming posts, I will break down each component, explain its internal working in depth, and explore how these components come together in modern LLM architectures.
I also want to explore how modern LLMs achieve context windows of 2M tokens and beyond, including the architectural techniques and engineering decisions behind them.
The goal is to build a complete understanding of modern LLMs, from data preparation and model architecture to training, fine-tuning, and inference.
Follow along at vijayai.site.
I build LLM, RAG and speech systems and write about how they work from first principles, so anyone can understand what happens inside a model.