Skip to content

Compact SVD is slower on very wide matrices #323

Open

Description

@Yue-Zhengyuan

On Julia 1.13.3 with one BLAS thread, LinearAlgebra.LAPACK.gesdd!('S', A) is slower on a 28 × 2744 matrix than on its 2744 × 28 transpose. A direct dgesdd call with preallocated workspace reproduces the gap, which points to the backend's wide LAPACK path.

Backend Direct dgesdd, tall Direct dgesdd, wide
Apple Accelerate 0.39 ms 6.32 ms
OpenBLAS 0.3.30 0.79 ms 1.73 ms

MatrixAlgebraKit's svd_compact! inherits this cost through its default SafeDivideAndConquer algorithm. On the same inputs, the tall and wide calls took 0.55 ms and 6.40 ms on Accelerate, and 0.96 ms and 1.80 ms on OpenBLAS.

LinearAlgebra MWE

Run either example with julia --startup-file=yes -O1 --threads=1. To select Julia's bundled OpenBLAS, insert using OpenBLAS_jll and BLAS.lbt_forward(OpenBLAS_jll.libopenblas_path; clear = true) before setting the BLAS thread count.

using LinearAlgebra, Random, Statistics

BLAS.set_num_threads(1)
println(BLAS.lbt_find_backing_library("dgesdd_", :ilp64).libname)

function median_ms(f, A)
    f(copy(A))
    return 1e3 * median([begin
        scratch = copy(A)
        @elapsed f(scratch)
    end for _ in 1:31])
end

A = randn(MersenneTwister(42), 2744, 28)
compact_svd!(A) = LAPACK.gesdd!('S', A)
@show median_ms(compact_svd!, A)
@show median_ms(compact_svd!, copy(transpose(A)))

MatrixAlgebraKit MWE

Run with MatrixAlgebraKit 0.6.14.

using MatrixAlgebraKit, LinearAlgebra, Random, Statistics

BLAS.set_num_threads(1)
println(BLAS.lbt_find_backing_library("dgesdd_", :ilp64).libname)

function median_ms(f, A)
    f(copy(A))
    return 1e3 * median([begin
        scratch = copy(A)
        @elapsed f(scratch)
    end for _ in 1:31])
end

A = randn(MersenneTwister(42), 2744, 28)
@show median_ms(svd_compact!, A)
@show median_ms(svd_compact!, copy(transpose(A)))

Activity

  1. self-assigned this
    on Nov 7, 2026
  2. lkdvos commented on Nov 7, 2026

    @lkdvos
    Member

    I'm not sure this is really something we can do anything about, you could try and report this upstream though?

  3. Yue-Zhengyuan commented on Nov 7, 2026

    @Yue-Zhengyuan
    MemberAuthor

    I agree this is an upstream issue. But is it feasible to have a temporary workaround in MAK by SVD-ing the adjoint when the input matrix is wide above a certain threshold?

  4. lkdvos commented on Nov 8, 2026

    @lkdvos
    Member

    In principle that's definitely possible, the more annoying part is that it's not so easy to detect which LAPACK backend is being used, and then where to put the cutoff for what sizes. It's mostly a maintenance thing, because that typically depends on the machine you're running it on as well. As an alternative, would it also work to have a QR first? That might also be faster (and is typically the first thing done inside these SVD routines anyways)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions