Gemma 4
Gemma 4 is a family of multimodal models from Google. This guide covers how to train them with Axolotl.
Getting started
Install Axolotl following the installation guide.
Install Cut Cross Entropy to reduce training VRAM usage.
Run the finetuning example:
# 26B MoE QLoRA (1x80GB)
axolotl train examples/gemma4/26b-a4b-moe-qlora.yaml
# 31B Dense QLoRA (1x80GB @ ~25.2 GiB)
axolotl train examples/gemma4/31b-qlora.yaml
# 31B Dense LoRA FSDP2, 32k sequences (multi-GPU)
axolotl train examples/gemma4/31b-lora-fsdp.yaml
# E2B vision LoRA (1x80GB @ ~10.4 GiB)
axolotl train examples/gemma4/e2b-vision-lora.yamlMoE Expert Quantization & Expert LoRA (26B-A4B only)
The 26B-A4B config uses ScatterMoE kernels via the transformers ExpertsInterface and quantizes expert weights on load. To learn about expert quantization, expert LoRA targeting, and related limitations, see the MoE Expert Quantization docs.
Limitations
- Flash Attention: FA2 (max head_dim=256) and FA4 (max head_dim=128) cannot serve Gemma 4’s
global_head_dim=512on their own. Useflex_attention, orgemma4_hybrid_attn_impl: trueto run the sliding-window layers under FA2 and the global (head_dim=512) layers undersdpa(requiresattn_implementation: flash_attention_2and a flash-attn build for your GPU arch). - LoRA kernels: Not supported for models with KV-sharing layers.
- lora_target_linear: Incompatible for multimodal models; use
lora_target_moduleswith a regex to restrict LoRA to the text backbone.
TIPS
- Gradient spikes: Gemma 4’s text attention uses
scaling=1.0with QK-RMSNorm, leaving the softmax structurally near-saturated. LoRA runs hit intermittent pre-clip gradient norms 100-1000× their neighbours with no matching loss spike (transformers#45676). The 31B configs compensate withlearning_rate: 5e-5andmax_grad_norm: 0.1; raise either at your own risk. gemma4_hybrid_attn_impl: truetrains ~2× faster thanflex_attentionon 31B (~25.2 GiB reserved, packing on) and avoids the flexhead_dim=512kernel, which can exhaust shared memory on Blackwell.- Read more on how to load your own dataset at docs.
- You can run full finetuning by removing
adapter: qlora,load_in_4bit: true, andquantize_moe_experts: truefrom the config. This is heavy and has not been tested.
Optimization Guides
Please check the Optimizations doc.