Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The ONNX Runtime shipped with Windows ML allows apps to configure execution providers (EPs) either based on Device Policies or explicitly, which provides more control over provider options and which devices should be used.
We recommend starting with explicit selection of EPs so that you can have more predictability in the results. After you have this working, you can experiment with using Device Policies to select execution providers in a natural, outcome-oriented way.
Explicit selection of EPs
To explicitly select an EP, use the environment's GetEpDevices function to enumerate all available devices, and select the EP devices you want to use. Then use AppendExecutionProvider (C#) or AppendExecutionProvider_V2 (C++) to append specific devices and provide custom provider options to the desired EP. You can see all of our supported EPs here.
using Microsoft.ML.OnnxRuntime;
using System;
using System.Linq;
using System.Collections.Generic;
// Assuming you've created an OrtEnv named 'ortEnv'
// 1. Enumerate devices
var epDevices = ortEnv.GetEpDevices();
// 2. Filter to your desired execution provider
var selectedEpDevices = epDevices
.Where(d => d.EpName == "ReplaceWithExecutionProvider")
.ToList();
if (selectedEpDevices.Count == 0)
{
throw new InvalidOperationException("ReplaceWithExecutionProvider is not available on this system.");
}
// 3. Configure provider-specific options (varies based on EP)
// and append the EP with the correct devices (varies based on EP)
var sessionOptions = new SessionOptions();
var epOptions = new Dictionary<string,string>{ ["provider_specific_option"] = "4" };
sessionOptions.AppendExecutionProvider(ortEnv, new[] { selectedEpDevices.First() }, epOptions);
Browse all available EPs in the supported EPs docs. For more details on EP selection, see the ONNX Runtime OrtApi documentation.
Using Device Policies for execution provider selection
In addition to explicitly selecting EPs, you can also use Device Policies, which are a natural, outcome-oriented way to specify how you want your AI workload to run. To do this, use SessionOptions.SetEpSelectionPolicy, passing in OrtExecutionProviderDevicePolicy values. There are a variety of values you can use for automatic selection, like MAX_PERFORMANCE, PREFER_NPU, MAX_EFFICIENCY, and more. See the ONNX OrtExecutionProviderDevicePolicy docs for other values you can use.
// Configure the session to select an EP and device for MAX_EFFICIENCY which typically
// will choose an NPU if available with a CPU fallback.
var sessionOptions = new SessionOptions();
sessionOptions.SetEpSelectionPolicy(ExecutionProviderDevicePolicy.MAX_EFFICIENCY);
Next steps
After selecting execution providers, you're ready to run inference on a model using ONNX Runtime!