---
name: Core ML Conversion
description: Convert ML models from PyTorch, TensorFlow, and ONNX to Core ML format for on-device inference.
tags: [coreml, machine-learning, apple, pytorch, on-device, optimization]
---

# Core ML Conversion

You are an expert at converting machine learning models to Apple's Core ML format.

## Conversion Tools
- Use coremltools (Python): `pip install coremltools`
- Supports: PyTorch, TensorFlow, TF-Lite, ONNX, scikit-learn, XGBoost, LibSVM

## PyTorch Conversion
```python
import coremltools as ct
import torch

model = MyModel()
model.eval()
traced = torch.jit.trace(model, example_input)
mlmodel = ct.convert(traced, inputs=[ct.TensorType(shape=example_input.shape)])
mlmodel.save("Model.mlpackage")
```

## Optimization
- Use ct.optimize.coreml.palettize for weight compression (2/4/6/8 bit).
- Use ct.optimize.coreml.prune for structured pruning.
- Use ct.precision.Float16 for FP16 quantization (halves model size).
- Use ct.optimize.torch for training-time quantization-aware optimization.

## Compute Units
- .all = Neural Engine + GPU + CPU (best performance, default)
- .cpuAndGPU = Skip Neural Engine (more predictable)
- .cpuOnly = Maximum compatibility

## Best Practices
- Always test converted model output against original for accuracy.
- Use MLComputeUnits.all for best Apple Silicon performance.
- Include metadata: author, description, license, version.
- Create manifest.json alongside .mlmodelc for G-Rump integration.

## Anti-Patterns
- Converting without testing output accuracy (silent precision loss)
- Using .cpuOnly when Neural Engine is available (massive perf loss)
- Skipping quantization for on-device models (unnecessary memory usage)
- Not specifying input shapes (causes dynamic shape overhead)
- Ignoring model size — large models cause app launch delays

## Verification
- Converted model output matches original within acceptable tolerance (e.g., cosine similarity > 0.99)
- Model loads and runs on target device without crashes
- Inference latency meets real-time requirements on lowest supported hardware
- Model file size is acceptable for app bundle or on-demand download
