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:
- A Windows 10 (version 1709 or later) or Windows 11 system.
- A DirectX 12-compatible GPU.
- The latest GPU drivers installed from your hardware vendor (NVIDIA, AMD, Intel).
- A C++ development environment, such as Visual Studio 2019 or later, with the "Desktop development with C++" workload installed.
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:
- Include the necessary DirectML headers:
#include <directml.h>
. - Link against the DirectML library:
dml.lib
.
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:
- Dive into the Sample Gallery for practical examples of various ML tasks.
- Consult the API Reference for detailed information on DirectML interfaces and structures.
- Follow the Tutorials to learn specific techniques and best practices.
Start building your AI-powered applications on Windows today with DirectML!