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
devicenintloggerILogger<MetalPerformanceShadersBackend>
Properties
Capabilities
Gets the capabilities supported by MPS on this device.
public MPSCapabilities Capabilities { get; }
Property Value
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
inputReadOnlySpan<float>gammaReadOnlySpan<float>betaReadOnlySpan<float>meanReadOnlySpan<float>varianceReadOnlySpan<float>outputSpan<float>epsilonfloat
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
inputReadOnlySpan<float>inputHeightintinputWidthintinputChannelsintkernelReadOnlySpan<float>kernelHeightintkernelWidthintoutputChannelsintoutputSpan<float>outputHeightintoutputWidthintstrideYintstrideXintpaddingYintpaddingXint
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
aReadOnlySpan<float>rowsAintcolsAintbReadOnlySpan<float>rowsBintcolsBintcSpan<float>rowsCintcolsCintalphafloatbetafloattransposeAbooltransposeBbool
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
matrixReadOnlySpan<float>rowsintcolsintvectorReadOnlySpan<float>resultSpan<float>alphafloatbetafloattransposebool
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
inputReadOnlySpan<float>outputSpan<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
operationTypeMPSOperationTypedataSizeintcapabilitiesMPSCapabilities