Winner (speed)
Unsloth under valid protocol (lora_dropout=0, SDPA, shared text-field SFT path).
Validity rule
Unsloth results only count when the train log shows non-zero QKV/O/MLP patches, e.g.:
patched 36 layers with 36 QKV layers, 36 O layers and 36 MLP layers
lora_dropout=0.05 disables Unsloth LoRA fast-patching → invalid comparison.
Benchmark summary
HF–Unsloth Fine-Tuning Benchmark
Table with columns: Exp, Stage, Backend, Model, Len, BS, r, 4bit, GC, Attn, Time (s), Peak VRAM (GB), Tokens/s, GPU util., Train loss, Eval loss, Task acc.| Exp | Stage | Backend | Model | Len | BS | r | 4bit | GC | Attn | Time (s) | Peak VRAM (GB) | Tokens/s | GPU util. | Train loss | Eval loss | Task acc. |
|---|
| domain-native | current_native | hf | Qwen/Qwen2.5-7B-Instruct | 2048 | 1 | 16 | True | True | sdpa | 482.659 | 7.250 | 515.386 | 98.290% | 1.492 | 1.496 | 53.700% |
| domain-native | current_native | unsloth | Qwen/Qwen2.5-7B-Instruct | 2048 | 1 | 16 | True | True | sdpa | 329.054 | 8.001 | 755.973 | 94.510% | 0.283 | 0.039 | 92.500% |
| attn-eager | standard | hf | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | True | True | eager | 656.763 | 4.112 | 253.859 | 42.740% | 2.530 | 2.792 | n/a |
| attn-eager | standard | unsloth | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | True | True | eager | 477.447 | 4.579 | 356.741 | 43.390% | 1.142 | 1.031 | n/a |
| attn-sdpa | standard | hf | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | True | True | sdpa | 607.349 | 4.110 | 274.513 | 43.030% | 2.532 | 2.792 | n/a |
| attn-sdpa | standard | unsloth | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | True | True | sdpa | 475.224 | 4.579 | 358.410 | 43.520% | 1.141 | 1.030 | n/a |
| baseline-v2 | standard | hf | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | True | True | sdpa | 614.957 | 4.110 | 271.117 | 42.680% | 2.532 | 2.792 | n/a |
| baseline-v2 | standard | unsloth | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | True | True | sdpa | 495.407 | 4.396 | 343.808 | 42.700% | 1.141 | 1.030 | n/a |
| batch-1 | standard | hf | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | True | True | sdpa | 611.442 | 4.110 | 272.675 | 43.090% | 2.532 | 2.792 | n/a |
| batch-1 | standard | unsloth | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | True | True | sdpa | 473.779 | 4.579 | 359.503 | 42.700% | 1.141 | 1.030 | n/a |
| batch-2 | standard | hf | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 2 | 16 | True | True | sdpa | 389.750 | 5.597 | 427.774 | 79.130% | 2.531 | 2.653 | n/a |
| batch-2 | standard | unsloth | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 2 | 16 | True | True | sdpa | 266.380 | 5.400 | 639.406 | 71.290% | 1.142 | 1.050 | n/a |
| batch-4 | standard | hf | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 4 | 16 | True | True | sdpa | 421.767 | 8.571 | 395.301 | 95.350% | 2.531 | 2.510 | n/a |
| batch-4 | standard | unsloth | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 4 | 16 | True | True | sdpa | 224.611 | 7.039 | 758.313 | 92.500% | 1.141 | 1.041 | n/a |
| batch-8 | standard | hf | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 8 | 16 | True | True | sdpa | 537.209 | 14.514 | 310.354 | 98.420% | 2.532 | 2.412 | n/a |
| batch-8 | standard | unsloth | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 8 | 16 | True | True | sdpa | 243.715 | 10.318 | 698.869 | 96.320% | 1.142 | 1.033 | n/a |
| ctx-2048 | standard | hf | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | True | True | sdpa | 610.781 | 4.110 | 272.970 | 42.220% | 2.532 | 2.792 | n/a |
| ctx-2048 | standard | unsloth | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | True | True | sdpa | 480.084 | 4.579 | 354.782 | 43.890% | 1.141 | 1.030 | n/a |
| ctx-4096 | standard | hf | Qwen/Qwen3-4B-Instruct-2507 | 4096 | 1 | 16 | True | True | sdpa | 608.464 | 4.110 | 274.009 | 42.160% | 2.532 | 2.792 | n/a |
| ctx-4096 | standard | unsloth | Qwen/Qwen3-4B-Instruct-2507 | 4096 | 1 | 16 | True | True | sdpa | 476.558 | 4.579 | 357.407 | 42.880% | 1.142 | 1.031 | n/a |
| ctx-8192 | standard | hf | Qwen/Qwen3-4B-Instruct-2507 | 8192 | 1 | 16 | True | True | sdpa | 607.715 | 4.110 | 274.347 | 42.300% | 2.532 | 2.792 | n/a |
| ctx-8192 | standard | unsloth | Qwen/Qwen3-4B-Instruct-2507 | 8192 | 1 | 16 | True | True | sdpa | 474.750 | 4.579 | 358.768 | 44.860% | 1.142 | 1.031 | n/a |
| gc-off | standard | hf | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | True | False | sdpa | 440.163 | 8.283 | 378.780 | 42.740% | 2.532 | 2.792 | n/a |
| gc-off | standard | unsloth | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | True | False | sdpa | 476.613 | 4.579 | 357.366 | 43.230% | 1.142 | 1.031 | n/a |
| gc-on | standard | hf | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | True | True | sdpa | 607.213 | 4.110 | 274.574 | 43.050% | 2.532 | 2.792 | n/a |
| gc-on | standard | unsloth | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | True | True | sdpa | 477.994 | 4.579 | 356.333 | 43.650% | 1.142 | 1.031 | n/a |
| lora-16bit | standard | hf | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | False | True | sdpa | 482.783 | 9.365 | 345.342 | 40.400% | 1.064 | 0.978 | n/a |
| lora-16bit | standard | unsloth | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | False | True | sdpa | 414.708 | 8.721 | 410.711 | 37.820% | 1.130 | 1.020 | n/a |
| optim-adamw-8bit | standard | hf | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | True | True | sdpa | 604.343 | 4.110 | 275.878 | 42.630% | 2.532 | 2.792 | n/a |
| optim-adamw-8bit | standard | unsloth | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | True | True | sdpa | 480.621 | 4.396 | 354.386 | 41.730% | 1.141 | 1.030 | n/a |
| optim-adamw-torch | standard | hf | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | True | True | sdpa | 612.079 | 4.110 | 272.391 | 42.830% | 2.532 | 2.792 | n/a |
| optim-adamw-torch | standard | unsloth | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | True | True | sdpa | 476.239 | 4.579 | 357.646 | 43.650% | 1.142 | 1.031 | n/a |
| pack-off | standard | hf | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | True | True | sdpa | 614.606 | 4.110 | 271.271 | 41.770% | 2.532 | 2.792 | n/a |
| pack-off | standard | unsloth | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | True | True | sdpa | 473.190 | 4.579 | 359.950 | 44.550% | 1.142 | 1.031 | n/a |
| pack-on | standard | hf | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | True | True | sdpa | 240.013 | 6.817 | 694.650 | 97.650% | 2.015 | 2.156 | n/a |
| pack-on | standard | unsloth | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | True | True | sdpa | 134.117 | 6.396 | 1269.975 | 93.110% | 1.420 | 1.252 | n/a |
| qlora-4bit | standard | hf | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | True | True | sdpa | 610.274 | 4.110 | 273.197 | 41.630% | 2.532 | 2.792 | n/a |
| qlora-4bit | standard | unsloth | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | True | True | sdpa | 475.059 | 4.579 | 358.535 | 43.100% | 1.142 | 1.031 | n/a |
| rank-16 | standard | hf | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | True | True | sdpa | 608.393 | 4.110 | 274.042 | 42.990% | 2.532 | 2.792 | n/a |
| rank-16 | standard | unsloth | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | True | True | sdpa | 476.764 | 4.579 | 357.252 | 43.040% | 1.142 | 1.031 | n/a |
| rank-32 | standard | hf | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 32 | True | True | sdpa | 607.869 | 4.243 | 274.278 | 43.240% | 2.532 | 2.792 | n/a |
| rank-32 | standard | unsloth | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 32 | True | True | sdpa | 476.751 | 4.948 | 357.262 | 42.950% | 1.118 | 1.023 | n/a |
| rank-64 | standard | hf | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 64 | True | True | sdpa | 612.269 | 4.481 | 272.307 | 43.440% | 2.532 | 2.792 | n/a |
| rank-64 | standard | unsloth | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 64 | True | True | sdpa | 479.361 | 5.856 | 355.317 | 45.200% | 1.102 | 1.015 | n/a |
| rank-8 | standard | hf | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 8 | True | True | sdpa | 605.379 | 4.048 | 275.406 | 42.320% | 2.532 | 2.792 | n/a |
| rank-8 | standard | unsloth | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 8 | True | True | sdpa | 480.488 | 4.395 | 354.484 | 41.950% | 1.174 | 1.044 | n/a |
| tgt-all-linear | standard | hf | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | True | True | sdpa | 605.263 | 4.110 | 275.459 | 43.570% | 2.532 | 2.792 | n/a |
| tgt-all-linear | standard | unsloth | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | True | True | sdpa | 474.498 | 4.579 | 358.958 | 43.200% | 1.142 | 1.030 | n/a |
| tgt-attention | standard | hf | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | True | True | sdpa | 529.253 | 4.031 | 315.019 | 45.720% | 2.532 | 2.792 | n/a |
| tgt-attention | standard | unsloth | Qwen/Qwen3-4B-Instruct-2507 | 2048 | 1 | 16 | True | True | sdpa | 412.106 | 4.314 | 413.304 | 45.100% | 1.185 | 1.060 | n/a |
Each run directory contains its resolved config, metrics, saved adapter, and task evaluation when applicable.
Lessons
AI Engineer Lessons — HF vs Unsloth QLoRA (L4)
Living document. Updated after each valid experiment cell.
Core decision so far
Under valid protocol (lora_dropout=0, SDPA, shared text SFT path), Unsloth wins on throughput ( ~+27% tokens/s, ~−19% wall time) with slightly higher peak VRAM (~+0.3 GB) on Qwen3-4B QLoRA / L4.
Invalid runs with dropout=0.05 showed the opposite — those are not Unsloth measurements.
Lesson 1 — Framework comparisons need validity gates
Table with columns: Mistake, Symptom, Fix| Mistake | Symptom | Fix |
|---|
lora_dropout=0.05 “because PEFT defaults” | Unsloth logs 0 QKV layers patched | Force lora_dropout=0 for both backends when measuring Unsloth |
Trusting install of xformers | ABI mismatch (torch/cuda/py) | Uninstall broken wheel; pin attn_implementation=sdpa (or install matching FA2) |
| Mixing chat-messages vs text-field paths | Different loss scales | Shared dataset_text_field="text" for both |
| Declaring a winner from loss alone | Unsloth cut-CE vs HF CE | Compare speed/VRAM; quality via shared generation eval |
Engineer rule: Before reading the table, grep the Unsloth log for patched … QKV layers. If zeros → discard the cell.
Lesson 2 — What Unsloth actually optimizes
Unsloth is not “a different Trainer API for free speed.” It:
- Patches attention / MLP / LoRA kernels (requires dropout=0 for LoRA fast path)
- Uses custom gradient checkpointing (
use_gradient_checkpointing="unsloth")
- Often uses cut-cross-entropy (loss numbers ≠ HF)
- Still sits on TRL
SFTTrainer in our harness
So the fair A/B is: same data, seed, LoRA geometry, seq, batch, optim, attn backend — with Unsloth’s required constraints respected.
Lesson 3 — Method knobs > random hyperparam spray
Prefer experiments that change stack decisions:
Table with columns: Method, Why it matters, Use-case signal| Method | Why it matters | Use-case signal |
|---|
optim=adamw_torch vs adamw_8bit | Optimizer state VRAM vs step speed | Long SFT on 24GB; 8bit often frees VRAM for larger batch/context |
| Packing on/off | Throughput vs sample boundary noise | Short instruction data → packing helps; tool-call JSON → often pack-off safer |
| Target modules attention vs all-linear | Capacity vs VRAM/time | Format learning / tool schemas often need MLP too |
| GC on/off | Activation memory vs compute | Off only if VRAM headroom; else OOM |
| 4-bit QLoRA vs 16-bit LoRA | Memory ceiling | 16-bit may OOM on L4 at long context |
| Context 2k/4k/8k | Activation scaling | RAG/long tool traces need longer ctx → Unsloth+GC+8bit optim |
Hyperparam rank/batch sweeps are secondary once the method is chosen.
Lesson 4 — Production path after the winner
Once Unsloth is the speed winner:
- Re-run method matrix on Unsloth only (optim / packing / targets)
- Domain SFT (appointment native tools) with Unsloth
- Eval with task metrics (tool-call / format / args) — not train loss
- Export adapter → optional merge → GGUF/vLLM → FastAPI
- Publish adapters + experiment log to Hub for CV / reproducibility
Open follow-ups
Security note
Never commit Hugging Face tokens. Use HF_TOKEN env var; rotate any token pasted into chat.
Reproduce
pip install -r requirements-benchmark.txtpython scripts/run_l4_benchmark.py --stage standard --lora-dropout 0 --attn-implementation sdpa
Repo card for Hikmet58/qwen3-4b-unsloth-qlora-l4-bench.