Repository navigation
Closed
Labels
good first issueGood for newcomersGood for newcomerstriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate moduleThis issue has been looked at a team member, and triaged and prioritized into an appropriate module
Description
Good First Issue: Enable Gemma 4 on MLX Backend
Summary
Add support for exporting and running Google's Gemma 4 model on the MLX delegate. Gemma 3 (1B and 4B) already works on MLX and is tested in CI. Gemma 4 should follow the same path.
Background
Gemma 3 is currently supported on MLX via two export paths:
- HuggingFace path (
export_llm_hf.py): UsesAutoModelForCausalLMfrom HuggingFace transformers. Gemma 3 1B is tested in CI with both4wandnvfp4quantization. - export_llm path (
extension/llm/export/export_llm.py): Uses the internal Llama-based model definition with HF weight conversion. Gemma 3 has a dedicatedexamples/models/gemma3/directory withconvert_weights.py.
Gemma 4 likely uses a similar architecture to Gemma 3 (dense transformer with sliding window attention and GQA). If the architecture is compatible, enabling it may be as simple as testing it through the existing HF export path.
Steps
1. Test via the HuggingFace export path (try this first)
The HF path auto-detects model architecture, so Gemma 4 may work out of the box:
python -m executorch.backends.mlx.examples.llm.export_llm_hf \
--model-id google/gemma-4-<VARIANT> \
--output gemma4_mlx.pte \
--qlinear 4w \
--qembedding 4w \
--use-custom-sdpa \
--use-custom-kv-cacheIf this fails, note the error. Common issues:
- Unsupported aten op: Add a handler in
ops.py(see the "Add MLX Op Handler" issue template) - Data-dependent control flow: May need source transformations to avoid
torch.exportgraph breaks - New attention pattern: May need updates to the sliding window / KV cache handling
2. Test via the export_llm pipeline
If the HF path works but you also want the export_llm path:
- Create
examples/models/gemma4/with:__init__.pyconvert_weights.py(remap HF checkpoint keys to ExecuTorch transformer format)config/with model config JSON
- Add
gemma4entries toHUGGING_FACE_REPO_IDSand the weight converter dispatch inexamples/models/llama/export_llama_lib.py - Follow the pattern from
examples/models/gemma3/
3. Run inference
python -m executorch.backends.mlx.examples.llm.run_llm_hf \
--pte gemma4_mlx.pte \
--model-id google/gemma-4-<VARIANT> \
--prompt "What is the capital of France?" \
--max-new-tokens 504. Add CI test
Add a Gemma 4 entry to the test-mlx-llm matrix in .github/workflows/mlx.yml:
matrix:
model:
- id: "google/gemma-4-<VARIANT>"
name: "gemma4"What might need changes
- If Gemma 4 has new architectural features (e.g., new attention type, new activation, MoE): these may need new op handlers or source transformations
- If HuggingFace transformers has export-incompatible code (like Phi-4's LongRoPE data-dependent branching): may need to wait for a transformers fix or use the export_llm path instead
- Sliding window configuration: Gemma models use sliding window attention. The MLX export path auto-detects this from the HF config and uses
RingBufferKVCache
References
Metadata
Metadata
Assignees
Labels
good first issueGood for newcomersGood for newcomerstriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate moduleThis issue has been looked at a team member, and triaged and prioritized into an appropriate module
Type
Projects
- StatusShow more project fieldsDone
Activity
zeel2104 commentedon Apr 16, 2026
I would like to work on it. Could you assign it to me? previously contributed to Nvidia, Microsoft, pytorch, apache etc
metascroy commentedon Apr 16, 2026
Done! Thanks for taking this on!
zeel2104 commentedon Apr 23, 2026
Hey @metascroy
I have opened a PR, a summary of what it does
I validated the HuggingFace MLX path for Gemma 4 on Apple Silicon with
google/gemma-4-E2B-it.Working configuration:
--qlinear 4w--use-custom-sdpa--use-custom-kv-cacheWhat is now working:
export_llm_hf.pyexport succeedsrun_llm_hf.pyinference succeedsPYTHONPATHSmoke test prompt:
What is the capital of France?Output included:
ParisI also updated MLX docs and CI, but only for the exact Gemma 4 configuration that was validated.
What is still not covered in this PR:
extension.llm.export.export_llm/examples/models/gemma4/pathqembedding 4wornvfp4google/gemma-4-E2B-itReasoning:
qembedding 4wand broader quantization combinations were not part of the final working configuration I could validate end to endexport_llmpath is a separate chunk of work because it would require addingexamples/models/gemma4/, a weight converter, config wiring, and validating that the internal Llama-style model path cleanly represents Gemma 4’s attention/cache layoutWhat I’ll do next:
export_llmpathPlease let me know if you want any modifications or any other change
metascroy commentedon Apr 23, 2026
Awesome @zeel2104! Can you link your PR here, or tag me as a reviewer?
zeel2104 commentedon Apr 23, 2026
Hey @metascroy, I have opened the PR here: #19065