Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mat: calculate Q lazily when calling QR.ToQ #1970

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kortschak
Copy link
Member

@kortschak kortschak commented Apr 17, 2024

When a matrix is very tall, calculating Q will currently allocate a large Q at the end of the factorisation, even if it is not going to be used. The eager calculation was intended to prevent repeated re-calculation of Q when it is used. So move the Q calculation to ToQ, but make it conditional on the stored Q value being empty, and empty Q at the end of the factorisation.

Please take a look.

Fixes #1968

Copy link

@tvkn tvkn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

Do you want to add a dedicated test for the issue it fixes?

@kortschak
Copy link
Member Author

kortschak commented Apr 18, 2024

Do you want to add a dedicated test for the issue it fixes?

It's in the table (well, it will be when I remember to commit it).

When a matrix is very tall, calculating Q will currently allocate a large Q at
the end of the factorisation, even if it is not going to be used. The eager
calculation was intended to prevent repeated re-calculation of Q when it is
used. So move the Q calculation to ToQ, but make it conditional on the stored
Q value being empty, and empty Q at the end of the factorisation.
@@ -98,23 +98,9 @@ func (qr *QR) factorize(a Matrix, norm lapack.MatrixNorm) {
lapack64.Geqrf(qr.qr.mat, qr.tau, work, len(work))
putFloat64s(work)
qr.updateCond(norm)
qr.updateQ()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid this doesn't work because q is currently needed in the At method. The test passes just because QTo is called in the test before EqualApprox (which calls At).

We could reconstruct the necessary row of Q in each call to At which is terrible but hopefully nobody uses QR as Matrix except in the call to Solve?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other alternative is to lazily calculate Q for At the same way it is for ToQ, with a warning that it may cause OoM. I think your approach is probably better. A warning in the docs that it will be extremely inefficient should be enough.

Copy link

@tvkn tvkn Apr 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could lazily compute q in the At() method if q is nil or empty, in the same way it is done in the Qto.

But I guess I'm also a bit confused as to why we need q at all in At() in the first place. When we factorize a matrix a, qr stores a copy of that matrix. Why not return directly qr.At()?

Isn't https://github.com/gonum/gonum/blame/2ad11cabb395b96efc5b67fa1b64480762d9e703/mat/qr.go#L46 also faulty?
I'm expected we should return Q*R at element (i,j), but instead it seems we're returning Q*A at element (i,j), with A = Q*R, the matrix we factorize.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other alternative is to lazily calculate Q for At the same way it is for ToQ.

Agreed. If one element is needed, then most likely all are.

qr stores a copy of that matrix. Why not return directly qr.At()?

That copy is overwritten by lapack64.Geqrf which efficiently stores both Q and R in the same matrix.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another alternative is to indeed store a in Factorize and compute Q in QTo without storing it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we would need to copy a in this situation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the end I think it's best to lazily compute Q in At. QR.At should never be called in practice.

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.

mat: calling qr.Factorize leads to OOM for matrixes with many rows
3 participants