Edit

Share via


Select an execution device

With Windows ML APIs, you can select a device you prefer to evaluate your model on. You select a device when you create a session. To do so, you will use the LearningModelDevice class, which defines the device used to evaluate the machine learning model.

Here, we'll make changes in the classifier.cs file of the previous app, so we can run our model evaluation on our devices's GPU. We didn't touch that file in the previous tutorial, as it was automatically generated by WinML Code Generator.

  1. Open the classifier.cs file and add the following code into the CreateFromStreamAsync method.
// Select GPU or another DirectX device to evaluate the model.
LearningModelDevice device = new LearningModelDevice(LearningModelDeviceKind.DirectX);

To choose a different execution device, simply switch the DirectX field to a different one.

  1. Now, you can select this device when you create a session. Change the LearningModelSession constructor to specify the execution device.
// Create the evaluation session with the model and device.
learningModel.session = new LearningModelSession(learningModel.model, device);

The CreateFromStreamAsync method now as follows:

CreateFromStreamAsync(IRandomAccessStreamReference stream)
        {
            classifierModel learningModel = new classifierModel();
            learningModel.model = await LearningModel.LoadFromStreamAsync(stream);

            // Select GPU or another DirectX device to evaluate the model.
            LearningModelDevice device = new LearningModelDevice(LearningModelDeviceKind.DirectX);
            
            // Create the evaluation session with the model and device.
            learningModel.session = new LearningModelSession(learningModel.model, device);
            learningModel.binding = new LearningModelBinding(learningModel.session);
            return learningModel;
        }

That’s all! You successfully selected the GPU for model evaluation.

If you do not 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.

Windows AI supports using a device that the caller has already created. To learn more about advanced device creation, review the Create a session documentation.

Note

To learn mor about how you select your execution device, please review the LearningModelDevice documentation.