# CoreML Tools — Convert ML Models to Apple CoreML Format > A Python package for converting trained models from TensorFlow, PyTorch, and other frameworks into Apple's CoreML format for on-device inference on iPhone, iPad, and Mac. ## Install Save as a script file and run: # CoreML Tools — Convert ML Models to Apple CoreML Format ## Quick Use ```bash pip install coremltools ``` ```python import coremltools as ct import torch model = torch.hub.load("pytorch/vision", "resnet18", pretrained=True) model.eval() example_input = torch.rand(1, 3, 224, 224) traced = torch.jit.trace(model, example_input) mlmodel = ct.convert(traced, inputs=[ct.ImageType(shape=(1, 3, 224, 224))]) mlmodel.save("ResNet18.mlpackage") ``` ## Introduction CoreML Tools is Apple's open-source Python library for converting machine learning models into the CoreML format (.mlpackage or .mlmodel). Once converted, models run natively on Apple Neural Engine, GPU, and CPU across iOS, iPadOS, macOS, watchOS, and tvOS with no additional dependencies. ## What CoreML Tools Does - Converts models from PyTorch, TensorFlow, TensorFlow Lite, scikit-learn, and ONNX to CoreML - Applies post-training quantization (INT8, FP16, palettization) to reduce model size - Supports neural network, pipeline, and ML program model representations - Validates converted models by comparing outputs against the original framework - Generates Xcode-ready .mlpackage files for drag-and-drop integration into apps ## Architecture Overview The conversion pipeline starts by importing a model from its source framework into CoreML Tools' intermediate representation (MIL — Model Intermediate Language). MIL normalizes operations across frameworks into a common graph. Optimization passes then apply quantization, graph pruning, and operation fusion. Finally, a backend serializer writes the optimized graph into the CoreML protobuf format, bundled as an .mlpackage with weights and metadata. ## Self-Hosting & Configuration - Install via pip on macOS or Linux (conversion works on both; inference requires Apple hardware) - Use ct.convert() with framework-specific inputs for one-line conversion - Specify compute units (CPU, GPU, Neural Engine, or all) in the conversion call - Apply quantization with ct.optimize utilities to shrink model size for mobile deployment - Test converted models locally using the predict() method before deploying to devices ## Key Features - Unified converter handles PyTorch (traced and exported), TensorFlow, and ONNX models - Post-training compression includes weight quantization, pruning, and palettization - Models leverage Apple Neural Engine for hardware-accelerated inference on A-series and M-series chips - MIL intermediate representation enables custom optimization passes - Supports vision, NLP, audio, and tabular model types ## Comparison with Similar Tools - **ONNX Runtime** — cross-platform inference engine; CoreML Tools targets Apple-specific hardware acceleration - **TensorFlow Lite** — mobile inference for Android and iOS; CoreML Tools produces Apple-optimized models - **OpenVINO** — Intel hardware optimization; CoreML Tools targets Apple Neural Engine and GPU - **TorchScript** — PyTorch serialization format; CoreML Tools converts to a format native to Apple platforms - **NVIDIA TensorRT** — GPU inference optimization for NVIDIA hardware; CoreML Tools targets Apple silicon ## FAQ **Q: Can I convert models on Linux?** A: Yes. Model conversion runs on Linux and macOS. Running inference on the converted model requires Apple hardware. **Q: Which PyTorch export methods are supported?** A: Both torch.jit.trace and torch.export (ExecuTorch path) are supported for conversion. **Q: How much smaller are quantized models?** A: INT8 quantization typically reduces model size by 4x compared to FP32 with minimal accuracy loss. **Q: Does it support transformer models?** A: Yes. CoreML Tools can convert transformer-based models including vision transformers and language models. ## Sources - https://github.com/apple/coremltools - https://coremltools.readme.io/ --- Source: https://tokrepo.com/en/workflows/asset-de281956 Author: Script Depot