Metal.jl 1.11 to 1.13: Zero-copy arrays, Enzyme, and profiling in Xcode
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 xThe 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))
trueForward 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 XcodeOpen 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:

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.11 | Metal.jl 1.13 | speedup | |
|---|---|---|---|
sum(x) | 1.16 | 0.87 | 1.3× |
sum(A; dims=1) | 2.46 | 0.98 | 2.5× |
maximum(abs, x) | 1.54 | 0.88 | 1.8× |
any(isnan, x) | 1.95 | 0.87 | 2.2× |
findall(>(0.5f0), x) | 15.6 | 4.4 | 3.5× |
x[x .> 0.5f0] | 19.4 | 5.3 | 3.6× |
reverse(x) | error | 2.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.1 | Metal.jl 1.13 | |
|---|---|---|
first broadcast, a .* 2f0 .+ 1f0 | 230 ms | 162 ms |
then sum(a) | 286 ms | 1 ms |
then a vector-addition kernel with @metal | 185 ms | 26 ms |
Other changes #
- Matrix-vector products are faster, as Metal.jl now uses its own kernels
instead of MPSGraph for most matrix products on macOS 26 and later:
A * xwith a 65536×4096 matrix takes 17 ms instead of 35 ms inFloat32, and 9 ms instead of 26 ms inFloat16. Multiplying two 4096×4096Float32matrices is as fast as before (73 ms, was 75 ms). On M5 and later GPUs,Float32products can trade precision for speed: setting theMetal.matmul_precisionscoped value to:relaxedmade a 4096×4096 product take 13 ms instead of 29 ms on an M6. metalcalllaunches a compute pipeline that Metal.jl didn’t compile, e.g. one from a Metal library built with Apple’s tools, like CUDA.jl’scudacall. It launches on the current task’s queue, ordered with the rest of Metal.jl’s work.@metalkeywords:maxthreadssets an upper bound on the threadgroup size, and lowering it can give each thread more registers.minthreads(macOS 26 and later) requires an exact threadgroup size.fastmath=truecompiles a kernel with@fastmathsemantics and Apple’s fast math functions.- Atomics were reworked: ordered atomics, acquire and release fences, and
atomic
Float32addition in threadgroup memory now work from macOS 15 (MSL 3.2) instead of requiring MSL 4.1, so KernelAbstractions’@atomicworks on every supported macOS. M2 and newer GPUs also support atomicminandmaxonUInt64in device memory. - Device-side tensor operations: a kernel using Metal 4’s
matmul2dcan now keep its result in anMtlCooperativeTensor, and scale or combine it before writing it to memory. Tensor views can also have compile-time dimensions. Float16sin,cosandsincosare computed inFloat32and rounded back, as Apple’s half-precision versions were off by up to ~5000 ulp on the M1. A compute-bound loop ofsinandcostakes 3.6× as long.- KernelAbstractions 0.10 is supported, through the new KernelInterface.jl. Kernels over a 3-D range no longer compute their index with integer divisions, which are slow on Apple GPUs. This made some copy kernels 5-6× faster.
- Bounds-checked kernels are faster: a kernel only records where it threw, and the host builds the error message. In one benchmark, a bounds-checked kernel took 2.3 ms instead of 6.4 ms. The error also names the thread that threw.
See the new NEWS.md
for the full release notes. If something doesn’t work for you, please file an
issue.