InfrastructureHabr AI→ original

Fine-Tuning Gemma 4 with GPUs on Cloud Run: From 89% to 94% in Animal Classification

Gemma 4 now ships under the Apache 2.0 license with an updated architecture that natively handles images, video, and audio, plus built-in function calling and structured JSON support. On Cloud Run with an NVIDIA RTX PRO 6000, the Gemma 4 31B model takes up 62 GB of VRAM in bfloat16, but with QLoRA quantization via bitsandbytes, that drops to just 18–20 GB. That leaves room for long context and multimodal data. Key changes: the image is passed before the text using an <image> placeholder, instructions are merged into the chat template, the number of image tokens is now dynamic, and the PEFT target_modules parameter must be set to "all-linear" to stabilize training. • Baseline accuracy 89%, rising to 94% after fine-tuning on Oxford-IIIT Pet • Training parameters: Rank 64, Alpha 64, learning rate 5e-5 • QLoRA quantization cuts memory use to 18–20 GB versus 62 GB in bfloat16

AI-processed from Habr AI; edited by Hamidun News
Fine-Tuning Gemma 4 with GPUs on Cloud Run: From 89% to 94% in Animal Classification
Source: Habr AI. Collage: Hamidun News.
Listen to article
Article Fact Sheet & Key Figures
ParameterDetails
Primary sourceHabr AI ↗
Editor-in-Chief
Key metric #1Baseline accuracy 89%, rising to 94% after fine-tuning on Oxford-IIIT Pet
Training parametersRank 64, Alpha 64, learning rate 5e-5
Key metric #3QLoRA quantization cuts memory use to 18–20 GB versus 62 GB in bfloat16

Gemma 4 is Google's new generation of open models, built with improved reasoning capabilities and a more efficient architecture. The model ships under the Apache 2.0 license and features an updated architecture that requires changes to loading logic and data preparation compared to previous Gemma versions.

Architectural changes in Gemma 4

Key improvements:

• Native processing of images, video, and audio • Built-in support for function calling and structured JSON • Dynamic token counts for multimodal inputs

Gemma 4 requires the image to be passed before the text, using an <image> placeholder that tells the processor where to insert the image tokens. Instructions are merged with the user prompt into a single chat template. Unlike Gemma 3, the number of image tokens is now dynamic, so tokenizing the text separately can cause a length mismatch. The fix is to search for the response tokens in the full array starting from the end, which pinpoints the masking boundary accurately.

Memory optimization on Cloud Run

The infrastructure runs on an NVIDIA RTX PRO 6000 with 96 GB of VRAM. Gemma 4 31B in bfloat16 precision takes up roughly 62 GB of VRAM. Applying 4-bit QLoRA quantization through the bitsandbytes library cuts that down to 18–20 GB, leaving enough headroom for long-context processing and multimodal data.

One crucial detail: when using QLoRA, standard LoRA can ignore the model wrapper and cause errors. The fix is to set the PEFT parameter target_modules="all-linear" to stabilize the training process.

Results and training parameters

Gemma 4's baseline accuracy on the test set was 89%. After fine-tuning on the full Oxford-IIIT Pet dataset, accuracy rose to 94%. The configuration used: Rank 64, Alpha 64, learning rate 5e-5.

Running this on Google Cloud requires a project with billing enabled, the Cloud Run and Cloud Build APIs activated, and a Hugging Face token to access the model weights. It's recommended to first test the script on CPU with the 2B model and a minimal dataset before scaling up to a full run.

Model weights are uploaded to Google Cloud Storage in the same region as the Cloud Run service to speed things up. The image is built via Cloud Build, specifying the region, project, and repository name. The final Cloud Run job is created with NVIDIA RTX PRO 6000 support, the GCS bucket mounted as a local disk, and arguments to launch the fine-tuning process with paths to the model and output directory.

Story

Key Questions Answered

What are the key details on Architectural changes in Gemma 4?

Key improvements: Native processing of images, video, and audio Built-in support for function calling and structured JSON Dynamic token counts for multimodal inputs Gemma 4 requires the image to be passed before the text, using an <image> placeholder that tells the processor where to insert the image tokens. Instructions are merged with the user prompt into a single chat template. Unlike Gemma 3, the number of image tokens is now dynamic, so tokenizing the text separately can cause a length mismatch. The fix is to search for the response tokens in the full array starting fr

What are the key details on Memory optimization on Cloud Run?

The infrastructure runs on an NVIDIA RTX PRO 6000 with 96 GB of VRAM. Gemma 4 31B in bfloat16 precision takes up roughly 62 GB of VRAM. Applying 4-bit QLoRA quantization through the bitsandbytes library cuts that down to 18–20 GB, leaving enough headroom for long-context processing and multimodal data. One crucial detail: when using QLoRA, standard LoRA can ignore the model wrapper and cause errors. The fix is to set the PEFT parameter target_modules="all-linear" to stabilize the training process.

What are the key details on Results and training parameters?

Gemma 4's baseline accuracy on the test set was 89%. After fine-tuning on the full Oxford-IIIT Pet dataset, accuracy rose to 94%. The configuration used: Rank 64, Alpha 64, learning rate 5e-5. Running this on Google Cloud requires a project with billing enabled, the Cloud Run and Cloud Build APIs activated, and a Hugging Face token to access the model weights. It's recommended to first test the script on CPU with the 2B model and a minimal dataset before scaling up to a full run. Model weights are uploaded to Google Cloud Storage in the same region as the Cloud Run service

ZK
Zhemal Khamidun — CPO at Alpina Digital, product manager of AlpinaGPT. Building AI products since 2019. Hamidun News is a pet project that grew into a production aggregator with 6,000+ readers.
✦ Unified AI Workspace · Nomad

Test Frontier LLMs & Autonomous Agents in One Workspace

Run Claude Opus 5.5, GPT-6, Gemini 3.1, and DeepSeek side-by-side in Nomad AI (nomadnet.ai) — unified model routing, deep research, and multimodal workflows under one subscription.

Try Nomad AI →Launch Nomad Agent
What do you think?
Loading comments…