View in English

  • Apple 开发者
    • 入门汇总

    探索“入门汇总”

    • 概览
    • 学习
    • Apple Developer Program

    及时了解最新动态

    • 最新动态
    • 开发者你好
    • 平台

    探索“平台”

    • Apple 平台
    • iOS
    • iPadOS
    • macOS
    • Apple tvOS
    • visionOS
    • watchOS
    • App Store

    精选

    • 设计
    • 分发
    • 游戏
    • 配件
    • 网页
    • Home
    • CarPlay 车载
    • 技术

    探索“技术”

    • 概览
    • Xcode
    • Swift
    • SwiftUI

    精选

    • 辅助功能
    • App Intents
    • Apple 智能
    • 游戏
    • 机器学习与 AI
    • 安全性
    • Xcode Cloud
    • 社区

    探索“社区”

    • 概览
    • “与 Apple 会面交流”活动
    • 社区主导的活动
    • 开发者论坛
    • 开源

    精选

    • WWDC
    • Swift Student Challenge
    • 开发者故事
    • App Store 大奖
    • Apple 设计大奖
    • Apple Developer Centers
    • 文档

    探索“文档”

    • 文档库
    • 技术概述
    • 示例代码
    • 《人机界面指南》
    • 视频

    发布说明

    • 精选更新
    • iOS
    • iPadOS
    • macOS
    • watchOS
    • visionOS
    • Apple tvOS
    • Xcode
    • 下载

    探索“下载”

    • 所有下载
    • 操作系统
    • 应用程序
    • 设计资源

    精选

    • Xcode
    • TestFlight
    • 字体
    • SF Symbols
    • Icon Composer
    • 支持

    探索“支持”

    • 概览
    • 帮助指南
    • 开发者论坛
    • “反馈助理”
    • 联系我们

    精选

    • 《开发者账户帮助》
    • 《App 审核指南》
    • 《App Store Connect 帮助》
    • 即将实行的要求
    • 协议和准则
    • 系统状态
  • 快速链接

    • 活动
    • 新闻
    • 论坛
    • 示例代码
    • 视频
 

视频

打开菜单 关闭菜单
  • 专题
  • 所有视频
  • 关于

