Metal.jl is the Julia package for programming the GPU of an Apple silicon Mac. It compiles Julia code to Apple’s intermediate representation (AIR) and runs it through the Metal framework, so neither Xcode nor a Metal toolchain is needed. Like the other JuliaGPU back-ends, it can be used at several levels: the MtlArray type for array programming, kernels written in Julia and launched with @metal, and wrappers for the Metal and Metal Performance Shaders (MPS) APIs.

Installation #

Metal.jl is installed with Julia’s package manager, and works with the Metal framework that is part of macOS. It requires a Mac with Apple silicon (M1 or newer) running macOS 15 or later, and the native Apple silicon build of Julia 1.10 or later; Intel Macs are not supported. Metal.versioninfo() reports the system, the toolchain and the GPU that was found:

pkg> add Metal

julia> using Metal

julia> Metal.versioninfo()
macOS 26.7.0, Darwin 25.6.0

Toolchain:
- Julia: 1.13.4
- LLVM: 20.1.8
- Metal: 4.0 (MSL), 2.8 (AIR), 1.2.9 (metallib)

Julia packages:
- Metal.jl: 1.12.0
- GPUArrays: 12.2.0
- GPUCompiler: 2.22.2
- KernelInterface: 0.4.6
- ObjectiveC: 7.2.0
- LLVM: 10.4.3
- LLVMDowngrader_jll: 0.12.2+0
- KernelAbstractions: 0.10.7

1 device:
- Apple M3 Pro (14 GPU cores, 80.000 KiB allocated; Apple9, Metal4 family)

Array programming #

The easiest way to use the GPU is through MtlArray. Operations on these arrays, such as broadcasting, reductions, sorting and matrix multiplication, run on the GPU:

julia> a = MtlArray(Float32[1 2; 3 4])
2×2 MtlMatrix{Float32, Metal.SharedStorage}:
 1.0  2.0
 3.0  4.0

julia> a .+ 1f0
2×2 MtlMatrix{Float32, Metal.SharedStorage}:
 2.0  3.0
 4.0  5.0

julia> sum(sin.(a); dims=1)
1×2 MtlMatrix{Float32, Metal.SharedStorage}:
 0.982591  0.152495

julia> Array(a * a)
2×2 Matrix{Float32}:
  7.0  10.0
 15.0  22.0

Metal.jl also uses Apple’s Metal Performance Shaders and MPSGraph frameworks behind standard Julia interfaces: matrix multiplication, linear solvers and factorizations from LinearAlgebra, FFTs through AbstractFFTs.jl, random numbers, and NNlib.jl’s softmax, convolution and pooling. Apple GPUs have no double-precision arithmetic, so Float64 can’t be used in GPU code; the mtl function converts existing data to Float32.

Kernel programming #

When array operations are not enough, you can write a kernel in Julia and launch it with @metal, specifying the number of threads per threadgroup and the number of threadgroups:

julia> function vadd(a, b, c)
           i = thread_position_in_grid().x
           if i <= length(c)
               @inbounds c[i] = a[i] + b[i]
           end
           return
       end
vadd (generic function with 1 method)

julia> a = MtlArray(rand(Float32, 1000)); b = MtlArray(rand(Float32, 1000)); c = similar(a);

julia> @metal threads=256 groups=cld(length(c), 256) vadd(a, b, c);

julia> Array(c) ≈ Array(a) + Array(b)
true

For kernels that are portable across GPU back-ends, use KernelAbstractions.jl with MetalBackend().

Learn more #