Skip to content

[BUG] Add Gemma 4 model architecture support (gemma4) #389

@sftinc

Description

@sftinc

Describe the bug

MLXLLM does not recognize the Gemma 4 model architecture. When attempting to load any Gemma 4 MLX model (e.g., mlx-community/gemma-4-E2B-it-4bit), the model fails to load because the gemma4 architecture type is not registered in the model registry. Gemma 4 was released on April 2, 2026 with MLX-converted weights available on Hugging Face from day one, but the Swift library doesn't support loading them.

To Reproduce

import MLXLLM
import MLXLMCommon

let modelConfig = ModelConfiguration(id: "mlx-community/gemma-4-E2B-it-4bit")
let container = try await LLMModelFactory.shared.loadContainer(configuration: modelConfig)
// Fails: model type "gemma4" is not supported

Expected behavior

MLXLLM should recognize and load Gemma 4 models, including the new architectural features:

  • Per-Layer Embeddings (PLE)
  • Shared KV Cache (later layers reuse KV states from earlier layers)
  • Dual RoPE (standard RoPE for sliding-window layers, proportional RoPE for global layers)
  • MoE variant (26B model with 3.8B active parameters)

The Python mlx_lm and mlx-vlm libraries added Gemma 4 support on launch day (v0.4.3), so the model weights and conversion are already validated.

Desktop (please complete the following information):

  • OS Version: macOS 26.3.1
  • Device: M2 MacBook Pro, 24 GB
  • Version: mlx-swift 0.10.0, mlx-swift-examples 2.29.1

Additional context

Available MLX-converted Gemma 4 models on Hugging Face:

  • mlx-community/gemma-4-26b-a4b-it-4bit (MoE, 3.8B active)
  • mlx-community/gemma-4-31b-it-4bit (Dense)
  • unsloth/gemma-4-E2B-it-UD-MLX-4bit (Edge, ~2B)
  • unsloth/gemma-4-E4B-it-UD-MLX-4bit (Edge, ~4B)

Full collection: https://huggingface.co/collections/mlx-community/gemma-4

The Python MLX ecosystem already supports these models — this is a request to bring parity to the Swift side.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions