gemma-skills

repository·main·Indexed 20 days ago

https://github.com/google-gemma/gemma-skills

A repository providing specialized capabilities for interacting with Gemma models. It includes gemma-dev for application development and general knowledge, and gemma-trainer for training, fine-tuning, and adapting models using methods such as SFT, DPO, RLHF, and Reward Modeling on local hardware.

Tokens
3.3K
Snippets
7
Records
21
Agent score
72%

What's inside gemma-skills

  1. Choose the right Gemma training method (SFT, DPO, or RM)

    main

    Select a training workflow based on your objective:

    MethodGoalPrerequisitesOutput
    Supervised Fine-Tuning (SFT)Teaching new domains, specialized tasks, or custom structures.Raw text, instruction pairs, or chat logs.Adaptor trained on prompt/completion pairs.
    Direct Preference Optimization (DPO)Aligning style, behavior, tone, or safety with human preferences.A previously SFT-trained Gemma model and pairwise preference datasets.Aligned model weights (no separate reward head).
    Reward Modeling (RM)Training a scoring system to evaluate response quality.Binary preference pairwise datasets.A classification-style reward head on top of Gemma.
  2. Speed up inference with Multi-Token Prediction (MTP)

    main

    For lower latency, use Multi-Token Prediction (MTP). This uses a lightweight assistant model to propose multiple candidate tokens, which the target model verifies in a single pass.

    Each Gemma 4 target model has a corresponding assistant model following the naming convention <target-model-id>-assistant.

    Assistant Model Repos:

    • google/gemma-4-E2B-it-assistant
    • google/gemma-4-E4B-it-assistant
    • google/gemma-4-12B-it-assistant
    • google/gemma-4-31B-it-assistant
    • google/gemma-4-26B-A4B-it-assistant
  3. Prepare and validate Gemma datasets

    main

    Formatting errors are a primary cause of training failure. Always validate your dataset files using the utility script assets/dataset_prep.py.

    Gemma Chat Prompt Format

    Ensure datasets match the official chat template:

    system
    Your instruction here
    
    user
    Your query here
    
    model
    Your response here

    Tip: Use the tokenizer's apply_chat_template during tokenization to avoid formatting drift.

  4. Perform Direct Preference Optimization (DPO) alignment

    main

    Execute alignment using the assets/dpo_train.py template.

    Critical Rules for DPO:

    1. SFT First: You must perform SFT on the base model using your specific instruction format before running DPO. Running DPO directly on an out-of-domain base model typically fails or degrades formatting.
    2. Beta Parameter: Set beta (DPO temperature) to 0.1. Values between 0.1 and 0.5 control how strictly the model adheres to the reference policy.
  5. Install gemma-skills using Vercel skills CLI

    main

    You can manage and install skills using the Vercel skills CLI via npx.

    To interactively browse and select skills from this repository, use the --list flag. To install a specific skill globally, use the --skill flag followed by the skill name.

    # Interactively browse and install skills.
    npx skills add google-gemma/gemma-skills --list
    
    # Install a specific skill (e.g., gemma-dev).
    npx skills add google-gemma/gemma-skills --skill gemma-dev --global
  6. Fine-tune Gemma 4 for Vision (Multimodal SFT)

    main

    Gemma 4 models (E2B/E4B/12B/26B/31B) support multimodal vision fine-tuning. Use standard Hugging Face SFTTrainer with a custom visual data collator.

    Dataset Format: Prepare a dataset containing local image paths or PIL images within the message structure:

    {
      "messages": [
        {"role": "user", "content": [
            {"type": "image", "url": "path/to/image.png"},
            {"type": "text", "text": "Describe this image."}
        ]},
        {"role": "assistant", "content": [
            {"type": "text", "text": "An abstract oil painting with vibrant warm gradients."}
        ]}
      ]
    }
  7. Fine-tune Gemma 4 for Audio (Multimodal SFT)

    main

    Gemma 4 models (E2B/E4B/12B) support multimodal audio fine-tuning. Feed raw audio arrays (sampled at 16kHz) through the model processor to produce input_features.

    Dataset Format: Maintain conversational formatting, replacing the image type with audio type:

    {
      "messages": [
        {"role": "user", "content": [
            {"type": "text", "text": "Describe this audio."},
            {"type": "audio", "url": "path/to/audio.wav"}
        ]},
        {"role": "assistant", "content": [
            {"type": "text", "text": "This is an audio file of a bird chirping."}
        ]}
      ]
    }
  8. Convert trained models to GGUF format

    main

    After LoRA training, convert your model to GGUF for inference.

    If you used Unsloth for training, you can export directly to GGUF natively. This handles both merging and quantization automatically.

    Option 2: Manual Conversion with llama.cpp

    If you did not use Unsloth, manually convert your merged Hugging Face model directory using llama.cpp.

  9. Perform Supervised Fine-Tuning (SFT) with QLoRA

    main

    Launch a local QLoRA fine-tuning session using the assets/sft_train.py script.

    Recommended LoRA Hyperparameters:

    • Rank (r): 16 or 32.
    • Alpha (lora_alpha): 32 or 64 (Target lora_alpha = 2 * r).
    • Dropout (lora_dropout): 0.05 or 0.1.
    • Target Modules: Use PEFT's Gemma 4 defaults (targets LM layers).
    • Learning Rate: 2e-4 for QLoRA; 2e-5 for full fine-tuning.