惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
博客园_首页
罗磊的独立博客
云风的 BLOG
云风的 BLOG
J
Java Code Geeks
Last Week in AI
Last Week in AI
D
DataBreaches.Net
Jina AI
Jina AI
博客园 - Franky
大猫的无限游戏
大猫的无限游戏
Apple Machine Learning Research
Apple Machine Learning Research
V
V2EX
D
Docker
MongoDB | Blog
MongoDB | Blog
B
Blog RSS Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
宝玉的分享
宝玉的分享
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
博客园 - 三生石上(FineUI控件)
有赞技术团队
有赞技术团队
人人都是产品经理
人人都是产品经理
H
Help Net Security
T
The Blog of Author Tim Ferriss

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Why Your Gemma 4 Fine-Tuning is Failing (and How to Fix It)
Kajal Rawat · 2026-05-08 · via DEV Community

TL;DR: Gemma 4 is a multimodal beast with an Apache 2.0 license, but its new ClippableLinear layers and dynamic image tokens will break standard LoRA scripts. Use target_modules="all-linear" and backward-search masking to hit 94%+ accuracy on Cloud Run.

Gemma 4 has officially landed, and with an Apache 2.0 license and a 356K context window, it’s the new king of open-weight models. But if you try to drop it into your old Gemma 3 or Llama scripts, it will fail.

I’ve been deep-diving into the architecture, and there are three specific "under-the-hood" changes that will break your pipeline if you aren't careful. Here is how to master Gemma 4 using Cloud Run Jobs and NVIDIA RTX 6000 Pro GPUs.

1. The "ClippableLinear" Gotcha ⚠️

Gemma 4 uses a new custom layer wrapper called Gemma4ClippableLinear. This is a genius move for stability—it clips activations to prevent the loss from exploding during long-context training.

The Problem: Standard LoRA often tries to attach directly to the inner weights, bypassing the clipping logic. This leads to "unstable loss" or "NaN" errors.
The Fix:Use target_modules="all-linear".

Pro-Tip: Instead of being surgical, go broad. This recursively wraps the layers without breaking the clipping logic and ensures the vision tower is updated alongside the language backbone.

2. Multimodal Label Masking (The Precision Secret)

Gemma 4 is hyper-efficient with media. It uses a dynamic number of soft tokens for images. This means you can’t simply calculate prompt length by tokenizing the text alone—the image tokens will shift your alignment.

The Strategy: Backward-Search Collation

Don't calculate; search. In your data collator, search the input_ids array backward to find your label, then step back to the <|turn> token. This mathematically guarantees that you aren't accidentally training the model on your own prompts.

# Use the Assistant turn marker as your masking anchor
# This ensures zero-alignment shift regardless of image token count.
assistant_start_token = tokenizer.convert_tokens_to_ids("<|turn>")

Enter fullscreen mode Exit fullscreen mode

3. The Power of "Serverless" Fine-Tuning

Using Cloud Run Jobs with NVIDIA RTX 6000 Pro (96GB VRAM) is the "cheat code" for independent devs. You get 96GB of HBM, which is enough to run:

  • Gemma 4 31B (Dense) via QLoRA (4-bit).
  • Base footprint: ~18-20GB.
  • The Rest:Massive overhead for high-resolution images or long-context video frames.

Results Breakdown (Oxford-IIIT Pet Dataset)

Model Training Samples Accuracy
Gemma 3 Baseline 4,000 67%
Gemma 4 Baseline 4,000 89%
Gemma 4 (Fine-tuned) 4,000 94.2% (SOTA)

🛠 Quick-Start Migration Guide

1. Load the Correct Class

Forget AutoModelForCausalLM. Gemma 4 is multimodal by design.

from transformers import AutoModelForMultimodalLM
model = AutoModelForMultimodalLM.from_pretrained(model_id, model_kwargs)

Enter fullscreen mode Exit fullscreen mode

2. Image-First Prompting

Gemma 4 prefers a stable convention: Image data must come before text.

[
  {"type": "image"}, 
  {"type": "text", "text": "Analyze this pet breed."}
]

Enter fullscreen mode Exit fullscreen mode

3. Deploy to Cloud Run

gcloud beta run jobs execute gemma4-finetuning-job \
  --region europe-west4 \
  --gpu 1 \
  --gpu-type nvidia-rtx-pro-6000 \
  --args="--model-id","/mnt/gcs/gemma-4-31b-it/","--train-size","4000"

Enter fullscreen mode Exit fullscreen mode

Final Thoughts

Gemma 4 isn't just an "upgrade"—the 26B MoE variant and the 31B Dense model are redefining what "open-weight" means. By moving to an all-linear LoRA approach and leveraging serverless Blackwell GPUs, we can achieve SOTA results in hours, not days.

What are you building with the new 256K context window? Let’s discuss in the comments! 👇

gemma #googlecloud #machinelearning #opensource #ai #cloudrun