Downloads · 30 days
0
SanfranTroy/gemma4_e2b_q4_mlx_lm
gemma4_e2b_q4_mlx_lm is a machine learning model from SanfranTroy. Use it for the machine learning task on the model card, and read the license before you ship it in a product.
Run Gemma 4 models on Apple Silicon via mlx-lm.
Downloads · 30 days
0
Access
Public
Updated Apr 5, 2026
Repo size
—
Likes
0
Public
Click a slice to open those files.
.py42.6 KB · 83%
From the Hugging Face model README
Run Gemma 4 models on Apple Silicon via mlx-lm.
| Model | model_type | PLE | MoE | KV Sharing | k_eq_v |
|---|---|---|---|---|---|
| gemma-4-E2B | gemma4 | Yes | No | No | Yes |
| gemma-4-E4B | gemma4 | Yes | No | No | Yes |
| gemma-4-31B | gemma4 | No | No | Yes | Yes |
| gemma-4-26B-A4B | gemma4 | No | Yes | Yes | Yes |
# Create conda environment (recommended)
conda create -n mlx_gemma python=3.11
conda activate mlx_gemma
# Install required packages
pip install mlx mlx-lm
# Find the mlx-lm models directory
MODELS_DIR=$(python3 -c "import mlx_lm; from pathlib import Path; print(Path(mlx_lm.__file__).parent / 'models')")
# Copy the files
cp gemma4_text.py gemma4.py "$MODELS_DIR/"
# Verify installation
python3 -c "from mlx_lm.models import gemma4_text, gemma4; print('Installation successful')"
Or use the install script:
bash install.sh
# Option A: Use a pre-converted model from mlx-community (if available)
python3 -m mlx_lm.generate --model mlx-community/gemma-4-E2B-it-4bit --prompt "Hello"
# Option B: Convert from HuggingFace
python3 -m mlx_lm.convert \
--hf-path google/gemma-4-E2B-it \
--mlx-path ./gemma-4-e2b-q4 \
-q
# Text generation
python3 -m mlx_lm.generate \
--model ./gemma-4-e2b-q4 \
--max-tokens 500 \
--prompt "Explain quantum computing in simple terms."
# Interactive chat
python3 -m mlx_lm.chat --model ./gemma-4-e2b-q4
from mlx_lm import load, generate
model, tokenizer = load("./gemma-4-e2b-q4")
prompt = "What is machine learning?"
messages = [{"role": "user", "content": prompt}]
formatted = tokenizer.apply_chat_template(
messages, add_generation_prompt=True, tokenize=False
)
response = generate(model, tokenizer, prompt=formatted, max_tokens=500)
print(response)
python3 validate_gemma4.py --model ./gemma-4-e2b-q4
python3 validate_gemma4.py --model ./gemma-4-e2b-q4 --keys
python3 validate_gemma4.py --model ./gemma-4-e2b-q4 --inference
# Requires transformers + torch
pip install transformers torch
python3 validate_gemma4.py \
--model ./gemma-4-e2b-q4 \
--numerical \
--hf-model google/gemma-4-E2B-it
Use this checklist when reproducing the setup on a new machine:
python3 -c "from mlx_lm.models import gemma4; print('OK')" succeedsvalidate_gemma4.py --model <path> weight mapping passesvalidate_gemma4.py --model <path> --inference inference passes| HuggingFace (PyTorch) | MLX (this implementation) |
|---|---|
Gemma4TextScaledWordEmbedding | nn.Embedding + * hidden_size**0.5 scaling |
Gemma4RMSNorm(with_scale=True) | nn.RMSNorm (1+weight convention) |
Gemma4RMSNorm(with_scale=False) | RMSNoScale (no learnable weight) |
Gemma4TextAttention | Attention (auto sliding/full dispatch) |
Gemma4TextMLP | MLP (auto double-wide dispatch) |
Gemma4TextDecoderLayer | TransformerBlock |
Gemma4TextRouter + Gemma4TextExperts | Router + Experts |
per_layer_input_gate + per_layer_projection | Same names |
layer_scalar | Same name (buffer -> parameter) |
nn.RMSNorm uses (1 + weight) * x internally via mx.fast.rms_norm.
This matches how mlx-lm handles Gemma 3, so weight loading should be consistent.Ensure gemma4.py and gemma4_text.py are correctly copied into mlx_lm/models/.
Some text-only checkpoints set model_type to gemma4_text in config.json. Both files must be installed.
Check the RMSNorm weight convention. Run validate_gemma4.py --numerical for numerical verification.
Use --max-kv-size 512 to limit KV cache size.
gemma4_text.py # Core text model (Attention, MLP, MoE, PLE, etc.)
gemma4.py # Multimodal wrapper (text_config extraction, weight sanitization)
validate_gemma4.py # Validation script
install.sh # One-command installer
README.md # This file
| Machine | Chip | Unified Memory | Model | Quant | Peak Memory | Generation Speed |
|---|---|---|---|---|---|---|
| MacBook Air M5 | Apple M5 | 24 GB | gemma-4-E2B-it | Q4 | 2.66 GB | ~80 tok/s |
| MacBook Pro M5 | Apple M5 Pro | 36 GB | gemma-4-E2B-it | Q4 | — | — |
This implementation is licensed under the Apache License 2.0. The Gemma 4 model weights are subject to Google's Gemma license.