Upload folder using huggingface_hub
Browse files- .gitattributes +4 -0
- README.md +47 -0
- guides/HuggingFace_ChatTemplates.html +0 -0
- guides/HuggingFace_LLM_FullFineTune_Tutorial.html +0 -0
- guides/HuggingFace_PEFT_LoRA_Conceptual.html +0 -0
- guides/HuggingFace_PEFT_Quicktour.html +0 -0
- guides/HuggingFace_TRL_SFTTrainer.html +0 -0
- guides/Unsloth_Documentation.html +136 -0
- guides/Unsloth_FineTune_Guide.html +235 -0
- guides/Unsloth_LoRA_Hyperparameters.html +365 -0
- how-to.html +281 -0
- papers/Attention_Vaswani2017.pdf +3 -0
- papers/LoRA_Hu2021.pdf +3 -0
- papers/PEFT_Survey.pdf +3 -0
- papers/QLoRA_Dettmers2023.pdf +3 -0
.gitattributes
CHANGED
|
@@ -58,3 +58,7 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 58 |
# Video files - compressed
|
| 59 |
*.mp4 filter=lfs diff=lfs merge=lfs -text
|
| 60 |
*.webm filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
# Video files - compressed
|
| 59 |
*.mp4 filter=lfs diff=lfs merge=lfs -text
|
| 60 |
*.webm filter=lfs diff=lfs merge=lfs -text
|
| 61 |
+
papers/Attention_Vaswani2017.pdf filter=lfs diff=lfs merge=lfs -text
|
| 62 |
+
papers/LoRA_Hu2021.pdf filter=lfs diff=lfs merge=lfs -text
|
| 63 |
+
papers/PEFT_Survey.pdf filter=lfs diff=lfs merge=lfs -text
|
| 64 |
+
papers/QLoRA_Dettmers2023.pdf filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Shared References
|
| 2 |
+
|
| 3 |
+
Papers and documentation that apply to **more than one** of the three spam-classifier projects live in this folder. Project-specific references live inside each project's own `docs/references/` folder.
|
| 4 |
+
|
| 5 |
+
## How to browse
|
| 6 |
+
|
| 7 |
+
**Double-click [`how-to.html`](how-to.html)** to open the full reference index in your browser. It has clickable links to every local PDF and HTML guide, plus online fallbacks.
|
| 8 |
+
|
| 9 |
+
## What's in here
|
| 10 |
+
|
| 11 |
+
### Papers
|
| 12 |
+
- `papers/Attention_Vaswani2017.pdf` — The original Transformer paper
|
| 13 |
+
- `papers/LoRA_Hu2021.pdf` — LoRA fine-tuning technique (used by MLX + Liquid)
|
| 14 |
+
- `papers/QLoRA_Dettmers2023.pdf` — Quantized LoRA (applies to the MLX project's 4-bit model)
|
| 15 |
+
- `papers/PEFT_Survey.pdf` — Han 2024 survey of parameter-efficient fine-tuning methods
|
| 16 |
+
|
| 17 |
+
### Guides (HTML, open in browser)
|
| 18 |
+
- `guides/HuggingFace_LLM_FullFineTune_Tutorial.html` — LearnHuggingFace full fine-tune walkthrough
|
| 19 |
+
- `guides/HuggingFace_PEFT_Quicktour.html` — HF PEFT quickstart
|
| 20 |
+
- `guides/HuggingFace_PEFT_LoRA_Conceptual.html` — What LoRA `r`, `alpha`, `target_modules` mean
|
| 21 |
+
- `guides/HuggingFace_TRL_SFTTrainer.html` — TRL `SFTTrainer` reference (used by Liquid project)
|
| 22 |
+
- `guides/HuggingFace_ChatTemplates.html` — `tokenizer.apply_chat_template()` reference
|
| 23 |
+
- `guides/Unsloth_Documentation.html` — Unsloth docs home (faster TRL alternative)
|
| 24 |
+
- `guides/Unsloth_FineTune_Guide.html` — Step-by-step Unsloth fine-tuning walkthrough
|
| 25 |
+
- `guides/Unsloth_LoRA_Hyperparameters.html` — Practical LoRA hyperparameter advice
|
| 26 |
+
|
| 27 |
+
## Per-project references
|
| 28 |
+
|
| 29 |
+
Each project has its own folder with project-specific material (model tech reports, library docs, XAI papers, etc.):
|
| 30 |
+
|
| 31 |
+
| Project | Reference folder |
|
| 32 |
+
|---------|------------------|
|
| 33 |
+
| MLX | `../spam-classifier-mlx/docs/references/` |
|
| 34 |
+
| Liquid | `../spam-classifier-liquid/docs/references/` |
|
| 35 |
+
| XAI | `../spam-xai-project/docs/references/` |
|
| 36 |
+
|
| 37 |
+
Each contains its own `how-to.html`.
|
| 38 |
+
|
| 39 |
+
## Re-downloading or updating
|
| 40 |
+
|
| 41 |
+
If you want to refresh any of the downloaded files (e.g., HuggingFace updated their docs), re-run the fetch script from the LLM Project root:
|
| 42 |
+
|
| 43 |
+
```
|
| 44 |
+
bash fetch_references.sh
|
| 45 |
+
```
|
| 46 |
+
|
| 47 |
+
The script is safe to re-run — it overwrites existing files.
|
guides/HuggingFace_ChatTemplates.html
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
guides/HuggingFace_LLM_FullFineTune_Tutorial.html
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
guides/HuggingFace_PEFT_LoRA_Conceptual.html
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
guides/HuggingFace_PEFT_Quicktour.html
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
guides/HuggingFace_TRL_SFTTrainer.html
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
guides/Unsloth_Documentation.html
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Unsloth Docs
|
| 2 |
+
|
| 3 |
+
Unsloth lets you run and train AI models on your own local hardware.
|
| 4 |
+
|
| 5 |
+
Our docs will guide you through running & training your own model locally.
|
| 6 |
+
|
| 7 |
+
<a href="fine-tuning-for-beginners" class="button primary">Get started</a> <a href="https://github.com/unslothai/unsloth" class="button secondary">Our GitHub</a>
|
| 8 |
+
|
| 9 |
+
<table data-card-size="large" data-view="cards" data-full-width="false"><thead><tr><th></th><th></th><th data-hidden data-card-cover data-type="image">Cover image</th><th data-hidden data-card-target data-type="content-ref"></th></tr></thead><tbody><tr><td><h4>Google Gemma 4</h4></td><td>Run and train Google's new Gemma 4 models!</td><td><a href="https://3215535692-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FkEjWOJqBWCtIN9Cg6CdI%2FGemma%204%20landscape.png?alt=media&token=57d3f596-dae8-4eab-80e6-0847794ffc8d">Gemma 4 landscape.png</a></td><td><a href="../models/gemma-4">gemma-4</a></td></tr><tr><td><h4><strong>Introducing Unsloth Studio</strong></h4></td><td>A new open, no-code web UI to train and run LLMs.</td><td><a href="https://3215535692-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FstfdTMsoBMmsbQsgQ1Ma%2Flandscape%20clip%20gemma.gif?alt=media&token=eec5f2f7-b97a-4c1c-ad01-5a041c3e4013">landscape clip gemma.gif</a></td><td><a href="../new/studio">studio</a></td></tr></tbody></table>
|
| 10 |
+
|
| 11 |
+
<table data-view="cards" data-full-width="false"><thead><tr><th></th><th></th><th data-hidden data-card-cover data-type="image">Cover image</th><th data-hidden data-card-target data-type="content-ref"></th></tr></thead><tbody><tr><td><strong>Qwen3.5</strong></td><td>New Qwen3.5 Small & Medium LLMs are here!</td><td><a href="https://3215535692-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fvw6yRxJDCeBl1CIsQkki%2Fqwen35.png?alt=media&token=28fe0357-351a-49e1-a176-bb21ecc8542a">qwen35.png</a></td><td><a href="../models/qwen3.5">qwen3.5</a></td></tr><tr><td><strong>GLM-5.1</strong></td><td>Run the new SOTA open model locally.</td><td><a href="https://3215535692-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FK69rPUGatLzCBK9uaqxU%2Fglm51%20logo.png?alt=media&token=934ef701-0233-47fd-ad49-6c1a5959b684">glm51 logo.png</a></td><td><a href="../models/glm-5.1">glm-5.1</a></td></tr><tr><td><strong>NVIDIA Nemotron 3</strong></td><td>Run the new 4B and 120B models by NVIDIA.</td><td><a href="https://3215535692-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FllPS7l6rpEr68mytlxXU%2Fnemotron%203%20logo.png?alt=media&token=7bd05673-6b97-41c2-b657-530b7e6e4e3c">nemotron 3 logo.png</a></td><td><a href="../models/nemotron-3">nemotron-3</a></td></tr><tr><td><strong>Faster MoE is here!</strong></td><td>Train MoE LLMs 12x faster with less VRAM.</td><td><a href="https://3215535692-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fh9BrTJR8CZghHOe1Yrgj%2Ffaster%20moe%201920.png?alt=media&token=404e70ea-6aa1-4af0-a01c-7490d8147c4e">faster moe 1920.png</a></td><td><a href="../basics/faster-moe">faster-moe</a></td></tr><tr><td><strong>Claude Code & Codex</strong></td><td>Learn to run local LLMs via Claude & OpenAI.</td><td><a href="https://3215535692-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FM3el6W6XCMc0iBEgdeov%2Fclaude%20code%20codex.png?alt=media&token=e45dbc05-9af6-40f7-bcf8-59b79ac44909">claude code codex.png</a></td><td><a href="../basics/claude-code">claude-code</a></td></tr><tr><td><strong>Qwen3-Coder-Next</strong></td><td>Run & fine-tune the new 80B coding model.</td><td><a href="https://3215535692-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2F47HGDuvMBPAkh4vcaGMg%2Fqwen3-coder-next%20logo.png?alt=media&token=244ae539-fea4-40e8-9ee2-b6ec7fb44060">qwen3-coder-next logo.png</a></td><td><a href="../models/qwen3-coder-next">qwen3-coder-next</a></td></tr></tbody></table>
|
| 12 |
+
|
| 13 |
+
{% columns %}
|
| 14 |
+
{% column width="50%" %}
|
| 15 |
+
{% content-ref url="fine-tuning-llms-guide" %}
|
| 16 |
+
[fine-tuning-llms-guide](https://unsloth.ai/docs/get-started/fine-tuning-llms-guide)
|
| 17 |
+
{% endcontent-ref %}
|
| 18 |
+
|
| 19 |
+
{% content-ref url="unsloth-notebooks" %}
|
| 20 |
+
[unsloth-notebooks](https://unsloth.ai/docs/get-started/unsloth-notebooks)
|
| 21 |
+
{% endcontent-ref %}
|
| 22 |
+
{% endcolumn %}
|
| 23 |
+
|
| 24 |
+
{% column width="50%" %}
|
| 25 |
+
{% content-ref url="unsloth-model-catalog" %}
|
| 26 |
+
[unsloth-model-catalog](https://unsloth.ai/docs/get-started/unsloth-model-catalog)
|
| 27 |
+
{% endcontent-ref %}
|
| 28 |
+
|
| 29 |
+
{% content-ref url="../models/tutorials" %}
|
| 30 |
+
[tutorials](https://unsloth.ai/docs/models/tutorials)
|
| 31 |
+
{% endcontent-ref %}
|
| 32 |
+
{% endcolumn %}
|
| 33 |
+
{% endcolumns %}
|
| 34 |
+
|
| 35 |
+
### 🦥 Why Unsloth?
|
| 36 |
+
|
| 37 |
+
* We directly collab with teams behind [gpt-oss](https://docs.unsloth.ai/new/gpt-oss-how-to-run-and-fine-tune#unsloth-fixes-for-gpt-oss), [Qwen3](https://www.reddit.com/r/LocalLLaMA/comments/1kaodxu/qwen3_unsloth_dynamic_ggufs_128k_context_bug_fixes/), [Llama 4](https://github.com/ggml-org/llama.cpp/pull/12889), [Mistral](https://unsloth.ai/docs/models/tutorials/devstral-how-to-run-and-fine-tune), [Gemma 1-3](https://news.ycombinator.com/item?id=39671146) and [Phi-4](https://unsloth.ai/blog/phi4), where we’ve **fixed critical bugs** that greatly improved model accuracy. Andrej Karpathy for example has [praised our work](https://x.com/karpathy/status/1765473722985771335).
|
| 38 |
+
* Unsloth streamlines local training, inference, data, and deployment
|
| 39 |
+
* Unsloth supports inference and training for 500+ models: [vision](https://unsloth.ai/docs/basics/vision-fine-tuning), [TTS](https://unsloth.ai/docs/basics/text-to-speech-tts-fine-tuning), [embedding](https://unsloth.ai/docs/basics/embedding-finetuning), [RL](https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide)
|
| 40 |
+
|
| 41 |
+
### ⭐ Features
|
| 42 |
+
|
| 43 |
+
Unsloth lets you run and train models for text, [audio](https://unsloth.ai/docs/basics/text-to-speech-tts-fine-tuning), [embedding](https://unsloth.ai/docs/new/embedding-finetuning), [vision](https://unsloth.ai/docs/basics/vision-fine-tuning) and more. Unsloth provides many key features for both inference and training:
|
| 44 |
+
|
| 45 |
+
#### Inference
|
| 46 |
+
|
| 47 |
+
* Search + download + run any model like GGUFs, LoRA adapters, safetensors.
|
| 48 |
+
* [Self-healing tool calling](https://unsloth.ai/docs/new/studio/chat#auto-healing-tool-calling) / web search and call OpenAI-compatible APIs.
|
| 49 |
+
* [Auto inference parameter](https://unsloth.ai/docs/new/studio/chat#auto-parameter-tuning) tuning and edit chat templates.
|
| 50 |
+
* [Export or save](https://unsloth.ai/docs/new/studio/export) your model to GGUF, 16-bit safetensor etc.
|
| 51 |
+
* [Compare outputs](https://unsloth.ai/docs/new/studio/chat#model-arena) with two different model side by side.
|
| 52 |
+
|
| 53 |
+
#### Training
|
| 54 |
+
|
| 55 |
+
* Train and [RL](https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide) 500+ models \~2x faster with \~70% less VRAM (no accuracy loss)
|
| 56 |
+
* Supports full fine-tuning, pre-training, 4-bit, 16-bit and FP8 training.
|
| 57 |
+
* [Auto-create datasets](https://unsloth.ai/docs/new/studio/data-recipe) from PDF, CSV, DOCX files. Edit data in a visual node workflow.
|
| 58 |
+
* Observability: Monitor training live, track loss, GPU usage, customize graphs
|
| 59 |
+
* Most efficient [**reinforcement learning**](https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide) library, using 80% less VRAM for GRPO, [FP8](https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide/fp8-reinforcement-learning) etc.
|
| 60 |
+
* [Multi-GPU](https://unsloth.ai/docs/basics/multi-gpu-training-with-unsloth) works but a much better version is coming!
|
| 61 |
+
|
| 62 |
+
### Quickstart
|
| 63 |
+
|
| 64 |
+
Unsloth supports MacOS, Linux, [Windows](https://unsloth.ai/docs/get-started/install/windows-installation), [NVIDIA](https://unsloth.ai/docs/get-started/install/pip-install), Intel and CPU setups. See: [unsloth-requirements](https://unsloth.ai/docs/get-started/fine-tuning-for-beginners/unsloth-requirements "mention"). Use the same commands to update:
|
| 65 |
+
|
| 66 |
+
#### **MacOS, Linux, WSL:**
|
| 67 |
+
|
| 68 |
+
```bash
|
| 69 |
+
curl -fsSL https://unsloth.ai/install.sh | sh
|
| 70 |
+
```
|
| 71 |
+
|
| 72 |
+
#### **Windows PowerShell:**
|
| 73 |
+
|
| 74 |
+
```bash
|
| 75 |
+
irm https://unsloth.ai/install.ps1 | iex
|
| 76 |
+
```
|
| 77 |
+
|
| 78 |
+
#### Docker
|
| 79 |
+
|
| 80 |
+
Use our official **Docker image**: [`unsloth/unsloth`](https://hub.docker.com/r/unsloth/unsloth) which currently works for Windows, WSL and Linux. MacOS support coming soon.
|
| 81 |
+
|
| 82 |
+
#### Launch Unsloth
|
| 83 |
+
|
| 84 |
+
```bash
|
| 85 |
+
unsloth studio -H 0.0.0.0 -p 8888
|
| 86 |
+
```
|
| 87 |
+
|
| 88 |
+
#### New Models
|
| 89 |
+
|
| 90 |
+
<table data-view="cards"><thead><tr><th></th><th data-hidden></th><th data-hidden data-card-cover data-type="image">Cover image</th><th data-hidden data-card-target data-type="content-ref"></th></tr></thead><tbody><tr><td><strong>Kimi K2.5</strong></td><td></td><td><a href="https://3215535692-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FgcSsB0cPhjj8inDt1bqf%2Fkimi%20k25%20logo.png?alt=media&token=19aec00a-7e0f-4980-b2b7-98b65a23123e">kimi k25 logo.png</a></td><td><a href="../models/kimi-k2.5">kimi-k2.5</a></td></tr><tr><td><strong>MiniMax-M2.5</strong></td><td></td><td><a href="https://3215535692-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2F0yrdjCKbV8qnqyTrQ1pZ%2Fminimax2.5%20logo.png?alt=media&token=183839fe-6750-4c95-b058-c991ec8a5dec">minimax2.5 logo.png</a></td><td><a href="../models/minimax-m25">minimax-m25</a></td></tr><tr><td><strong>GLM-4.7-Flash</strong></td><td></td><td><a href="https://3215535692-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2F6PQZ23CoUdZs1EZCjtYn%2Fglm4.7flash.png?alt=media&token=d3dc776e-ef3e-4eb3-ad4e-bf45e7b5745a">glm4.7flash.png</a></td><td><a href="../models/glm-4.7-flash">glm-4.7-flash</a></td></tr></tbody></table>
|
| 91 |
+
|
| 92 |
+
### What is Fine-tuning and RL? Why?
|
| 93 |
+
|
| 94 |
+
[**Fine-tuning** an LLM](https://unsloth.ai/docs/get-started/fine-tuning-llms-guide) customizes its behavior, enhances domain knowledge, and optimizes performance for specific tasks. By fine-tuning a pre-trained model (e.g. Llama-3.1-8B) on a dataset, you can:
|
| 95 |
+
|
| 96 |
+
* **Update Knowledge**: Introduce new domain-specific information.
|
| 97 |
+
* **Customize Behavior**: Adjust the model’s tone, personality, or response style.
|
| 98 |
+
* **Optimize for Tasks**: Improve accuracy and relevance for specific use cases.
|
| 99 |
+
|
| 100 |
+
[**Reinforcement Learning (RL)**](https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide) is where an "agent" learns to make decisions by interacting with an environment and receiving **feedback** in the form of **rewards** or **penalties**.
|
| 101 |
+
|
| 102 |
+
* **Action:** What the model generates (e.g. a sentence).
|
| 103 |
+
* **Reward:** A signal indicating how good or bad the model's action was (e.g. did the response follow instructions? was it helpful?).
|
| 104 |
+
* **Environment:** The scenario or task the model is working on (e.g. answering a user’s question).
|
| 105 |
+
|
| 106 |
+
**Example fine-tuning or RL use-cases**:
|
| 107 |
+
|
| 108 |
+
* Enables LLMs to predict if a headline impacts a company positively or negatively.
|
| 109 |
+
* Can use historical customer interactions for more accurate and custom responses.
|
| 110 |
+
* Fine-tune LLM on legal texts for contract analysis, case law research, and compliance.
|
| 111 |
+
|
| 112 |
+
You can think of a fine-tuned model as a specialized agent designed to do specific tasks more effectively and efficiently. **Fine-tuning can replicate all of RAG's capabilities**, but not vice versa.
|
| 113 |
+
|
| 114 |
+
{% columns %}
|
| 115 |
+
{% column width="50%" %}
|
| 116 |
+
{% content-ref url="fine-tuning-for-beginners/faq-+-is-fine-tuning-right-for-me" %}
|
| 117 |
+
[faq-+-is-fine-tuning-right-for-me](https://unsloth.ai/docs/get-started/fine-tuning-for-beginners/faq-+-is-fine-tuning-right-for-me)
|
| 118 |
+
{% endcontent-ref %}
|
| 119 |
+
|
| 120 |
+
{% content-ref url="../basics/inference-and-deployment" %}
|
| 121 |
+
[inference-and-deployment](https://unsloth.ai/docs/basics/inference-and-deployment)
|
| 122 |
+
{% endcontent-ref %}
|
| 123 |
+
{% endcolumn %}
|
| 124 |
+
|
| 125 |
+
{% column width="50%" %}
|
| 126 |
+
{% content-ref url="reinforcement-learning-rl-guide" %}
|
| 127 |
+
[reinforcement-learning-rl-guide](https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide)
|
| 128 |
+
{% endcontent-ref %}
|
| 129 |
+
|
| 130 |
+
{% content-ref url="../basics/unsloth-dynamic-2.0-ggufs" %}
|
| 131 |
+
[unsloth-dynamic-2.0-ggufs](https://unsloth.ai/docs/basics/unsloth-dynamic-2.0-ggufs)
|
| 132 |
+
{% endcontent-ref %}
|
| 133 |
+
{% endcolumn %}
|
| 134 |
+
{% endcolumns %}
|
| 135 |
+
|
| 136 |
+
<figure><img src="https://3215535692-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-134302f2507d4313b9575917c9a43b0a0028856c%2Flarge%20sloth%20wave.png?alt=media" alt="" width="188"><figcaption></figcaption></figure>
|
guides/Unsloth_FineTune_Guide.html
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Fine-tuning LLMs Guide
|
| 2 |
+
|
| 3 |
+
## 1. What Is Fine-tuning?
|
| 4 |
+
|
| 5 |
+
Fine-tuning / training / post-training models customizes its behavior, enhances + injects knowledge, and optimizes performance for domains and specific tasks. For example:
|
| 6 |
+
|
| 7 |
+
* OpenAI’s **GPT-5** was post-trained to improve instruction following and helpful chat behavior.
|
| 8 |
+
* The standard method of post training is called Supervised Fine-Tuning (SFT). Other methods include preference optimization (DPO, ORPO), distillation and [Reinforcement Learning (RL)](https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide) (GRPO, GSPO), where an "agent" learns to make decisions by interacting with an environment and receiving **feedback** in the form of **rewards** or **penalties**.
|
| 9 |
+
|
| 10 |
+
With [Unsloth](https://github.com/unslothai/unsloth), you can fine-tune or do RL for free on Colab, Kaggle, or locally with just 3GB VRAM by using our [notebooks](https://docs.unsloth.ai/get-started/unsloth-notebooks). By fine-tuning a pre-trained model on a dataset, you can:
|
| 11 |
+
|
| 12 |
+
* **Update + Learn New Knowledge**: Inject and learn new domain-specific information.
|
| 13 |
+
* **Customize Behavior**: Adjust the model’s tone, personality, or response style.
|
| 14 |
+
* **Optimize for Tasks**: Improve accuracy and relevance for specific use cases.
|
| 15 |
+
|
| 16 |
+
**Example fine-tuning or RL use-cases**:
|
| 17 |
+
|
| 18 |
+
* Enables LLMs to predict if a headline impacts a company positively or negatively.
|
| 19 |
+
* Can use historical customer interactions for more accurate and custom responses.
|
| 20 |
+
* Fine-tune LLM on legal texts for contract analysis, case law research, and compliance.
|
| 21 |
+
|
| 22 |
+
You can think of a fine-tuned model as a specialized agent designed to do specific tasks more effectively and efficiently. **Fine-tuning can replicate all of RAG's capabilities**, but not vice versa.
|
| 23 |
+
|
| 24 |
+
{% columns %}
|
| 25 |
+
{% column %}
|
| 26 |
+
|
| 27 |
+
#### :question:What is LoRA/QLoRA?
|
| 28 |
+
|
| 29 |
+
In LLMs, we have model weights. Llama 70B has 70 billion numbers. Instead of changing all 70B numbers, we instead add thin matrices A and B to each weight, and optimize those. This means we only optimize 1% of weights. LoRA is when the original model is 16-bit unquantized while QLoRA quantizes to 4-bit to save 75% memory.
|
| 30 |
+
{% endcolumn %}
|
| 31 |
+
|
| 32 |
+
{% column %}
|
| 33 |
+
|
| 34 |
+
<figure><img src="https://3215535692-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-715b6260aae497f160d7f9a1019bcfa472675dcf%2Fimage%20(7)%20(1)%20(1).png?alt=media" alt=""><figcaption><p>Instead of optimizing Model Weights (yellow), we optimize 2 thin matrices A and B.</p></figcaption></figure>
|
| 35 |
+
{% endcolumn %}
|
| 36 |
+
{% endcolumns %}
|
| 37 |
+
|
| 38 |
+
#### Fine-tuning misconceptions:
|
| 39 |
+
|
| 40 |
+
You may have heard that fine-tuning does not make a model learn new knowledge or RAG performs better than fine-tuning. That is **false**. You can train a specialized coding model with fine-tuning and RL while RAG can’t change the model’s weights and only augments what the model sees at inference time. Read more FAQ + misconceptions [here](https://unsloth.ai/docs/fine-tuning-for-beginners/faq-+-is-fine-tuning-right-for-me#fine-tuning-vs.-rag-whats-the-difference):
|
| 41 |
+
|
| 42 |
+
{% content-ref url="fine-tuning-for-beginners/faq-+-is-fine-tuning-right-for-me" %}
|
| 43 |
+
[faq-+-is-fine-tuning-right-for-me](https://unsloth.ai/docs/get-started/fine-tuning-for-beginners/faq-+-is-fine-tuning-right-for-me)
|
| 44 |
+
{% endcontent-ref %}
|
| 45 |
+
|
| 46 |
+
> [**Introducing Unsloth Studio:** ](https://unsloth.ai/docs/new/studio) Our new open-source web UI for training and running models. This means you can now fine-tune models with no-code and have observability and automatic dataset creation features.
|
| 47 |
+
|
| 48 |
+
<div data-with-frame="true"><figure><img src="https://3215535692-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FxV1PO5DbF3ksB51nE2Tw%2Fmore%20cropped%20ui%20for%20homepage.png?alt=media&token=f75942c9-3d8d-4b59-8ba2-1a4a38de1b86" alt="" width="563"><figcaption></figcaption></figure></div>
|
| 49 |
+
|
| 50 |
+
## 2. Choose the Right Model + Method
|
| 51 |
+
|
| 52 |
+
If you're a beginner, it is best to start with a small instruct model like Llama 3.1 (8B) and experiment from there. You'll also need to decide between normal fine-tuning, RL, QLoRA or LoRA training:
|
| 53 |
+
|
| 54 |
+
* **Reinforcement Learning (RL)** is used when you need a model to excel at a specific behavior (e.g., tool-calling) using an environment and reward function rather than labeled data. We have several [notebook examples](https://unsloth.ai/docs/unsloth-notebooks#grpo-reasoning-rl-notebooks), but for most use-cases, standard SFT is sufficient.
|
| 55 |
+
* **LoRA** is a parameter efficient training method that typically keeps the base model’s weights frozen and trains a small set of added low-rank adapter weights (in 16-bit precision).
|
| 56 |
+
* **QLoRA** combines LoRA with 4-bit precision to handle very large models with minimal resources.
|
| 57 |
+
* Unsloth also supports full fine-tuning (FFT) and pretraining, which require significantly more resources, but FFT is usually unnecessary. When done correctly, LoRA can match FFT.
|
| 58 |
+
* Unsloth **all types models**: [text-to-speech](https://unsloth.ai/docs/basics/text-to-speech-tts-fine-tuning), [embedding](https://unsloth.ai/docs/basics/embedding-finetuning), GRPO, RL, [vision](https://unsloth.ai/docs/basics/vision-fine-tuning), multimodal and more.
|
| 59 |
+
|
| 60 |
+
{% hint style="info" %}
|
| 61 |
+
Research shows that **training and serving in the same precision** helps preserve accuracy. This means if you want to serve in 4-bit, train in 4-bit and vice versa.
|
| 62 |
+
{% endhint %}
|
| 63 |
+
|
| 64 |
+
We recommend starting with QLoRA, as it is one of the most accessible and effective methods for training models. Our [dynamic 4-bit](https://unsloth.ai/blog/dynamic-4bit) quants, the accuracy loss for QLoRA compared to LoRA is now largely recovered.
|
| 65 |
+
|
| 66 |
+
<figure><img src="https://3215535692-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-cfc51c261e6d24df3aa967d9b9a482313465cbc1%2Fmodel%20name%20change.png?alt=media" alt="" width="563"><figcaption></figcaption></figure>
|
| 67 |
+
|
| 68 |
+
You can change the model name to whichever model you like by matching it with model's name on Hugging Face e.g. '`unsloth/llama-3.1-8b-unsloth-bnb-4bit`'.
|
| 69 |
+
|
| 70 |
+
We recommend starting with **Instruct models**, as they allow direct fine-tuning using conversational chat templates (ChatML, ShareGPT etc.) and require less data compared to **Base models** (which uses Alpaca, Vicuna etc). Learn more about the differences between [instruct and base models here](https://unsloth.ai/docs/get-started/what-model-should-i-use#instruct-or-base-model).
|
| 71 |
+
|
| 72 |
+
* Model names ending in **`unsloth-bnb-4bit`** indicate they are [**Unsloth dynamic 4-bit**](https://unsloth.ai/blog/dynamic-4bit) **quants**. These models consume slightly more VRAM than standard BitsAndBytes 4-bit models but offer significantly higher accuracy.
|
| 73 |
+
* If a model name ends with just **`bnb-4bit`**, without "unsloth", it refers to a standard BitsAndBytes 4-bit quantization.
|
| 74 |
+
* Models with **no suffix** are in their original **16-bit or 8-bit formats**. While they are the original models from the official model creators, we sometimes include important fixes - such as chat template or tokenizer fixes. So it's recommended to use our versions when available.
|
| 75 |
+
|
| 76 |
+
There are other settings which you can toggle:
|
| 77 |
+
|
| 78 |
+
* **`max_seq_length = 2048`** – Controls context length. While Llama-3 supports 8192, we recommend 2048 for testing. Unsloth enables 4× longer context fine-tuning.
|
| 79 |
+
* **`dtype = None`** – Defaults to None; use `torch.float16` or `torch.bfloat16` for newer GPUs.
|
| 80 |
+
* **`load_in_4bit = True`** – Enables 4-bit quantization, reducing memory use 4× for fine-tuning. Disabling it enables LoRA 16-bit fine-tuning. You can also enable 16-bit LoRA with `load_in_16bit = True`
|
| 81 |
+
* To enable full fine-tuning (FFT), set `full_finetuning = True`. For 8-bit fine-tuning, set `load_in_8bit = True`.
|
| 82 |
+
* **Note:** Only one training method can be set to `True` at a time.
|
| 83 |
+
|
| 84 |
+
{% hint style="info" %}
|
| 85 |
+
A common mistake is jumping straight into full fine-tuning (FFT), which is compute-heavy. Start by testing with LoRA or QLoRA first, if it won’t work there, it almost certainly won’t work with FFT. And if LoRA fails, don’t assume FFT will magically fix it.
|
| 86 |
+
{% endhint %}
|
| 87 |
+
|
| 88 |
+
You can also do [Text-to-speech (TTS)](https://unsloth.ai/docs/basics/text-to-speech-tts-fine-tuning), [reasoning (GRPO)](https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide), [vision](https://unsloth.ai/docs/basics/vision-fine-tuning), [RL](https://unsloth.ai/docs/get-started/reinforcement-learning-rl-guide/preference-dpo-orpo-and-kto) (GRPO, DPO), [continued pretraining](https://unsloth.ai/docs/basics/continued-pretraining), text completion and other training methodologies with Unsloth.
|
| 89 |
+
|
| 90 |
+
{% columns %}
|
| 91 |
+
{% column %}
|
| 92 |
+
Read our guide on choosing models:
|
| 93 |
+
|
| 94 |
+
{% content-ref url="fine-tuning-llms-guide/what-model-should-i-use" %}
|
| 95 |
+
[what-model-should-i-use](https://unsloth.ai/docs/get-started/fine-tuning-llms-guide/what-model-should-i-use)
|
| 96 |
+
{% endcontent-ref %}
|
| 97 |
+
{% endcolumn %}
|
| 98 |
+
|
| 99 |
+
{% column %}
|
| 100 |
+
For inidivudal tutorials on models:
|
| 101 |
+
|
| 102 |
+
{% content-ref url="../models/tutorials" %}
|
| 103 |
+
[tutorials](https://unsloth.ai/docs/models/tutorials)
|
| 104 |
+
{% endcontent-ref %}
|
| 105 |
+
{% endcolumn %}
|
| 106 |
+
{% endcolumns %}
|
| 107 |
+
|
| 108 |
+
## 3. Your Dataset
|
| 109 |
+
|
| 110 |
+
For LLMs, datasets are collections of data that can be used to train our models. In order to be useful for training, text data needs to be in a format that can be tokenized.
|
| 111 |
+
|
| 112 |
+
* You will need to create a dataset usually with 2 columns - question and answer. The quality and amount will largely reflect the end result of your fine-tune so it's imperative to get this part right.
|
| 113 |
+
* You can [synthetically generate data](https://unsloth.ai/docs/get-started/datasets-guide#synthetic-data-generation) and structure your dataset (into QA pairs) using ChatGPT or local LLMs.
|
| 114 |
+
* You can also use our new Synthetic Dataset notebook which automatically parses documents (PDFs, videos etc.), generates QA pairs and auto cleans data using local models like Llama 3.2. [Access the notebook here.](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Meta_Synthetic_Data_Llama3_2_\(3B\).ipynb)
|
| 115 |
+
* Fine-tuning can learn from an existing repository of documents and continuously expand its knowledge base, but just dumping data alone won’t work as well. For optimal results, curate a well-structured dataset, ideally as question-answer pairs. This enhances learning, understanding, and response accuracy.
|
| 116 |
+
* But, that's not always the case, e.g. if you are fine-tuning a LLM for code, just dumping all your code data can actually enable your model to yield significant performance improvements, even without structured formatting. So it really depends on your use case.
|
| 117 |
+
|
| 118 |
+
***Read more about creating your dataset:***
|
| 119 |
+
|
| 120 |
+
{% content-ref url="fine-tuning-llms-guide/datasets-guide" %}
|
| 121 |
+
[datasets-guide](https://unsloth.ai/docs/get-started/fine-tuning-llms-guide/datasets-guide)
|
| 122 |
+
{% endcontent-ref %}
|
| 123 |
+
|
| 124 |
+
For most of our notebook examples, we utilize the [Alpaca dataset](https://docs.unsloth.ai/basics/tutorial-how-to-finetune-llama-3-and-use-in-ollama#id-6.-alpaca-dataset) however other notebooks like Vision will use different datasets which may need images in the answer ouput as well.
|
| 125 |
+
|
| 126 |
+
### 4. Understand Training Hyperparameters
|
| 127 |
+
|
| 128 |
+
Learn how to choose the right [hyperparameters](https://unsloth.ai/docs/get-started/fine-tuning-llms-guide/lora-hyperparameters-guide) using best practices from research and real-world experiments - and understand how each one affects your model's performance.
|
| 129 |
+
|
| 130 |
+
**For a complete guide on how hyperparameters affect training, see:**
|
| 131 |
+
|
| 132 |
+
{% content-ref url="fine-tuning-llms-guide/lora-hyperparameters-guide" %}
|
| 133 |
+
[lora-hyperparameters-guide](https://unsloth.ai/docs/get-started/fine-tuning-llms-guide/lora-hyperparameters-guide)
|
| 134 |
+
{% endcontent-ref %}
|
| 135 |
+
|
| 136 |
+
## 5. Install + Requirements
|
| 137 |
+
|
| 138 |
+
You can use Unsloth via two main ways, our free notebooks or locally.
|
| 139 |
+
|
| 140 |
+
### Unsloth Notebooks
|
| 141 |
+
|
| 142 |
+
We would recommend beginners to utilise our pre-made [notebooks](https://unsloth.ai/docs/get-started/unsloth-notebooks) first as it's the easiest way to get started with guided steps. You can later export the notebooks to use locally.
|
| 143 |
+
|
| 144 |
+
Unsloth has step-by-step notebooks for [text-to-speech](https://unsloth.ai/docs/basics/text-to-speech-tts-fine-tuning), [embedding](https://unsloth.ai/docs/basics/embedding-finetuning), GRPO, RL, [vision](https://unsloth.ai/docs/basics/vision-fine-tuning), multimodal, different use-cases and more.
|
| 145 |
+
|
| 146 |
+
### Local Installation
|
| 147 |
+
|
| 148 |
+
You can also install Unsloth locally via [docker](https://unsloth.ai/docs/get-started/install/docker "mention") or `pip install unsloth` (with Linux, WSL or [Windows](https://unsloth.ai/docs/get-started/install/windows-installation)). Also depending on the model you're using, you'll need enough VRAM and resources.
|
| 149 |
+
|
| 150 |
+
Installing Unsloth will require a Windows or Linux device. Once you install Unsloth, you can copy and paste our notebooks and use them in your own local environment. See:
|
| 151 |
+
|
| 152 |
+
{% columns %}
|
| 153 |
+
{% column %}
|
| 154 |
+
{% content-ref url="fine-tuning-for-beginners/unsloth-requirements" %}
|
| 155 |
+
[unsloth-requirements](https://unsloth.ai/docs/get-started/fine-tuning-for-beginners/unsloth-requirements)
|
| 156 |
+
{% endcontent-ref %}
|
| 157 |
+
{% endcolumn %}
|
| 158 |
+
|
| 159 |
+
{% column %}
|
| 160 |
+
{% content-ref url="install" %}
|
| 161 |
+
[install](https://unsloth.ai/docs/get-started/install)
|
| 162 |
+
{% endcontent-ref %}
|
| 163 |
+
{% endcolumn %}
|
| 164 |
+
{% endcolumns %}
|
| 165 |
+
|
| 166 |
+
## 6. Training + Evaluation
|
| 167 |
+
|
| 168 |
+
Once you have everything set, it's time to train! If something's not working, remember you can always change hyperparameters, your dataset etc.
|
| 169 |
+
|
| 170 |
+
You’ll see a log of numbers during training. This is the training loss, which shows how well the model is learning from your dataset. For many cases, a loss around 0.5 to 1.0 is a good sign, but it depends on your dataset and task. If the loss is not going down, you might need to adjust your settings. If the loss goes to 0, that could mean overfitting, so it's important to check validation too.
|
| 171 |
+
|
| 172 |
+
<figure><img src="https://3215535692-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-feb9b0f5763d41cecaec9a3a9cd227ad918f0ca7%2Fimage.png?alt=media" alt="" width="375"><figcaption><p>The training loss will appear as numbers</p></figcaption></figure>
|
| 173 |
+
|
| 174 |
+
We generally recommend keeping the default settings unless you need longer training or larger batch sizes.
|
| 175 |
+
|
| 176 |
+
* **`per_device_train_batch_size = 2`** – Increase for better GPU utilization but beware of slower training due to padding. Instead, increase `gradient_accumulation_steps` for smoother training.
|
| 177 |
+
* **`gradient_accumulation_steps = 4`** – Simulates a larger batch size without increasing memory usage.
|
| 178 |
+
* **`max_steps = 60`** – Speeds up training. For full runs, replace with `num_train_epochs = 1` (1–3 epochs recommended to avoid overfitting).
|
| 179 |
+
* **`learning_rate = 2e-4`** – Lower for slower but more precise fine-tuning. Try values like `1e-4`, `5e-5`, or `2e-5`.
|
| 180 |
+
|
| 181 |
+
#### Evaluation
|
| 182 |
+
|
| 183 |
+
In order to evaluate, you could do manually evaluation by just chatting with the model and see if it's to your liking. You can also enable evaluation for Unsloth, but keep in mind it can be time-consuming depending on the dataset size. To speed up evaluation you can: reduce the evaluation dataset size or set `evaluation_steps = 100`.
|
| 184 |
+
|
| 185 |
+
For testing, you can also take 20% of your training data and use that for testing. If you already used all of the training data, then you have to manually evaluate it. You can also use automatic eval tools but keep in mind that automated tools may not perfectly align with your evaluation criteria.
|
| 186 |
+
|
| 187 |
+
## 7. Running + Deploying the model
|
| 188 |
+
|
| 189 |
+
Now let's run the model after we completed the training process! You can edit the yellow underlined part! In fact, because we created a multi turn chatbot, we can now also call the model as if it saw some conversations in the past like below:
|
| 190 |
+
|
| 191 |
+
<div><figure><img src="https://3215535692-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-f2d5f23fa62ec89e06bf20fea433f9a1e42a2fe3%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure> <figure><img src="https://3215535692-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-cdf5d779635901dce7793df92531dbf3caf0fb0a%2Fimage%20(47).png?alt=media" alt=""><figcaption></figcaption></figure></div>
|
| 192 |
+
|
| 193 |
+
Reminder Unsloth itself provides **2x faster inference** natively as well, so always do not forget to call `FastLanguageModel.for_inference(model)`. If you want the model to output longer responses, set `max_new_tokens = 128` to some larger number like 256 or 1024. Notice you will have to wait longer for the result as well!
|
| 194 |
+
|
| 195 |
+
### Saving + Deployment
|
| 196 |
+
|
| 197 |
+
For saving and deploying your model in desired inference engines like Ollama, vLLM, Open WebUI, you will need to use the LoRA adapter on top of the base model. We have designated guides for each framework:
|
| 198 |
+
|
| 199 |
+
{% content-ref url="../basics/inference-and-deployment" %}
|
| 200 |
+
[inference-and-deployment](https://unsloth.ai/docs/basics/inference-and-deployment)
|
| 201 |
+
{% endcontent-ref %}
|
| 202 |
+
|
| 203 |
+
{% columns %}
|
| 204 |
+
{% column %}
|
| 205 |
+
If you’re running inference on a single device (like a laptop or Mac), use llama.cpp to convert to GGUF format to use in Ollama, llama.cpp, LM Studio etc:
|
| 206 |
+
|
| 207 |
+
{% content-ref url="../basics/inference-and-deployment/saving-to-gguf" %}
|
| 208 |
+
[saving-to-gguf](https://unsloth.ai/docs/basics/inference-and-deployment/saving-to-gguf)
|
| 209 |
+
{% endcontent-ref %}
|
| 210 |
+
{% endcolumn %}
|
| 211 |
+
|
| 212 |
+
{% column %}
|
| 213 |
+
If you’re deploying an LLM for enterprise or multi-user inference for FP8, AWQ, use vLLM:
|
| 214 |
+
|
| 215 |
+
{% content-ref url="../basics/inference-and-deployment/vllm-guide" %}
|
| 216 |
+
[vllm-guide](https://unsloth.ai/docs/basics/inference-and-deployment/vllm-guide)
|
| 217 |
+
{% endcontent-ref %}
|
| 218 |
+
{% endcolumn %}
|
| 219 |
+
{% endcolumns %}
|
| 220 |
+
|
| 221 |
+
We can now save the fine-tuned model as a small 100MB file called a LoRA adapter like below. You can instead push to the Hugging Face hub as well if you want to upload your model! Remember to get a Hugging Face [token](https://huggingface.co/settings/tokens) and add your token!
|
| 222 |
+
|
| 223 |
+
<div><figure><img src="https://3215535692-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-8c577103f7c4fe883cabaf35c8437307c6501686%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure> <figure><img src="https://3215535692-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-1a1be852ca551240bdce47cf99e6ccd7d31c1326%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure></div>
|
| 224 |
+
|
| 225 |
+
After saving the model, we can again use Unsloth to run the model itself! Use `FastLanguageModel` again to call it for inference!
|
| 226 |
+
|
| 227 |
+
## 8. We're done!
|
| 228 |
+
|
| 229 |
+
You've successfully fine-tuned a language model and exported it to your desired inference engine with Unsloth!
|
| 230 |
+
|
| 231 |
+
To learn more about fine-tuning tips and tricks, head over to our blogs which provide tremendous and educational value: <https://unsloth.ai/blog/>
|
| 232 |
+
|
| 233 |
+
If you need any help on fine-tuning, you can also join our Discord server [here](https://discord.gg/unsloth) or [Reddit r/unsloth](https://www.reddit.com/r/unsloth/). Thanks for reading and hopefully this was helpful!
|
| 234 |
+
|
| 235 |
+
<figure><img src="https://3215535692-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-69482ba90d417f7bf98dddaf83795cdd3eb20efc%2Fsloth%20sparkling%20square.png?alt=media" alt="" width="188"><figcaption></figcaption></figure>
|
guides/Unsloth_LoRA_Hyperparameters.html
ADDED
|
@@ -0,0 +1,365 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# LoRA fine-tuning Hyperparameters Guide
|
| 2 |
+
|
| 3 |
+
LoRA hyperparameters are tunable settings that govern how Low-Rank Adaptation [fine-tunes](https://unsloth.ai/docs/get-started/fine-tuning-llms-guide) LLMs. With many choices (e.g., learning rate and epochs) and countless combinations, picking the right values is key to accuracy, stability, quality, and fewer hallucinations. Done well, **LoRA can match full fine-tuning performance** while using 4× less VRAM.
|
| 4 |
+
|
| 5 |
+
You'll learn the best practices for these parameters, based on insights from hundreds of research papers and experiments, and see how they impact the model. **While we recommend using Unsloth's defaults**, understanding these concepts will give you full control.\
|
| 6 |
+
\
|
| 7 |
+
The goal is to change hyperparameter numbers to increase accuracy while counteracting [**overfitting or underfitting**](#overfitting-poor-generalization-too-specialized). Overfitting occurs when the model memorizes the training data, harming its ability to generalize to new, unseen inputs. The objective is a model that generalizes well, not one that simply memorizes.
|
| 8 |
+
|
| 9 |
+
{% columns %}
|
| 10 |
+
{% column %}
|
| 11 |
+
|
| 12 |
+
#### :question:But what is LoRA?
|
| 13 |
+
|
| 14 |
+
In LLMs, we have model weights. Llama 70B has 70 billion numbers. Instead of changing all 70b numbers, we instead add thin matrices A and B to each weight, and optimize those. This means we only optimize 1% of weights.
|
| 15 |
+
{% endcolumn %}
|
| 16 |
+
|
| 17 |
+
{% column %}
|
| 18 |
+
|
| 19 |
+
<figure><img src="https://3215535692-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-715b6260aae497f160d7f9a1019bcfa472675dcf%2Fimage%20(7)%20(1)%20(1).png?alt=media" alt=""><figcaption><p>Instead of optimizing Model Weights (yellow), we optimize 2 thin matrices A and B.</p></figcaption></figure>
|
| 20 |
+
{% endcolumn %}
|
| 21 |
+
{% endcolumns %}
|
| 22 |
+
|
| 23 |
+
## :1234: Key Fine-tuning Hyperparameters
|
| 24 |
+
|
| 25 |
+
### **Learning Rate**
|
| 26 |
+
|
| 27 |
+
Defines how much the model’s weights are adjusted during each training step.
|
| 28 |
+
|
| 29 |
+
* **Higher Learning Rates**: Lead to faster initial convergence but can cause training to become unstable or fail to find an optimal minimum if set too high.
|
| 30 |
+
* **Lower Learning Rates**: Result in more stable and precise training but may require more epochs to converge, increasing overall training time. While low learning rates are often thought to cause underfitting, they actually can lead to **overfitting** or even prevent the model from learning.
|
| 31 |
+
* **Typical Range**: `2e-4` (0.0002) to `5e-6` (0.000005).\
|
| 32 |
+
:green\_square: ***For normal LoRA/QLoRA Fine-tuning***, *we recommend* **`2e-4`** *as a starting point.*\
|
| 33 |
+
:blue\_square: ***For Reinforcement Learning** (DPO, GRPO etc.), we recommend* **`5e-6` .**\
|
| 34 |
+
:white\_large\_square: ***For Full Fine-tuning,** lower learning rates are generally more appropriate.*
|
| 35 |
+
|
| 36 |
+
### **Epochs**
|
| 37 |
+
|
| 38 |
+
The number of times the model sees the full training dataset.
|
| 39 |
+
|
| 40 |
+
* **More Epochs:** Can help the model learn better, but a high number can cause it to **memorize the training data**, hurting its performance on new tasks.
|
| 41 |
+
* **Fewer Epochs:** Reduces training time and can prevent overfitting, but may result in an undertrained model if the number is insufficient for the model to learn the dataset's underlying patterns.
|
| 42 |
+
* **Recommended:** 1-3 epochs. For most instruction-based datasets, training for more than 3 epochs offers diminishing returns and increases the risk of overfitting.
|
| 43 |
+
|
| 44 |
+
### **LoRA or QLoRA**
|
| 45 |
+
|
| 46 |
+
LoRA uses 16-bit precision, while QLoRA is a 4-bit fine-tuning method.
|
| 47 |
+
|
| 48 |
+
* **LoRA:** 16-bit fine-tuning. It's slightly faster and slightly more accurate, but consumes significantly more VRAM (4× more than QLoRA). Recommended for 16-bit environments and scenarios where maximum accuracy is required.
|
| 49 |
+
* **QLoRA:** 4-bit fine-tuning. Slightly slower and marginally less accurate, but uses much less VRAM (4× less).\
|
| 50 |
+
:sloth: *70B LLaMA fits in <48GB VRAM with QLoRA in Unsloth -* [*more details here*](https://unsloth.ai/blog/llama3-3)*.*
|
| 51 |
+
|
| 52 |
+
### Hyperparameters & Recommendations:
|
| 53 |
+
|
| 54 |
+
<table><thead><tr><th width="154.39678955078125">Hyperparameter</th><th width="383.6192626953125">Function</th><th>Recommended Settings</th></tr></thead><tbody><tr><td><strong>LoRA Rank</strong> (<code>r</code>)</td><td>Controls the number of trainable parameters in the LoRA adapter matrices. A higher rank increases model capacity but also memory usage.</td><td>8, 16, 32, 64, 128<br><br>Choose 16 or 32</td></tr><tr><td><strong>LoRA Alpha</strong> (<code>lora_alpha</code>)</td><td>Scales the strength of the fine-tuned adjustments in relation to the rank (<code>r</code>).</td><td><code>r</code> (standard) or <code>r * 2</code> (common heuristic). <a href="#lora-alpha-and-rank-relationship">More details here</a>.</td></tr><tr><td><strong>LoRA Dropout</strong></td><td>A regularization technique that randomly sets a fraction of LoRA activations to zero during training to prevent overfitting. <strong>Not that useful</strong>, so we default set it to 0.</td><td>0 (default) to 0.1</td></tr><tr><td><strong>Weight Decay</strong></td><td>A regularization term that penalizes large weights to prevent overfitting and improve generalization. Don't use too large numbers!</td><td>0.01 (recommended) - 0.1</td></tr><tr><td><strong>Warmup Steps</strong></td><td>Gradually increases the learning rate at the start of training.</td><td>5-10% of total steps</td></tr><tr><td><strong>Scheduler Type</strong></td><td>Adjusts the learning rate dynamically during training.</td><td><code>linear</code> or <code>cosine</code></td></tr><tr><td><strong>Seed (<code>random_state</code>)</strong></td><td>A fixed number to ensure reproducibility of results.</td><td>Any integer (e.g., <code>42</code>, <code>3407</code>)</td></tr><tr><td><strong>Target Modules</strong></td><td><p>Specify which parts of the model you want to apply LoRA adapters to — either the attention, the MLP, or both.</p><p><br>Attention: <code>q_proj, k_proj, v_proj, o_proj</code><br><br>MLP: <code>gate_proj, up_proj, down_proj</code></p></td><td>Recommended to target all major linear layers: <code>q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj</code>.</td></tr></tbody></table>
|
| 55 |
+
|
| 56 |
+
## :deciduous\_tree: Gradient Accumulation and Batch Size equivalency
|
| 57 |
+
|
| 58 |
+
### Effective Batch Size
|
| 59 |
+
|
| 60 |
+
Correctly configuring your batch size is critical for balancing training stability with your GPU's VRAM limitations. This is managed by two parameters whose product is the **Effective Batch Size**.\
|
| 61 |
+
\
|
| 62 |
+
**Effective Batch Size** = `batch_size * gradient_accumulation_steps`
|
| 63 |
+
|
| 64 |
+
* A **larger Effective Batch Size** generally leads to smoother, more stable training.
|
| 65 |
+
* A **smaller Effective Batch Size** may introduce more variance.
|
| 66 |
+
|
| 67 |
+
While every task is different, the following configuration provides a great starting point for achieving a stable **Effective Batch Size** of 16, which works well for most fine-tuning tasks on modern GPUs.
|
| 68 |
+
|
| 69 |
+
| Parameter | Description | Recommended Setting |
|
| 70 |
+
| --------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------- |
|
| 71 |
+
| **Batch Size** (`batch_size`) | <p>The number of samples processed in a single forward/backward pass on one GPU.<br><br><strong>Primary Driver of VRAM Usage</strong>. Higher values can improve hardware utilization and speed up training, but only if they fit in memory.</p> | 2 |
|
| 72 |
+
| **Gradient Accumulation** (`gradient_accumulation_steps`) | <p>The number of micro-batches to process before performing a single model weight update.<br><br><strong>Primary Driver of Training Time.</strong> Allows simulation of a larger <code>batch\_size</code> to conserve VRAM. Higher values increase training time per epoch.</p> | 8 |
|
| 73 |
+
| **Effective Batch Size** (Calculated) | The true batch size used for each gradient update. It directly influences training stability, quality, and final model performance. | <p>4 to 16<br>Recommended: 16 (from 2 \* 8)</p> |
|
| 74 |
+
|
| 75 |
+
### The VRAM & Performance Trade-off
|
| 76 |
+
|
| 77 |
+
Assume you want 32 samples of data per training step. Then you can use any of the following configurations:
|
| 78 |
+
|
| 79 |
+
* `batch_size = 32, gradient_accumulation_steps = 1`
|
| 80 |
+
* `batch_size = 16, gradient_accumulation_steps = 2`
|
| 81 |
+
* `batch_size = 8, gradient_accumulation_steps = 4`
|
| 82 |
+
* `batch_size = 4, gradient_accumulation_steps = 8`
|
| 83 |
+
* `batch_size = 2, gradient_accumulation_steps = 16`
|
| 84 |
+
* `batch_size = 1, gradient_accumulation_steps = 32`
|
| 85 |
+
|
| 86 |
+
While all of these are equivalent for the model's weight updates, they have vastly different hardware requirements.
|
| 87 |
+
|
| 88 |
+
The first configuration (`batch_size = 32`) uses the **most VRAM** and will likely fail on most GPUs. The last configuration (`batch_size = 1`) uses the **least VRAM,** but at the cost of slightly slower training**.** To avoid OOM (out of memory) errors, always prefer to set a smaller `batch_size` and increase `gradient_accumulation_steps` to reach your target **Effective Batch Size**.
|
| 89 |
+
|
| 90 |
+
### :sloth: Unsloth Gradient Accumulation Fix
|
| 91 |
+
|
| 92 |
+
Gradient accumulation and batch sizes <mark style="color:green;">**are now fully equivalent in Unsloth**</mark> due to our bug fixes for gradient accumulation. We have implemented specific bug fixes for gradient accumulation that resolve a common issue where the two methods did not produce the same results. This was a known challenge in the wider community, but for Unsloth users, the two methods are now interchangeable.
|
| 93 |
+
|
| 94 |
+
[Read our blog post](https://unsloth.ai/blog/gradient) for more details.
|
| 95 |
+
|
| 96 |
+
Prior to our fixes, combinations of `batch_size` and `gradient_accumulation_steps` that yielded the same **Effective Batch Size** (i.e., `batch_size × gradient_accumulation_steps = 16`) did not result in equivalent training behavior. For example, configurations like `b1/g16`, `b2/g8`, `b4/g4`, `b8/g2`, and `b16/g1` all have an **Effective Batch Size** of 16, but as shown in the graph, the loss curves did not align when using standard gradient accumulation:
|
| 97 |
+
|
| 98 |
+
<figure><img src="https://3215535692-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-66eb907fd9ce38ab29dacef82794d0525057aeb4%2FBefore_-_Standard_gradient_accumulation_UQOFkUggudXuV9dzrh8MA.svg?alt=media" alt=""><figcaption><p>(Before - Standard Gradient Accumulation)</p></figcaption></figure>
|
| 99 |
+
|
| 100 |
+
After applying our fixes, the loss curves now align correctly, regardless of how the **Effective Batch Size** of 16 is achieved:
|
| 101 |
+
|
| 102 |
+
<figure><img src="https://3215535692-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-61f7c60412a2a39584f75cce5dca41e3e35eb7f2%2FAfter_-_Unsloth_gradient_accumulation_6Y4pJdJF0vruzradUpymY.svg?alt=media" alt=""><figcaption><p>(After - 🦥 <mark style="color:green;">Unsloth Gradient Accumulation</mark>)</p></figcaption></figure>
|
| 103 |
+
|
| 104 |
+
## 🦥 **LoRA Hyperparameters in Unsloth**
|
| 105 |
+
|
| 106 |
+
The following demonstrates a standard configuration. **While Unsloth provides optimized defaults**, understanding these parameters is key to manual tuning.
|
| 107 |
+
|
| 108 |
+
<div data-full-width="false"><figure><img src="https://3215535692-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-9843f8cc26aac6445236250f5c32394186eace59%2Fnotebook_parameter_screenshott.png?alt=media" alt=""><figcaption></figcaption></figure></div>
|
| 109 |
+
|
| 110 |
+
1. ```python
|
| 111 |
+
r = 16, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128
|
| 112 |
+
```
|
| 113 |
+
|
| 114 |
+
The rank (`r`) of the fine-tuning process. A larger rank uses more memory and will be slower, but can increase accuracy on complex tasks. We suggest ranks like 8 or 16 (for fast fine-tunes) and up to 128. Using a rank that is too large can cause overfitting and harm your model's quality.\\
|
| 115 |
+
2. ```python
|
| 116 |
+
target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
|
| 117 |
+
"gate_proj", "up_proj", "down_proj",],
|
| 118 |
+
```
|
| 119 |
+
|
| 120 |
+
For optimal performance, <mark style="background-color:blue;">**LoRA should be applied to all major linear layers**</mark>. [Research has shown](#lora-target-modules-and-qlora-vs-lora) that targeting all major layers is crucial for matching the performance of full fine-tuning. While it's possible to remove modules to reduce memory usage, we strongly advise against it to preserve maximum quality as the savings are minimal.\\
|
| 121 |
+
3. ```python
|
| 122 |
+
lora_alpha = 16,
|
| 123 |
+
```
|
| 124 |
+
|
| 125 |
+
A scaling factor that controls the strength of the fine-tuned adjustments. Setting it equal to the rank (`r`) is a reliable baseline. A popular and effective heuristic is to set it to double the rank (`r * 2`), which makes the model learn more aggressively by giving more weight to the LoRA updates. [More details here](#lora-alpha-and-rank-relationship).\\
|
| 126 |
+
4. ```python
|
| 127 |
+
lora_dropout = 0, # Supports any, but = 0 is optimized
|
| 128 |
+
```
|
| 129 |
+
|
| 130 |
+
A regularization technique that helps [prevent overfitting](#overfitting-poor-generalization-too-specialized) by randomly setting a fraction of the LoRA activations to zero during each training step. [Recent research suggests](https://arxiv.org/abs/2410.09692) that for **the short training runs** common in fine-tuning, `lora_dropout` may be an unreliable regularizer.\
|
| 131 |
+
🦥 *Unsloth's internal code can optimize training when* `lora_dropout = 0`*, making it slightly faster, but we recommend a non-zero value if you suspect overfitting.*\\
|
| 132 |
+
5. ```python
|
| 133 |
+
bias = "none", # Supports any, but = "none" is optimized
|
| 134 |
+
```
|
| 135 |
+
|
| 136 |
+
Leave this as `"none"` for faster training and reduced memory usage. This setting avoids training the bias terms in the linear layers, which adds trainable parameters for little to no practical gain.\\
|
| 137 |
+
6. ```python
|
| 138 |
+
use_gradient_checkpointing = "unsloth", # True or "unsloth" for very long context
|
| 139 |
+
```
|
| 140 |
+
|
| 141 |
+
Options are `True`, `False`, and `"unsloth"`.\
|
| 142 |
+
🦥 *We recommend* `"unsloth"` *as it reduces memory usage by an extra 30% and supports extremely long context fine-tunes. You can read more on* [*our blog post about long context training*](https://unsloth.ai/blog/long-context)*.*\\
|
| 143 |
+
7. ```python
|
| 144 |
+
random_state = 3407,
|
| 145 |
+
```
|
| 146 |
+
|
| 147 |
+
The seed to ensure deterministic, reproducible runs. Training involves random numbers, so setting a fixed seed is essential for consistent experiments.\\
|
| 148 |
+
8. ```python
|
| 149 |
+
use_rslora = False, # We support rank stabilized LoRA
|
| 150 |
+
```
|
| 151 |
+
|
| 152 |
+
An advanced feature that implements [**Rank-Stabilized LoRA**](https://arxiv.org/abs/2312.03732). If set to `True`, the effective scaling becomes `lora_alpha / sqrt(r)` instead of the standard `lora_alpha / r`. This can sometimes improve stability, particularly for higher ranks. [More details here](#lora-alpha-and-rank-relationship).\\
|
| 153 |
+
9. ```python
|
| 154 |
+
loftq_config = None, # And LoftQ
|
| 155 |
+
```
|
| 156 |
+
|
| 157 |
+
An advanced technique, as proposed in [**LoftQ**](https://arxiv.org/abs/2310.08659), initializes LoRA matrices with the top 'r' singular vectors from the pretrained weights. This can improve accuracy but may cause a significant memory spike at the start of training.
|
| 158 |
+
|
| 159 |
+
### **Verifying LoRA Weight Updates:**
|
| 160 |
+
|
| 161 |
+
When validating that **LoRA** adapter weights have been updated after fine-tuning, avoid using **np.allclose()** for comparison. This method can miss subtle but meaningful changes, particularly in **LoRA A**, which is initialized with small Gaussian values. These changes may not register as significant under loose numerical tolerances. Thanks to [contributors](https://github.com/unslothai/unsloth/issues/3035) for this section.
|
| 162 |
+
|
| 163 |
+
To reliably confirm weight updates, we recommend:
|
| 164 |
+
|
| 165 |
+
* Using **checksum or hash comparisons** (e.g., MD5)
|
| 166 |
+
* Computing the **sum of absolute differences** between tensors
|
| 167 |
+
* Inspecting t**ensor statistics** (e.g., mean, variance) manually
|
| 168 |
+
* Or using **np.array\_equal()** if exact equality is expected
|
| 169 |
+
|
| 170 |
+
## :triangular\_ruler:LoRA Alpha and Rank relationship
|
| 171 |
+
|
| 172 |
+
{% hint style="success" %}
|
| 173 |
+
It's best to set `lora_alpha = 2 * lora_rank` or `lora_alpha = lora_rank`
|
| 174 |
+
{% endhint %}
|
| 175 |
+
|
| 176 |
+
{% columns %}
|
| 177 |
+
{% column width="50%" %}
|
| 178 |
+
$$
|
| 179 |
+
\hat{W} = W + \frac{\alpha}{\text{rank}} \times AB
|
| 180 |
+
$$
|
| 181 |
+
|
| 182 |
+
<figure><img src="https://3215535692-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-8e4f60c002f22e8ca9c534b48323e9e77e4b5ea6%2Fimage.png?alt=media" alt=""><figcaption><p>rsLoRA other scaling options. sqrt(r) is the best.</p></figcaption></figure>
|
| 183 |
+
|
| 184 |
+
$$
|
| 185 |
+
\hat{W}\_{\text{rslora}} = W + \frac{\alpha}{\sqrt{\text{rank}}} \times AB
|
| 186 |
+
$$
|
| 187 |
+
{% endcolumn %}
|
| 188 |
+
|
| 189 |
+
{% column %}
|
| 190 |
+
The formula for LoRA is on the left. We need to scale the thin matrices A and B by alpha divided by the rank. <mark style="background-color:blue;">**This means we should keep alpha/rank at least = 1**</mark>.
|
| 191 |
+
|
| 192 |
+
According to the [rsLoRA (rank stabilized lora) paper](https://arxiv.org/abs/2312.03732), we should instead scale alpha by the sqrt of the rank. Other options exist, but theoretically this is the optimum. The left plot shows other ranks and their perplexities (lower is better). To enable this, set `use_rslora = True` in Unsloth.
|
| 193 |
+
|
| 194 |
+
Our recommendation is to set the <mark style="background-color:green;">**alpha to equal to the rank, or at least 2 times the rank.**</mark> This means alpha/rank = 1 or 2.
|
| 195 |
+
{% endcolumn %}
|
| 196 |
+
{% endcolumns %}
|
| 197 |
+
|
| 198 |
+
## :dart: LoRA Target Modules and QLoRA vs LoRA
|
| 199 |
+
|
| 200 |
+
{% hint style="success" %}
|
| 201 |
+
Use:\
|
| 202 |
+
`target_modules = ["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj",]` to target both **MLP** and **attention** layers to increase accuracy.
|
| 203 |
+
|
| 204 |
+
**QLoRA uses 4-bit precision**, reducing VRAM usage by over 75%.
|
| 205 |
+
|
| 206 |
+
**LoRA (16-bit)** is slightly more accurate and faster.
|
| 207 |
+
{% endhint %}
|
| 208 |
+
|
| 209 |
+
According to empirical experiments and research papers like the original [QLoRA paper](https://arxiv.org/pdf/2305.14314), it's best to apply LoRA to both attention and MLP layers.
|
| 210 |
+
|
| 211 |
+
{% columns %}
|
| 212 |
+
{% column %}
|
| 213 |
+
|
| 214 |
+
<figure><img src="https://3215535692-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-16bef8165ccace21d0533f1941b8268a165c6a37%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>
|
| 215 |
+
{% endcolumn %}
|
| 216 |
+
|
| 217 |
+
{% column %}
|
| 218 |
+
The chart shows RougeL scores (higher is better) for different target module configurations, comparing LoRA vs QLoRA.
|
| 219 |
+
|
| 220 |
+
The first 3 dots show:
|
| 221 |
+
|
| 222 |
+
1. **QLoRA-All:** LoRA applied to all FFN/MLP and Attention layers.\
|
| 223 |
+
:fire: *This performs best overall.*
|
| 224 |
+
2. **QLoRA-FFN**: LoRA only on FFN.\
|
| 225 |
+
Equivalent to: `gate_proj`, `up_proj`, `down_proj.`
|
| 226 |
+
3. **QLoRA-Attention**: LoRA applied only to Attention layers.\
|
| 227 |
+
Equivalent to: `q_proj`, `k_proj`, `v_proj`, `o_proj`.
|
| 228 |
+
{% endcolumn %}
|
| 229 |
+
{% endcolumns %}
|
| 230 |
+
|
| 231 |
+
## :sunglasses: Training on completions only, masking out inputs
|
| 232 |
+
|
| 233 |
+
The [QLoRA paper](https://arxiv.org/pdf/2305.14314) shows that masking out inputs and **training only on completions** (outputs or assistant messages) can further **increase accuracy** by a few percentage points (*1%*). Below demonstrates how this is done in Unsloth:
|
| 234 |
+
|
| 235 |
+
{% columns %}
|
| 236 |
+
{% column %}
|
| 237 |
+
**NOT** training on completions only:
|
| 238 |
+
|
| 239 |
+
**USER:** <mark style="background-color:green;">Hello what is 2+2?</mark>\
|
| 240 |
+
**ASSISTANT:** <mark style="background-color:green;">The answer is 4.</mark>\
|
| 241 |
+
**USER:** <mark style="background-color:green;">Hello what is 3+3?</mark>\
|
| 242 |
+
**ASSISTANT:** <mark style="background-color:green;">The answer is 6.</mark>
|
| 243 |
+
{% endcolumn %}
|
| 244 |
+
|
| 245 |
+
{% column %}
|
| 246 |
+
**Training** on completions only:
|
| 247 |
+
|
| 248 |
+
**USER:** ~~Hello what is 2+2?~~\
|
| 249 |
+
**ASSISTANT:** <mark style="background-color:green;">The answer is 4.</mark>\
|
| 250 |
+
**USER:** ~~Hello what is 3+3?~~\
|
| 251 |
+
**ASSISTANT:** <mark style="background-color:green;">The answer is 6</mark><mark style="background-color:green;">**.**</mark>
|
| 252 |
+
{% endcolumn %}
|
| 253 |
+
{% endcolumns %}
|
| 254 |
+
|
| 255 |
+
The QLoRA paper states that **training on completions only** increases accuracy by quite a bit, especially for multi-turn conversational finetunes! We do this in our [conversational notebooks here](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Llama3.2_\(1B_and_3B\)-Conversational.ipynb).
|
| 256 |
+
|
| 257 |
+
<figure><img src="https://3215535692-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-7e73b480d1db1dd3d52dd0d4a7e24caff6a54be0%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>
|
| 258 |
+
|
| 259 |
+
To enable **training on completions** in Unsloth, you will need to define the instruction and assistant parts. :sloth: *We plan to further automate this for you in the future!*
|
| 260 |
+
|
| 261 |
+
For Llama 3, 3.1, 3.2, 3.3 and 4 models, you define the parts as follows:
|
| 262 |
+
|
| 263 |
+
```python
|
| 264 |
+
from unsloth.chat_templates import train_on_responses_only
|
| 265 |
+
trainer = train_on_responses_only(
|
| 266 |
+
trainer,
|
| 267 |
+
instruction_part = "<|start_header_id|>user<|end_header_id|>\n\n",
|
| 268 |
+
response_part = "<|start_header_id|>assistant<|end_header_id|>\n\n",
|
| 269 |
+
)
|
| 270 |
+
```
|
| 271 |
+
|
| 272 |
+
For Gemma 2, 3, 3n models, you define the parts as follows:
|
| 273 |
+
|
| 274 |
+
```python
|
| 275 |
+
from unsloth.chat_templates import train_on_responses_only
|
| 276 |
+
trainer = train_on_responses_only(
|
| 277 |
+
trainer,
|
| 278 |
+
instruction_part = "<start_of_turn>user\n",
|
| 279 |
+
response_part = "<start_of_turn>model\n",
|
| 280 |
+
)
|
| 281 |
+
```
|
| 282 |
+
|
| 283 |
+
##  :mag\_right:Training on assistant responses only for vision models, VLMs
|
| 284 |
+
|
| 285 |
+
For language models, we can use `from unsloth.chat_templates import train_on_responses_only` as described previously. For vision models, use the extra arguments as part of `UnslothVisionDataCollator` just like before!
|
| 286 |
+
|
| 287 |
+
{% code overflow="wrap" %}
|
| 288 |
+
|
| 289 |
+
```python
|
| 290 |
+
class UnslothVisionDataCollator:
|
| 291 |
+
def __init__(
|
| 292 |
+
self,
|
| 293 |
+
...
|
| 294 |
+
# from unsloth.chat_templates import train_on_responses_only
|
| 295 |
+
# trainer = train_on_responses_only(
|
| 296 |
+
# trainer,
|
| 297 |
+
# instruction_part = "<|start_header_id|>user<|end_header_id|>\n\n",
|
| 298 |
+
# response_part = "<|start_header_id|>assistant<|end_header_id|>\n\n",
|
| 299 |
+
# )
|
| 300 |
+
train_on_responses_only = False, # EQUIVALENT to train_on_responses_only for LLMs
|
| 301 |
+
instruction_part = None, # EQUIVALENT to train_on_responses_only(instruction_part = ...)
|
| 302 |
+
response_part = None, # EQUIVALENT to train_on_responses_only(response_part = ...)
|
| 303 |
+
force_match = True, # Match newlines as well!
|
| 304 |
+
)
|
| 305 |
+
```
|
| 306 |
+
|
| 307 |
+
{% endcode %}
|
| 308 |
+
|
| 309 |
+
For example for Llama 3.2 Vision:
|
| 310 |
+
|
| 311 |
+
```python
|
| 312 |
+
UnslothVisionDataCollator(
|
| 313 |
+
model, tokenizer,
|
| 314 |
+
...
|
| 315 |
+
train_on_responses_only = True,
|
| 316 |
+
instruction_part = "<|start_header_id|>user<|end_header_id|>\n\n",
|
| 317 |
+
response_part = "<|start_header_id|>assistant<|end_header_id|>\n\n",
|
| 318 |
+
...
|
| 319 |
+
)
|
| 320 |
+
```
|
| 321 |
+
|
| 322 |
+
## :key: **Avoiding Overfitting & Underfitting**
|
| 323 |
+
|
| 324 |
+
### **Overfitting** (Poor Generalization/Too Specialized)
|
| 325 |
+
|
| 326 |
+
The model memorizes the training data, including its statistical noise, and consequently fails to generalize to unseen data.
|
| 327 |
+
|
| 328 |
+
{% hint style="success" %}
|
| 329 |
+
If your training loss drops below 0.2, your model is likely **overfitting** — meaning it may perform poorly on unseen tasks.
|
| 330 |
+
|
| 331 |
+
One simple trick is LoRA alpha scaling — just multiply the alpha value of each LoRA matrix by 0.5. This effectively scales down the impact of fine-tuning.
|
| 332 |
+
|
| 333 |
+
**This is closely related to merging / averaging weights.**\
|
| 334 |
+
You can take the original base (or instruct) model, add the LoRA weights, then divide the result by 2. This gives you an averaged model — which is functionally equivalent to reducing the `alpha` by half.
|
| 335 |
+
{% endhint %}
|
| 336 |
+
|
| 337 |
+
**Solution:**
|
| 338 |
+
|
| 339 |
+
* **Adjust the learning rate:** A high learning rate often leads to overfitting, especially during short training runs. For longer training, a higher learning rate may work better. It’s best to experiment with both to see which performs best.
|
| 340 |
+
* **Reduce the number of training epochs**. Stop training after 1, 2, or 3 epochs.
|
| 341 |
+
* **Increase** `weight_decay`. A value of `0.01` or `0.1` is a good starting point.
|
| 342 |
+
* **Increase** `lora_dropout`. Use a value like `0.1` to add regularization.
|
| 343 |
+
* **Increase batch size or gradient accumulation steps**.
|
| 344 |
+
* **Dataset expansion** - make your dataset larger by combining or concatenating open source datasets with your dataset. Choose higher quality ones.
|
| 345 |
+
* **Evaluation early stopping** - enable evaluation and stop when the evaluation loss increases for a few steps.
|
| 346 |
+
* **LoRA Alpha Scaling** - scale the alpha down after training and during inference - this will make the finetune less pronounced.
|
| 347 |
+
* **Weight averaging** - literally add the original instruct model and the finetune and divide the weights by 2.
|
| 348 |
+
|
| 349 |
+
### **Underfitting** (Too Generic)
|
| 350 |
+
|
| 351 |
+
The model fails to capture the underlying patterns in the training data, often due to insufficient complexity or training duration.
|
| 352 |
+
|
| 353 |
+
**Solution:**
|
| 354 |
+
|
| 355 |
+
* **Adjust the Learning Rate:** If the current rate is too low, increasing it may speed up convergence, especially for short training runs. For longer runs, try lowering the learning rate instead. Test both approaches to see which works best.
|
| 356 |
+
* **Increase Training Epochs:** Train for more epochs, but monitor validation loss to avoid overfitting.
|
| 357 |
+
* **Increase LoRA Rank** (`r`) and alpha: Rank should at least equal to the alpha number, and rank should be bigger for smaller models/more complex datasets; it usually is between 4 and 64.
|
| 358 |
+
* **Use a More Domain-Relevant Dataset**: Ensure the training data is high-quality and directly relevant to the target task.
|
| 359 |
+
* **Decrease batch size to 1**. This will cause the model to update more vigorously.
|
| 360 |
+
|
| 361 |
+
{% hint style="success" %}
|
| 362 |
+
Fine-tuning has no single "best" approach, only best practices. Experimentation is key to finding what works for your specific needs. Our notebooks automatically set optimal parameters based on many papers research and our experiments, giving you a great starting point. Happy fine-tuning!
|
| 363 |
+
{% endhint %}
|
| 364 |
+
|
| 365 |
+
***Acknowledgements:** A huge thank you to* [*Eyera*](https://huggingface.co/Orenguteng) *for contributing to this guide!*
|
how-to.html
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<title>Shared References — LLM Fine-Tuning Projects</title>
|
| 6 |
+
<style>
|
| 7 |
+
:root {
|
| 8 |
+
--bg: #fafafa;
|
| 9 |
+
--card: #ffffff;
|
| 10 |
+
--text: #1a1a1a;
|
| 11 |
+
--muted: #666;
|
| 12 |
+
--accent: #2c5aa0;
|
| 13 |
+
--accent-soft: #e8f0fe;
|
| 14 |
+
--border: #e0e0e0;
|
| 15 |
+
--code-bg: #f4f4f4;
|
| 16 |
+
}
|
| 17 |
+
* { box-sizing: border-box; }
|
| 18 |
+
body {
|
| 19 |
+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
|
| 20 |
+
background: var(--bg);
|
| 21 |
+
color: var(--text);
|
| 22 |
+
max-width: 900px;
|
| 23 |
+
margin: 2rem auto;
|
| 24 |
+
padding: 0 1.5rem;
|
| 25 |
+
line-height: 1.6;
|
| 26 |
+
}
|
| 27 |
+
h1 { color: var(--accent); border-bottom: 3px solid var(--accent); padding-bottom: 0.5rem; }
|
| 28 |
+
h2 { color: var(--accent); margin-top: 2.5rem; border-bottom: 1px solid var(--border); padding-bottom: 0.3rem; }
|
| 29 |
+
h3 { margin-top: 1.8rem; color: #333; }
|
| 30 |
+
.intro { background: var(--accent-soft); border-left: 4px solid var(--accent); padding: 1rem 1.2rem; border-radius: 4px; }
|
| 31 |
+
.nav { display: flex; flex-wrap: wrap; gap: 0.5rem; margin: 1rem 0; }
|
| 32 |
+
.nav a {
|
| 33 |
+
background: var(--card);
|
| 34 |
+
border: 1px solid var(--border);
|
| 35 |
+
padding: 0.4rem 0.9rem;
|
| 36 |
+
border-radius: 20px;
|
| 37 |
+
text-decoration: none;
|
| 38 |
+
color: var(--accent);
|
| 39 |
+
font-size: 0.9rem;
|
| 40 |
+
}
|
| 41 |
+
.nav a:hover { background: var(--accent-soft); }
|
| 42 |
+
.item {
|
| 43 |
+
background: var(--card);
|
| 44 |
+
border: 1px solid var(--border);
|
| 45 |
+
border-radius: 6px;
|
| 46 |
+
padding: 1rem 1.2rem;
|
| 47 |
+
margin: 0.8rem 0;
|
| 48 |
+
}
|
| 49 |
+
.item .title { font-weight: 600; font-size: 1.05rem; }
|
| 50 |
+
.item .title a { color: var(--accent); text-decoration: none; }
|
| 51 |
+
.item .title a:hover { text-decoration: underline; }
|
| 52 |
+
.item .meta { color: var(--muted); font-size: 0.85rem; margin-top: 0.2rem; }
|
| 53 |
+
.item .desc { margin-top: 0.5rem; font-size: 0.95rem; }
|
| 54 |
+
.tag {
|
| 55 |
+
display: inline-block;
|
| 56 |
+
font-size: 0.75rem;
|
| 57 |
+
padding: 2px 8px;
|
| 58 |
+
border-radius: 10px;
|
| 59 |
+
margin-left: 6px;
|
| 60 |
+
vertical-align: middle;
|
| 61 |
+
}
|
| 62 |
+
.tag-local { background: #d4edda; color: #155724; }
|
| 63 |
+
.tag-online { background: #fff3cd; color: #856404; }
|
| 64 |
+
.tag-mlx { background: #e8d4f0; color: #5a2872; }
|
| 65 |
+
.tag-liquid { background: #d4e6f0; color: #1a4870; }
|
| 66 |
+
.tag-xai { background: #fde5d4; color: #8c4820; }
|
| 67 |
+
code {
|
| 68 |
+
background: var(--code-bg);
|
| 69 |
+
padding: 2px 6px;
|
| 70 |
+
border-radius: 3px;
|
| 71 |
+
font-size: 0.88em;
|
| 72 |
+
}
|
| 73 |
+
pre {
|
| 74 |
+
background: var(--code-bg);
|
| 75 |
+
padding: 0.9rem 1.1rem;
|
| 76 |
+
border-radius: 4px;
|
| 77 |
+
border-left: 3px solid var(--accent);
|
| 78 |
+
overflow-x: auto;
|
| 79 |
+
}
|
| 80 |
+
.note { background: #fffbe6; border-left: 4px solid #f0c036; padding: 0.8rem 1rem; border-radius: 4px; margin: 1rem 0; }
|
| 81 |
+
footer { margin-top: 3rem; padding-top: 1.5rem; border-top: 1px solid var(--border); color: var(--muted); font-size: 0.85rem; }
|
| 82 |
+
</style>
|
| 83 |
+
</head>
|
| 84 |
+
<body>
|
| 85 |
+
|
| 86 |
+
<h1>Shared References — LLM Fine-Tuning Projects</h1>
|
| 87 |
+
|
| 88 |
+
<div class="intro">
|
| 89 |
+
<strong>What this folder is:</strong> Papers and documentation that apply to more than one of the three spam-classifier projects (MLX, Liquid, XAI). Project-specific references live inside each project's own <code>docs/references/</code> folder.
|
| 90 |
+
</div>
|
| 91 |
+
|
| 92 |
+
<div class="nav">
|
| 93 |
+
<a href="#papers">📄 Papers</a>
|
| 94 |
+
<a href="#guides">📘 Guides</a>
|
| 95 |
+
<a href="#projects">🗂 Projects</a>
|
| 96 |
+
<a href="#citing">📚 Citing</a>
|
| 97 |
+
</div>
|
| 98 |
+
|
| 99 |
+
<div class="note">
|
| 100 |
+
All local links open files on your computer (no internet needed). Online links are provided as fallbacks and for the latest versions.
|
| 101 |
+
</div>
|
| 102 |
+
|
| 103 |
+
<h2 id="papers">📄 Foundational Papers</h2>
|
| 104 |
+
|
| 105 |
+
<p>These papers apply to all three LLM-focused projects. They cover the core ideas behind modern fine-tuning: the Transformer architecture, and parameter-efficient tuning with LoRA and QLoRA.</p>
|
| 106 |
+
|
| 107 |
+
<div class="item">
|
| 108 |
+
<div class="title">
|
| 109 |
+
<a href="papers/Attention_Vaswani2017.pdf">Attention Is All You Need</a>
|
| 110 |
+
<span class="tag tag-local">local</span>
|
| 111 |
+
</div>
|
| 112 |
+
<div class="meta">Vaswani et al. (2017) · NeurIPS · <a href="https://arxiv.org/abs/1706.03762">arXiv:1706.03762</a></div>
|
| 113 |
+
<div class="desc">The original Transformer paper. Every modern LLM (Qwen, LFM2, GPT, Llama, etc.) is descended from this architecture. Read this if you want to understand <em>why</em> the models you're fine-tuning work the way they do.</div>
|
| 114 |
+
</div>
|
| 115 |
+
|
| 116 |
+
<div class="item">
|
| 117 |
+
<div class="title">
|
| 118 |
+
<a href="papers/LoRA_Hu2021.pdf">LoRA: Low-Rank Adaptation of Large Language Models</a>
|
| 119 |
+
<span class="tag tag-local">local</span>
|
| 120 |
+
<span class="tag tag-mlx">MLX</span>
|
| 121 |
+
<span class="tag tag-liquid">Liquid</span>
|
| 122 |
+
</div>
|
| 123 |
+
<div class="meta">Hu et al. (2021) · ICLR 2022 · <a href="https://arxiv.org/abs/2106.09685">arXiv:2106.09685</a></div>
|
| 124 |
+
<div class="desc">The LoRA paper. Introduces the trick of freezing the base model's weights and training tiny "adapter" matrices instead. This is exactly what <code>mlx_lm.lora</code> and HuggingFace PEFT do. Both the MLX and Liquid projects use this technique.</div>
|
| 125 |
+
</div>
|
| 126 |
+
|
| 127 |
+
<div class="item">
|
| 128 |
+
<div class="title">
|
| 129 |
+
<a href="papers/QLoRA_Dettmers2023.pdf">QLoRA: Efficient Finetuning of Quantized LLMs</a>
|
| 130 |
+
<span class="tag tag-local">local</span>
|
| 131 |
+
<span class="tag tag-mlx">MLX</span>
|
| 132 |
+
</div>
|
| 133 |
+
<div class="meta">Dettmers et al. (2023) · NeurIPS · <a href="https://arxiv.org/abs/2305.14314">arXiv:2305.14314</a></div>
|
| 134 |
+
<div class="desc">QLoRA extends LoRA to work on quantized (compressed) base models. When the MLX project fine-tunes a 4-bit quantized Qwen model, it's automatically using QLoRA under the hood. This paper explains why that works.</div>
|
| 135 |
+
</div>
|
| 136 |
+
|
| 137 |
+
<div class="item">
|
| 138 |
+
<div class="title">
|
| 139 |
+
<a href="papers/PEFT_Survey.pdf">Parameter-Efficient Fine-Tuning for Large Models: A Comprehensive Survey</a>
|
| 140 |
+
<span class="tag tag-local">local</span>
|
| 141 |
+
</div>
|
| 142 |
+
<div class="meta">Han et al. (2024) · <a href="https://arxiv.org/abs/2403.14608">arXiv:2403.14608</a></div>
|
| 143 |
+
<div class="desc">A recent survey of all the different parameter-efficient fine-tuning methods (LoRA, Adapters, Prefix Tuning, Prompt Tuning, etc.). Useful background if you want to know what alternatives exist beyond LoRA.</div>
|
| 144 |
+
</div>
|
| 145 |
+
|
| 146 |
+
<h2 id="guides">📘 Fine-Tuning Guides</h2>
|
| 147 |
+
|
| 148 |
+
<p>Official documentation and tutorials from HuggingFace and Unsloth. Downloaded as HTML so they work offline.</p>
|
| 149 |
+
|
| 150 |
+
<h3>HuggingFace Ecosystem</h3>
|
| 151 |
+
|
| 152 |
+
<div class="item">
|
| 153 |
+
<div class="title">
|
| 154 |
+
<a href="guides/HuggingFace_LLM_FullFineTune_Tutorial.html">Fully Fine-tune a Small Language Model (LearnHuggingFace)</a>
|
| 155 |
+
<span class="tag tag-local">local</span>
|
| 156 |
+
</div>
|
| 157 |
+
<div class="meta">LearnHuggingFace community tutorial · <a href="https://www.learnhuggingface.com/notebooks/hugging_face_llm_full_fine_tune_tutorial">online version</a></div>
|
| 158 |
+
<div class="desc">A beginner-friendly walkthrough of full fine-tuning with HuggingFace Transformers. This was one of the starting points for the MLX and Liquid projects — even though our projects use LoRA (not full fine-tuning), the tutorial is great for understanding the HuggingFace workflow.</div>
|
| 159 |
+
</div>
|
| 160 |
+
|
| 161 |
+
<div class="item">
|
| 162 |
+
<div class="title">
|
| 163 |
+
<a href="guides/HuggingFace_PEFT_Quicktour.html">PEFT Quicktour</a>
|
| 164 |
+
<span class="tag tag-local">local</span>
|
| 165 |
+
<span class="tag tag-liquid">Liquid</span>
|
| 166 |
+
</div>
|
| 167 |
+
<div class="meta">HuggingFace PEFT docs · <a href="https://huggingface.co/docs/peft/quicktour">online version</a></div>
|
| 168 |
+
<div class="desc">Official quickstart for the PEFT library, which provides <code>LoraConfig</code> and friends. This is the library the Liquid project uses for its LoRA adapters.</div>
|
| 169 |
+
</div>
|
| 170 |
+
|
| 171 |
+
<div class="item">
|
| 172 |
+
<div class="title">
|
| 173 |
+
<a href="guides/HuggingFace_PEFT_LoRA_Conceptual.html">PEFT Conceptual Guide: LoRA</a>
|
| 174 |
+
<span class="tag tag-local">local</span>
|
| 175 |
+
</div>
|
| 176 |
+
<div class="meta">HuggingFace PEFT docs · <a href="https://huggingface.co/docs/peft/main/en/conceptual_guides/lora">online version</a></div>
|
| 177 |
+
<div class="desc">Explains what <code>r</code>, <code>lora_alpha</code>, <code>target_modules</code>, and <code>lora_dropout</code> actually mean — the exact parameters used in the Liquid notebook's <code>LoraConfig(...)</code> call.</div>
|
| 178 |
+
</div>
|
| 179 |
+
|
| 180 |
+
<div class="item">
|
| 181 |
+
<div class="title">
|
| 182 |
+
<a href="guides/HuggingFace_TRL_SFTTrainer.html">TRL SFTTrainer Documentation</a>
|
| 183 |
+
<span class="tag tag-local">local</span>
|
| 184 |
+
<span class="tag tag-liquid">Liquid</span>
|
| 185 |
+
</div>
|
| 186 |
+
<div class="meta">HuggingFace TRL docs · <a href="https://huggingface.co/docs/trl/sft_trainer">online version</a></div>
|
| 187 |
+
<div class="desc">Reference for the <code>SFTTrainer</code> class (Supervised Fine-Tuning Trainer), which is what the Liquid notebook uses to actually run training. Explains all the <code>SFTConfig</code> options.</div>
|
| 188 |
+
</div>
|
| 189 |
+
|
| 190 |
+
<div class="item">
|
| 191 |
+
<div class="title">
|
| 192 |
+
<a href="guides/HuggingFace_ChatTemplates.html">Chat Templates Guide</a>
|
| 193 |
+
<span class="tag tag-local">local</span>
|
| 194 |
+
<span class="tag tag-mlx">MLX</span>
|
| 195 |
+
<span class="tag tag-liquid">Liquid</span>
|
| 196 |
+
</div>
|
| 197 |
+
<div class="meta">HuggingFace Transformers docs · <a href="https://huggingface.co/docs/transformers/chat_templating">online version</a></div>
|
| 198 |
+
<div class="desc">Critical read. Explains <code>tokenizer.apply_chat_template()</code>, the function both MLX and Liquid notebooks call before generating text. If you forget this step, the model receives raw text instead of the chat format it was trained on and produces garbage.</div>
|
| 199 |
+
</div>
|
| 200 |
+
|
| 201 |
+
<h3>Unsloth (Alternative to TRL)</h3>
|
| 202 |
+
|
| 203 |
+
<div class="item">
|
| 204 |
+
<div class="title">
|
| 205 |
+
<a href="guides/Unsloth_Documentation.html">Unsloth Documentation Home</a>
|
| 206 |
+
<span class="tag tag-local">local</span>
|
| 207 |
+
</div>
|
| 208 |
+
<div class="meta">Unsloth docs · <a href="https://docs.unsloth.ai/">online version</a></div>
|
| 209 |
+
<div class="desc">Unsloth is an alternative fine-tuning library that claims 2-5x faster training and uses less memory than vanilla TRL. Liquid AI's cookbook has both a TRL version and an Unsloth version of their example notebook. This is the landing page.</div>
|
| 210 |
+
</div>
|
| 211 |
+
|
| 212 |
+
<div class="item">
|
| 213 |
+
<div class="title">
|
| 214 |
+
<a href="guides/Unsloth_FineTune_Guide.html">Unsloth Fine-Tuning Guide</a>
|
| 215 |
+
<span class="tag tag-local">local</span>
|
| 216 |
+
</div>
|
| 217 |
+
<div class="meta">Unsloth docs · <a href="https://unsloth.ai/docs/get-started/fine-tuning-llms-guide">online version</a></div>
|
| 218 |
+
<div class="desc">Step-by-step Unsloth fine-tuning walkthrough. A good alternative read if you want to compare the Unsloth workflow to the TRL workflow we used in the Liquid project.</div>
|
| 219 |
+
</div>
|
| 220 |
+
|
| 221 |
+
<div class="item">
|
| 222 |
+
<div class="title">
|
| 223 |
+
<a href="guides/Unsloth_LoRA_Hyperparameters.html">Unsloth LoRA Hyperparameters Guide</a>
|
| 224 |
+
<span class="tag tag-local">local</span>
|
| 225 |
+
</div>
|
| 226 |
+
<div class="meta">Unsloth docs · <a href="https://unsloth.ai/docs/get-started/fine-tuning-llms-guide/lora-hyperparameters-guide">online version</a></div>
|
| 227 |
+
<div class="desc">Practical advice on choosing LoRA rank, alpha, dropout, target modules, and learning rate. Applies to both MLX and Liquid projects since they both use the same LoRA technique (just with different libraries).</div>
|
| 228 |
+
</div>
|
| 229 |
+
|
| 230 |
+
<h2 id="projects">🗂 Projects</h2>
|
| 231 |
+
|
| 232 |
+
<p>Each project has its own <code>docs/references/how-to.html</code> with project-specific papers, guides, and quick-start commands:</p>
|
| 233 |
+
|
| 234 |
+
<div class="item">
|
| 235 |
+
<div class="title">
|
| 236 |
+
<a href="../spam-classifier-mlx/docs/references/how-to.html">MLX Spam Classifier</a>
|
| 237 |
+
<span class="tag tag-mlx">MLX</span>
|
| 238 |
+
</div>
|
| 239 |
+
<div class="desc">Fine-tunes Qwen3.5-0.8B on Apple Silicon using Apple's MLX framework and the <code>mlx_lm.lora</code> command. See project README for quickstart.</div>
|
| 240 |
+
</div>
|
| 241 |
+
|
| 242 |
+
<div class="item">
|
| 243 |
+
<div class="title">
|
| 244 |
+
<a href="../spam-classifier-liquid/docs/references/how-to.html">Liquid AI Spam Classifier</a>
|
| 245 |
+
<span class="tag tag-liquid">Liquid</span>
|
| 246 |
+
</div>
|
| 247 |
+
<div class="desc">Fine-tunes Liquid AI's LFM2.5-1.2B-Instruct using HuggingFace TRL + PEFT. Based on the official Liquid4All cookbook notebook.</div>
|
| 248 |
+
</div>
|
| 249 |
+
|
| 250 |
+
<div class="item">
|
| 251 |
+
<div class="title">
|
| 252 |
+
<a href="../spam-xai-project/docs/references/how-to.html">Spam XAI Classifier</a>
|
| 253 |
+
<span class="tag tag-xai">XAI</span>
|
| 254 |
+
</div>
|
| 255 |
+
<div class="desc">scikit-learn spam classifier (Random Forest + Logistic Regression + SVM ensemble) with LIME, SHAP, and ELI5 explainability. Deployed via Gradio + HuggingFace Spaces.</div>
|
| 256 |
+
</div>
|
| 257 |
+
|
| 258 |
+
<h2 id="citing">📚 Citing These Sources</h2>
|
| 259 |
+
|
| 260 |
+
<pre>
|
| 261 |
+
Vaswani, A., Shazeer, N., Parmar, N., et al. (2017). Attention Is All You Need.
|
| 262 |
+
In Advances in Neural Information Processing Systems 30 (NeurIPS 2017).
|
| 263 |
+
https://arxiv.org/abs/1706.03762
|
| 264 |
+
|
| 265 |
+
Hu, E., Shen, Y., Wallis, P., Allen-Zhu, Z., Li, Y., Wang, S., Wang, L., & Chen, W. (2021).
|
| 266 |
+
LoRA: Low-Rank Adaptation of Large Language Models. arXiv:2106.09685.
|
| 267 |
+
|
| 268 |
+
Dettmers, T., Pagnoni, A., Holtzman, A., & Zettlemoyer, L. (2023).
|
| 269 |
+
QLoRA: Efficient Finetuning of Quantized LLMs. NeurIPS 2023. arXiv:2305.14314.
|
| 270 |
+
|
| 271 |
+
Han, Z., Gao, C., Liu, J., Zhang, J., & Zhang, S.Q. (2024).
|
| 272 |
+
Parameter-Efficient Fine-Tuning for Large Models: A Comprehensive Survey.
|
| 273 |
+
arXiv:2403.14608.
|
| 274 |
+
</pre>
|
| 275 |
+
|
| 276 |
+
<footer>
|
| 277 |
+
Spring 2026 · ENGT 375 Applied Machine Learning · ODU · Generated for local offline reference.
|
| 278 |
+
</footer>
|
| 279 |
+
|
| 280 |
+
</body>
|
| 281 |
+
</html>
|
papers/Attention_Vaswani2017.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:bdfaa68d8984f0dc02beaca527b76f207d99b666d31d1da728ee0728182df697
|
| 3 |
+
size 2215244
|
papers/LoRA_Hu2021.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e9a0d3128767db616085dc0f4e6e455e672e89af823e8ed1282793682787395a
|
| 3 |
+
size 1609513
|
papers/PEFT_Survey.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f601b0ab8d72b1d65630953eadeb9d6b2caf7c3506252781d05bcbe46a350a6a
|
| 3 |
+
size 1628045
|
papers/QLoRA_Dettmers2023.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:33a4e757c19d6ea8d3cca8958fce8fe405e290272e135c99ee66065c775d5bd1
|
| 3 |
+
size 1065470
|