Skip to Content
💡WebNN Origin Trial 即将到来!开发者很快将可以注册试用密钥进行体验 →
API 参考Browser CompatibilityWebNN API

Browser Compatibility

WebNN APIs

2025 年 5 月 20 日:DirectML 在 Microsoft Build 2025 上正式被弃用。WebNN 通过 Windows ML 访问 OpenVINO 等执行提供程序(EP)来获得硬件加速。Chromium WebNN 服务中已不再包含 DirectML 后端(dml/);DirectML 现在只能作为 ONNX Runtime 的 EP 被使用。

平台 / 构建条件CPU (device: "cpu")GPU (device: "gpu")NPU (device: "npu")
Windows 11 24H2+,且启用特性 kWebNNOnnxRuntime(默认开启)ONNX Runtime(Windows ML):默认 CPU EP,如有同厂商 CPU EP 则一并加入(ort/environment.cc:SelectEpDevicesForCpu)ONNX Runtime:选取第一个 GPU EP 设备(独立显卡优先于集成显卡,WebGPU EP 优先于 DirectML EP),并以 CPU EP 作为回退;没有 GPU 或仅有 WARP 等软件适配器时回退到 CPU(SelectEpDevicesForGpu)ONNX Runtime:选取第一个 NPU EP 设备并以 CPU EP 作为回退;没有 NPU 或 NPU 驱动低于该 EP 要求的最低版本时,回退到 GPU 的选择逻辑(SelectEpDevicesForNpu)
Windows 11 24H2 之前的版本、Windows App Runtime 不可用、kWebNNOnnxRuntime 被禁用或 ORT 环境创建失败渲染进程内的 LiteRT + XNNPACK(webnn_context_provider_impl.cc:FallbackToTFLite)GPU 进程中的 LiteRT GPU 加速器(WebGPU),不可用时使用进程内 LiteRT(tflite/graph_impl_litert.cc)进程内 LiteRT;暂不支持 NPU 加速器(crbug.com/454732289)
macOS ≥14.4、Apple Silicon、非无痕模式,且启用特性 kWebNNCoreML(默认开启)Core ML,使用 MLComputeUnitsCPUOnly(coreml/graph_impl_coreml.mm)Core ML,使用 MLComputeUnitsAll;若开启 kWebNNCoreMLExplicitGPUOrNPU(默认关闭)则使用 MLComputeUnitsCPUAndGPUCore ML,使用 MLComputeUnitsAll;若开启 kWebNNCoreMLExplicitGPUOrNPU 则使用 MLComputeUnitsCPUAndNeuralEngine
macOS Intel、macOS <14.4、无痕模式或 Core ML 被禁用LiteRT + XNNPACK(进程内)LiteRT GPU 加速器(WebGPU),随后回退到进程内进程内 LiteRT
iOSkWebNNCoreML 默认关闭(Core ML 沙箱问题,crbug.com/417496826),因此使用 LiteRT + XNNPACKLiteRT GPU 加速器,随后回退到进程内进程内 LiteRT
ChromeOS、Linux、Android渲染进程内的 LiteRT + XNNPACK可用时使用 GPU 进程中的 LiteRT GPU 加速器(WebGPU/Dawn);否则使用进程内 LiteRT进程内 LiteRT;暂不支持 NPU 加速器
  • 后端选择由 WebNNContextProviderImpl::CreateWebNNContext(webnn_context_provider_impl.cc)实现:Windows 优先尝试 ONNX Runtime,Apple 平台尝试 Core ML,其余平台(或任何一次尝试失败时)进入 FallbackToTFLite,使用 LiteRT。
  • features.gni 在 Android、ChromeOS、Linux、Apple 和 Windows 上启用 TFLite/LiteRT(webnn_use_tflite、webnn_use_litert)。webnn_use_chrome_ml_api(enable_ml_internal)仅是构建开关;本页早期版本中描述的 OpenCL 和 NNAPI 委托路径不在当前源码中。
  • LiteRT 的路由(ShouldUseInProcessTflite):cpu 和 npu 请求在渲染进程中处理;gpu 请求会先尝试在 GPU 进程中使用 WebGPU 加速器(在沙箱锁定前预加载)创建 LiteRT 上下文,若不支持,则由渲染进程创建进程内上下文。开启 kWebNNLiteRTGpuInRenderer(默认关闭)后,GPU 请求在渲染进程中运行。
  • 只要构建时包含 XNNPACK(BUILD_LITERT_WITH_XNNPACK),LiteRT 就会启用 XNNPACK CPU 加速器,因此 GPU 请求始终保留 CPU 作为回退。
  • ONNX Runtime 需要 Windows 11 24H2 或更高版本(kWinAppRuntimeSupportedMinVersion)以及 Windows App Runtime;使用 --webnn-ort-ignore-ihv-eps 或 Windows 版本较低时,仅使用内置 EP。--webnn-ort-disable-cpu-fallback 会禁用 CPU EP 回退。
  • kWebNNCompilerProcess(Apple 默认开启,Windows 默认关闭)会把图编译移到独立的 Compiler 进程中。
  • 仅当设备上存在相应计算单元时,Core ML 才会提供 gpu/npu(coreml/graph_impl_coreml.mm)。

Note

  • The WebNN API mainly supported with Chromium-based browsers on ChromeOS, Linux, macOS, Windows and Android.
  • Chromium-based browsers include but are not limited to Google Chrome, Microsoft Edge, Opera, Vivaldi, Brave, Samsung Internet etc.
InterfaceMethodChromium Version
navigator.mlM112
MLM112
MLcreateContext()M112
MLContextM112
MLContextdispatch()M128
MLContextcreateTensor()M129
MLContextreadTensor(tensor)M129
MLContextreadTensor(tensor, outputData)M129
MLContextwriteTensor()M129
MLContextopSupportLimits()M128
MLGraphM112
MLOperandM112
MLOperandMLNumberM132
MLTensorM124
MLTensordestroy()M124
MLGraphBuilderM112
MLGraphBuilderMLGraphBuilder() constructorM112
MLGraphBuilderinput(name, descriptor)M112
MLGraphBuilderconstant(descriptor, buffer)M112
MLGraphBuilderconstant(type, value)M112
MLGraphBuilderbuild(outputs)M112
Last updated on