DirectML API Reference
Welcome to the DirectX Machine Learning (DirectML) API reference. DirectML is a high-performance, hardware-accelerated machine learning inference and training API for Windows. It provides a consistent way to leverage the power of DirectX 12-compatible GPUs for your AI workloads.
Overview
DirectML is designed to be flexible and efficient, allowing developers to integrate machine learning models directly into their DirectX applications. It abstracts away the complexities of underlying hardware, enabling a unified API for a wide range of GPUs.
Key Components
The DirectML API is organized into several key components:
- Device Management: Creating and managing DirectML devices.
- Operators: A rich set of mathematical operators for building neural networks.
- Tensors: Representing data (inputs, outputs, weights) as tensors.
- Execution: Compiling and executing operator graphs.
- Resource Binding: Binding tensors to operator inputs and outputs.
Core APIs
Here are some of the fundamental APIs you'll work with:
IDMLDevice
Represents a DirectML device, typically derived from a ID3D12Device
. This object is central to all DirectML operations.
HRESULT DMLCreateDevice(
ID3D12Device* pD3D12Device,
REFIID riid,
void** ppvDevice
);
Operators
DirectML provides a comprehensive set of operators, including:
DML_ACTIVATION_RELU_OPERATOR
DML_ACTIVATION_SIGMOID_OPERATOR
DML_ACTIVATION_TANH_OPERATOR
DML_CONVOLUTION_OPERATOR
DML_GEMM_OPERATOR
DML_ELEMENT_WISE_ADD_OPERATOR
DML_ELEMENT_WISE_MULTIPLY_OPERATOR
DML_REDUCE_SUM_OPERATOR
DML_ softmax_OPERATOR
Example Usage Snippet
The following code demonstrates a basic tensor creation and operator execution flow:
// Assume pDMLDevice is a valid IDMLDevice
// Assume pD3D12CommandList is a valid ID3D12GraphicsCommandList
DML_BUFFER_DESC inputTensorDesc = {...}; // Define input tensor properties
Microsoft::WRL::ComPtr<IDMLBuffer> pInputTensor;
pDMLDevice->CreateBuffer(&inputTensorDesc, &pInputTensor);
DML_BUFFER_DESC outputTensorDesc = {...}; // Define output tensor properties
Microsoft::WRL::ComPtr<IDMLBuffer> pOutputTensor;
pDMLDevice->CreateBuffer(&outputTensorDesc, &pOutputTensor);
// Define and create a specific operator (e.g., ReLU)
DML_RELU_OPERATOR_DESC reluDesc = {
pInputTensor.Get(),
pOutputTensor.Get(),
/* other properties */
};
Microsoft::WRL::ComPtr<IDMLOperator> pReluOperator;
pDMLDevice->CreateOperator(&reluDesc, IID_PPV_ARGS(&pReluOperator));
// Create an execution operator
Microsoft::WRL::ComPtr<IDMLCommandRecorder> pRecorder;
pDMLDevice->CreateCommandRecorder(&pRecorder);
Microsoft::WRL::ComPtr<IDMLCommandList> pDMLCommandList;
pDMLDevice->CreateCommandList(DML_EXECUTION_FLAG_NONE, &pDMLCommandList);
DML_BINDING_DESC inputBinding = { DML_BINDING_TYPE_NONE, nullptr, 0 };
DML_BINDING_DESC outputBinding = { DML_BINDING_TYPE_NONE, nullptr, 0 };
pRecorder->RecordOperator(pDMLCommandList.Get(), pReluOperator.Get(), &inputBinding, &outputBinding, nullptr);
// ... Close and execute the command list on a D3D12 Queue ...