blender-motion-state-inspection
AFaffaan-m/ECC315k4,381 new stars in the last 7 days
Guides local and multi-GPU post-training, fine-tuning, alignment, scaling, and export for Gemma 4 models across Unsloth and Hugging Face TRL/PEFT. Use when fine-tuning or adapting Gemma models with SFT, (Q)LoRA, Full Fine-Tuning, 26B-A4B MoE, Continued Pre-Training, Custom Chat Templates, Unused Tokens, Vision/Audio multimodal SFT, DPO, GRPO verifiable rewards, Reward Modeling, PyTorch FSDP2, TorchAO QAT, dataset validation/distillation, or vLLM, GGUF, and LiteRT-LM export. Don't use for inference-only application building or general model selection without training (use gemma-dev).
When configuring a Gemma 4 training run, match the stack to the user's hardware, checkpoint architecture, and modality:
FastVisionModel / FastModel). It
supports Gemma 4 natively (including 4-bit 26B-A4B MoE), uses up to 60–70%
less VRAM, and trains up to 2x faster than standard PEFT.accelerate launch), full fine-tuning, custom trainable_token_indices, or
TorchAO QAT, use Hugging Face TRL (SFTTrainer, DPOTrainer,
GRPOTrainer, RewardTrainer) with PEFT.E2B, E4B, 12B, 31B): Use QLoRA (4-bit
NF4 quantization) via BitsAndBytesConfig or Unsloth to fit consumer
16–24 GB VRAM.26B-A4B): Routed 128 experts are fused 3D nn.Parameter
tensors (45.7 GB in bfloat16) that BitsAndBytesConfig skips. On a
24–40 GB single GPU, use Unsloth FastVisionModel(load_in_4bit=True);
in pure HF PEFT on 80 GB / FSDP2, load in bfloat16 and pass
target_parameters=["experts.gate_up_proj", "experts.down_proj"].E2B, E4B, 12B only): 26B-A4B and 31B have no audio
encoder! Always train audio LoRA in bfloat16 (uniform 4-bit NF4
degrades audio projections).E2B / E4B (Per-Layer Embeddings): Never use
bitsandbytes 8-bit AdamW (paged_adamw_8bit), which leaves
nn.Embedding / embed_tokens_per_layer optimizer states in fp32
(+22 GB VRAM OOM). Always set optim="adamw_torch_8bit"
(torchao).max_length / max_seq_length to
2048–8192 tokens locally to prevent Out-Of-Memory (OOM) errors.eval_on_start=True: Whenever an
eval_dataset is provided, pass eval_on_start=True so Epoch 0 baseline
validation loss is logged before step 1, and run held-out task evaluation
both before and after trainer.train().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):
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-5 on unfrozen embeddings), and mix a 10–20% general replay buffer
to prevent catastrophic forgetting.pt) model custom turn
delimiters (e.g. ChatML <|im_start|> / <|im_end|>). Mark assistant spans
in Jinja2 with {% generation %}...{% endgeneration %} and set
assistant_only_loss=True.262,144 vocabulary. Register built-in
<unused0>–<unused6226> slots via
AddedToken(slot, special=False, normalized=False), initialize embeddings
(including embed_tokens_per_layer when present) from subword means, and
train via PEFT trainable_token_indices.(prompt, chosen, rejected) data
after SFT without a separate reward model.<|channel>thought...<channel|>) or strict verifiable
output contracts (math, code execution, SQL, parsers) using TRL
GRPOTrainer (num_generations=4..8, beta=0.0,
chat_template_kwargs={"enable_thinking": True}).AutoModelForSequenceClassification with num_labels=1) on binary
preference pairs to score response quality.Formatting issues are the #1 cause of poor training runs. Validate local files
using [assets/dataset_prep.py].
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|>
SFT (Supervised Fine-Tuning): Supports two structures:
Conversational (messages): List of role/content turns (pass
completion_only_loss=True in SFTConfig to 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 (prompt and completion): SFTTrainer masks
the prompt automatically (completion_only_loss=True by 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, and
rejected:
{
"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."
}
To generate synthetic training data for compact models (E2B / E4B) from a
larger teacher (31B or 26B-A4B), use [assets/distill_dataset.py].
Use [assets/sft_train.py] for quick local single-GPU SFT, or fetch the exact
canonical guide in Section 7 for specialized setups.
r): 16 or 32; Alpha (lora_alpha): 32 or 64
(lora_alpha = 2 * r); Dropout (lora_dropout): 0.0 (Unsloth) or
0.05.2e-4 for (Q)LoRA; 2e-5–5e-5 with cosine schedule +
10% warmup for full fine-tuning (optim="adamw_torch_8bit").LoraConfig, always
pass exclude_modules=r"model\.(vision_tower|audio_tower)\..*" so PEFT
skips custom Gemma4ClippableLinear layers inside frozen vision/audio
encoders.26B-A4B MoE LoRA: Pass both target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"] and
target_parameters=["experts.gate_up_proj", "experts.down_proj"], and
freeze the router via
exclude_modules=r"(model\.(vision_tower|audio_tower)\..*|.*router.*)".[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 with beta=0.1
(0.1–0.5 controls reference adherence) and ref_model=None when using
PEFT LoRA.GRPOTrainer): Needs only a prompt column plus ground-truth
columns for deterministic reward functions (e.g., concise closed thought
trace + regex Answer: parser + exact answer match). Set beta=0.0 and
num_generations=4..8.[assets/reward_train.py]): Initialize with
AutoModelForSequenceClassification (num_labels=1) to train a scalar
quality head on (prompt, chosen, rejected) pairs.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.
Supported checkpoints: E2B, E4B, 12B (Unified), 26B-A4B, 31B.
Configure token budget via processor.image_processor.max_soft_tokens
(70, 140, 280, 560, or 1120) 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\"}"}
]}
]
}
Supported checkpoints: E2B, E4B, or 12B (Unified) only (26B-A4B
and 31B are text-and-vision only).
Resample raw audio waveforms to 16kHz mono (soundfile / librosa) and
train with bfloat16 LoRA (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."}
]}
]
}
FSDP (fsdp_version: 2,
TRANSFORMER_BASED_WRAP). For multi-GPU 4-bit QLoRA data parallelism, pin
each worker with device_map={"": PartialState().process_index}.bfloat16 fine-tuning, run a
short recovery pass (500–1,500 steps, lr=1e-5) using TorchAO QATConfig
(Int8DynamicActivationIntxWeightConfig(weight_dtype=torch.int4, weight_granularity=PerGroup(32))) filtered to
model.language_model.layers.* linear modules to simulate 4-bit rounding
before converting to packed int4.merged_model/ directly with
vllm serve ./merged_model --dtype bfloat16, or attach multiple unmerged
adapters dynamically via --enable-lora --lora-modules ....llama.cpp / Ollama):
model.save_pretrained_gguf(..., quantization_method="q4_k_m") (see
Saving to GGUF).llama.cpp Conversion: Run
convert_hf_to_gguf.py ./merged_model --outtype f16 followed by
llama-quantize ... Q4_K_M..litertlm): For mobile, web, and
IoT NPU/GPU acceleration on E2B and E4B, 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).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.
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.
Use fetch_url (or curl) to read the index at https://gemma.ai/llms.txt or
fetch the specific raw Markdown guide directly:
adamw_torch_8bit)26B-A4B)<unused*>)AFaffaan-m/ECC315k4,381 new stars in the last 7 days
Generate flow fields and particle systems with p5.js, using seeded randomness for original algorithmic art.
ANanthropics/skills203k3,265 new stars in the last 7 days
ANanthropics/skills203k3,265 new stars in the last 7 days
HAharry0703/MoneyPrinterTurbo147k1,608 new stars in the last 7 days
SHShubhamsaboo/awesome-llm-apps147k907 new stars in the last 7 days
GAgarrytan/gstack143k598 new stars in the last 7 days