Han reviews prior fixes for Gemma and Phi-3, including using approximate GELU, setting Phi-3’s sliding window to 2,048, and unfusing QKV matrices for LoRA fine-tuning. He says the session will focus on eight Llama 3 bugs, some of which were still under embargo.
Fixing bugs in Gemma, Llama, & Phi 3
Speaker
Daniel Han of Unsloth presents at AI Engineer on bugs in Gemma, Llama 3, and Phi-3, with a focus on Llama 3 fine-tuning issues and fixes.
Han warns that duplicate BOS tokens in fine-tuning data can lower inference accuracy, and says Hugging Face’s `apply_chat_template` can help detect them. He also cautions against using the Llama 3 Instruct template with the base model because it includes untrained tokens that can cause NaN gradients. Suggested fixes include replacing untrained token embeddings with the mean of trained embeddings, or training the embeddings and LM head.
Han says the pad token and EOS token must have different IDs because padding is masked from the cross-entropy loss; if EOS is masked too, generation may continue indefinitely. He notes that Phi-3 uses the same token for both and describes Unsloth’s approach of choosing an unused token or adding a new pad token.
Han says Ollama’s chat template must match the one used during fine-tuning and describes notebooks that generate a model file for export. Community fixes include using CPU conversion for GGUF with llama.cpp and a warning for duplicate BOS tokens. For LoRA, he recommends targeting all linear layers, suggests ranks such as 16, 32, 64, or 128, and describes rank-times-two as a useful LoRA alpha setting.
Han describes Unsloth’s offloaded gradient checkpointing for four-times-longer context, with a stated 1% to 2% slowdown, and advises offloading to system RAM before disk. In the Ollama notebook, he demonstrates using a CSV dataset, shaping data into instruction and output columns, and customizing chat templates. He suggests batch size 2 with gradient accumulation 4, for an effective batch size of 8, and a learning rate of 2e-4 or lower.
Takeaways
- Check training data and chat templates for duplicate BOS tokens before fine-tuning.
- Use the Llama 3 Instruct template only with the Instruct model; untrained base-model tokens can produce NaN gradients.
- Give padding and EOS separate token IDs so the loss does not mask the model’s stopping token.
- Match the Ollama template to the fine-tuning template when exporting a model.
- For longer sequences, tune maximum sequence length to the dataset and consider system-RAM gradient offloading.
Mentioned in this talk
Search inside this talk
What was claimed 9
- feature
Unsloth: Automatically detects and removes extra BOS tokens during fine-tuning.“In Unsloth, we check for this automatically and remove the extra BOS token for you.”
YouTube Unverified1 source· Talk
- feature
Unsloth: Automatically replaces untrained tokens with the mean of trained token embeddings.“In Unsloth, we do this automatically too.”
YouTube Unverified1 source· Talk
- feature
Unsloth: Checks for an available untrained token for padding or adds a new pad token.“If there are no untrained tokens, we add an extra pad token ourselves.”
YouTube Unverified1 source· Talk
- feature
Unsloth: Provides Ollama notebooks for fine-tuning with Alpaca data or an uploaded CSV.“We have two Colab notebooks for Ollama.”
YouTube Unverified1 source· Talk
- feature
llama.cpp: Warns when double BOS tokens are used.
“llama.cpp now warns you when you’re using double BOS tokens.”
YouTube Unverified1 source· Talk
- feature
Unsloth: Supports four times longer context with a reported 1% to 2% slowdown.“We also support four times longer context ... it’s only 1% to 2% slower.”
YouTube Unverified1 source· Talk
- feature
Unsloth: Supports fine-tuning Llama, Mistral, Gemma 3, and other models.“Unsloth supports fine-tuning models including Llama, Mistral, Gemma 3, and more.”
YouTube Unverified1 source· Talk
- metricLlama 3 fine-tuning: Use batch size 2 and gradient accumulation 4 for effective batch size 8.
“Use a batch size of two and gradient accumulation of four.”
YouTube Unverified1 source· Talk