更多视频

  • 简介
  • 概要
  • 代码
  • 使用 MLX 探索 Swift 中的数值计算

    使用 MLX Swift 将 NumPy 风格的计算原生引入 Swift。了解如何在一个类型安全的环境中统筹管理图像处理、张量运算和神经网络训练等任务,从而消除机器学习工作流程中的跨语言障碍。探索相关 API,尽享熟悉的编译器、工具和调试体验,同时充分发挥 GPU 加速的优势。

    章节

    • 0:00 - Introduction
    • 0:57 - MLX Swift and the Apple ecosystem
    • 3:04 - MLX Swift
    • 4:28 - Mandelbrot
    • 6:34 - Heat distribution
    • 8:12 - Faster convergence with SOR
    • 10:17 - Curve fitting
    • 12:17 - The full MLX toolkit and ecosystem
    • 13:47 - Next steps

    资源

    • MLX Swift LM on GitHub
    • MLX Swift Examples
    • MLX Examples
    • MLX Swift
    • MLX LM - Python API
    • MLX Explore - Python API
    • MLX Framework
    • MLX
      • 高清视频
      • 标清视频

    相关视频

    WWDC26

    • 使用 MLX 在 Mac 上本地运行 AI 智能体
    • 使用 MLX 探索分布式推理和训练

    WWDC25

    • 借助 MLX 在 Apple 芯片上探索大语言模型
    • 开始使用适用于 Apple 芯片的 MLX
  • 搜索此视频…
    • 3:04 - Power iteration with MLX Swift arrays

      import MLX
      let n = 100
      let steps = 10
      let B = MLXRandom.normal([n, n])
      var v = MLXRandom.normal([n])
      
      // get symmetric matrix A = Bᵀ + B
      let A = B.T + B
      
      // Power iteration → top eigenvector of A.
      //   v ← A v / ‖A v‖
      for _ in 0 ..< steps {
          let Av = matmul(A, v)
          v = Av / norm(Av)
          eval(v)
      }
      
      // recover the eigenvalue.
      //   λ = vᵀ A v
      let lambda = matmul(matmul(v.T, A), v)
      
      print(lambda)
    • 5:09 - Mandelbrot set in plain Swift (scalar)

      // Plain Swift, scalar-at-a-time
      var counts = Array2D<Int>(width: w, height: h)
      
      for y in 0 ..< h {
          for x in 0 ..< w {
              let c = Complex(xMin + Float(x) * xStep, yMin + Float(y) * yStep)
              var z = Complex<Float>.zero
              var limit = maxIterations
              for i in 0 ..< maxIterations {
                  z = z * z + c
                  if z.lengthSquared > radiusSquared {
                      limit = i
                      break
                  }
              }
              counts[x, y] = limit
          }
      }
    • 5:27 - Mandelbrot set in MLX Swift (array)

      // Compute the Mandelbrot set on a grid of complex numbers
      import MLX
      
      let x = linspace(-2.0, 0.5, count: w)
      let y = linspace(-1.25, 1.25, count: h).reshaped(h, 1)
      let c = x + y.asImaginary()
      
      var z = MLXArray.zeros(like: c)
      var counts = MLXArray.zeros(c.shape, dtype: .int16)
      
      for _ in 0 ..< maxIterations {
          z = z * z + c                       // iterate z ← z² + c
          counts = counts + (abs(z) .< 2)     // count bounded iterations
      }
    • 7:27 - Jacobi iteration with conv2d

      // Jacobi iteration: average the four neighbors
      
      // Convolution weights
      let kernel = MLXArray(converting: [
          0,    0.25, 0,
          0.25, 0,    0.25,
          0,    0.25, 0,
      ]).reshaped(1, 3, 3, 1)
      
      // Initial value
      var temperature = heatSources
      
      // Run this in a loop until convergence
      let next = conv2d(temperature, kernel, padding: 1)
      temperature = which(heatMask, heatSources, next)
    • 9:17 - Successive Over-Relaxation (SOR)

      // Successive Over-Relaxation: blend the previous and next state
      let ω: Float = 2.0 / (1.0 + sin(Float.pi / Float(max(M, N))))
      
      let redMask   = checkerboard(rows: M, cols: N, phase: 0)
      let blackMask = checkerboard(rows: M, cols: N, phase: 1)
      
      // Update red cells using black neighbors
      let sorRed  = ω * conv2d(temperature, kernel, padding: 1) + (1 - ω) * temperature
      temperature = which(redMask, sorRed, temperature)
      temperature = which(heatMask, heatSources, temperature)
      
      // Update black cells using (now-updated) red neighbors
      let sorBlack = ω * conv2d(temperature, kernel, padding: 1) + (1 - ω) * temperature
      temperature  = which(blackMask, sorBlack, temperature)
      temperature  = which(heatMask, heatSources, temperature)
    • 11:13 - Curve fitting with automatic differentiation

      // Define a loss, then optimize it with autodiff
      // x, y: data points as MLXArrays
      func f(_ θ: MLXArray) -> MLXArray {
          θ[0] + θ[1] * x + θ[2] * x ** 2
      }
      
      func loss(_ θ: MLXArray) -> MLXArray {
          mean((f(θ) - y) ** 2)
      }
      
      var θ = zeros([numParams])
      let gradLoss = grad(loss)
      
      for _ in 0 ..< steps {
          let g = gradLoss(θ)         // ∇L(θ)
          θ = θ - learningRate * g    // parameter update
          eval(θ)                     // force evaluation
      }
    • 0:00 - Introduction
    • What numerical computing is and its applications — from simulations in chemistry, biology, and physics to signal processing, rendering, fractals, and machine learning training via gradient descent.

    • 0:57 - MLX Swift and the Apple ecosystem
    • Where MLX Swift fits among Apple's existing numerical computing frameworks (Accelerate, BNNS, Metal Performance Shaders, Swift Numerics) — and why to choose MLX Swift when your primary goal is writing mathematical code that looks like the math, with automatic GPU execution and automatic differentiation.

    • 3:04 - MLX Swift
    • Core MLX Swift concepts — n-dimensional arrays as the central abstraction (similar to NumPy), lazy evaluation that builds a compute graph before executing, and a walkthrough of the power iteration algorithm showing how operations like matmul, norm, and transpose map directly to mathematical notation.

    • 4:28 - Mandelbrot
    • Computing the Mandelbrot fractal as a showcase for array computing — comparing a scalar-at-a-time plain Swift implementation against an MLX Swift version that applies z = z² + c across the entire grid of complex numbers at once, running on the GPU with up to 10x speedup in fewer lines of code.

    • 6:34 - Heat distribution
    • Finding steady-state temperature in a room using Jacobi iteration — modeling heat as a 2D grid, implementing the four-neighbor stencil as a single conv2d call, and applying boundary conditions with an elementwise ternary. A convolution as physics.

    • 8:12 - Faster convergence with SOR
    • How Successive Over-Relaxation (SOR) reaches steady state in N iterations versus N² for Jacobi — using an omega parameter to overshoot updates, a red/black checkerboard pattern to simulate in-place updates, and a side-by-side comparison showing SOR completing 100x faster with the same minimal code.

    • 10:17 - Curve fitting
    • How automatic differentiation flips forward computation — given data points and a parametric function (a quadratic), using MLX's grad to derive gradients automatically and running a gradient descent loop to fit the curve without writing a single derivative by hand.

    • 12:17 - The full MLX toolkit and ecosystem
    • Overview of MLX's complete numerical computing toolkit — linear algebra, FFTs, n-dimensional convolutions, reductions, scans, indexing, random number generation, optimizers (SGD, Adam, RMSprop) — and the Swift ecosystem of packages including mlx-swift, mlx-swift-lm, and mlx-swift-examples.

    • 13:47 - Next steps
    • How to get started with mlx-swift and mlx-swift-examples (LLM integration, stable diffusion, model training, and session examples), MLX's multi-language support (Swift, Python, C++, C).

