用 Metal tensors 把自訂 ML operation 做到 Apple Silicon 底層
這場 session 是給 ML framework、Core AI model 與 Metal app 團隊的底層優化路線圖:用 TensorOps 處理 quantized data、multi-plane tensors、FlashAttention 類 fused operation,最後把 custom Metal kernel 接回 Core AI 模型轉換流程。
當 Core AI 或 MLX 的高階 API 不夠時,Apple 希望你用 TensorOps 寫可攜的 Metal kernel:直接吃 quantized tensors、少搬資料,並把自訂 operation 封進 Core AI asset。
這支影片在拆四個底層 ML kernel 問題
什麼時候值得下到 Metal
高階 Core AI / MLX 適合部署模型;當你要實作研究新 op、貢獻 MLX / llama.cpp,或在 Metal app 裡跑自訂模型,就需要 TensorOps。
不要先解壓再算
TensorOps 支援 4/8-bit integer、4/8-bit float、2-bit integer 與 MX scaling formats,目標是直接用 quantized tensor 進 matmul,降低 memory bandwidth。
Multi-plane tensor 把 data 和 scale 綁在一起
iOS / macOS 27 起,單一 MTLTensor 可同時代表 quantized data plane 與 FP8 E8M0 scale plane,避免散落的 buffer metadata。
FlashAttention 是 TensorOps 高階能力展示
用 cooperative tensors、row reductions、map_iterator 與 direct matmul input,把 QK、SoftMax、V 的工作融合,減少 threadgroup memory round trip。
從 quantized tensor 到 Core AI custom op 的工作流
定義 quantized MTLTensorDescriptor
在 descriptor 指定 MetalFloat8、integer 量化型別、usage 與 dimensions;新 data types 有額外 alignment 要求,必須查 Metal 文件。
為 scales 建 auxiliary plane
使用 MTLTensorAuxiliaryPlaneDescriptor 描述 FP8 E8M0 scale plane 與 block factors,讓 data、scale、metadata 一起進 tensor object。
在 shader 中切 tile 並交給 TensorOps matmul
用 threadgroup ID slice input/output tensors;大多情況直接把 quantized tensor 餵給 TensorOps,讓它自動處理 dequantization 與 hardware acceleration。
用 cooperative tensors fusion 複雜 op
FlashAttention 類 op 需要讓 SIMD group 擁有完整 row,做 row max / SoftMax,再把 cooperative tensor 直接當第二次 matmul input。
把 Metal kernel 接回 Core AI conversion
在 Python 轉換流程註冊 TorchMetalKernel,替換 PyTorch attention implementation,再 export 成含 custom kernel 的 Core AI asset。
重點 1:TensorOps 是 Apple Silicon GPU 的 ML kernel 抽象層
影片把 Apple ML stack 分成高階 Core AI / MLX、低階 Metal Performance Shaders / Metal Performance Primitives,以及 TensorOps。TensorOps 是 Metal Shading Language API,讓你寫 tensor operation 時自動使用不同 Apple Silicon GPU generation 的硬體加速,特別是 M5 shader core 裡的 Neural Accelerator。
這不是要一般 app 團隊全部改寫 kernel;它適合需要自訂 operation、追新研究 operator、或把 PyTorch / Core AI 模型裡的瓶頸 fused 成 Metal kernel 的團隊。
重點 2:FlashAttention 示範的是「少搬資料」的設計哲學
Attention 原本需要 Q x K、row-wise SoftMax、再乘 V。FlashAttention 的核心是把這些 operation fuse 成單一 kernel,避免把中間矩陣反覆寫回 memory。影片示範使用 execution_simdgroup scope 讓每個 SIMD group 擁有完整 rows,接著用 cooperative tensor 儲存 QK 中間結果並做 row reduction。
is_compatible_as_left_input / right input 判斷,失敗時才 fallback store/reload。重點 3:Core AI integration 讓自訂 kernel 進入模型資產
最後 demo 將 custom FlashAttention kernel 整合進 SAM3 image segmentation 模型。流程是在 Python 端把 Metal kernel body 註冊成 TorchMetalKernel,替換 Hugging Face attention implementation,載入模型並從 PyTorch export 成最佳化 Core AI asset。推論時,模型可用文字提示標出圖片中的車輛區域,證明 custom attention kernel 已進入模型流程。
風險與遷移注意事項
對 ML / Metal 團隊的實作啟發
來源
主要來源為 Apple Developer 官方影片頁、transcript、chapters 與 code snippets;YouTube 影片頁用於 iframe 與發布 metadata。官方頁:Optimize custom machine learning operations with Metal tensors - WWDC26。YouTube:WWDC26: Optimize custom machine learning operations with Metal tensors | Apple。
- 01TensorOps 是寫 Apple Silicon 自訂 ML kernel 的低階路徑,適合量化、融合 operation 與 Core AI custom op。
- 02直接使用 quantized MTLTensor 與 scale plane,可減少 bandwidth 壓力並讓 TensorOps 自動使用硬體加速。
- 03FlashAttention demo 的核心不是 API 花招,而是用 cooperative tensors 避免中間結果反覆落到 memory。