The Math Behind Using LoRA to Fine-Tune LLMs
If you've been following the world of Large Language Models (LLMs), one term you've likely come across is fine-tuning. But what actually happens under the hood? What does it do to an LLM?
In this article, you'll first build an understanding of what an LLM actually outputs. Then you'll learn how fine-tuning works, focusing on LoRA (Low-Rank Adaptation)—one of the most popular approaches to fine-tuning.
Throughout, you'll follow one small example to see how everything fits together. No prior machine learning or math background is required.
Table of Contents
- How an LLM Picks the Next Token
- What Is Fine-Tuning?
- How LoRA Works
- Low-Rank Factorization
- How LoRA Uses This Idea
- How to Fine-Tune intern-llm with LoRA
- How to Prepare the Dataset
- The Fine-Tuning Loop
- The Adapter and Scaling
- Conclusion
- Take-Home Exercise
How an LLM Picks the Next Token
What's at the core of an LLM's behavior? It's a token: the single unit that decides the tone, nature, and characteristics of the response the model generates.
Even the basic unit used for calculating LLM usage is a token. In general, a token can be a character, a sub-word, or a word.
Throughout this article, the term token refers to a word. In practice, LLMs often split words into sub-word tokens to balance vocabulary size against expressiveness—a detail worth keeping in mind as 2026-era multimodal and long-context models push tokenization strategies further.
(Article continues…)
via FreeCodeCamp
