DirectML Operator Cookbook

Add

The Add operator computes the element‑wise sum of two tensors.


// C++ example using DirectML
IDMLDevice* device = nullptr;
IDMLCommandRecorder* recorder = nullptr;
DML_TENSOR_DESC aDesc = DML_TENSOR_DESC::CreateTensor({1, 3, 224, 224});
DML_TENSOR_DESC bDesc = aDesc;
IDMLCompiledOperator* addOp = nullptr;
auto addDesc = DML_ELEMENT_WISE_ADD_OPERATOR_DESC{ aDesc, bDesc };
DML_OPERATOR_DESC opDesc = { DML_OPERATOR_ELEMENT_WISE_ADD, &addDesc };
device->CreateOperator(&opDesc, IID_PPV_ARGS(&addOp));

Multiply

The Multiply operator performs element‑wise multiplication.


auto mulDesc = DML_ELEMENT_WISE_MULTIPLY_OPERATOR_DESC{ aDesc, bDesc };
DML_OPERATOR_DESC opDesc = { DML_OPERATOR_ELEMENT_WISE_MULTIPLY, &mulDesc };
device->CreateOperator(&opDesc, IID_PPV_ARGS(&multiplyOp));

Convolution

Convolution applies a set of learnable filters to the input tensor.


DML_CONVOLUTION1D_OPERATOR_DESC convDesc = {};
convDesc.InputTensor = &inputDesc;
convDesc.FilterTensor = &filterDesc;
convDesc.BiasTensor = &biasDesc;
convDesc.Strides = 1;
convDesc.Dilations = 1;
convDesc.StartPadding = 0;
convDesc.EndPadding = 0;
DML_OPERATOR_DESC opDesc = { DML_OPERATOR_CONVOLUTION_1D, &convDesc };
device->CreateOperator(&opDesc, IID_PPV_ARGS(&convOp));

ReLU (Activation)

The Rectified Linear Unit (ReLU) sets all negative values to zero.


DML_ACTIVATION_RELU_OPERATOR_DESC reluDesc = {};
reluDesc.InputTensor = &inputDesc;
reluDesc.OutputTensor = &outputDesc;
DML_OPERATOR_DESC opDesc = { DML_OPERATOR_ACTIVATION_RELU, &reluDesc };
device->CreateOperator(&opDesc, IID_PPV_ARGS(&reluOp));

Softmax

Softmax converts logits into probabilities that sum to one.


DML_SOFTMAX_OPERATOR_DESC softmaxDesc = {};
softmaxDesc.InputTensor = &logitsDesc;
softmaxDesc.Axis = 1;
DML_OPERATOR_DESC opDesc = { DML_OPERATOR_SOFTMAX, &softmaxDesc };
device->CreateOperator(&opDesc, IID_PPV_ARGS(&softmaxOp));

Batch Normalization

Batch normalization stabilizes training by normalizing activations.


DML_BATCH_NORMALIZATION_OPERATOR_DESC bnDesc = {};
bnDesc.InputTensor = &inputDesc;
bnDesc.ScaleTensor = &scaleDesc;
bnDesc.BiasTensor = &biasDesc;
bnDesc.MeanTensor = &meanDesc;
bnDesc.VarTensor = &varDesc;
bnDesc.Epsilon = 1e-5f;
DML_OPERATOR_DESC opDesc = { DML_OPERATOR_BATCH_NORMALIZATION, &bnDesc };
device->CreateOperator(&opDesc, IID_PPV_ARGS(&bnOp));