Core ML vs PyTorch vs TensorFlow for iOS Deployment

Train in the chosen frameworkExport and validate model behaviourBenchmark the iOS integration
PyTorch and TensorFlow can support model development and training. Core ML provides an Apple deployment path. Decide where training occurs and verify the converted model against the app’s size and latency goals.

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:

 \mathcal{T} \in \mathbb{R}^{I_1 \times I_2 \times \cdots \times I_n}

Where  I_1, I_2, \ldots, I_n 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:

     (\mathcal{A} + \mathcal{B})_{i_1, i_2, \ldots, i_n} = \mathcal{A}_{i_1, i_2, \ldots, i_n} + \mathcal{B}_{i_1, i_2, \ldots, i_n}

  • Tensor Multiplication: Extends matrix multiplication across dimensions:

     (\mathcal{C})_{i,j} = \sum_k \mathcal{A}_{i,k} \cdot \mathcal{B}_{k,j}

    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.convert with the intended deployment target, then compare its predictions with the original. See Apple’s PyTorch conversion guide.

Mathematical representation of a linear layer:

 Y = XW + b

Where:

  •  X \in \mathbb{R}^{N \times D} : Input tensor
  •  W \in \mathbb{R}^{D \times K} : Weight matrix
  •  b \in \mathbb{R}^K : 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:

 T'_{NHWC} = \text{Permute}(T_{NCHW}, [0, 2, 3, 1])

Where  [0, 2, 3, 1] indicates the new axis order.

Example:

Input tensor:  T_{NCHW} \in \mathbb{R}^{1 \times 3 \times 224 \times 224}

Output tensor:  T'_{NHWC} \in \mathbb{R}^{1 \times 224 \times 224 \times 3}


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

 W \in \mathbb{R}^{F \times C \times K_H \times K_W}

For comparison, a convolution that expects height, width, input channels, and output channels would use

 W' \in \mathbb{R}^{K_H \times K_W \times C \times F}

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.

 W' = \text{Transpose}(W, [2, 3, 1, 0])


6. Matrix Multiplications

6.1 Mathematical Definition

Matrix multiplication underpins fully connected layers. For matrices:

  •  A \in \mathbb{R}^{m \times n}
  •  B \in \mathbb{R}^{n \times p}

The product  C is:

 C_{i,j} = \sum_{k=1}^n A_{i,k} B_{k,j}

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 ImageType defaults differ for PyTorch and TensorFlow.

1.2 Mathematical Representation

To convert tensors from NCHW to NHWC, we permute the axes:

 T'_{NHWC} = \text{Permute}(T_{NCHW}, [0, 2, 3, 1])

This reorders the dimensions of the tensor. For example:

Input tensor in PyTorch:

 T_{NCHW} \in \mathbb{R}^{1 \times 3 \times 224 \times 224}

The same data after an explicit channels-last permutation:

 T'_{NHWC} \in \mathbb{R}^{1 \times 224 \times 224 \times 3}

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:  W \in \mathbb{R}^{F \times C \times K_H \times K_W}
  • Height-width-input-output layout in this example:  W' \in \mathbb{R}^{K_H \times K_W \times C \times F}

2.2 Transposing Weights

For the example layout above, this permutation moves the two spatial axes to the front:

 W' = \text{Transpose}(W, [2, 3, 1, 0])

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:

 Y_{i,j,f} = \sum_{c=1}^{C} \sum_{k_h=1}^{K_H} \sum_{k_w=1}^{K_W} X_{i+k_h,j+k_w,c} \cdot W_{k_h,k_w,c,f} + b_f

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:

 C_{i,j} = \sum_{k=1}^{n} A_{i,k} \cdot B_{k,j}

Where:

  •  A \in \mathbb{R}^{m \times n} : Input matrix
  •  B \in \mathbb{R}^{n \times p} : Weight matrix
  •  C \in \mathbb{R}^{m \times p} : 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  O(n^3) . 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:

 \text{Swish}(x) = x \cdot \sigma(x), \quad \sigma(x) = \frac{1}{1 + e^{-x}}

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:

 \text{Swish}(x) \approx a_0 + a_1x + a_2x^2

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  FP16 (16-bit floating point) to improve performance during inference. Conversion from  FP32 (32-bit floating point) involves rounding:

 x_{FP16} = \text{Round}(x_{FP32})

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.