Developer Footer

  • 视频
  • WWDC26
  • 使用 MLX 探索 Swift 中的数值计算
  • 打开菜单 关闭菜单
    • iOS
    • iPadOS
    • macOS
    • Apple tvOS
    • visionOS
    • watchOS
    打开菜单 关闭菜单
    • Swift
    • SwiftUI
    • Swift Playground
    • TestFlight
    • Xcode
    • Xcode Cloud
    • SF Symbols
    打开菜单 关闭菜单
    • 辅助功能
    • 配件
    • Apple 智能
    • App 扩展
    • App Store
    • 音频与视频 (英文)
    • 增强现实
    • 设计
    • 分发
    • 教育
    • 字体 (英文)
    • 游戏
    • 健康与健身
    • App 内购买项目
    • 本地化
    • 地图与位置
    • 机器学习与 AI
    • 开源资源 (英文)
    • 安全性
    • Safari 浏览器与网页 (英文)
    打开菜单 关闭菜单
    • 完整文档 (英文)
    • 部分主题文档 (简体中文)
    • 教程
    • 下载
    • 论坛 (英文)
    • 视频
    打开菜单 关闭菜单
    • 支持文档
    • 联系我们
    • 错误报告
    • 系统状态 (英文)
    打开菜单 关闭菜单
    • Apple 开发者
    • App Store Connect
    • 证书、标识符和描述文件 (英文)
    • 反馈助理
    打开菜单 关闭菜单
    • Apple Developer Program
    • Apple Developer Enterprise Program
    • App Store Small Business Program
    • MFi Program (英文)
    • Mini Apps Partner Program
    • News Partner Program (英文)
    • Video Partner Program (英文)
    • 安全赏金计划 (英文)
    • Security Research Device Program (英文)
    打开菜单 关闭菜单
    • 与 Apple 会面交流
    • Apple Developer Center
    • App Store 大奖 (英文)
    • Apple 设计大奖
    • Apple Developer Academies (英文)
    • WWDC
    阅读最近新闻。
    获取 Apple Developer App。
    版权所有 © 2026 Apple Inc. 保留所有权利。
    使用条款 隐私政策 协议和准则