December 2026 deployment checks for an on-device model
Treat the original framework examples as historical. Verify the exact export path, target OS and supported operators in your current environment. Include preprocessing, memory use and startup time in device tests.
Should you use TensorFlow or Core ML on iPhone?
If you trained a model in TensorFlow or PyTorch and want to call it from Swift, you can convert a supported model with Core ML Tools, add it to Xcode, and run predictions on the iPhone using Core ML. If you already ship a .tflite model on Android and want to share it with your iOS app, Google’s LiteRT can run that model on iOS too. The training framework and the runtime you ship are separate decisions. For a side-by-side training example, see the same model in Keras, PyTorch and MLX.
For example, suppose your app classifies a photo. Keep one test photo and the original model’s output. Run the same photo through the iPhone app using the same crop, color order, pixel scaling, and class labels. Compare the predicted scores before measuring speed. A model can load successfully while a preprocessing difference changes its prediction.
Apple’s guide to converting models for Core ML and Google’s guide to running LiteRT on iOS describe the two deployment paths.
1. Introduction
Core ML runs models on Apple devices. A Swift app supplies inputs and receives predictions through a model interface in Xcode. Core ML can use the CPU, GPU, or Neural Engine. Which processors a prediction uses depends on the model, the device, and the runtime configuration.
During conversion, Core ML Tools translates supported operations and records the model’s expected inputs and outputs. Three details matter when you check whether a converted model still behaves like the original.
- Tensor representations and operations.
- Graph transformations (dynamic to static).
- Optimizations specific to Core ML and Apple hardware.
2. Tensor Representations and Operations
2.1 Tensor Definitions
Tensors are generalizations of scalars, vectors, and matrices. They represent data in multi-dimensional arrays, crucial for neural network computations. A tensor is mathematically defined as:
![]()
Where
are the dimensions of the tensor. For example:
- A scalar is a 0-dimensional tensor.
- A vector is a 1-dimensional tensor.
- A matrix is a 2-dimensional tensor.
2.2 Tensor Operations
Neural networks heavily rely on tensor operations like addition and multiplication:
- Tensor Addition: Adds tensors element-wise, assuming identical shapes:

- Tensor Multiplication: Extends matrix multiplication across dimensions:
This forms the backbone of linear transformations in neural networks.
3. Computational Graphs
3.1 Static vs. Dynamic Graphs
A PyTorch model in eager mode can choose its next operation while Python is running. To deploy it through Core ML, capture the computations in a supported form. A trace records one execution path. If a model branches on its input, test examples that exercise each path and choose an export method that represents the required behavior.
3.2 Graph Conversion
For a PyTorch model, separate graph capture from conversion.
- Capture the model. Set the model to evaluation mode. Use a tracing or export workflow supported by your version of Core ML Tools and the operations in your model.
- Convert and validate. Pass the captured model to
coremltools.convertwith the intended deployment target, then compare its predictions with the original. See Apple’s PyTorch conversion guide.
Mathematical representation of a linear layer:
![]()
Where:
: Input tensor
: Weight matrix
: Bias vector
4. Tensor Format Conversion
4.1 Format Differences
Tensors differ in layout conventions across frameworks:
- NCHW places channels before height and width, a common convention for PyTorch image models.
- NHWC places channels last, a common convention for TensorFlow image models. Check the actual input shape before rearranging data.
4.2 Permutation
To convert tensors from NCHW to NHWC, their axes are permuted:
![]()
Where
indicates the new axis order.
Example:
Input tensor: ![]()
Output tensor: ![]()
5. Weight Serialization
5.1 Convolutional Layer Weights
For an ordinary PyTorch convolution with one group, the weight axes are output channels, input channels, kernel height, and kernel width. The shape is
![]()
For comparison, a convolution that expects height, width, input channels, and output channels would use
![]()
5.2 Transposition
To move between those two conventions, transpose the axes as shown below. Core ML Tools generally handles supported convolution weights during conversion. Applying the same transpose manually a second time changes the filter.
![]()
6. Matrix Multiplications
6.1 Mathematical Definition
Matrix multiplication underpins fully connected layers. For matrices:
The product
is:
![]()
6.2 Optimization
Runtime choices affect how matrix multiplication executes. Profile the converted model on the oldest device you intend to support and record latency for its actual input size.
1. Detailed Tensor Format Conversion
1.1 Context of Conversion
Tensors in machine learning represent data as multi-dimensional arrays. Different frameworks use different formats for storing these arrays:
- Channels first (NCHW). One RGB image of height and width 224 can have shape [1, 3, 224, 224].
- Channels last (NHWC). The same image can have shape [1, 224, 224, 3]. Core ML Tools accounts for the source model’s layout. Its
ImageTypedefaults differ for PyTorch and TensorFlow.
1.2 Mathematical Representation
To convert tensors from NCHW to NHWC, we permute the axes:
![]()
This reorders the dimensions of the tensor. For example:
Input tensor in PyTorch:
![]()
The same data after an explicit channels-last permutation:
![]()
1.3 Why This Matters
Moving an axis and reshaping an array produce different results. To check a permutation, pick a pixel and a color channel and verify that the value at the new indices is the same. Check the exported input description before applying the permutation in Swift.
2. Weight Serialization and Transformation
2.1 Weight Storage Formats
In convolutional layers, weights are stored as 4D tensors. The format varies by framework:
- PyTorch:

