Skip to content

Default to QR iteration for the SVD of a ROCMatrix - #1221

Merged
luraess merged 1 commit into
mainfrom
lr/svd-default-qr
Nov 16, 2026
Merged

luraess merged 1 commit into
mainfrom
lr/svd-default-qr

Conversation

@luraess

@luraess luraess commented Nov 13, 2026

Copy link
Copy Markdown
Member

svd, svd!, svdvals and svdvals! of a ROCMatrix default to QRAlgorithm() (rocSOLVER's gesvd) instead of JacobiAlgorithm() (gesvdj), as CUDA.jl does in JuliaGPU/CUDA.jl#3571, but for the opposite reason: on AMD the Jacobi routine is the faster one, and it is inaccurate.

rocSOLVER's gesvdj is not a one-sided Jacobi on A: it diagonalizes A'A with its Jacobi eigensolver and takes U and S from a QR of AV (roclapack_gesvdj.hpp). Squaring the matrix loses every singular value below sqrt(eps)·σ₁, and the convergence test cannot see it. On a Float64 matrix with singular values graded from 1 to 1e-20 (n = 11), gesvdj returns info = 0 and a residual of 1e-19 with singular values that are right down to 1e-6·σ₁ and 3–80 % wrong from 1e-8·σ₁ on; gesvd matches LAPACK down to the eps·σ₁ floor. On a graded 128×128 ComplexF64 matrix the factorization is accurate to 9e-9 (gesvd: 8e-15), the numerical rank at rtol 1e-12 comes out one short, and cond is 20× too small. Reported upstream in ROCm/rocm-libraries#12868 (in fp32 there; the double-precision numbers are in a comment).

Speed goes the other way from NVIDIA: gesvdj is 3–9× faster than gesvd at every size and element type on MI250X and MI300A (ComplexF64, 1466×1466: 1.45 s vs 7.8 s on MI250X, 0.81 s vs 5.6 s on MI300A; ROCm 7.2.3). So a Float64 svd, cond, rank or pinv that is silently accurate to 1e-8 is traded for one that takes longer. JacobiAlgorithm() stays available and documented as the fast path for well-conditioned matrices. LinearAlgebra.DivideAndConquer() now maps to the default as well; QRIteration() maps to QR as before.

Tests: the default equals QRAlgorithm() on tall, wide, square, rank-deficient and zero matrices; a graded 64×64 matrix in Float64 and ComplexF64 is resolved to 1e-12 through the default. Docs: the SVD section and the QRAlgorithm/JacobiAlgorithm docstrings state the trade-off.

One defect of gesvd turned up on the way: for a rank-deficient ComplexF32 matrix (and only that element type) rocSOLVER returns the singular vector of a zero singular value with a norm between 0.84 and 1.05, in about half of the random matrices, deterministically per matrix, in both work modes; singular values, reconstruction and info are fine, and gesvdj is not affected. Same with rocSOLVER 3.37.0 (the ROCm 10.1.0 artifacts) on MI300A. Reported in ROCm/rocm-libraries#15788; the rank-deficient test checks the vectors of the non-zero singular values for that element type, and the docs mention it.

Behaviour change (minor release): the default svd is slower; callers who want the previous speed pass alg = JacobiAlgorithm(). API and return types are unchanged. Verified on MI250X and MI300A.

🤖 Generated with Claude Code

rocSOLVER's gesvdj diagonalizes A'A with a Jacobi eigensolver and takes
U and S from a QR of AV, so singular values below sqrt(eps)*σ₁ are lost
while info = 0 and the residual is below tolerance: a Float64 svd, cond
or rank could silently be accurate to 1e-8 only. gesvd matches LAPACK
down to the eps*σ₁ floor. Make QRAlgorithm() the default of svd, svd!,
svdvals and svdvals! (and the target of DivideAndConquer()), as CUDA.jl
does; JacobiAlgorithm() stays available as the documented fast path
(3-9x faster on MI250X and MI300A) for well-conditioned matrices.

Upstream: ROCm/rocm-libraries#12868 (gesvdj accuracy), #15788 (cgesvd
singular vectors of a rank-deficient ComplexF32 matrix, worked around
in the test).
@github-actions

Copy link
Copy Markdown
Contributor

Documentation preview: https://amdgpu.juliagpu.org/previews/PR1221/

@github-actions

Copy link
Copy Markdown
Contributor

Benchmark results

Compared with 028576c57, the merge base.
The environment differs from the baseline's: Julia 1.13.3 → 1.13.4, AcceleratedKernels 0.5.12 → 0.5.13, MozillaCACerts_jll 2026.10.20 → 2026.11.5, OpenBLAS_jll 0.3.30+0 → 0.3.30+1, StaticArrays 1.9.24 → 1.9.25, StaticArraysStatisticsExt 1.9.24 → 1.9.25, StructUtils 2.12.0 → 2.12.1, StructUtilsStaticArraysCoreExt 2.12.0 → 2.12.1, StructUtilsTablesExt 2.12.0 → 2.12.1, p7zip_jll 17.8.3+0 → 17.8.5+0.

