Gemma Training and Fine-Tuning Skill
1. Core Principles: Local & Multi-GPU Setup
When configuring a Gemma 4 training run, match the stack to the user's hardware, checkpoint architecture, and modality:
- Prioritize Unsloth on a Single GPU: For local single-GPU LoRA or QLoRA
training, recommend Unsloth (
FastVisionModel/FastModel). It supports Gemma 4 natively (including 4-bit26B-A4BMoE), uses up to 60–70% less VRAM, and trains up to 2x faster than standard PEFT. - Use Hugging Face TRL + PEFT for Multi-GPU or Custom Pipelines: For
multi-GPU environments (using DDP or PyTorch FSDP2 via
accelerate launch), full fine-tuning, customtrainable_token_indices, or TorchAO QAT, use Hugging Face TRL (SFTTrainer,DPOTrainer,GRPOTrainer,RewardTrainer) with PEFT. - Size Quantization & Optimizer to the Checkpoint:
- Dense text & vision (
E2B,E4B,12B,31B): Use QLoRA (4-bit NF4 quantization) viaBitsAndBytesConfigor Unsloth to fit consumer 16–24 GB VRAM. - MoE (
26B-A4B): Routed 128 experts are fused 3Dnn.Parametertensors (45.7 GBinbfloat16) thatBitsAndBytesConfigskips. On a 24–40 GB single GPU, use UnslothFastVisionModel(load_in_4bit=True); in pure HF PEFT on 80 GB / FSDP2, load inbfloat16and passtarget_parameters=["experts.gate_up_proj", "experts.down_proj"]. - Audio (
E2B,E4B,12Bonly):26B-A4Band31Bhave no audio encoder! Always train audio LoRA inbfloat16(uniform 4-bit NF4 degrades audio projections). - Full Fine-Tuning on
E2B/E4B(Per-Layer Embeddings): Never usebitsandbytes8-bit AdamW (paged_adamw_8bit), which leavesnn.Embedding/embed_tokens_per_layeroptimizer states infp32(+22 GB VRAM OOM). Always setoptim="adamw_torch_8bit"(torchao).
- Dense text & vision (
- Manage Context Window & Max Length: Although Gemma 4 supports 128K–256K
inference context, limit local training
max_length/max_seq_lengthto 2048–8192 tokens locally to prevent Out-Of-Memory (OOM) errors. - Evaluate Before & After + Enable
eval_on_start=True: Whenever aneval_datasetis provided, passeval_on_start=Trueso Epoch 0 baseline validation loss is logged before step 1, and run held-out task evaluation both before and aftertrainer.train().
2. Choosing the Right Training Method
Route the user's goal to the right post-training stage and checkpoint type
(default to instruction-tuned -it checkpoints; use base pt checkpoints only
for Continued Pre-Training or custom chat templates):
- Continued Pre-Training (CPT): Adapting a base (
pt) checkpoint (e.g.google/gemma-4-E2B) to raw domain corpora or underrepresented languages. Use sequence packing (packing=True,eval_packing=False),lr=5e-5(1e-5on unfrozen embeddings), and mix a 10–20% general replay buffer to prevent catastrophic forgetting. - Custom Chat Templates: Teaching a base (
pt) model custom turn delimiters (e.g. ChatML<|im_start|>/<|im_end|>). Mark assistant spans in Jinja2 with{% generation %}...{% endgeneration %}and setassistant_only_loss=True. - Supervised Fine-Tuning (SFT — LoRA, QLoRA, Full FT, MoE): Teaching specialized tasks, domain schemas, personas, or multimodal extraction on prompt/completion or conversational pairs.
- Unused Control / Routing Tokens: Adding single-token domain routing or
source tags without resizing the
262,144vocabulary. Register built-in<unused0>–<unused6226>slots viaAddedToken(slot, special=False, normalized=False), initialize embeddings (includingembed_tokens_per_layerwhen present) from subword means, and train via PEFTtrainable_token_indices. - Direct Preference Optimization (DPO): Aligning response tone,
conciseness, style, or safety on pairwise
(prompt, chosen, rejected)data after SFT without a separate reward model. - Reinforcement Learning with Verifiable Rewards (GRPO): Teaching concise
reasoning budgets (
<|channel>thought...<channel|>) or strict verifiable output contracts (math, code execution, SQL, parsers) using TRLGRPOTrainer(num_generations=4..8,beta=0.0,chat_template_kwargs={"enable_thinking": True}). - Reward Modeling (RM): Training a scalar scoring head
(
AutoModelForSequenceClassificationwithnum_labels=1) on binary preference pairs to score response quality.
3. Dataset Preparation & Validation
Formatting issues are the #1 cause of poor training runs. Validate local files
using [assets/dataset_prep.py].
Gemma 4 Chat & Thinking Format
Ensure prompts match Gemma 4's official turn and thinking delimiters via
tokenizer.apply_chat_template / processor.apply_chat_template:
<|turn>system
Your instruction here<turn|>
<|turn>user
Your query here<turn|>
<|turn>model
<|thought>
Optional reasoning trace<thought|>Your response here<turn|>
Format Specifications
-
SFT (Supervised Fine-Tuning): Supports two structures:
-
Conversational (
messages): List of role/content turns (passcompletion_only_loss=TrueinSFTConfigto mask user/system prompts):{ "messages": [ {"role": "user", "content": "Tell me a joke."}, {"role": "assistant", "content": "Why did the computer go to the doctor? It had a virus!"} ] } -
Prompt-Completion (
promptandcompletion):SFTTrainermasks thepromptautomatically (completion_only_loss=Trueby default):{ "prompt": [ {"role": "system", "content": "You are an expert SQL assistant."}, {"role": "user", "content": "Write a query to count active users."} ], "completion": [ {"role": "assistant", "content": "SELECT COUNT(*) FROM users WHERE active = 1;"} ] }
-
-
DPO & Reward Modeling: Pairwise samples with
prompt,chosen, andrejected:{ "prompt": "Write a python function to compute factorial.", "chosen": "def factorial(n):\n return 1 if n <= 1 else n * factorial(n - 1)", "rejected": "factorial is computed using recursion or loops. Just import math." }
Dataset Distillation & Synthesis (Teacher-Student)
To generate synthetic training data for compact models (E2B / E4B) from a
larger teacher (31B or 26B-A4B), use [assets/distill_dataset.py].
4. Fine-Tuning Workflows & Key Guardrails
Supervised Fine-Tuning (SFT) & MoE
Use [assets/sft_train.py] for quick local single-GPU SFT, or fetch the exact
canonical guide in Section 7 for specialized setups.
- LoRA Hyperparameters:
- Rank (
r):16or32; Alpha (lora_alpha):32or64(lora_alpha = 2 * r); Dropout (lora_dropout):0.0(Unsloth) or0.05. - Learning Rate:
2e-4for (Q)LoRA;2e-5–5e-5with cosine schedule +10%warmup for full fine-tuning (optim="adamw_torch_8bit"). - Exclude Frozen Multimodal Towers: In HF PEFT
LoraConfig, always passexclude_modules=r"model\.(vision_tower|audio_tower)\..*"so PEFT skips customGemma4ClippableLinearlayers inside frozen vision/audio encoders. 26B-A4BMoE LoRA: Pass bothtarget_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"]andtarget_parameters=["experts.gate_up_proj", "experts.down_proj"], and freeze the router viaexclude_modules=r"(model\.(vision_tower|audio_tower)\..*|.*router.*)".
- Rank (
Direct Preference Optimization (DPO), GRPO & Reward Modeling
- DPO (
[assets/dpo_train.py]): Always perform SFT on your target instruction format before running DPO (running DPO directly on an unaligned base model degrades formatting). Start withbeta=0.1(0.1–0.5controls reference adherence) andref_model=Nonewhen using PEFT LoRA. - GRPO (
GRPOTrainer): Needs only apromptcolumn plus ground-truth columns for deterministic reward functions (e.g., concise closed thought trace + regexAnswer:parser + exact answer match). Setbeta=0.0andnum_generations=4..8. - Reward Modeling (
[assets/reward_train.py]): Initialize withAutoModelForSequenceClassification(num_labels=1) to train a scalar quality head on(prompt, chosen, rejected)pairs.
5. Multimodal Fine-Tuning (Vision & Audio)
Always place image or audio content items before text inside user
turns, and mask all prompt tokens plus <|image|> / <|audio|> soft tokens to
-100 in your custom SFTTrainer data collator.
Vision SFT
-
Supported checkpoints:
E2B,E4B,12B(Unified),26B-A4B,31B. -
Configure token budget via
processor.image_processor.max_soft_tokens(70,140,280,560, or1120) before training and save the processor alongside the model:{ "messages": [ {"role": "user", "content": [ {"type": "image", "url": "path/to/image.png"}, {"type": "text", "text": "Extract the receipt line items as JSON."} ]}, {"role": "assistant", "content": [ {"type": "text", "text": "{\"total\": \"14.50\"}"} ]} ] }
Audio SFT
-
Supported checkpoints:
E2B,E4B, or12B(Unified) only (26B-A4Band31Bare text-and-vision only). -
Resample raw audio waveforms to 16kHz mono (
soundfile/librosa) and train withbfloat16LoRA (use_reentrant=False):{ "messages": [ {"role": "user", "content": [ {"type": "audio", "url": "path/to/audio.wav"}, {"type": "text", "text": "Transcribe this speech."} ]}, {"role": "assistant", "content": [ {"type": "text", "text": "This is an audio file of a bird chirping."} ]} ] }
6. Scaling, Quantization-Aware Training & Export
- Multi-GPU Sharding (PyTorch FSDP2): Shard weights, gradients, and
optimizer states across GPUs with Accelerate
FSDP(fsdp_version: 2,TRANSFORMER_BASED_WRAP). For multi-GPU 4-bit QLoRA data parallelism, pin each worker withdevice_map={"": PartialState().process_index}. - Post-Tuning Quantization-Aware Training (TorchAO QAT): If post-training
4-bit rounding hurts accuracy after custom
bfloat16fine-tuning, run a short recovery pass (500–1,500 steps,lr=1e-5) using TorchAOQATConfig(Int8DynamicActivationIntxWeightConfig(weight_dtype=torch.int4, weight_granularity=PerGroup(32))) filtered tomodel.language_model.layers.*linear modules to simulate 4-bit rounding before converting to packedint4. - vLLM (Cloud / GPU Serving): Serve
merged_model/directly withvllm serve ./merged_model --dtype bfloat16, or attach multiple unmerged adapters dynamically via--enable-lora --lora-modules .... - GGUF Conversion (
llama.cpp/ Ollama):- Option 1 — Unsloth Native Export (Recommended): Use
model.save_pretrained_gguf(..., quantization_method="q4_k_m")(see Saving to GGUF). - Option 2 — Manual
llama.cppConversion: Runconvert_hf_to_gguf.py ./merged_model --outtype f16followed byllama-quantize ... Q4_K_M.
- Option 1 — Unsloth Native Export (Recommended): Use
- On-Device Deployment with LiteRT-LM (
.litertlm): For mobile, web, and IoT NPU/GPU acceleration onE2BandE4B, export with the LiteRT-LM CLI tool (litert-torch export_hf --model=./merged_model --externalize_embedder) so Per-Layer Embeddings stay separated (see LiteRT-LM guide).
7. Documentation Lookup (Live Canonical Code on gemma.ai)
Whenever generating or customizing training scripts beyond the bundled
[assets/*.py] templates, retrieve the live official guide so your code uses
the latest verified package versions, collators, and hyperparameters.
When MCP is Installed (Preferred)
If the search_documentation tool (from the Google MCP server) is
available, query search_documentation first and trust its results as the
up-to-date source of truth.
When MCP is NOT Installed (Fallback / URL Fetch)
Use fetch_url (or curl) to read the index at https://gemma.ai/llms.txt or
fetch the specific raw Markdown guide directly:
- Fine-tuning overview & hardware sizing
- Best practices & hyperparameters
- Dataset formatting & prompt masking
- Continued pre-training
- Custom chat templates
- (Q)LoRA fine-tuning
- Full fine-tuning (
adamw_torch_8bit) - MoE fine-tuning (
26B-A4B) - Unused tokens (
<unused*>) - Vision fine-tuning
- Audio fine-tuning
- Preference optimization (DPO)
- Verifiable rewards (GRPO)
- Multi-GPU scaling (FSDP2) & TorchAO QAT
- Export to vLLM, GGUF, and LiteRT-LM
- Unsloth single-GPU swap-in
