# Large Language Models and Efficient Fine-Tuning

> How LLMs are pretrained and aligned, and how to adapt open models cheaply with LoRA, quantization and instruction data. Free notes, key concepts, resources and...

Canonical: https://www.internhack.in/roadmaps/tracks/machine-learning/llm-fine-tuning
Last updated: 2026-11-28

How LLMs are pretrained and aligned, and how to adapt open models cheaply with LoRA, quantization and instruction data.

Advanced · about 12 hours · difficulty 4/5

Learn first: [Fine-Tuning with Hugging Face Transformers](https://www.internhack.in/roadmaps/tracks/machine-learning/hugging-face-transformers), [GPUs and Efficient Training](https://www.internhack.in/roadmaps/tracks/machine-learning/gpu-training)

Large language models are decoder-only transformers trained on huge text corpora to predict the next token, then refined to follow instructions. As an ML engineer you will more often adapt or evaluate them than pretrain one.

### How LLMs are built

- **Pretraining**: next-token prediction on trillions of tokens. Scaling laws relate loss to parameters, data and compute.
- **Supervised fine-tuning (SFT)** on instruction and response pairs teaches the model to be helpful.
- **Preference tuning** (RLHF, DPO and related methods) aligns outputs with human preferences.
- **Decoding**: greedy, temperature, top-k and top-p sampling change output style and diversity.

### Adapting open models

- Try **prompting and retrieval (RAG)** first; they are cheaper and easier to update than fine-tuning.
- Fine-tune when you need a consistent format, domain style or smaller, faster model.
- **LoRA** trains small low-rank adapter matrices instead of all weights. **QLoRA** does this on a 4-bit quantized base model, so a 7B model fits on a single consumer GPU.
- Libraries: Hugging Face **PEFT** and **TRL** for SFT and DPO.

### Evaluation

Hold out test prompts, compare against the base model, and combine automatic metrics with human or model-graded review. Watch for **catastrophic forgetting** and for test data that leaked into training.

**Good enough to move on** when you can fine-tune a small open model with LoRA on an instruction dataset and show, with an evaluation set, whether it actually improved.

## Key concepts

- **Next-token pretraining and scaling**
- **Supervised instruction tuning**
- **RLHF and DPO**
- **Decoding and sampling**
- **LoRA and QLoRA**
- **PEFT and TRL libraries**
- **Prompting vs RAG vs fine-tuning**
- **LLM evaluation**

## Mini project

Fine-tune a 1B to 3B parameter open model with QLoRA to answer in a fixed JSON format for a narrow task, and compare format accuracy and answer quality with the base model on 50 held-out prompts.

## Check yourself

Why does LoRA need far less GPU memory than full fine-tuning, and when would you choose RAG over fine-tuning?

## Free resources

- [Hugging Face Learn](https://huggingface.co/learn) (Hugging Face)
- [PEFT documentation](https://huggingface.co/docs/peft/index) (Hugging Face)
- [TRL documentation](https://huggingface.co/docs/trl/index) (Hugging Face)

Previous: [Fine-Tuning with Hugging Face Transformers](https://www.internhack.in/roadmaps/tracks/machine-learning/hugging-face-transformers) · Next: [Autoencoders and Variational Autoencoders](https://www.internhack.in/roadmaps/tracks/machine-learning/autoencoders-vaes)

## More in Language, Sequences and Transformers

- [Text Preprocessing and Representations](https://www.internhack.in/roadmaps/tracks/machine-learning/text-representation)
- [Recurrent Networks and Sequence Modelling](https://www.internhack.in/roadmaps/tracks/machine-learning/sequence-models)
- [Attention and the Transformer Architecture](https://www.internhack.in/roadmaps/tracks/machine-learning/attention-and-transformers)
- [Fine-Tuning with Hugging Face Transformers](https://www.internhack.in/roadmaps/tracks/machine-learning/hugging-face-transformers)
- [Large Language Models and Efficient Fine-Tuning](https://www.internhack.in/roadmaps/tracks/machine-learning/llm-fine-tuning)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Machine Learning Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/machine-learning)
