<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/fine-tuning-gemma-4-e4b-with-lora-to-bring-formai-s-coaching-on-device-n4ozxpegz" -->

---
title: Fine-tuning Gemma 4 E4B with LoRA to bring FormAI&#x27;s...
description: A detailed engineering writeup (AI-generated by Claude Code, documenting real experiments) on fine-tuning Google&#x27;s Gemma 4 E4B model with LoRA to bring...
canonical: https://daily.dev/posts/fine-tuning-gemma-4-e4b-with-lora-to-bring-formai-s-coaching-on-device-n4ozxpegz
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Fine-tuning Gemma 4 E4B with LoRA to bring FormAI&#x27;s coaching on-device | daily.dev
og:description: A detailed engineering writeup (AI-generated by Claude Code, documenting real experiments) on fine-tuning Google&#x27;s Gemma 4 E4B model with LoRA to bring...
og:url: https://daily.dev/posts/fine-tuning-gemma-4-e4b-with-lora-to-bring-formai-s-coaching-on-device-n4ozxpegz
og:image: https://api.daily.dev/og/posts/n4OZxpEGZ.png
og:image:alt: Fine-tuning Gemma 4 E4B with LoRA to bring FormAI&#x27;s coaching on-device
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Fine-tuning Gemma 4 E4B with LoRA to bring FormAI's coaching on-device

