Skip to content

Performance regression in invquad #99

Closed

Description

@mohdibntarek

Hi,

It seems that the invquad function is significantly slower on the master branch compared to the latest release v0.9.7.

using PDMats, LinearAlgebra, ForwardDiff, BenchmarkTools

A = PDMat(Matrix{Float64}(I, 250, 250));
x = rand(250);

@btime invquad($A, $x);

# v0.9.7
## 42.301 μs (1 allocation: 2.13 KiB)

# master
## 80.592 μs (3 allocations: 490.52 KiB)

### Duals ###

xd = ForwardDiff.Dual{Nothing}.(x)
@btime invquad($A, $xd);

# v0.9.7
## 40.114 μs (4 allocations: 2.17 KiB)

# master
## 330.757 μs (7 allocations: 978.94 KiB)

This slowdown is because the implementation of invquad for vectors went from:

invquad(M::PDMat, x) = dot(x, M.chol \ x)

to

invquad(M::PDMat, x) = sum(abs2, M.chol.L \ x)

Activity

  1. mohdibntarek commented on Jul 7, 2019

    @mohdibntarek
    ContributorAuthor

    M.chol.L seems to copy the array. M.chol.U' is faster and allocates less.

  2. mohdibntarek commented on Jul 7, 2019

    @mohdibntarek
    ContributorAuthor

    Not for the duals though. It seems that conversion happens for the generic fallback.

  3. mohdibntarek commented on Jul 7, 2019

    @mohdibntarek
    ContributorAuthor

    In https://github.com/JuliaLang/julia/blob/df9210ebfb6d04832a2cbf1f44357c959a0b3457/stdlib/LinearAlgebra/src/triangular.jl#L1881, copying happens. I suppose this is necessary for BLAS calls, but when the eltype is the same as TAB and for pure Julia implementations, this doesn't seem necessary IIUC.

  4. mohdibntarek commented on Jul 7, 2019

    @mohdibntarek
    ContributorAuthor

    It seems that copying here can be completely avoided since dispatch happens on ldiv! anyways for BLAS types. But I may be wrong.

  5. devmotion commented on Jun 25, 2026

    @devmotion
    Member

    This looks fixed on current master. Re-running the exact benchmark from the report (A = PDMat(Matrix{Float64}(I, 250, 250)), x = rand(250), xd = ForwardDiff.Dual{Nothing}.(x)):

    Float64 Dual
    invquad 2064 B 2064 B

    The dual path now matches the Float64 path, and allocation scales as O(n·#partials) rather than O(n²) — invquad/quad use the lazy chol_lower/chol_upper factors instead of M.chol.L/.U (which copy the factor). Added a regression test in #240.

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

Metadata

Metadata

Assignees

No one assigned

    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