Metal.jl 1.12 stores MtlArrays in memory that the CPU can access directly, so reading them from Julia no longer requires a copy. It also lets Xcode show which lines of a Julia kernel take the most GPU time. With Metal.jl 1.13, Enzyme can differentiate functions that launch your own Metal kernels.

Metal.jl 1.11 added support for macOS 27 and Julia 1.13. When upgrading, note that Metal.jl 1.12 requires macOS 15 or later (up from macOS 14), and moves to KernelAbstractions 0.10. Unless noted otherwise, timings are from an M1 Mac mini running macOS 27 and Julia 1.13.

Unified memory arrays by default #

Apple silicon Macs have unified memory: the CPU and GPU share the same physical memory. Metal.jl used to allocate arrays in private storage anyway, which only the GPU can access, so reading an array from the CPU meant copying it. Since Metal.jl 1.12, arrays use shared storage by default, which the CPU can read and write directly, and which is as fast as private storage on the GPU:

julia> using Metal, BenchmarkTools

julia> a = MtlArray(rand(Float32, 1000))
1000-element MtlVector{Float32, Metal.SharedStorage}:
...

julia> @btime $a[1]       # previously: 188 µs
  3.042 ns (0 allocations: 0 bytes)

julia> @btime Array($a)   # previously: 118 µs
  207.068 ns (4 allocations: 4.09 KiB)

Scalar indexing of shared arrays is cheap, so Metal.allowscalar(false) no longer errors with them. To keep an array on the GPU only, allocate it as MtlArray{T,N,Metal.PrivateStorage}(...), or set the default_storage preference to "private".

In the other direction, unsafe_wrap(MtlArray, x) lets the GPU use an existing Julia Array without copying it. Before Metal.jl 1.12, this only worked for arrays whose memory was page-aligned. For example:

julia> x = rand(Float32, 10^8);

julia> y = unsafe_wrap(MtlArray, x);

julia> y .= sin.(y) .* 2f0;   # computes on the GPU

julia> Metal.synchronize()    # wait for the GPU before reading x

The broadcast and synchronization take 18 ms, where copying x to an MtlArray, computing, and copying the result back would take 66 ms. One gotcha: don’t resize x while y is in use.

Automatic differentiation with Enzyme #

With Metal.jl 1.13, Enzyme.jl can differentiate functions that launch Metal kernels. In the following example, it differentiates the whole loss function, including the @metal launch and sum(e):

using Metal, Enzyme

function energy!(e, x)
    i = thread_position_in_grid().x
    @inbounds e[i] = sin(x[i])^2
    return
end

function loss(e, x)
    @metal threads=length(x) energy!(e, x)
    return sum(e)
end

x  = MtlArray(Float32[0.1, 0.2, 0.3, 0.4])
e  = similar(x)
dx = Metal.zeros(Float32, 4)   # receives the gradient with respect to x
de = Metal.zeros(Float32, 4)   # scratch space for the gradient of e
autodiff(Reverse, loss, Active, Duplicated(e, de), Duplicated(x, dx))
julia> dx
4-element MtlVector{Float32, Metal.SharedStorage}:
 0.19866931
 0.3894183
 0.56464255
 0.71735615

julia> Array(dx) ≈ sin.(2 .* Array(x))
true

Forward and reverse mode both work, for kernels launched with @metal or written with KernelAbstractions, as well as for reductions and matrix products. Broadcasts such as sin.(x) can’t be differentiated yet, nor can kernels that use the device RNG. In reverse mode, loops in kernels also need a trip count that is known at compile time. The documentation lists all limitations.

Profiling kernels in Xcode #

Xcode can replay a recording of GPU work (a GPU frame capture) and profile the captured kernels down to individual lines of source code. Since Metal.jl 1.12, this works for Julia kernels: Metal.jl embeds their source code in the compiled kernel, and labels kernels and arrays with their Julia types. To try it, follow the quick start in the documentation: save a kernel in a file (Xcode reads the source from there), start Julia with METAL_CAPTURE_ENABLED=1, include the file, and capture a launch with Metal.@capture:

julia> Metal.@capture @metal threads=256 groups=cld(N, 256)
                             gather_sin(y, x, UInt32(N))
┌ Info: GPU frame capture saved to /Users/me/project/gather_sin_1.gputrace;
└       open the resulting trace in Xcode

Open the trace in Xcode, check Profile after replay, and click Replay. Xcode then shows each line’s share of the kernel’s GPU time. Here, the load and sin on lines 6 and 8 take most of it:

Xcode showing the source of a Julia kernel with per-line GPU time percentages

The same guide also shows how to record a timeline of GPU work in Instruments, with Metal.@profile external=true.

Faster reductions and searches #

Metal.jl 1.12 uses GPUArrays.jl 12 for reductions, findall, logical indexing and reverse. Its implementations are built on AcceleratedKernels.jl and shared by all of Julia’s GPU back-ends, and they are faster than Metal.jl’s own. For a Float32 vector x with 10⁷ elements and a 1000×10000 matrix A, best of 50 runs including synchronization, in ms:

Metal.jl 1.11Metal.jl 1.13speedup
sum(x)1.160.871.3×
sum(A; dims=1)2.460.982.5×
maximum(abs, x)1.540.881.8×
any(isnan, x)1.950.872.2×
findall(>(0.5f0), x)15.64.43.5×
x[x .> 0.5f0]19.45.33.6×
reverse(x)error2.5

sort and cumsum still use MPSGraph, and are about as fast as before. sortperm(x) is now stable by default, as in Base, which made it slower: 213 ms instead of 95 ms. If the order of equal elements doesn’t matter, sortperm(x; alg=QuickSort) uses MPSGraph as before.

Faster first use #

Metal.jl now precompiles more of the code that the first operations of a session need, and compiles the rest faster. For example, using a 1024-element Float32 array:

Metal.jl 1.11.1Metal.jl 1.13
first broadcast, a .* 2f0 .+ 1f0230 ms162 ms
then sum(a)286 ms1 ms
then a vector-addition kernel with @metal185 ms26 ms

Other changes #

See the new NEWS.md for the full release notes. If something doesn’t work for you, please file an issue.