Table of Contents

Class MetalPerformanceShadersBackend

Namespace
DotCompute.Backends.Metal.MPS
Assembly
DotCompute.Backends.Metal.dll

Metal Performance Shaders backend for optimized ML and linear algebra operations. Provides 3x+ speedup for matrix operations compared to custom kernels.

public sealed class MetalPerformanceShadersBackend : IDisposable
Inheritance
MetalPerformanceShadersBackend
Implements
Inherited Members
Extension Methods

Constructors

MetalPerformanceShadersBackend(nint, ILogger<MetalPerformanceShadersBackend>)

public MetalPerformanceShadersBackend(nint device, ILogger<MetalPerformanceShadersBackend> logger)

Parameters

device nint
logger ILogger<MetalPerformanceShadersBackend>

Properties

Capabilities

Gets the capabilities supported by MPS on this device.

public MPSCapabilities Capabilities { get; }

Property Value

MPSCapabilities

Methods

BatchNormalization(ReadOnlySpan<float>, ReadOnlySpan<float>, ReadOnlySpan<float>, ReadOnlySpan<float>, ReadOnlySpan<float>, Span<float>, float)

Performs batch normalization. Uses MPSCNNBatchNormalization for optimal performance.

public void BatchNormalization(ReadOnlySpan<float> input, ReadOnlySpan<float> gamma, ReadOnlySpan<float> beta, ReadOnlySpan<float> mean, ReadOnlySpan<float> variance, Span<float> output, float epsilon = 1E-05)

Parameters

input ReadOnlySpan<float>
gamma ReadOnlySpan<float>
beta ReadOnlySpan<float>
mean ReadOnlySpan<float>
variance ReadOnlySpan<float>
output Span<float>
epsilon float

Convolution2D(ReadOnlySpan<float>, int, int, int, ReadOnlySpan<float>, int, int, int, Span<float>, int, int, int, int, int, int)

Performs 2D convolution operation. Uses MPSCNNConvolution for optimal performance.

public void Convolution2D(ReadOnlySpan<float> input, int inputHeight, int inputWidth, int inputChannels, ReadOnlySpan<float> kernel, int kernelHeight, int kernelWidth, int outputChannels, Span<float> output, int outputHeight, int outputWidth, int strideY = 1, int strideX = 1, int paddingY = 0, int paddingX = 0)

Parameters

input ReadOnlySpan<float>
inputHeight int
inputWidth int
inputChannels int
kernel ReadOnlySpan<float>
kernelHeight int
kernelWidth int
outputChannels int
output Span<float>
outputHeight int
outputWidth int
strideY int
strideX int
paddingY int
paddingX int

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

MatrixMultiply(ReadOnlySpan<float>, int, int, ReadOnlySpan<float>, int, int, Span<float>, int, int, float, float, bool, bool)

Performs matrix multiplication: C = alpha * (A * B) + beta * C Uses MPSMatrixMultiplication for optimal performance.

public void MatrixMultiply(ReadOnlySpan<float> a, int rowsA, int colsA, ReadOnlySpan<float> b, int rowsB, int colsB, Span<float> c, int rowsC, int colsC, float alpha = 1, float beta = 0, bool transposeA = false, bool transposeB = false)

Parameters

a ReadOnlySpan<float>
rowsA int
colsA int
b ReadOnlySpan<float>
rowsB int
colsB int
c Span<float>
rowsC int
colsC int
alpha float
beta float
transposeA bool
transposeB bool

MatrixVectorMultiply(ReadOnlySpan<float>, int, int, ReadOnlySpan<float>, Span<float>, float, float, bool)

Performs matrix-vector multiplication: y = alpha * (A * x) + beta * y Uses MPSMatrixVectorMultiplication for optimal performance.

public void MatrixVectorMultiply(ReadOnlySpan<float> matrix, int rows, int cols, ReadOnlySpan<float> vector, Span<float> result, float alpha = 1, float beta = 0, bool transpose = false)

Parameters

matrix ReadOnlySpan<float>
rows int
cols int
vector ReadOnlySpan<float>
result Span<float>
alpha float
beta float
transpose bool

ReLU(ReadOnlySpan<float>, Span<float>)

Applies ReLU activation function: y = max(0, x) Uses MPSCNNNeuronReLU for optimal performance.

public void ReLU(ReadOnlySpan<float> input, Span<float> output)

Parameters

input ReadOnlySpan<float>
output Span<float>

ShouldUseMPS(MPSOperationType, int, MPSCapabilities)

Determines if MPS should be used for a given operation type and size. Custom kernels may be faster for small operations.

public static bool ShouldUseMPS(MPSOperationType operationType, int dataSize, MPSCapabilities capabilities)

Parameters

operationType MPSOperationType
dataSize int
capabilities MPSCapabilities

Returns

bool