Times are the minimum time of every benchmark. A change is unusual when it exceeds both three times the benchmark's typical change between consecutive runs on the default branch and a minimum per category (performance 3%, latency 5%, applications 3%, other 3%). A benchmark that starts or stops allocating is listed however few the allocations. A single run can still be a fluke, so rerun before drawing conclusions.

Geometric mean Unusual: slower Unusual: faster Compared
Performance +1.8% 9 2 69
Latency +0.6% 0 0 3
Applications +1.0% 1 1 14
Benchmark Baseline This PR Change
Performance
array/iteration/logical 79.8 µs 106 µs +32.3% 🔴 slower
array/iteration/findall/bool 84.6 µs 107 µs +26.0% 🔴 slower
array/reductions/mapreduce/Int64/dims=1L 66.3 µs 52.7 µs −20.5% 🟢 faster
array/accumulate/Float32/dims=2L 110 µs 137 µs +24.3% 🔴 slower
array/reverse/2dL 67.5 µs 81.7 µs +20.9% 🔴 slower
array/reductions/reduce/Float32/dims=2L 54.5 µs 65.4 µs +20.0% 🔴 slower
array/reductions/mapreduce/Float32/dims=1 42.5 µs 50.4 µs +18.4% 🔴 slower
array/accumulate/Int64/dims=2 97.0 µs 82.0 µs −15.4% 🟢 faster
array/iteration/findmin/2d 76.7 µs 87.2 µs +13.7% 🔴 slower
array/random/randn/Float32 61.1 µs 67.9 µs +11.3% 🔴 slower
array/iteration/findall/int 92.9 µs 97.3 µs +4.8% 🔴 slower
Applications
gemm/tiled_unbounded 1.92 ms 2.62 ms +36.0% 🔴 slower
byval/reference 39.7 µs 37.2 µs −6.4% 🟢 faster
All benchmarks
Benchmark Baseline This PR Change
Performance
amdgpu/synchronization/context/device 1.61 µs 1.65 µs +2.3%
amdgpu/synchronization/stream/blocking 210 ns 210 ns +0.0%
amdgpu/synchronization/stream/nonblocking 305 ns 305 ns +0.0%
array/accumulate/Float32/1d 76.1 µs 75.6 µs −0.6%
array/accumulate/Float32/dims=1 60.8 µs 60.6 µs −0.3%
array/accumulate/Float32/dims=1L 99.9 µs 99.3 µs −0.5%
array/accumulate/Float32/dims=2 89.0 µs 91.7 µs +3.1%
array/accumulate/Float32/dims=2L 110 µs 137 µs +24.3% 🔴 slower
array/accumulate/Int64/1d 91.8 µs 88.3 µs −3.8%
array/accumulate/Int64/dims=1 65.5 µs 65.2 µs −0.5%
array/accumulate/Int64/dims=1L 92.5 µs 99.2 µs +7.2%
array/accumulate/Int64/dims=2 97.0 µs 82.0 µs −15.4% 🟢 faster
array/accumulate/Int64/dims=2L 136 µs 136 µs +0.3%
array/broadcast 20.9 µs 21.5 µs +2.6%
array/construct 1.51 µs 1.50 µs −0.5%
array/copy 37.5 µs 37.3 µs −0.6%
array/copyto!/cpu_to_gpu 85.2 µs 85.4 µs +0.2%
array/copyto!/gpu_to_cpu 84.7 µs 84.4 µs −0.3%
array/copyto!/gpu_to_gpu 24.6 µs 24.9 µs +1.4%
array/iteration/findall/bool 84.6 µs 107 µs +26.0% 🔴 slower
array/iteration/findall/int 92.9 µs 97.3 µs +4.8% 🔴 slower
array/iteration/findfirst/bool 47.8 µs 50.1 µs +4.9%
array/iteration/findfirst/int 47.9 µs 48.6 µs +1.5%
array/iteration/findmin/1d 88.5 µs 88.5 µs +0.1%
array/iteration/findmin/2d 76.7 µs 87.2 µs +13.7% 🔴 slower
array/iteration/logical 79.8 µs 106 µs +32.3% 🔴 slower
array/iteration/scalar 311 µs 317 µs +1.9%
array/permutedims/2d 55.6 µs 54.8 µs −1.3%
array/permutedims/3d 53.2 µs 43.6 µs −18.1%
array/permutedims/4d 48.3 µs 48.2 µs −0.3%
array/random/rand!/Float32 22.1 µs 22.6 µs +2.4%
array/random/rand!/Int64 22.3 µs 21.1 µs −5.5%
array/random/rand/Float32 37.3 µs 38.7 µs +3.6%
array/random/rand/Int64 41.6 µs 42.3 µs +1.7%
array/random/randn!/Float32 31.6 µs 28.9 µs −8.5%
array/random/randn/Float32 61.1 µs 67.9 µs +11.3% 🔴 slower
array/reductions/mapreduce/Float32/1d 65.9 µs 67.3 µs +2.0%
array/reductions/mapreduce/Float32/dims=1 42.5 µs 50.4 µs +18.4% 🔴 slower
array/reductions/mapreduce/Float32/dims=1L 54.7 µs 54.6 µs −0.2%
array/reductions/mapreduce/Float32/dims=2 52.5 µs 52.5 µs +0.1%
array/reductions/mapreduce/Float32/dims=2L 65.7 µs 65.7 µs −0.0%
array/reductions/mapreduce/Int64/1d 68.7 µs 68.8 µs +0.2%
array/reductions/mapreduce/Int64/dims=1 47.3 µs 47.0 µs −0.6%
array/reductions/mapreduce/Int64/dims=1L 66.3 µs 52.7 µs −20.5% 🟢 faster
array/reductions/mapreduce/Int64/dims=2 52.9 µs 53.1 µs +0.3%
array/reductions/mapreduce/Int64/dims=2L 70.0 µs 71.4 µs +1.9%
array/reductions/reduce/Float32/1d 66.5 µs 67.5 µs +1.4%
array/reductions/reduce/Float32/dims=1 50.8 µs 50.6 µs −0.4%
array/reductions/reduce/Float32/dims=1L 54.4 µs 54.4 µs −0.1%
array/reductions/reduce/Float32/dims=2 52.6 µs 52.4 µs −0.4%
array/reductions/reduce/Float32/dims=2L 54.5 µs 65.4 µs +20.0% 🔴 slower
array/reductions/reduce/Int64/1d 68.5 µs 68.6 µs +0.0%
array/reductions/reduce/Int64/dims=1 47.3 µs 46.9 µs −0.8%
array/reductions/reduce/Int64/dims=1L 64.7 µs 65.6 µs +1.5%
array/reductions/reduce/Int64/dims=2 53.2 µs 53.0 µs −0.4%
array/reductions/reduce/Int64/dims=2L 66.9 µs 70.9 µs +6.1%
array/reverse/1d 38.6 µs 37.5 µs −2.9%
array/reverse/1d_inplace 22.8 µs 27.4 µs +19.9%
array/reverse/1dL 58.7 µs 58.2 µs −0.8%
array/reverse/1dL_inplace 30.0 µs 30.5 µs +1.8%
array/reverse/2d 41.7 µs 39.8 µs −4.5%
array/reverse/2d_inplace 25.5 µs 25.8 µs +1.4%
array/reverse/2dL 67.5 µs 81.7 µs +20.9% 🔴 slower
array/reverse/2dL_inplace 34.2 µs 34.5 µs +0.8%
array/sorting/1d 287 µs 290 µs +0.9%
kernel/indexing 19.4 µs 19.7 µs +1.4%
kernel/indexing_checked 23.2 µs 24.2 µs +4.4%
kernel/launch 938 ns 930 ns −0.8%
kernel/rand 30.7 µs 28.0 µs −8.8%
Latency
import 1.75 s 1.77 s +1.5%
precompile 24.8 s 24.9 s +0.3%
ttfp 2.08 s 2.08 s +0.1%
Applications
bitonic_sort 1.07 ms 1.05 ms −1.5%
byval/reference 39.7 µs 37.2 µs −6.4% 🟢 faster
byval/slices=1 40.3 µs 40.5 µs +0.7%
byval/slices=2 157 µs 157 µs +0.0%
byval/slices=3 238 µs 217 µs −8.9%
convolution 52.3 µs 52.7 µs +0.7%
floyd_warshall 8.88 ms 8.94 ms +0.7%
gemm/tiled 2.63 ms 2.60 ms −0.9%
gemm/tiled_unbounded 1.92 ms 2.62 ms +36.0% 🔴 slower
histogram 408 µs 408 µs +0.0%
prefix_sum 195 µs 196 µs +0.3%
stencil/diffusion3d 1.56 ms 1.56 ms +0.1%
stencil/diffusion3d_checked 1.60 ms 1.60 ms +0.0%
volumerhs 4.86 ms 4.84 ms −0.5%

History: https://juliagpu.org/perf/?pkg=amdgpu

@luraess
luraess merged commit 058d770 into main Nov 16, 2026
12 checks passed
@luraess
luraess deleted the lr/svd-default-qr branch November 16, 2026 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant