DirectML Samples

Accelerating machine learning on Windows

Getting Started with DirectML

Welcome to DirectML! DirectML is a high-performance, hardware-accelerated machine learning library for Windows. It enables you to efficiently run machine learning inference and training models directly on the GPU, leveraging the power of your hardware for faster and more responsive AI experiences.

Prerequisites

Before you begin, ensure you have the following:

Step 1: Set Up Your Development Environment

DirectML is a component of the Windows SDK. Ensure you have the latest Windows SDK installed. You can download it from the Windows SDK download page.

When setting up your Visual Studio project:

Step 2: Initialize DirectML

The first step in using DirectML is to initialize it. This involves creating a DirectML device and a command queue. You'll typically do this after initializing your Direct3D 12 device.

Here's a simplified code snippet:


#include <dxgi1_6.h>
#include <d3d12.h>
#include <directml.h>
#include <dxmlddquiet.h> // For DirectML quiet initialization

// Assuming you have a valid ID3D12Device *d3dDevice already created

IDMLDevice *dmlDevice = nullptr;
IDMLCommandRecorder *commandRecorder = nullptr;
ID3D12CommandQueue *commandQueue = nullptr; // Your existing D3D12 command queue

HRESULT hr = DirectX::ML::DMLCreateDevice(
    d3dDevice.Get(),
    DML_CREATE_DEVICE_FLAG_NONE,
    IID_PPV_ARGS(&dmlDevice)
);

if (SUCCEEDED(hr)) {
    hr = dmlDevice->CreateCommandRecorder(IID_PPV_ARGS(&commandRecorder));
}

// You can obtain the D3D12 command queue from your D3D12 device.
// Example: d3dDevice->CreateCommandQueue(&commandDesc, IID_PPV_ARGS(&commandQueue));
        

Step 3: Define Your ML Operator

DirectML represents machine learning operations as operators. You define these operators using structures that describe the input and output tensors, the operator type, and its specific parameters.

For example, defining a fused softmax operator:


// Assuming you have input and output tensor descriptions defined

DML_OPERATOR_DESC operatorDesc = {};
operatorDesc.Type = DML_OPERATOR_TYPE_ softmax; // Example: Softmax operator

DML_SOFTMAX_OPERATOR_DESC softmaxDesc = {};
softmaxDesc.InputTensor = &inputTensorDesc;
softmaxDesc.OutputTensor = &outputTensorDesc;
softmaxDesc.Axis = 0; // Example axis

operatorDesc.DefinedBy = &softmaxDesc;

IDMLOperator *dmlOperator = nullptr;
DML_EXECUTE_FLAGS executeFlags = DML_EXECUTE_FLAG_NONE;

// Compile the operator
hr = dmlDevice->CompileOperator(
    &operatorDesc,
    DML_FEATURE_LEVEL_1_0, // Or your target feature level
    IID_PPV_ARGS(&dmlOperator)
);

// If you need to execute multiple operators sequentially, you might consider binding them.
        

Step 4: Execute the Operator

Once an operator is compiled, you can record its execution onto a command list and then submit it to the command queue for processing by the GPU.


// Assuming you have a command list (ID3D12GraphicsCommandList *commandList)
// and your DML operator (IDMLOperator *dmlOperator)

commandRecorder->RecordOperator(commandList.Get(), dmlOperator);

// Close the command list and execute it
commandList->Close();
ID3D12CommandList* ppCommandLists[] = { commandList.Get() };
commandQueue->ExecuteCommandLists(_countof(ppCommandLists), ppCommandLists);

// Present or wait for completion as needed
        

Next Steps

This guide provides a basic overview. To explore more advanced scenarios and functionalities:

Start building your AI-powered applications on Windows today with DirectML!