Skip to main content

Fine-Tune LLM on Custom Documents

Overview

Fine-tuning is the process of taking a pre-trained language model and training it further on a custom dataset so that it learns a specific task, format, style, or domain. This topic focuses on:
  • LoRA
  • Adapters
  • Parameter-Efficient Fine-Tuning
  • Preparing a small custom dataset
  • Training and evaluation
  • Saving and using the fine-tuned model

1. What Is Fine-Tuning?

A pre-trained LLM already contains general language knowledge.
For example:
The resulting model becomes better at the specific patterns represented in the training data.

2. Why Fine-Tune an LLM?

Fine-tuning can be useful when a model needs to learn:
  • A specific response format
  • Domain-specific terminology
  • A particular writing style
  • Question-answering patterns
  • Classification tasks
  • Instruction-following behavior
Example:
By training on many examples with a similar structure, the model can learn the expected response pattern.

3. Fine-Tuning vs RAG

Fine-tuning and RAG solve different problems.

Fine-Tuning

RAG

A useful distinction is:
Fine-tuning and RAG can also be combined.

4. The Problem With Full Fine-Tuning

A large language model can contain millions or billions of parameters. Traditional fine-tuning updates all model parameters.
This requires:
  • More GPU memory
  • More computation
  • More training time
  • More storage
For large models, full fine-tuning can be expensive. This leads to Parameter-Efficient Fine-Tuning, commonly called PEFT.

5. Parameter-Efficient Fine-Tuning

PEFT trains only a small portion of additional parameters instead of updating the complete model.
Conceptually:
Popular PEFT techniques include:
  • LoRA
  • Adapters
  • Prefix Tuning
  • Prompt Tuning
The most commonly used technique is LoRA.

6. LoRA

LoRA stands for:
Instead of updating the original model weight matrix directly, LoRA adds small trainable matrices. Traditional fine-tuning:
LoRA:
The effective weight update can be represented as:
LoRA approximates the update as:
Therefore:
Where:
  • W is the original pre-trained weight matrix
  • A and B are smaller trainable matrices
  • ΔW is the learned weight update

7. Why Is LoRA Efficient?

A full weight matrix can be very large. Example:
Instead of training the complete matrix, LoRA uses a smaller rank.
Only the smaller matrices are trained.
Benefits include:
  • Lower GPU memory requirements
  • Fewer trainable parameters
  • Faster training
  • Smaller adapter files
  • Base model can remain unchanged

8. Important LoRA Parameters

r

The rank of the LoRA matrices. Example:
A higher rank allows the adapter to learn more complex changes but increases the number of trainable parameters. Common small values include:

lora_alpha

Controls the scaling of the LoRA update. Example:
The LoRA update is commonly scaled approximately by:
For:
the scaling factor is:

lora_dropout

Applies dropout during LoRA training. Example:
Dropout can help reduce overfitting.

target_modules

Defines which model layers receive LoRA adapters. Example:
These names depend on the architecture of the base model. Different models may use different module names.

9. Adapters

Adapters are small trainable neural network components inserted into an existing pre-trained model. Conceptually:
The original model remains mostly frozen. Only the adapter components are trained.
Adapters allow multiple task-specific modules to be created for the same base model.

10. LoRA vs Adapters

Both approaches are examples of parameter-efficient fine-tuning.

11. Preparing a Small Custom Dataset

A fine-tuning dataset usually contains examples of:
For instruction fine-tuning:
Example:
Another example:

12. Common Dataset Formats

JSON


JSONL

Each line contains one JSON object.
JSONL is commonly used for larger datasets.

CSV


13. Dataset Structure for Instruction Fine-Tuning

A simple structure is:
For example:
Another common structure is:
The exact format depends on the model and training pipeline.

14. Formatting Training Examples

For a text-generation model, structured data is often converted into one training string. Example:
Python example:
The goal is to make every training example follow a consistent format.
Consistency helps the model learn the expected pattern.

15. Example Small Custom Dataset

A file called custom_data.json could contain:
For real fine-tuning, more examples are generally needed than this small demonstration dataset.

16. Dataset Splitting

The dataset should usually be divided into:
A common split is:
Example:
For larger datasets, a test set can also be added.

17. Data Quality Is Important

Fine-tuning quality depends heavily on the dataset. Poor data:
Good data:
Important data preparation steps include:
  • Remove duplicates
  • Remove incorrect examples
  • Keep formatting consistent
  • Remove unnecessary text
  • Check for empty values
  • Ensure instructions match responses

18. Basic Fine-Tuning Workflow


19. Tokenization

Language models do not directly process normal text. Text must first be converted into tokens.
Example conceptually:
becomes:
The model processes token IDs instead of raw text.

20. Sequence Length

Training examples must usually have a maximum token length. Example:
If text is longer than the limit, it may be truncated.
Choosing the sequence length affects:
  • Memory usage
  • Training speed
  • Amount of context available
Longer sequences generally require more memory.

21. Important Training Parameters

Common parameters include:
Controls how much the trainable parameters change during each update.
Controls how many times the model sees the training dataset.
Controls the number of examples processed together.
Allows gradients from multiple smaller batches to be accumulated before updating the model. Effective batch size is approximately:
Example:

22. Overfitting

Overfitting happens when the model memorizes the training data instead of learning general patterns.
Possible signs:
  • Training loss continues decreasing
  • Validation performance becomes worse
  • Model repeats training examples
  • Poor performance on new inputs
Ways to reduce overfitting include:
  • More diverse data
  • Fewer training epochs
  • Dropout
  • Lower LoRA rank
  • Validation during training

23. Saving the LoRA Adapter

One advantage of LoRA is that only the adapter weights need to be saved.
The base model does not need to be duplicated for every fine-tuned task. Example:
During inference:

24. Important Fine-Tuning Concepts


25. Complete Architecture


Summary

Fine-tuning allows a pre-trained language model to adapt to a custom task or response pattern. Traditional fine-tuning:
Parameter-efficient fine-tuning with LoRA:
The complete learning workflow is:
The key idea is: