CUDA.jl 6.5 and 6.6: Kernel profiling, fused FFTs, and faster sorting
CUDA.jl 6.5 and 6.6 make it much easier to find out why a kernel is slow, with hardware metrics and source-level sampling. cuFFT can now fuse surrounding broadcasts into a transform, and sorting, scans and reductions are rebuilt on AcceleratedKernels.jl, which makes them several times faster.
Looking inside kernels #
The existing CUDA.@profile tells you which kernels take time. Two new macros
tell you why.
CUDA.@metrics reads the GPU’s hardware counters while your code runs, and
shows how close each kernel comes to the peak throughput of the compute units
and the memory system, like the “speed of light” section of Nsight Compute:
julia> a = CUDA.rand(Float32, 2^26); b = similar(a);
julia> CUDA.@metrics b .= sin.(a)
Collected hardware metrics for 1 kernel launch on RTX 5080,
executing each kernel 9 times. Kernels were serialized,
so these metrics describe each kernel in isolation.
Time (%) │ Avg time │ Compute │ Memory │ DRAM │ L2 │ L1/TEX │ Name
──────────┼───────────┼─────────┼────────┼───────┼───────┼────────┼──────────
100.00% │ 606.24 µs │ 23.6% │ 87.9% │ 87.9% │ 25.2% │ 16.5% │ bcast(…)This kernel uses 88% of the DRAM bandwidth and only a quarter of the compute
throughput. It is limited by memory bandwidth, so a faster sin would not help
much.
CUDA.@sample periodically records which instruction each warp is executing,
and why it is stalled. Like Profile.print, it shows the samples as a tree of
source lines, including the functions that were inlined into the kernel:
julia> prof = CUDA.@sample b .= sin.(a)
8741 samples, 7009 stalled (80%): memory 93%, execution 4%,
throttle 2%, issue 2%
Samples Stalled Self File:Line Function [Stall reasons]
────────────────────────────────────────────────────────────
8167 6696 280 @GPUArrays/src/host/broadcast.jl:62 bcast [long_scoreboard 91%, …]
7834 6481 0 └─ @Base/broadcast.jl:617 getindex [long_scoreboard 94%, …]
7834 6481 0 └─ @Base/broadcast.jl:621 _getindex [long_scoreboard 94%, …]
7501 6323 0 ├─ @Base/broadcast.jl:677 _broadcast_getindex [long_scoreboard 97%, …]
7501 6323 0 │ └─ @Base/broadcast.jl:704 _broadcast_getindex_evalf [long_scoreboard 97%, …]
7247 6222 7247 │ ├─ @CUDACore/src/device/intrinsics/math.jl:30 sin [long_scoreboard 98%, …]
...Most samples are attributed to sin, stalled on long_scoreboard, which means
the warp is waiting for a load from global memory: here, the value of a[i]
that sin needs. For more details, CUDA.code_sass(prof) and
CUDA.code_ptx(prof) show the same per instruction.
In addition, CUDA.@profile now reports how long the GPU was busy and idle, and
CUDA.Profile.write_trace("trace.json", p) writes a profile p that you can
open in Perfetto to see it on a timeline. Here is a trace of three
iterations of b .= sin.(a); sum(b). The GPU is idle between iterations,
because sum copies its result to the CPU before the next iteration starts. All
three macros can now also print their results as JSON, which is easier for
scripts and coding agents to process.
CUDA.@metrics requires CUDA 12.6 Update 2 or later, and CUDA.@sample
requires CUDA 13 and a GPU with compute capability 7.5 or higher. Both need
access to the GPU’s performance counters, which the driver restricts to
administrators by default; the profiling
documentation explains
how to lift that restriction.
Fusing broadcasts into FFTs #
To compute a spectrogram, you typically multiply every frame of a signal by a
window function, compute the FFT, and take the squared magnitude of the result.
On the GPU, the multiplication and the squared magnitude are separate kernels,
each reading and writing the whole array. In CUDA.jl 6.6, you can wrap the FFT
in the @~ macro of
LazyArrays.jl, and cuFFT then
applies the window while reading its input, and computes abs2 while writing
its output:
using CUDA, cuFFT, LazyArrays
x = CUDA.rand(Float32, 4096, 4096)
w = CUDA.rand(Float32, 4096)
p = plan_rfft(x, 1)
y = abs2.(p * (w .* x)) # three kernels here
y = abs2.(@~ p * (w .* x)) # one kernel
The result is the same, up to rounding, and the fused version is even faster
than the FFT on its own. Timings for this and similar expressions, with the same
x, w and p, on an RTX 5080, in µs (minimum of 200 runs):
without @~ | with @~ | speedup | |
|---|---|---|---|
abs2.(@~ p * (w .* x)) | 418 | 122 | 3.4× |
acc .+= abs2.(@~ p * (w .* x)) | 460 | 212 | 2.2× |
y .= (@~ p * (w .* x)) ./ 4096 | 491 | 168 | 2.9× |
p * x | 165 |
Fusion requires CUDA 13.5, a Blackwell or newer GPU, and an out-of-place
Float32 or Float64 transform. Where it isn’t possible, the same code runs
the broadcasts separately, so you can use @~ unconditionally. The cuFFT
documentation lists the details.
Faster sorting, scans and reductions #
CUDA.jl 6.5 uses GPUArrays.jl 12, which provides a single implementation of
sorting, scans, reductions, findall and logical indexing for all GPU
back-ends, built on
AcceleratedKernels.jl. This
replaces about 2,000 lines of CUDA-specific code, and is a lot faster (RTX 5080,
10⁷ elements):
| CUDA.jl 6.4 (ms) | CUDA.jl 6.6 (ms) | speedup | |
|---|---|---|---|
sort!, Float32 | 28.3 | 1.50 | 19× |
sortperm, Float32 | 64.0 | 7.2 | 8.9× |
findall(>(0.5f0), x) | 0.66 | 0.17 | 4.0× |
x[x .> 0.5f0] | 0.78 | 0.16 | 4.8× |
cumsum | 0.27 | 0.14 | 1.9× |
sum(A; dims=2), 1000×10000 | 0.143 | 0.019 | 7.7× |
These functions also follow Base more closely: sort! is stable by default,
empty arrays give the same results and errors as in Base, and reducing with a
custom operator no longer requires registering a neutral element.
Writing performance tests #
CUDA.@time now reports how many kernels were compiled, and how much time that
took:
julia> x = CUDA.rand(Float32, 1024);
julia> f(x) = x .* 2f0 .+ 1f0;
julia> CUDA.@time f(x);
0.271745 seconds (1.10 M CPU allocations: 55.365 MiB, 10.20% gc time)
(1 GPU allocation: 4.000 KiB, 0.00% memmgmt time)
(1 kernel compilation, 64.87% compilation time)
julia> CUDA.@time f(x);
0.000059 seconds (13 CPU allocations: 560 bytes)
(1 GPU allocation: 4.000 KiB, 18.80% memmgmt time)CUDA.@timed also counts synchronizations, i.e. how often the CPU waited for
the GPU, and returns both counts so that you can check them in a test suite:
using CUDA, Test
step!(x) = (x .+= 1f0; nothing)
loss(x) = sum(abs2, x)
# warm-up
x = CUDA.rand(Float32, 1024)
step!(x); loss(x)
t = CUDA.@timed step!(x)
@test t.gpu_compilestats.compile_count == 0
@test t.gpu_syncstats.sync_count == 0
t = CUDA.@timed loss(x)
@test t.gpu_syncstats.sync_count > 0
On Julia 1.12 and later, launching a kernel no longer allocates memory on the CPU. Before, every launch allocated between 32 bytes and a few kilobytes, depending on its arguments, which kept the garbage collector busy in code that launches many small kernels:
julia> kernel(a) = (a[threadIdx().x] += 1f0; nothing);
julia> launch(x) = @cuda threads=32 kernel(x);
julia> launch(x); # compile
julia> @allocated launch(x)
0Other changes #
- CUDA 13.5 is supported, and is the default on systems with a CUDA 13 driver.
- libNVVM: kernels can be compiled with NVIDIA’s libNVVM instead of LLVM’s
NVPTX back-end, using
@cuda codegen=:nvvm(experimental, requires CUDA 13.5). - Faster SVD:
svdandsvdvalsof a matrix now use QR iteration by default, which was 13-45× faster for theComplexF64matrices we tested and gives singular vectors that are closer to orthonormal (#3571). Usealg=JacobiAlgorithm()for the old behavior. - Vector loads: kernels load and store elements like
ComplexF32orSVector{4,Float32}with a single instruction, making broadcasts over them up to 1.5× faster (#3454). - cuDNN autotuning:
cuDNN.autotune!(true)selects convolution algorithms by timing them, like PyTorch’storch.backends.cudnn.benchmark(#3524). - Public APIs: the wrappers of cuBLAS, cuSOLVER, cuSPARSE, cuFFT, cuRAND, cuTENSOR, cuTensorNet, cuStateVec, CUPTI and NVML are now documented and covered by semantic versioning. Inconsistently named functions were renamed; the old names still work, with a deprecation warning.
- GPU-less environments:
CUDA.set_runtime_version!(:none)configures a project to use no CUDA toolkit, e.g. on a CI machine without a GPU.
Some of these changes are technically breaking. For example, since 6.5,
GC.gc() on its own no longer makes the memory of collected arrays available
right away, and in 6.6 the new SVD algorithm can return different singular
vectors. The full list is in
NEWS.md. If something
breaks for you, please file an issue.