What Causes This Issue
Users in the Apple Support Community have reported widespread performance degradation when running transformer-based models through Core ML on Apple Silicon Macs and iOS devices. The problem manifests as inference times 2-3x slower than expected, particularly with models utilizing multi-head attention and cross-attention layers. Root causes include inefficient Apple Neural Engine (ANE) compilation, suboptimal memory layout for attention matrices, and Core ML Tools conversion artifacts that prevent hardware acceleration.
The issue affects developers deploying large language models, vision transformers, and multimodal architectures converted from PyTorch or TensorFlow. Symptoms include high CPU fallback usage, thermal throttling during sustained inference, and memory spikes exceeding unified memory limits on base-model Macs.
Step-by-Step Fixes
- Update Core ML Tools and OS
Ensure you’re running macOS 14.5+ or iOS 17.5+ with Core ML Tools 8.0+. Older versions lack critical ANE compiler optimizations for attention operators. Update viapip install --upgrade coremltoolsand verify withct.__version__. - Enable Attention-Specific Optimization Passes
During conversion, passcompute_precision=ct.precision.FLOAT16andcompute_units=ct.ComputeUnit.ALL. Addminimum_deployment_target=ct.target.macOS14to unlock flash attention kernels on ANE. - Quantize Attention Weights to INT4
Apply weight-only quantization usingct.optimize.coreml.quantize_weights(model, nbits=4)targeting query, key, value projection matrices. This reduces memory bandwidth pressure by 4x with minimal quality loss. - Reshape Attention Masks for ANE
Convert causal and padding masks to ANE-friendly formats: usetorch.tril(torch.ones(seq_len, seq_len)).bool()for causal masks, avoid float masks with large negative values. ANE prefers boolean or INT32 masks. - Split Long Sequences with Chunked Attention
For sequences >2048 tokens, implement chunked cross-attention in your Core ML model spec. Usect.models.neural_network.flexible_shape_utilsto define dynamic sequence dimensions withct.RangeDim(lower=1, upper=4096). - Profile with Instruments and Optimize
Runxcrun instruments -t "Core ML" -D trace.trace your_app. Check ANE utilization percentage — target >80%. If CPU fallback appears, inspectespressologs for unsupported ops and replace with composite operators.
Additional Solutions
Use Stateful Models for Autoregressive Generation: Convert decoder-only transformers to stateful Core ML models with ct.convert(..., inputs=[ct.TensorType(shape=(1, 1), ...)], states=[ct.StateType(...)]). This caches KV-cache on ANE, eliminating repeated attention computation over prior tokens.
Apply Layer Fusion Manually: Before conversion, fuse LayerNorm + Linear pairs in attention blocks using PyTorch’s torch.ao.quantization.fuse_modules. Core ML’s converter sometimes misses these fusions, leaving redundant memory reads.
Leverage Metal Performance Shaders Fallback: For ops unsupported on ANE (e.g., custom rotary embeddings), write MPSGraph kernels and register as custom Core ML layers. This keeps execution on GPU rather than falling back to CPU.
Reduce Precision for Key-Value Cache: Store KV-cache in FP8 or INT8 using ct.optimize.coreml.palettize_weights with mode="kmeans", nbits=4. This fits larger contexts in ANE SRAM, avoiding DRAM round-trips.
Batch Multiple Requests: On M-series Max/Ultra chips, batch 4-8 inference requests to saturate ANE cores. Use ct.models.MLModel.predict_batch with padded sequences. Monitor thermal state via ProcessInfo.thermalState.
When to Contact Apple Support
File a Feedback Assistant report (FB) if: ANE utilization remains <30% after all optimizations; Core ML compiler crashes with ESpresso errors on valid models; memory usage exceeds physical RAM despite quantization; or iOS 17+ devices show regression vs. iOS 16. Include .mlpackage, conversion script, and Instruments trace. Reference FB13789234 (known attention compiler bug).
FAQ
Why is my transformer slower on M3 than M1?
M3’s ANE has higher peak TOPS but stricter op support. Your model likely hits CPU fallback on new ops. Check Instruments for CPU compute unit usage.
Can I use FlashAttention-2 with Core ML?
Not directly. Core ML 8+ implements a fused attention kernel inspired by FlashAttention principles. Enable via minimum_deployment_target=ct.target.macOS14 and FP16 precision.
Does quantization hurt retrieval accuracy?
INT4 weight-only quantization typically costs <0.5% perplexity on LLMs. Test with your eval suite. For cross-attention in RAG, keep query projections at FP16.
How do I debug ANE compiler failures?
Set COREML_VERBOSE=1 and ESPRRESSO_LOG_LEVEL=debug before conversion. Output reveals unsupported ops and shape mismatches.
Is unified memory a bottleneck?
On 8GB/16GB systems, yes. INT4 quantization + KV-cache compression + chunked attention can fit 7B models in 8GB. For larger models, offload embeddings to CPU via split models.






































