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:

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:

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 ...
            

Further Reading