安装 Windows ML 执行提供程序

借助 Windows ML,某些执行提供程序(EP)通过 Windows ML ExecutionProviderCatalog API 动态下载、安装和共享系统范围,并 自动更新。 若要查看可用的 EP,请参阅 Windows ML 执行提供程序。

本页介绍如何使用 EP 目录将 Windows ML EP 安装到用户的设备上。 有关直接在应用中捆绑 EP 的替代方法,请参阅 “自带 EP”。 若要在这些选项之间进行选择,请参阅 Windows ML 执行提供程序与自选方案。

安装后,需要使用 ONNX 运行时注册 Windows ML EPs,然后才能使用这些程序。

注释

当你的应用调用 EnsureReadyAsync() 或 EnsureAndRegisterCertifiedAsync() 时,Windows 会下载 Windows ML EP。 对于离线、受限网络、受管理或严格固定版本的环境,请参阅 自带 EP 和 Windows ML EP 与自带 EP 的对比。

安装所有兼容的 EP

最简单的做法是调用 EnsureAndRegisterCertifiedAsync(),它会下载并安装用户设备上可用的所有 EP,然后通过一次调用将它们全部注册到 ONNX Runtime 中。 如果你的应用需要对提供程序的发现、下载或注册进行更多控制,请参阅下面的 查找所有兼容的 EP 和 安装特定的 EP。

注释

首次运行时,该方法可能需要几秒钟,甚至几分钟,具体取决于网络速度以及需要下载的 EPs。

// Get the default ExecutionProviderCatalog
var catalog = ExecutionProviderCatalog.GetDefault();

// Ensure execution providers compatible with device are present (downloads if necessary)
// and then registers all present execution providers with ONNX Runtime
await catalog.EnsureAndRegisterCertifiedAsync();

查找所有兼容的 EP

可以通过调用 FindAllProviders() 方法来查看哪些 EP(包括未安装的 EP)可供用户的设备使用。

ExecutionProviderCatalog catalog = ExecutionProviderCatalog.GetDefault();

// Find all available EPs (including non-installed EPs)
ExecutionProvider[] providers = catalog.FindAllProviders();

foreach (var provider in providers)
{
    Console.WriteLine($"{provider.Name}: {provider.ReadyState}");
}

返回的执行提供程序因用户的设备和可用的执行提供程序而异。 在当前未安装任何执行提供程序的兼容 Qualcomm 设备上,上述代码输出以下代码...

QNNExecutionProvider: NotPresent

每个 ExecutionProvider 都有一个 ReadyState 属性,指示其在设备上的当前状态。 了解这些状态有助于确定应用需要执行的操作。

ReadyState 定义 后续步骤
NotPresent 客户端设备上未安装 EP。 调用 EnsureReadyAsync() 以下载并安装 EP,并将其添加到应用的运行时依赖项关系图。
NotReady EP 安装在客户端设备上,但尚未添加到应用的运行时依赖项关系图。 调用 EnsureReadyAsync() 以将 EP 添加到应用的运行时依赖项关系图。
Ready EP 安装在客户端设备上,并已添加到应用的运行时依赖项关系图。 调用 TryRegister() 以向 ONNX 运行时注册 EP。

安装特定的 EP

如果您的应用程序要使用特定的 ExecutionProvider,并且其 ReadyState 是 NotPresent,则可以通过调用 EnsureReadyAsync() 来下载并安装它。

首先,你将使用FindAllProviders()获取所有兼容的 EP,然后可以调用特定的EnsureReadyAsync()下载特定的执行提供程序,并调用注册特定的执行提供程序。

// Download and install a NotPresent EP
var result = await provider.EnsureReadyAsync();

// Check that the download and install was successful
bool installed = result.Status == ExecutionProviderReadyResultState.Success;

处理安装结果

EnsureReadyAsync() 返回 ExecutionProviderReadyResult。 在创建会话或注册提供程序之前检查其 状态 。 如果状态为 Failure,请检查 ExtendedError(HRESULT)和 DiagnosticText,并参阅 执行提供程序下载问题排查。 如果状态为 InProgress,则操作尚未完成;在继续操作之前等待结果。

安装进度显示

用于下载和安装 EP 的 API 包括提供进度更新的回调,以便显示进度指示器以保持用户知情。

反映下载进度的进度栏的屏幕截图

// Start the download and install of a NotPresent EP
var operation = provider.EnsureReadyAsync();

// Listen to progress callback
operation.Progress = (asyncInfo, progressInfo) =>
{
    // Dispatch to UI thread (varies based on UI platform)
    _dispatcherQueue.TryEnqueue(() =>
    {
        // progressInfo is out of 100, convert to 0-1 range
        double normalizedProgress = progressInfo / 100.0;

        // Display the progress to the user
        Progress = normalizedProgress;
    };
};

// Await for the download and install to complete
var result = await operation;

// Check that the download and install was successful
bool installed = result.Status == ExecutionProviderReadyResultState.Success;

后续步骤

安装执行提供程序后,请参阅 “注册 Windows ML EP ”,了解如何在 ONNX 运行时中注册它们以供使用。

生产应用示例

对于生产应用程序,下面是一个示例,展示你的应用可能希望如何操作,以便给你和你的用户控制下载发生时间的权力。 可以在注册之前检查新的执行提供程序是否可用并有条件地下载它们:

using Microsoft.Windows.AI.MachineLearning;

var catalog = ExecutionProviderCatalog.GetDefault();

// Filter to the EPs our app supports/uses
var providers = catalog.FindAllProviders().Where(p =>
    p.Name == "MIGraphXExecutionProvider" ||
    p.Name == "VitisAIExecutionProvider" ||
    p.Name == "OpenVINOExecutionProvider" ||
    p.Name == "QNNExecutionProvider" ||
    p.Name == "NvTensorRtRtxExecutionProvider"
);

if (providers.Any(p => p.ReadyState == ExecutionProviderReadyState.NotPresent))
{
    // Show UI to user asking if they want to download new execution providers
    bool userWantsToDownload = await ShowDownloadDialogAsync();

    if (userWantsToDownload)
    {
        // Download all EPs
        foreach (var p in providers)
        {
            if (p.ReadyState == ExecutionProviderReadyState.NotPresent)
            {
                // Ignore result handling here; production code could inspect status
                await p.EnsureReadyAsync();
            }
        }

        // And register all EPs
        await catalog.RegisterCertifiedAsync();
    }
    else
    {
        // Register only already-present EPs
        await catalog.RegisterCertifiedAsync();
    }
}

另请参阅