Core AI:在 Apple silicon 上运行模型
Apple 的设备端 AI 栈一直缺了一级台阶。Foundation Models 为你提供封装好且免费的系统 LLM。Core ML 运行一个固定的转换后模型,由转换器替你做出硬件决策。MLX 提供一个供你嵌入的数组框架和一个由你选择的模型。iOS 27 在这三者之下补上了那一级台阶:Core AI,一个一句话概要为“在你的 app 中于 Apple silicon 上运行 AI 模型”的框架。1它是模型执行层面,是当你想要亲自掌控专属化、缓存与推理调度,而非接受更高层默认设置时所触及的地方。
在第 324 场 session 中,Apple 将 Core AI 定位为驱动设备端 Apple Intelligence 的同一个推理框架,如今开放给你自己 app 的智能能力使用。15
这个定位之所以重要,是因为 Core AI 位于多数 app 应当使用的抽象层之下。Apple 将其描述为以 Apple silicon 为前提设计,让你的 app 能跨 CPU、GPU 与 Neural Engine 使用最新的模型架构与推理技术,并配以一套 Swift API:让常见任务简单易行,同时在需要时给你更多对模型专属化、缓存与推理性能的掌控。1本文的主旨是:当你有一个模型,想要明确掌控它在何处、如何执行时,去选择 Core AI;当你不需要这种掌控时,则停留在 Core ML 或 Foundation Models。这个框架回报的是一种具体需求,而非默认偏好。
TL;DR / 关键要点
- Core AI 将未专属化的
AIModelAsset(廉价地检视模型的结构与元数据)与已专属化的AIModel(在设备上运行推理)分开,其中AIModelCache保存设备专属的产物,AssetError表示 asset 操作的失败。2364 - 推理数据流经
NDArray,即一个由标量值组成的多维数组,由NDArrayDescriptor描述,后者固定形状、标量类型与内存布局的预期。57 - 你通过
SpecializationOptions用ComputeUnitKind(CPU、GPU 或 Neural Engine)来定向硬件,并将异步工作调度到ComputeStream上。8910 InferenceFunction拥有权重与缓冲区并运行推理;InferenceFunctionDescriptor让你先检视它的输入、输出与状态签名。该函数是Sendable的,因此你可以并发运行它。1413- 模型从磁盘上的
.aimodelbundle 加载,Core AI 在框架之外还附带了准备、转换与调试工具。当你需要对专属化与调度的明确掌控时,去选择 Core AI;否则停留在 Core ML 或 Foundation Models。21
贯穿整个设计的两个词:Asset 与 Model
Core AI 要你领会的第一件事,是磁盘上的模型与运行推理的模型是不同的对象,而将前者专属化为后者代价高昂。这个框架为各自赋予了一个类型。
AIModelAsset 是“一个未专属化的源模型 asset”。2你从磁盘上一个 .aimodel bundle 的 URL 创建它,并用它来检视模型,而无需付出专属化的代价。Apple 明确说明了这一区分存在的原因:模型 asset 让你能够查询模型信息而无需执行专属化——后者是一项昂贵的操作。通过 asset,你可以读取函数签名、输入与输出描述、计算与存储类型,以及作者提供的元数据。你不能做的是运行推理;asset 仅用于检视。2
// Call shape is illustrative; confirm the exact initializer against Apple's docs.
let asset = try AIModelAsset(url: bundleURL) // an .aimodel bundle on disk
// Inspect signatures, input/output descriptions, compute and storage types,
// and author-provided metadata — without specializing.
AIModel 是另一半:“一个用于在设备上运行推理的专属化模型”。3一个 AIModel 代表一个为当前设备硬件优化过的专属化 .aimodel asset,你通过从磁盘加载该 asset 来创建它。3asset 回答这是什么模型?;model 回答此时此地运行它。两者之间的代价不对称,正是 API 强迫你指明你想要哪一个的原因。如果你只构建 asset,检视一百个候选模型以挑选其一是廉价的;但若每次检视都进行专属化,那将是灾难性的。
专属化会产生设备专属的产物,而这些产物有个归处:AIModelCache,“一个为推理存储专属化模型产物的缓存”。6该缓存保存模型为执行其推理函数而加载的、经过优化的设备专属产物,Apple 指出每个缓存条目都包含一个由特定 .aimodel 或 .aimodelc 与某个专属化组合所构成的专属化 asset。6实际的理解是:专属化不是你想在每次启动时都重复的事情。缓存正是 Core AI 让昂贵步骤只发生一次、此后只发生廉价步骤(加载已缓存产物)的方式。
当 asset 操作出错时(bundle 缺失、.aimodel 格式错误、文件不可读),Core AI 会呈现一个 AssetError,“一个在模型 asset 操作期间发生的错误”。4像对待任何 I/O 边界那样对待它:asset 存在于磁盘上,磁盘操作会失败,而类型系统会准确告诉你该把 catch 放在哪里。
张量:NDArray 及其 Descriptor
推理把数字搬进来又搬出去,而 Core AI 装载这些数字的容器是 NDArray,“一个用于模型推理的标量值多维数组”。5如果你用过 NumPy 的 ndarray、MLX 数组或 MLMultiArray,这个概念的形态就很熟悉:一个具有既定布局的 n 维标量块。NDArray 以一种由其形状及其余描述性属性所定义的布局来存储数据。5
与之配套的类型是 NDArrayDescriptor,“一个对数组形状、标量类型与内存布局预期的描述”。7descriptor 就是契约。Apple 的表述很直接:descriptor 包含你提供给推理函数的数组值的各项预期,且多数预期是严格的。如果 descriptor 指定标量类型为 .float32,那么你提供的数组就必须使用 .float32。7你不必去猜某个函数想要的形状与类型;你向该函数的 descriptor 询问并遵从它即可。
// Call shape is illustrative; confirm exact property/method names against Apple's docs.
let inputDescriptor = function.descriptor.inputs.first! // an NDArrayDescriptor
// The descriptor fixes shape, scalar type, and layout; the array you build
// must satisfy those expectations (e.g. .float32 means .float32).
这里的设计启示与 asset/model 的区分如出一辙。Core AI 始终在一个昂贵的值对象之前摆上一个廉价的描述对象。你读取 descriptor 以了解契约,然后分配满足它的 NDArray,而非先行分配、再在推理时发现不匹配。专门针对图像输入,Core AI 还定义了 ImageDescriptor,“一个对图像尺寸与像素格式的描述”,使得视觉模型的像素输入也得到同样的“descriptor 优先”待遇。11
选择推理在何处运行
Apple silicon 有三处可供计算:CPU、GPU 与 Neural Engine。Core AI 之所以存在、而不止有 Core ML,原因在于 Core AI 让你能够指明框架定向其中哪一处,而非由它来推断。
ComputeUnitKind 是“一种可用于模型推理的硬件计算单元类型”。8你将计算单元类型与专属化选项配合使用,以控制框架在专属化模型时定向哪种硬件;默认情况下,专属化会使用设备上所有可用的计算单元。8默认值对多数工作而言就是正确答案,这正是关键所在:你只在有理由时才覆盖它(一条对延迟敏感、你想钉到 Neural Engine 上的路径;一次你想强制放到 CPU 上的调试过程;一条你正与其他 GPU 工作协调的 GPU 重负载流水线)。
你通过 SpecializationOptions 传递这一意图,它是承载专属化时所做选择的结构体。9专属化就是前文那个昂贵的步骤,而 SpecializationOptions 正是计算单元定向及其他专属化决策所在之处。由于缓存条目以特定 asset 与专属化组合作为键,改变你的选项就会改变你取回的是哪个已缓存产物,从而在定向与缓存之间形成闭环。6
调度是“如何运行”的另一个维度,Core AI 将其建模为 ComputeStream,“一个待异步运行的工作流”。10你提供一个计算流以将工作编码到流上,Apple 指出,编码到同一个流上的多次推理会根据所读写的值按需被串行化。10由此引出两个推论。其一,流是你的排序原语:把相互依赖的推理编码到一个流上,Core AI 便会按数据依赖关系为它们排序。其二,工作默认是异步的,因此流也是你在 Neural Engine 或 GPU 干活时让调用线程保持空闲的方式。
推理函数:真正运行的那个东西
一个加载好的 .aimodel 并不是单一可调用对象。模型会暴露具名函数(一个编码器、一个解码器、一个视觉塔、一个 prefill 步骤对一个 decode 步骤),而 Core AI 的执行单元是 InferenceFunction:“一个对输入值执行推理并产生输出值的函数”。14
在调用它之前,你先检视它。InferenceFunctionDescriptor 是“一个对推理函数签名的描述”,你用 descriptor 在运行推理之前检视一个函数的输入、输出与状态的名称和类型。13状态是值得停下来看一眼的细节:带状态的函数正是一个有状态模型(例如 transformer 解码循环中的 KV 缓存)在多次调用之间保留信息的方式,而 descriptor 会在你尝试驱动它之前告诉你某个函数带有状态。
InferenceFunction 本身拥有推理所需的资源,包括模型权重与中间缓冲区。你从一个模型加载一个函数,并调用 run(inputs:states:outputViews:) 来执行推理。14run 的签名在 Apple 自己的讨论中被点名,因此一次调用所需的三样东西是明确的:输入值、状态值,以及你想要写入的输出视图。
// run(inputs:states:outputViews:) is named in Apple's docs; surrounding
// loading/value-construction shapes are illustrative — confirm against Apple's docs.
let function: InferenceFunction = /* load from an AIModel */
let outputs = try function.run(
inputs: inputValues, // InferenceValue per input
states: stateValues, // any stateful values the function declares
outputViews: outputViews
)
有两个属性让这个函数在负载之下用起来很舒服。它是 Sendable 的,因此你可以从多个任务中并发运行它,Apple 指出它会按需自动分配额外的中间缓冲区以支持这种并发。14你无需为保护共享暂存空间而把调用排在一把锁后面串行化;该函数会为每个并发调用方管理自己的缓冲区。这与那些单一推理句柄实际上只能单线程使用的 API 相比,是个有意义的差别。
流经 run 的值是 InferenceValue 实例,“一个推理函数作为输入接受或作为输出产生的值”。12一个 InferenceValue 包装的要么是一个 NDArray,要么是一个像素缓冲区,你在推理后通过它的 value 属性取回结果。12正是这层包装,让单一的 run 签名既能承载张量输入又能承载图像输入而无需各自的重载:文本模型传入由 NDArray 支撑的值,视觉模型传入由像素缓冲区支撑的值,而函数会读取 descriptor 以得知它预期的是哪一种。
何时该去选择 Core AI
Core AI 最难的部分不是 API。难的是判断你究竟是否应该在这一层,而非更高的一层。诚实的决策树如下:
- Foundation Models,当 Apple 的系统模型能完成该任务时。摘要、分类、抽取、改写、结构化输出:这些属于 Foundation Models,它不耗费你的权重、内存预算,也没有专属化步骤。如果你的功能能契合,到此为止。降到 Core AI 去重新实现系统模型已经做到的事,是白费功夫。
- Core ML,当你有一个固定的、转换后的模型,并想让转换器替你做出硬件与优化决策时。Core ML 为一个锁定的生产模型定向 Neural Engine,兼顾紧凑的功耗与延迟,且不要求你过问专属化或调度。如果你不想去操心计算单元定向或计算流,那就是停留在 Core ML 的信号。
- MLX,当你想要一个供你嵌入并迭代的研究级数组框架时:你自己的训练循环、量化的开源权重模型、LoRA 微调、快速实验。MLX 是一个你随权重一起发布的库,而非一个系统级的模型执行层。它的胜出之处在于灵活性与迭代速度。
- Core AI,当你有一个要运行的模型,并想要这个框架明确的把手时:一个你在投入之前先检视的
AIModelAsset、一组钉住计算单元的SpecializationOptions、一个由你管理的AIModelCache、一个你调度到其上的ComputeStream,以及一批你并发调用的InferenceFunction。当更高层的默认设置成了挡在你面前的东西,而你又能说出自己需要覆盖哪一项默认时,你便会触及这一层。
贯穿整个栈的主线是:每往下一层,便用一个默认值换取一个把手。Foundation Models 把一切都交给你、什么也不要求你。Core AI 把操纵杆交给你、要求你知道该拉哪一根。如果你说不出自己需要哪一项专属化、缓存或调度的掌控,那你就还不需要 Core AI。
一份 WWDC 2026 lab 的说法,让 Core AI 与 Core ML 在新工作上的界线更加清晰。以下转述自一段本地转录的 WWDC 2026 Coding Intelligence, Machine Learning & AI Group Lab 录音:panel 上一位 Core AI 工程师表示,Apple 正要求所有从事神经网络工作的人今后转向 Core AI,Core ML 则保留下来,但聚焦于决策树之类的传统机器学习,而一切新东西都朝 Core AI 走。16请把它当作来自框架构建者的方向性信号,而非有文档依据的政策:如果你在一个新项目上要用神经网络,这场 lab 把 Core AI 框定为可供构建的那个层面。
一个模型如何抵达 Core AI
这个框架是一个更大工作流的运行时那一半。Apple 指出,Core AI 在框架之外还包含用于模型准备、集成与调试的额外工具:你为 Apple silicon 准备你的模型,把它们转换为 .aimodel 格式,并使用一个支持可视化与数值调试的配套 app。1事实简表对这些工具名称的描述被截断了,因此请对照 Apple 的 Core AI 文档确认确切的工具名称及其调用方式,而非信任任何拼凑出来的内容。1已得到核实的是流水线的形态:源模型经过准备、转换为 .aimodel、作为 AIModelAsset 加载以供检视、专属化为 AIModel,并通过其 InferenceFunction 运行,由 AIModelCache 保留专属化产物,从而让那个昂贵的步骤只发生一次。123614
FAQ
Apple 的 Core AI 框架是什么?
Core AI 是 iOS 27 用于在 Apple silicon 上运行 AI 模型的底层框架,Apple 将其概括为“在你的 app 中于 Apple silicon 上运行 AI 模型”。1它通过一套 Swift API 跨 CPU、GPU 与 Neural Engine 运行模型推理,让常见任务简单易行,同时在你需要时给你对模型专属化、缓存与推理性能的掌控。1作为一个模型执行层面,它位于 Foundation Models 与 Core ML 之下。
AIModelAsset 与 AIModel 有什么区别?
AIModelAsset 是一个未专属化的源 asset,你从磁盘上某个 .aimodel bundle 的 URL 创建它;你用它来检视一个模型的函数签名、输入与输出描述、计算与存储类型,以及元数据,而无需专属化——因为专属化代价高昂,而且 asset 不能运行推理。2AIModel 是为当前设备硬件优化、确实运行推理的专属化模型;你通过从磁盘加载该 asset 来创建它。3这一区分让你能够廉价地检视,并只在投入时才进行专属化。
Core AI 如何在 CPU、GPU 与 Neural Engine 之间做选择?
你通过 SpecializationOptions 用 ComputeUnitKind 来控制硬件定向。计算单元类型命名了一种可用于推理的硬件计算单元,你用它来控制框架在专属化模型时定向哪种硬件;默认情况下,专属化会使用设备上所有可用的计算单元。89只有在你有具体理由时——例如把一条对延迟敏感的路径钉到某一个计算单元上——你才覆盖默认值。
InferenceFunction 是什么,我该如何运行它?
InferenceFunction 对输入值执行推理并产生输出值,拥有模型权重与中间缓冲区。14你先通过 InferenceFunctionDescriptor 检视它的签名,后者描述该函数输入、输出与状态的名称和类型,然后从一个 AIModel 加载该函数并调用 run(inputs:states:outputViews:)。1314该函数是 Sendable 的,并会自动分配中间缓冲区以支持并发,因此多个任务可以同时运行它。14
我应该用 Core AI 来取代 Core ML 或 Foundation Models 吗?
当系统模型能完成任务时用 Foundation Models,当你有一个固定的转换后模型、并想让转换器替你做出硬件与优化决策时用 Core ML。当你想要对那些更高层替你处理的专属化(SpecializationOptions、ComputeUnitKind)、缓存(AIModelCache)与调度(ComputeStream)拥有明确掌控时,去选择 Core AI。89610如果你说不出自己需要哪项掌控,那就停留在上一层。
完整的 Apple Ecosystem 系列:MLX on Apple Silicon 介绍当你想要自己的模型与训练循环时所嵌入的数组框架;Apple Silicon’s TBDR and unified memory 介绍让 CPU/GPU/Neural Engine 共享成为可能的硬件底座;Core ML on-device inference 介绍 Core AI 之上的固定模型层;Foundation Models 则介绍位于栈顶、Apple 封装好的系统 LLM。枢纽位于 Apple Ecosystem Series。若想了解更广泛的 iOS 配合 AI agent 的背景,参见 iOS Agent Development guide。
References
-
Apple Developer Documentation: Core AI (iOS 27.0 beta). “Run AI models in your app on Apple silicon.” Core AI runs the latest model architectures and inference techniques across the CPU, GPU, and Neural Engine, with a Swift API that gives control over specialization, caching, and inference performance; it includes additional tools for model preparation, conversion to
.aimodel, integration, and debugging. ↩↩↩↩↩↩↩↩ -
Apple Developer Documentation:
AIModelAsset(iOS 27.0 beta). “An unspecialized source model asset.” Created from the URL of an.aimodelbundle on disk; used to inspect a model’s structure and metadata (function signatures, input/output descriptions, compute and storage types, author-provided metadata) without performing the expensive specialization step. It cannot perform inference. ↩↩↩↩↩↩ -
Apple Developer Documentation:
AIModel(iOS 27.0 beta). “A specialized model for running inference on a device.” Represents a specialized.aimodelasset optimized for the current device’s hardware; you create one by loading the asset from disk. ↩↩↩↩↩ -
Apple Developer Documentation:
AssetError(iOS 27.0 beta). “An error that occurs during model asset operations.” Declared asstruct AssetError. ↩↩ -
Apple Developer Documentation:
NDArray(iOS 27.0 beta). “A multidimensional array of scalar values used for model inference.” Stores data in a layout defined by its descriptive properties. Declared asstruct NDArray. ↩↩↩ -
Apple Developer Documentation:
AIModelCache(iOS 27.0 beta). “A cache that stores the specialized model artifacts for inference.” Holds the optimized, device-specific artifacts a model loads to execute its inference functions; each entry is a specialized asset formed from a specific.aimodelor.aimodelcand specialization combination. Declared asfinal class AIModelCache. ↩↩↩↩↩↩ -
Apple Developer Documentation:
NDArrayDescriptor(iOS 27.0 beta). “A description of an array’s shape, scalar type, and memory layout expectations.” Contains the expectations for an array value provided to an inference function; most expectations are strict (a.float32scalar type requires a.float32array). Declared asstruct NDArrayDescriptor. ↩↩↩ -
Apple Developer Documentation:
ComputeUnitKind(iOS 27.0 beta). “A type of hardware compute unit available for model inference.” Used with the specialization options to control which hardware the framework targets when specializing a model; by default specialization uses all available compute units on the device. Declared asenum ComputeUnitKind. ↩↩↩↩↩ -
Apple Developer Documentation:
SpecializationOptions(iOS 27.0 beta). The structure carrying the choices made at specialization time, including compute-unit targeting viaComputeUnitKind. Declared asstruct SpecializationOptions. ↩↩↩↩ -
Apple Developer Documentation:
ComputeStream(iOS 27.0 beta). “A stream of work to be run asynchronously.” Work is encoded onto the stream; multiple inferences encoded to the same stream are serialized as needed based on the values read and written. Declared asfinal class ComputeStream. ↩↩↩↩ -
Apple Developer Documentation:
ImageDescriptor(iOS 27.0 beta). “A description of an image’s dimensions and pixel format.” Declared asstruct ImageDescriptor. ↩ -
Apple Developer Documentation:
InferenceValue(iOS 27.0 beta). “A value that an inference function accepts as input or produces as output.” Wraps either anNDArrayor a pixel buffer; retrieved after inference using its value property. Declared asstruct InferenceValue. ↩↩ -
Apple Developer Documentation:
InferenceFunctionDescriptor(iOS 27.0 beta). “A description of an inference function’s signature.” Used to inspect the names and types of a function’s inputs, outputs, and states before running inference. Declared asstruct InferenceFunctionDescriptor. ↩↩↩ -
Apple Developer Documentation:
InferenceFunction(iOS 27.0 beta). “A function that performs inference on input values and produces output values.” Owns the resources needed for inference, including model weights and intermediate buffers; loaded from anAIModeland called viarun(inputs:states:outputViews:). It isSendableand automatically allocates additional intermediate buffers to support concurrent execution. Declared asstruct InferenceFunction. ↩↩↩↩↩↩↩↩ -
Apple, WWDC26 session 324, Meet Core AI. Apple states Core AI “is the inference framework powering on-device Apple Intelligence” and “now, it’s available for you to use, bringing that same power to your app’s own intelligence.” ↩
-
Apple, WWDC 2026 lab 8121, Coding Intelligence, Machine Learning & AI Group Lab. Paraphrased from a locally transcribed recording of the WWDC 2026 Coding Intelligence, Machine Learning & AI Group Lab; Apple published no captions for the labs, so the wording here is a paraphrase, not a quotation. A Core AI engineer on the panel said Apple is asking everyone working with neural networks to use Core AI going forward, with Core ML remaining in place but focused on traditional machine learning such as decision trees, and everything new moving to Core AI. ↩