Skip to content

Multiplying AbstractQ with structured matrices can cause large allocations #1270

@mikmoore

Description

@mikmoore

Came up on https://discourse.julialang.org/t/best-way-for-linear-regression-problem-on-product-features/127786.

julia> using LinearAlgebra

julia> N = 1_000_000; X = ones(N, 1); Y = Diagonal(ones(N)); Xqr = qr(X);

julia> Xqr \ Y
ERROR: OutOfMemoryError()

julia> Xqr.Q' * Y # tries to densely materialize Y # EDIT: No, see below where we discuss that this operation fails because the result is NxN.
ERROR: OutOfMemoryError()

julia> Matrix(Xqr.Q)' * Y # the materialized rectangular Q works fine
1×1000000 Matrix{Float64}:
 -0.001  -0.001  -0.001  -0.001  -0.001  …  -0.001  -0.001  -0.001  -0.001

julia> Xqr.R \ (Matrix(Xqr.Q)' * Y) # can solve the system by manual application
1×1000000 Matrix{Float64}:
 1.0e-6  1.0e-6  1.0e-6  1.0e-6  1.0e-6  …  1.0e-6  1.0e-6  1.0e-6  1.0e-6

This happens with both QRCompactWYQ (this case) and QRPackedQ (pivoted case), at least.

I haven't wrapped my head fully around what the full solution should be (AbstractQ representations make my head spin). What's efficient may vary with context, so it's possible there are some real design decisions involved around this issue. Broadly speaking, it seems that applying a "dimension-reducing" AbstractQ to certain structured matrices (Diagonal and AbstractSparseMatrix, for example) might benefit from materializing the AbstractQ rather than the structured matrix.

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