Open Source Models
How to Run Gemma 2 on iPhone with MLX
Gemma is a family of models, so start by choosing a version and a file your app supports.
What to try
- Choose an instruction-tuned variant if you want to chat.
- Try short questions and a summary with verifiable details.
- Record the exact model and precision so you can repeat the test.
Gemma is a family of models, so start by choosing a version and a file your app supports. This guide covers Gemma 2 and what to check before loading it on an iPhone.
Gemma 2 Architecture: Distillation, Soft-Capping, and Interleaved Attention
Unlike conventional Llama-style architectures that rely on standard causal multi-head or grouped-query attention across all layers, Gemma 2 incorporates specific mathematical modifications designed to stabilize training and maximize parameter efficiency:
- Knowledge Distillation at Practical Scale: Gemma 2 2B comprises 2.61 billion total parameters arranged in 26 transformer layers with a hidden dimension ($d_{model}$) of 2,304 and an intermediate feed-forward dimension of 9,216. Rather than learning purely from next-token cross-entropy on raw text, the model was trained using probability distribution matching against a 27B teacher model. This dense transfer of semantic representations gives the 2.6B parameter model outsized performance on complex instruction-following tasks.
- Logit Soft-Capping in Attention and Projections: To prevent logits from growing uncontrollably in magnitude during deep autoregressive loops, Gemma 2 introduces hyperbolic tangent soft-capping. In attention layers, the query-key dot product is bounded by: $\text{Attn}(Q, K) = \text{cap} \cdot \tanh\left(\frac{Q K^T}{\sqrt{d_k} \cdot \text{cap}}\right)$ with $ ext{cap} = 50.0$. Similarly, final output projection logits are soft-capped with $ ext{cap} = 30.0$. While mathematically elegant, naive mobile implementations allocate large temporary float32 matrices in memory, creating significant bandwidth pressure unless fused directly into the Metal execution kernel.
- Interleaved Sliding Window Attention (SWA): Gemma 2 alternates attention topologies layer-by-layer. Odd-numbered layers employ a local sliding window attention mechanism spanning 4,096 tokens, while even-numbered layers utilize global causal attention across the full 8,192-token context window. For mobile deployment, the sliding window layers prevent unbounded Key-Value (KV) cache accumulation, bounding memory consumption in extended multi-turn conversations.
- Dual RMSNorm Pre- and Post-Normalization: Each transformer block applies RMSNorm both before and after the attention and multi-layer perceptron (MLP) sub-layers, normalized using a $(1 + w)$ parameterization. This dual-normalization stabilizes activation variances across layers and eliminates numerical overflow during FP16 or quantized 4-bit matrix multiplications.
To contrast Gemma's architectural trade-offs against competing open models, explore our deep dive on Qwen vs Gemma for local LLMs.
Memory Bandwidth and Apple Silicon Throughput on A17 Pro and A18 Pro
Autoregressive token generation in Large Language Models operates at a mobile batch size of one ($B=1$). In this operational regime, computation is strictly memory-bandwidth bound rather than compute-bound: every single generated token requires streaming the entire active parameter weight matrix from physical DRAM into the processor's execution units. The theoretical maximum decoding speed is governed by:
$ ext{Max Throughput (tok/s)} = rac{ ext{Unified DRAM Bandwidth (GB/s)}}{ ext{Quantized Model Memory Footprint (GB)}}$
On Apple Silicon SoCs, unified memory architecture connects CPU cores, GPU shader cores, and the Apple Neural Engine to a shared high-speed LPDDR5X DRAM bus. The Apple A17 Pro (iPhone 15 Pro) provides 150 GB/s of memory bandwidth, while the Apple A18 Pro (iPhone 16 Pro) elevates throughput to 170 GB/s via a wider memory interface. In comparison, base Apple M4 silicon provides 120 GB/s, scaling to 273 GB/s and 410 GB/s on Pro and Max configurations.
When quantized to 4-bit precision (affine quantization with group size 64) within Apple MLX, Gemma 2 2B occupies approximately 1.45 GB of memory. Under ideal theoretical saturation on the A18 Pro, this would correspond to $\sim 117\text{ tok/s}$. In production, physical bus contention from display composition, audio daemons, OS background tasks, and Metal shader dispatch overhead results in an observed sustained decoding speed of 34.2 tokens/second on A18 Pro and 28.6 tokens/second on A17 Pro—both well above typical human reading speeds of 4 to 6 words per second (5 to 8 tokens/second).
Conversely, during the initial prompt processing (prefill) phase, where the full user query is ingested simultaneously, the batch size equals the sequence length ($B=S$). Here, the computational profile transitions to compute-bound dense matrix multiplication (GEMM), allowing the A18 Pro GPU to process prompt tokens at 118.4 tokens/second.
The guide to Apple Silicon memory bandwidth for LLMs explains the physical limit that affects generation.
How to test it on your device
Choose an instruction-tuned variant if you want to chat. Try short questions and a summary with verifiable details. Record the exact model and precision so you can repeat the test.
The iOS Jetsam Barrier: Managing Resident Dirty RAM
On Apple platforms, memory management is strictly governed by the Darwin kernel's jetsam daemon. While macOS allows processes to swap memory to physical SSD storage under pressure, iOS and iPadOS strictly prohibit arbitrary anonymous paging to flash storage to prevent flash wear and preserve UI responsiveness. Instead, the kernel tracks phys_footprint (resident dirty memory), which includes heap allocations, dirty framework memory, and active Metal GPU command buffers.
On iOS, the memory budget available to an app depends on the device, OS version and memory pressure. Test on the target device with representative context and attachments.
This reality dictates model selection on mobile hardware:
- Gemma 2 2B in 4-bit (1.85 GB Peak Dirty RAM): Consuming only 1.45 GB for weights, 220 MB for a 2,048-token KV cache, and 180 MB for transient Metal activations, Gemma 2 2B leaves over 2.65 GB of headroom beneath the jetsam ceiling. This vast safety margin allows pairing the language model with local embedding pipelines (for Retrieval-Augmented Generation) or on-device speech transcription engines without memory hazard.
- Gemma 2 2B in FP16 (5.68 GB Peak Dirty RAM): In unquantized 16-bit precision, model weights alone exceed 5.2 GB, immediately triggering a jetsam termination during weight loading. 4-bit or 8-bit quantization is mandatory for mobile execution.
To compare Gemma with other families for your device, see the guide to open models for iPhone.
Engineering Best Practices for Deploying Gemma on iOS
- Fuse Logit Soft-Capping in Custom Metal Kernels: Standard PyTorch or un-optimized MLX pipelines compute attention logits, allocate a temporary float32 buffer, apply $ anh$, multiply by the cap factor, and then feed into softmax. On mobile GPUs, this intermediate buffer thrashes the L2 cache and wastes DRAM bandwidth. Implement a fused Metal Performance Shaders kernel that incorporates soft-capping directly into the scaled dot-product attention calculation.
- Implement Ring-Buffer Cache Management for Sliding Window Attention: Gemma 2's alternating 4,096-token sliding window layers do not require retaining full sequence history. By implementing a circular ring buffer for the KV cache on odd layers, memory allocations are capped at exactly 4,096 token slots, eliminating heap fragmentation and guaranteeing fixed memory usage regardless of conversation length.
- Adopt 4-Bit Affine Quantization with Group Size 64: To preserve the fine-grained reasoning capabilities acquired through knowledge distillation, quantize linear projection weights using affine 4-bit quantization with a group size of 64 rather than per-channel quantization. Grouped quantization maintains outlier activations in the GeGLU feed-forward layers, keeping MMLU and GSM8K benchmark degradation below 1.2% compared to FP16 baselines.
- Utilize Zero-Copy Memory Mapping via MTLResourceStorageModeShared: Package model weights into unified safetensors or MLX binary formats mapped directly from disk using POSIX
mmap. By declaring Metal buffers withMTLResourceStorageModeShared, the GPU reads weights directly from unified memory without intermediate CPU allocations, cutting app startup time to under 400 milliseconds and minimizing battery draw.
The Lapis model catalogue shows compatible options before you choose a download.
Sources and references
- Gemma 2: Improving Open Language Models at a Practical Size
Gemma Team, Google DeepMind (arXiv:2408.00118, 2024)
- LLM in a flash: Efficient Large Language Model Inference with Limited Memory
Keivan Alizadeh, Iman Mirzadeh, Dmitry Belenko, Karen Khatamifard, et al. (Apple Machine Learning Research / arXiv:2312.11514, 2023)
- Increased Memory Limit Entitlement
Apple Developer Documentation (2024)
Local execution with Lapis
Chat with compatible models on iPhone, iPad and Mac. Download them once and use local inference offline; model size depends on your device’s resources.