- Height-width-input-output layout in this example:

2.2 Transposing Weights
For the example layout above, this permutation moves the two spatial axes to the front:
![]()
This permutation preserves the filter values when the receiving operation reads them in the new axis order.
2.3 Mathematical Proof of Consistency
The convolution operation is defined as:
![]()
The transposition aligns the indices for the kernel dimensions and channels, ensuring the output remains accurate post-conversion.
3. Matrix Multiplications in Fully Connected Layers
3.1 Definition
Fully connected layers rely on matrix multiplication, which is mathematically defined as:
![]()
Where:
: Input matrix
: Weight matrix
: Output matrix
3.2 Core ML Optimization
The speed of a fully connected layer depends on where it runs and how data reaches it. For example, Apple hardware can take advantage of
- Parallelism across GPU cores.
- Unified memory architecture to minimize memory transfer overhead.
3.3 Importance of Optimization
Matrix multiplication is computationally expensive with a time complexity of
. Optimizing this operation significantly reduces inference latency on Apple devices.
4. Non-Linear Activations
4.1 Swish Activation Function
The Swish activation function is mathematically defined as:
![]()
Apple’s ML Program operations include SiLU (Swish) for the iOS 15 target. Check the operations supported by your deployment target before changing a trained activation. See the Core ML Tools operation reference.
4.2 Polynomial Approximation
As a numerical exercise, a second-order polynomial can approximate Swish over a chosen, bounded input interval:
![]()
Fit the coefficients over the activation range the model actually sees and measure the largest error on held-out values. An approximation changes the model and needs separate validation.
5. Challenges in Model Conversion
5.1 Graph Translation
Some Python control flow and custom operators need extra attention during export. Identify the first unsupported operation in the converter output, check its inputs, and test a small model containing just that operation.
5.2 Numerical Precision
Core ML often uses
(16-bit floating point) to improve performance during inference. Conversion from
(32-bit floating point) involves rounding:
![]()
5.3 Validation
Validate the converted model with examples from the supported input range. Record two results.
- Prediction differences against the original model, using a tolerance suitable for the task.
- Measured inference latency on the target iPhone, with the first prediction timed separately from later ones.
Conclusion
Core ML optimizes machine learning models for deployment on Apple devices through tensor format alignment, weight serialization, and hardware acceleration. For an iOS release, keep a small reference set of inputs, expected predictions, and device timings so that a future conversion can be checked against the version that shipped.
Frequently Asked Questions
Does Core ML replace PyTorch or TensorFlow?
Core ML can run a model in an Apple app. PyTorch and TensorFlow are also used for training and experimentation.
How should I choose a training framework?
Use your existing model, team tools and export requirements. Verify the deployment path early.
Which metrics matter on an iPhone?
Check output quality, app and model size, startup time, steady-state latency and memory.
