Apple Developer onepage/WWDC26
#WWDC26#MLXSwift#MachineLearning2026-06-08 · 13:47

MLX Swift:用 像數學的 Swift 寫 GPU 數值運算

這場 session 說明 MLX Swift 在 Apple 數值運算生態的位置,並用 power iteration、Mandelbrot、heat distribution、SOR 與 curve fitting 展示 n-dimensional arrays、lazy evaluation、GPU execution 與 automatic differentiation。

// 一句話結論

MLX Swift 的價值是把 NumPy-style array programming、GPU 加速與 autodiff 帶進 Swift:你可以用接近數學式的程式描述矩陣、卷積與梯度,不必在 Swift app 與 Python prototype 之間來回搬運核心邏輯。

Apple Developer · WWDC26 · Video ID: KCL8f9ztKFkApple Developer 官方頁
Content map

這支影片在回答四個選型問題

定位

何時該選 MLX Swift?

Accelerate、BNNS、MPS、Swift Numerics 各有強項;若目標是用 Swift 寫接近數學式、可利用 GPU、可 autodiff 的 numerical code,MLX Swift 是更高階的選擇。

抽象

n-dimensional arrays 是核心模型

像 NumPy 一樣,MLX Swift 用 array 而不是單一 scalar 當主要操作單位。矩陣加法、transpose、matmul、norm、broadcasting 都讓程式碼讀起來更接近公式。

執行

Lazy evaluation 讓 GPU 與 grad 成立

操作先建立 compute graph,直到 eval 或讀值才執行。這同時支援 automatic GPU execution 與 grad 這類 function transformation。

生態

Swift 不是孤島

MLX 有 Swift、Python、C++、C front-ends,共享概念與 lazy-evaluation model;可以 Python prototype、Swift ship,也能參考 mlx-swift-lm 與 mlx-swift-examples。

Implementation flow

從數學式到 MLX Swift 的導入路徑

1

先把問題表成 array 操作

把 scalar loops 改寫成矩陣、向量或 grid。Mandelbrot 範例把每個 pixel 的迭代改成對整個 complex grid 一次操作。

2

讓核心公式留在程式表面

Power iteration 裡的 A = B.T + B、matmul(A, v)、norm(Av) 都直接對應線性代數,不被索引 bookkeeping 淹沒。

3

用 conv2d 表達鄰域運算

Heat distribution 的 Jacobi iteration 把四鄰域平均寫成 stencil kernel 與 conv2d,boundary condition 則用 elementwise ternary 保留固定熱源與牆。

4

選演算法,而不只換框架

SOR 與 Jacobi 程式形狀相近,但用 omega overshoot 與 red/black checkerboard 模擬 in-place update,從 N² 收斂改善到 N 級別。

5

用 grad 轉成最佳化迴圈

Curve fitting 範例定義 loss 後,讓 grad(loss) 自動產生相對參數的梯度,再用 gradient descent 更新,不手寫導數。

重點 1:MLX Swift 解的是「數學可讀性 + Apple 平台執行」

影片不是把 MLX Swift 包裝成所有數值任務的替代品,而是明確放在 Apple 生態中:Accelerate 適合 CPU vector primitives,BNNS 是神經網路 building blocks,Metal Performance Shaders 給低階 GPU kernels,Swift Numerics 補泛型數值型別。MLX Swift 的主場是高階 array computing。

判斷方式:如果你的程式正在寫大量 index loops、手動管理矩陣形狀,或先用 Python / NumPy prototype 再重寫 Swift,MLX Swift 值得評估。

重點 2:Lazy evaluation 是效能與 autodiff 的共同基礎

MLX Swift 操作 array 時先建立 compute graph,不會每一行立即執行。這讓框架可以把計算放到 GPU,也讓 grad 這類 transformation 從同一個 graph 推導梯度。但在 loop 裡要適時呼叫 eval,避免 graph 無限制變大。

重點 3:影片用三個例子展示不同計算形狀

Mandelbrot 是 embarrassingly parallel:每個點獨立,可一次套到整個 grid,官方示範指出視演算法而定可有 10x 等級加速。Heat distribution 則是鄰域問題,透過 convolution 表達 stencil。Curve fitting 則從 forward computation 轉成 optimization problem,用 autodiff 取得梯度。

重點 4:演算法選擇仍然比框架更重要

SOR 範例是影片最重要的工程提醒。Jacobi iteration 易懂但收斂慢,通常需要 N² iterations;SOR 用 omega overshoot 與 checkerboard update 模擬 in-place 更新,達到 N iterations 等級。MLX 讓兩者都好寫,但真正的效能差距來自演算法。

風險與遷移注意事項

不要把 NumPy 逐行翻譯當終點

API 熟悉不代表最佳。要重新檢查 shape、broadcasting、eval 邊界與是否真的適合 GPU。

loop 裡要管理 lazy graph

長迭代若忘了 eval,graph 可能成長過大;影片在 power iteration 與 gradient descent 都示範每步 flush。

邊界條件是數值程式的真風險

Heat distribution 範例的 heatMask 與 boundary condition 是 correctness 關鍵;不要只看 conv2d kernel 是否能跑。

Swift/Python 雙路線要定義 ownership

MLX 多 front-end 方便 prototype 與 ship,但團隊仍要決定哪個語言是 source of truth,避免兩份數值邏輯漂移。

來源

主要來源為 Apple Developer 官方影片頁、官方 transcript、章節與 code snippets;YouTube 影片頁用於 iframe 與發布 metadata。官方頁:Explore numerical computing in Swift with MLX - WWDC26。YouTube:WWDC26: Explore numerical computing in Swift with MLX | Apple。

// TL;DR