Metal Performance Shaders

Metal.MPS wraps Apple's Metal Performance Shaders framework. This page documents selected array operations and wrappers; consult Apple for framework concepts. See Numerical operations for the LinearAlgebra interfaces and implementation choices.

Matrix multiplication

Metal.MPS.matmul! — Function
MPS.matmul!(C, A, B, α=true, β=false; tA='N', tB='N') -> C

Compute C = α * opA(A) * opB(B) + β * C using MPSMatrixMultiplication, where opA and opB are the identity ('N') or the transpose ('T' or 'C'), as selected by tA and tB.

A, B and C are MtlArray matrices, or 3D arrays holding equally many matrices along the last dimension. Their element types are one of the combinations in MPS.MPS_VALID_MATMUL_TYPES. The multiplication executes asynchronously, ordered with the other work submitted from the current task.

LinearAlgebra.mul! selects between this, Metal.MPSGraphs.matmul! and Metal.jl's own kernels; use this function to select MPS explicitly.

source
Metal.MPS.matvecmul! — Function
MPS.matvecmul!(y, A, x, α=true, β=false; tA='N') -> y

Compute y = α * opA(A) * x + β * y using MPSMatrixVectorMultiplication, where opA is the identity ('N') or the transpose ('T' or 'C'), as selected by tA.

A is an MtlArray matrix and x and y are MtlArray vectors, with element types that are one of the combinations in MPS.MPS_VALID_MATVECMUL_TYPES. The multiplication executes asynchronously, ordered with the other work submitted from the current task.

source

Top-k selection

Metal.MPS.topk — Function
MPS.topk(A::MtlMatrix{T}, k) -> (I, V)

Like MPS.topk!, but return new k-row matrices of indices I and values V.

julia> A = MtlArray(Float32[1 6; 4 5; 2 9]);julia> I, V = MPS.topk(A, 2);julia> Array(I)2×2 Matrix{UInt32}: 0x00000002  0x00000003 0x00000003  0x00000001julia> Array(V)2×2 Matrix{Float32}: 4.0  9.0 2.0  6.0
source
Metal.MPS.topk! — Function
MPS.topk!(A::MtlMatrix{T}, I::MtlMatrix{UInt32}, V::MtlMatrix{T}, k) -> (I, V)

Find the k largest values in each column of A, with T Float32 or Float16, using MPSMatrixFindTopK. Write their 1-based row indices to the first k rows of I, and the values to those of V, in descending order. k can be at most 16. This interface is experimental, and might change without deprecation.

See also MPS.topk.

source

Random numbers

Metal.MPS.RNG — Type
MPS.RNG([device], [seed])

A random number generator backed by Metal Performance Shaders' Philox generator (MPSMatrixRandomPhilox). It fills MtlArrays with uniformly distributed Float32s or integers (rand!), or normally distributed Float32s (randn!); other element types are not supported. Every task has one, returned by Metal.mps_rng.

source

Matrix and vector descriptors

MPSMatrix and MPSVector describe an MtlArray to the MPS kernels.

Metal.MPS.MPSMatrix — Type
MPSMatrix(mat::MtlMatrix)

Metal matrix representation used in Performance Shaders.

Note that this results in a transposed view of the input, as Metal stores matrices row-major instead of column-major.

source
MPSMatrix(vec::MtlVector)

Metal matrix representation used in Performance Shaders.

Note that this results in a transposed view of the input, as Metal stores matrices row-major instead of column-major.

source
MPSMatrix(arr::MtlArray{T,3})

Metal batched matrix representation used in Performance Shaders.

Note that this results in a transposed view of the input, as Metal stores matrices row-major instead of column-major.

source