Gemma3 models trained with BF16 can produce residual stream activations that exceed FP16's representable range (±65,504). FP16 inference is a hard requirement for ANE. This causes:
Overflow to inf in FP16 computation
NaN propagation through subsequent layers
Complete model failure on ANE (which uses FP16)
Model Quality Benchmarks
FP16 Scaling for ANE Compatibility
Gemma3 4B QAT models produce activations that exceed FP16 range (±65,504) during inference. We apply weight scaling (α=0.1875) to prevent overflow:
Embedding weights scaled by α=0.1875 (3/16)
LM head logits divided by α to restore original scale
Zero runtime overhead - transformation applied at conversion time
ANEMLL applies LUT (Lookup Table) palettization to compress weights. This model uses per-channel grouping (per_channel=4) where every 4 output channels share one lookup table for finer granularity and better quality.
Model Architecture
4-Function Chunk Design
Each FFN chunk contains 4 CoreML functions to support Gemma 3's sliding window attention:
Single-token inference after KV rotation (position ≥ sliding_window)
prefill_rotate
Batch prefill after KV rotation (position ≥ sliding_window)
Why 4 functions? When position reaches the sliding window boundary (1024), KV cache must be rotated. The *_rotate functions handle inference after rotation, enabling efficient sliding window attention without recomputing the entire cache.
Gemma 3 Hybrid Local-Global Attention
Gemma 3 uses a hybrid attention architecture that combines local and global attention:
Local Attention (Sliding Window)
Applied to most layers
Window size: 1024 tokens
Efficient O(n × w) complexity where w = window size
Bounded memory usage for long sequences
Global Attention
Applied every 4th layer (layers 4, 8, 12, ...)
Full context access up to 4096 tokens
Preserves long-range dependencies
Advantages:
Memory Efficiency: ~75% smaller KV cache compared to full attention
Speed: Faster inference for long sequences
Quality: Global attention layers maintain document-level coherence
Scalability: Efficiently handles longer contexts than pure global attention
iOS/macOS Distribution
This folder contains pre-compiled CoreML models (.mlmodelc directories) ready for iOS and macOS deployment. No zip extraction required.
Requirements
macOS 15 (Sequoia) or later with Apple Silicon
iOS 17+ for mobile deployment
8GB+ RAM recommended
Python 3.9+ for testing
CoreML Tools 8.x+ and HuggingFace Transformers
Installation
bash
1# Install Git LFS (required for large model files)2brew install git-lfs
3git lfs install45# Install Python dependencies6pip install coremltools transformers
78# Clone the repository9git clone https://huggingface.co/anemll/anemll-google-gemma-3-4b-it-qat-int4-unquantized-ctx4096_0.3.5
1011# Navigate to iOS/macOS distribution folder12cd anemll-google-gemma-3-4b-it-qat-int4-unquantized-ctx4096_0.3.5/ios
1314# Verify models are ready (should see .mlmodelc directories)15ls -la *.mlmodelc