MLX Swift is an open-source array computing framework for Apple platforms that lets you write mathematical code using n-dimensional arrays, with automatic GPU execution and automatic differentiation via function transformations like `grad`. It brings NumPy-style numerical computing to Swift with lazy evaluation and a clean, math-like API.
• Write vectorized math that operates on entire arrays at once instead of scalar-by-scalar loops — code reads like the math, runs on the GPU by default
• Automatic differentiation via `grad` lets you compute gradients of arbitrary functions without hand-writing derivatives — the foundation of custom ML training loops
• Seamlessly interoperates with the broader MLX ecosystem (Python, C++, C) so you can prototype in Python and ship in Swift using the same concepts and operations
Demonstrates fitting a quadratic polynomial to noisy data points using MLX Swift's `grad` function transformation for automatic differentiation and a simple gradient descent loop — all running on the GPU.
import MLX
import MLXRandom
// Generate noisy quadratic data: y = 2x² - 3x + 1 + noise
let x = MLXArray(stride(from: -2.0, through: 2.0, by: 0.1).map { Float($0) })
let trueTheta = MLXArray([1.0, -3.0, 2.0] as [Float]) // [bias, linear, quadratic]
let noise = MLXRandom.normal([x.shape[0]])
let y = trueTheta[0] + trueTheta[1] * x + trueTheta[2] * x * x + noise * 0.1
// Define the model: f(x; theta) = theta[0] + theta[1]*x + theta[2]*x²
func predict(_ xVals: MLXArray, _ theta: MLXArray) -> MLXArray {
theta[0] + theta[1] * xVals + theta[2] * xVals * xVals
}
// Mean squared error loss
func loss(_ theta: MLXArray) -> MLXArray {
let predictions = predict(x, theta)
let diff = predictions - y
return (diff * diff).mean()
}
// Transform loss into a gradient function w.r.t. theta
// MLX derives the gradient automatically — no hand-written derivatives
let gradFn = grad(loss)
// Initialize parameters and run gradient descent
var theta = MLXArray([0.0, 0.0, 0.0] as [Float])
let learningRate: Float = 0.05
for step in 0..<200 {
let grads = gradFn(theta)
theta = theta - learningRate * grads
// eval() flushes the lazy compute graph each step
eval(theta)
if step % 50 == 0 {
let currentLoss = loss(theta)
eval(currentLoss)
print("Step \(step): loss = \(currentLoss.item(Float.self))")
}
}
// Read fitted coefficients (forces final computation)
let fitted = theta.asArray(Float.self)
print("Fitted: bias=\(fitted[0]), linear=\(fitted[1]), quadratic=\(fitted[2])")
// Expected approx: bias≈1.0, linear≈-3.0, quadratic≈2.0Foundation Models is a new Apple framework introduced in iOS 27 that gives developers on-device access to the same Apple Intelligence language model powering system features, enabling text generation, structured output, and tool-calling entirely on-device without a network connection.
iOS 27 opens the Foundation Models framework to third-party LLM providers via a new public LanguageModel protocol, enabling anyone to integrate custom, server-based, or open-source models using the same Swift API as Apple's on-device system model.
App Schemas let developers describe their app's content and actions using pre-defined domain schemas (like the Calendar domain) so Siri can understand, search, and act on app data without custom NLP. Entities conforming to IndexedEntity are donated to Spotlight's semantic index, enabling natural-language queries over app content.
In-depth guide
iOS 27 On-Device AI & Apple Intelligence →MLX uses lazy evaluation — operations build a compute graph and nothing executes until you call `eval()` or read a value. In loops, call `eval()` each iteration to prevent the compute graph from growing unboundedly. MLX is installed via Swift Package Manager (github.com/ml-explore/mlx-swift), not bundled with Xcode.
Requires Apple Silicon or A-series GPU for full GPU acceleration; runs on CPU fallback otherwise. Open-source via Swift Package Manager — not a built-in Apple framework.
Visual Intelligence brings iOS 17's Visual Look Up capabilities to a new developer-facing API surface in iOS 27, letting apps pipe live camera frames or static images through on-device scene understanding to extract subjects, text, barcodes, and rich semantic labels without any cloud round-trip.