Create a session
Once you load a LearningModel, you create a LearningModelSession, which binds the model to a device that runs and evaluates the model.
Choose a device
You can select a device when you create a session. You choose a device of type LearningModelDeviceKind:
- Default
- Let the system decide which device to use. Currently, the default device is the CPU.
- CPU
- Use the CPU, even if other devices are available.
- DirectX
- Use a DirectX hardware acceleration device, specifically the first adapter enumerated by IDXGIFactory1::EnumAdapters1.
- DirectXHighPerformance
- Same as DirectX, but will use DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE when enumerating adapters.
- DirectXMinPower
- Same as DirectX, but will use DXGI_GPU_PREFERENCE_MINIMUM_POWER when enumerating adapters.
If you don't specify a device, the system uses Default. We recommend using Default to get the flexibility of allowing the system to choose for you in the future.
The following video goes into more detail about each of the device kinds.
Advanced device creation
Windows AI supports using a device that the caller has already created. There are several options and considerations when doing this:
- CreateFromDirect3D11Device. Use this when you already have an existing IDirect3DDevice. Windows AI will use that same adapter to create a d3d12 device for its ML workloads. This is useful when you have a camera that is using a d3d11 device for VideoFrames and you want to use that same device for your LearningModelSession. In many cases it can avoid a memory copy. Note: VideoFrame tensorization is the only d3d11 workload Windows AI has. If you are not using that feature there is no advantage to sharing or creating a d3d11 device.
- CreateFromD3D12CommandQueue (native). Use this when you have a d3d12 device that you want to reuse. Windows AI will use that command queue for its ML workloads. It will also create an d3d11 device using D3D11On12CreateDevice. This is done only when needed and will be used for all d3d11 workloads such as VideoFrame tensorization. You can access this new device via the LearningModelDevice.Direct3D11Device property.
Example
The following example shows how to create a session from a model and a device:
private void CreateSession(LearningModel model, LearningModelDeviceKind kind)
{
// Create the evaluation session with the model and device
LearningModelSession session =
new LearningModelSession(model, new LearningModelDevice(kind));
}
See also
- Previous: Load a model
- Next: Bind a model
Note
Use the following resources for help with Windows ML:
- To ask or answer technical questions about Windows ML, please use the windows-machine-learning tag on Stack Overflow.
- To report a bug, please file an issue on our GitHub.