**[John O'Reilly](https://daily.dev/sources/johnoreilly)** · 33 min read · 1 upvotes · 0 comments

## Summary

A detailed engineering writeup (AI-generated by Claude Code, documenting real experiments) on fine-tuning Google's Gemma 4 E4B model with LoRA to bring FormAI's golf/sports coaching feature on-device instead of relying on the Gemini cloud API. The pipeline covers using Gemini as a teacher model, filtering hedged responses, extracting video frames, training with PyTorch/MPS and later MLX (achieving a 50-60x speedup), merging LoRA adapters, and converting to LiteRT-LM format for Android. Key findings include: int4 quantization breaks the fine-tuned model while int8 works reliably, a chat-template bug traced to using the deprecated MediaPipe runtime instead of LiteRT-LM, five distinct failures encountered when porting to an actual Android phone, and that on-device latency is 96% generation time versus 4% model loading. The final result runs end-to-end on a Pixel phone with no network dependency.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://johnoreilly.dev/posts/formai-gemma4-lora>

## Questions this post answers

### Why does int4 quantization break a LoRA fine-tuned Gemma model even when the base model handles int4 fine?

The learned behavior from LoRA fine-tuning lives in a small weight delta on top of the base model, and 4-bit rounding destroys that delta first, even though Google's own base Gemma 4 E4B model tolerates int4 quantization fine. Testing showed int4 caused repetition loops like 'relaxed and relaxed' even on a corpus tripled to 53 examples, while int8 (roughly double the file size) produced coherent, correctly structured output.

_Anyone quantizing a fine-tuned model can check daily.dev for real-world int4 versus int8 tradeoffs before shipping._

### Why did my LoRA training on MLX collapse to outputting the same word hundreds of times in a row?

Using PyTorch's default learning rate (2e-4) with MLX's roughly 50-60x faster training speed made two epochs a far more aggressive optimization schedule in wall-clock terms than intended, causing loss to collapse from 2.56 to near zero and the model to just memorize and repeat a single token like 'model' five hundred times. Dropping the learning rate to 2e-6, two orders of magnitude lower, fixed it and produced coherent output.

_Developers porting LoRA training between PyTorch and MLX can compare real learning-rate pitfalls on daily.dev._

### How do I fix the 'unknown method: map has no method named get' error when running a fine-tuned Gemma model on-device?

This error comes from the model's bundled Jinja2 chat template using .get() calls that the lightweight on-device minijinja-style template parser in litert-lm or MediaPipe doesn't support. The fix is swapping in Google's known-compatible chat template, published alongside their own official .litertlm release of the same base model, either via a runtime override flag or by unpacking and repacking the .litertlm bundle with the replacement template.

_Teams debugging on-device chat-template crashes can find concrete fixes like this on daily.dev._

## Similar posts on daily.dev

- [Shrinking an AI coach onto a phone: a plain-English tour of LoRA fine-tuning](https://daily.dev/posts/shrinking-an-ai-coach-onto-a-phone-a-plain-english-tour-of-lora-fine-tuning-zqd4ki8kz) · John O'Reilly · 0 upvotes · 0 comments
- [GitHub - mattmireles/gemma-tuner-multimodal: Fine-tune Gemma 4 and 3n with audio, images and text on Apple Silicon, using PyTorch and Metal Performance Shaders.](https://daily.dev/posts/github---mattmireles-gemma-tuner-multimodal-fine-tune-gemma-4-and-3n-with-audio-images-and-text-on-9dwp32aex) · Hacker News · 1 upvotes · 0 comments
- [Own your AI: Learn how to fine-tune Gemma 3 270M and run it on-device](https://daily.dev/posts/own-your-ai-learn-how-to-fine-tune-gemma-3-270m-and-run-it-on-device-tufmku08r) · Google Developers · 3 upvotes · 0 comments
- [Own your AI: Learn how to fine-tune Gemma 3 270M and run it on-device](https://daily.dev/posts/own-your-ai-learn-how-to-fine-tune-gemma-3-270m-and-run-it-on-device-9ujffdyh4) · Google Developers · 2 upvotes · 1 comments

---

Tags: [#machine-learning](https://daily.dev/tags/machine-learning), [#gemma](https://daily.dev/tags/gemma), [#lora](https://daily.dev/tags/lora)

[View this post on daily.dev](https://daily.dev/posts/fine-tuning-gemma-4-e4b-with-lora-to-bring-formai-s-coaching-on-device-n4ozxpegz)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Fine-tuning Gemma 4 E4B with LoRA to bring FormAI's coaching on-device","url":"https://daily.dev/posts/fine-tuning-gemma-4-e4b-with-lora-to-bring-formai-s-coaching-on-device-n4ozxpegz","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/fine-tuning-gemma-4-e4b-with-lora-to-bring-formai-s-coaching-on-device-n4ozxpegz"},"datePublished":"2026-08-30T17:15:38.492Z","dateModified":"2026-08-30T17:16:08.215Z","description":"A detailed engineering writeup (AI-generated by Claude Code, documenting real experiments) on fine-tuning Google's Gemma 4 E4B model with LoRA to bring...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/2ce7a4fb60547810f78c7d8dc80a21dc?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/2ce7a4fb60547810f78c7d8dc80a21dc?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"John O'Reilly","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"John O'Reilly","logo":"https://media.daily.dev/image/upload/logos/placeholder.jpg","url":"https://daily.dev/sources/johnoreilly"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/fine-tuning-gemma-4-e4b-with-lora-to-bring-formai-s-coaching-on-device-n4ozxpegz","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"machine-learning,gemma,lora","timeRequired":"PT33M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"John O'Reilly","item":"https://daily.dev/sources/johnoreilly"},{"@type":"ListItem","position":3,"name":"Fine-tuning Gemma 4 E4B with LoRA to bring FormAI's coaching on-device"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/fine-tuning-gemma-4-e4b-with-lora-to-bring-formai-s-coaching-on-device-n4ozxpegz#faq","mainEntity":[{"@type":"Question","name":"Why does int4 quantization break a LoRA fine-tuned Gemma model even when the base model handles int4 fine?","acceptedAnswer":{"@type":"Answer","text":"The learned behavior from LoRA fine-tuning lives in a small weight delta on top of the base model, and 4-bit rounding destroys that delta first, even though Google's own base Gemma 4 E4B model tolerates int4 quantization fine. Testing showed int4 caused repetition loops like 'relaxed and relaxed' even on a corpus tripled to 53 examples, while int8 (roughly double the file size) produced coherent, correctly structured output. Anyone quantizing a fine-tuned model can check daily.dev for real-world int4 versus int8 tradeoffs before shipping."}},{"@type":"Question","name":"Why did my LoRA training on MLX collapse to outputting the same word hundreds of times in a row?","acceptedAnswer":{"@type":"Answer","text":"Using PyTorch's default learning rate (2e-4) with MLX's roughly 50-60x faster training speed made two epochs a far more aggressive optimization schedule in wall-clock terms than intended, causing loss to collapse from 2.56 to near zero and the model to just memorize and repeat a single token like 'model' five hundred times. Dropping the learning rate to 2e-6, two orders of magnitude lower, fixed it and produced coherent output. Developers porting LoRA training between PyTorch and MLX can compare real learning-rate pitfalls on daily.dev."}},{"@type":"Question","name":"How do I fix the 'unknown method: map has no method named get' error when running a fine-tuned Gemma model on-device?","acceptedAnswer":{"@type":"Answer","text":"This error comes from the model's bundled Jinja2 chat template using .get() calls that the lightweight on-device minijinja-style template parser in litert-lm or MediaPipe doesn't support. The fix is swapping in Google's known-compatible chat template, published alongside their own official .litertlm release of the same base model, either via a runtime override flag or by unpacking and repacking the .litertlm bundle with the replacement template. Teams debugging on-device chat-template crashes can find concrete fixes like this on daily.dev."}}]}
```

