mlx-optiq
Fine-tuning · Gemma 4

Gemma 4 specifics

Every step in this guide works on Gemma 4 as written. This page covers what is different: which size to pick, how far each one stretches on a Mac, and the mixture-of-experts model.

All five Gemma 4 instruct sizes have OptiQ quants on Hugging Face: gemma-4-e2b-it, e4b-it, 12B-it, 26B-A4B-it and 31B-it, each with an -OptiQ-4bit build. The Gemma 4 family guide has the list with sizes and scores.

Pick a size

Longest LoRA sequence length on a 36 GB Mac: e2b 2,400 tokens, e4b 1,800, 26B-A4B 512, 31B 32.longest LoRA sequence, tokens (36 GB Mac)gemma-4-e2b-itgemma-4-e2b-it: 2,400 tokens, 22 GB peak2,400· 22 GB peakgemma-4-e4b-itgemma-4-e4b-it: 1,800 tokens, 24 GB peak1,800· 24 GB peakgemma-4-26B-A4B-itgemma-4-26B-A4B-it: 512 tokens, 27.6 GB peak512· 27.6 GB peakgemma-4-31B-itgemma-4-31B-it: 32 tokens, 21.4 GB peak32· 21.4 GB peak
Measured with the system-default GPU memory limit on a 36 GB Mac, adapting the last 16 blocks, query and value projections, rank 8.
ModelLongest sequencePeak memoryTokens/sec
gemma-4-e2b-it2,40022 GB~28
gemma-4-e4b-it1,80024 GB~22
gemma-4-26B-A4B-it51227.6 GB22.2
gemma-4-31B-it3221.4 GB30.9

E2B and E4B are where to start: they fit real examples and train fast. The dense 31B is the odd one out. Its large embedding and vocabulary leave almost no room for sequence length, so on a 36 GB Mac it is a model to serve rather than one to train. The 26B-A4B fits longer sequences than the 31B at the same memory, because only about 4B parameters are active per token.

These numbers use a conservative recipe. The trainer's default adapts every block and all seven projections, which is about three times as many LoRA modules and lowers the ceiling to match; drop --num-layers or --max-seq-length to get back under it.

The 26B-A4B recipe

gemma-moe.shbash
$ optiq lora train mlx-community/gemma-4-26B-A4B-it-OptiQ-4bit \
    --data ./my_data \
    --max-seq-length 512 \
    --rank 8 --rank-scaling by_bits \
    --num-layers 16 --iters 2000 \
    -o ./gemma_adapter

The attention projections are adapted; the expert pools are not, by default. --adapt-experts gives every expert its own LoRA, so the adapter grows with the expert count. Leave it off unless you have measured that the attention-only adapter falls short.

Data and chat template

Write rows in messages or prompt/completion form, exactly as on the dataset page. The trainer wraps them in Gemma's own chat template, so do not add Gemma's turn markers yourself.

Checking the result

Gemma 4 instruct models like a higher temperature than Qwen. When you compare the tuned model with its base, serve both with Google's recommended sampling so neither is judged at settings it was not built for:

serve.shbash
$ optiq serve --model mlx-community/gemma-4-e4b-it-OptiQ-4bit \
    --adapter ./gemma_adapter \
    --temp 1.0 --top-p 0.95 --top-k 64

For reasoning or math, 0.7 and 0.9 are a tighter pair.

Gemma 4 as a decision model

Gemma 4 is one of the two tested families for --decision. Trained on 1,080 records on a Mac, gemma-4-e2b-it-OptiQ-4bit-decision scored 0.802 accuracy on the Typed Decisions test split, the best of the five models in the decision models table. The recipe is on the decision model page.