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

fix equality of QRCompactWY #41363

Merged
merged 4 commits into from
Jun 28, 2021
Merged

fix equality of QRCompactWY #41363

merged 4 commits into from
Jun 28, 2021

Conversation

simeonschaub
Copy link
Member

Equality for QRCompactWY did not ignore the subdiagonal entries of
T leading to nondeterministic behavior. Perhaps T should be
directly stored as UpperTriangular in QRCompactWY, but that seems
potentially breaking.

This is pulled out from #41228, since this change should be less
controversial than the other changes there and this particular bug just
came up in ChainRules again.

Equality for `QRCompactWY` did not ignore the subdiagonal entries of
`T` leading to nondeterministic behavior. Perhaps `T` should be
directly stored as `UpperTriangular` in `QRCompactWY`, but that seems
potentially breaking.

This is pulled out from #41228, since this change should be less
controversial than the other changes there and this particular bug just
came up in ChainRules again.
@simeonschaub simeonschaub added linear algebra Linear algebra bugfix This change fixes an existing bug equality Issues relating to equality relations: ==, ===, isequal backport 1.7 labels Jun 25, 2021
@simeonschaub simeonschaub added the backport 1.6 Change should be backported to release-1.6 label Jun 25, 2021
@oxinabox
Copy link
Contributor

Perhaps T should be
directly stored as UpperTriangular in QRCompactWY, but that seems
potentially breaking.

Seems unlikely to be breaking.
It is a private field in a type that overloads getproperty
And to interact with it correctly you need to only interact with upper triangle

@@ -127,6 +127,16 @@ Base.iterate(S::QRCompactWY) = (S.Q, Val(:R))
Base.iterate(S::QRCompactWY, ::Val{:R}) = (S.R, Val(:done))
Base.iterate(S::QRCompactWY, ::Val{:done}) = nothing

function Base.hash(F::QRCompactWY, h::UInt)
return hash(F.factors, hash(UpperTriangular(F.T), hash(QRCompactWY, h)))
Copy link
Member

Choose a reason for hiding this comment

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

This is not correct. Generally, the T matrix will not be square but wide where the number of rows is a block size, see

qr!(A::StridedMatrix{<:BlasFloat}, ::NoPivot; blocksize=36) =
, and the number of columns is min(size(A)...). For matrices smaller than the block size T will be square but for larger matrices, it will consist of several triangular blocks side by side, so the hashing here will be slightly more complicated and depend on the block size.

Copy link
Member Author

@simeonschaub simeonschaub Jun 27, 2021

Choose a reason for hiding this comment

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

Ah, I see! (Random appreciation for the new sparse matrix printing:

julia> using LinearAlgebra, SparseArrays

julia> a = rand(100, 100);

julia> sparse(qr(a).T .== qr(a).T)
36×100 SparseMatrixCSC{Bool, Int64} with 1767 stored entries:
⠙⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠙⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠙⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⠀⠀⠙⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠙⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠙⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⠀⠀⠀⠀⠙⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠙⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠙⢿⣿⣿⣿⣿⣿⣿⣿⣿
⠀⠀⠀⠀⠀⠂⠛⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠙⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠙⢿⣿⣿⣿⣿⣿⣿
⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⢀⠐⠙⢿⣿⣿⣿⣿
⠀⠀⠐⠀⠀⠀⠀⠀⠀⢀⢙⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠁⠀⡀⠀⠙⢿⣿⣿
⠀⠀⠐⠀⠀⠀⠀⠀⠀⠀⠄⠀⠙⢿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⡀⠀⠀⢀⠀⠀⠙⢿
⠀⡀⠀⠀⠀⠀⠀⠀⠂⠒⠒⠀⠀⠀⠙⢿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣿⣿⠀⠀⠀⠀⠀⠀⠀⢀⠀⠀⠀⡀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣈⡀⠀⠀⠀⠀⠀⠀⠙⢿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⠀⠀⠀⠀⠀⠀⠀⠀⠀⡀⠂⠀⢀⠀

)

@simeonschaub
Copy link
Member Author

@oxinabox See Andreas' comment. I was misinformed, unfortunately just making T an upper triangular matrix does not work for larger sizes.

@simeonschaub simeonschaub merged commit 74fab49 into master Jun 28, 2021
@simeonschaub simeonschaub deleted the sds/fix_qr_eq branch June 28, 2021 08:28
simeonschaub added a commit that referenced this pull request Jun 28, 2021
Equality for `QRCompactWY` did not ignore the subdiagonal entries of
`T` leading to nondeterministic behavior.

This is pulled out from #41228, since this change should be less
controversial than the other changes there and this particular bug just
came up in ChainRules again.
@simeonschaub simeonschaub removed the backport 1.6 Change should be backported to release-1.6 label Jun 28, 2021
KristofferC pushed a commit that referenced this pull request Jun 29, 2021
* fix equality of QRCompactWY (#41363)

Equality for `QRCompactWY` did not ignore the subdiagonal entries of
`T` leading to nondeterministic behavior.

This is pulled out from #41228, since this change should be less
controversial than the other changes there and this particular bug just
came up in ChainRules again.
@KristofferC KristofferC mentioned this pull request Jun 29, 2021
45 tasks
KristofferC pushed a commit that referenced this pull request Jun 30, 2021
Equality for `QRCompactWY` did not ignore the subdiagonal entries of
`T` leading to nondeterministic behavior.

This is pulled out from #41228, since this change should be less
controversial than the other changes there and this particular bug just
came up in ChainRules again.

(cherry picked from commit 74fab49)
johanmon pushed a commit to johanmon/julia that referenced this pull request Jul 5, 2021
Equality for `QRCompactWY` did not ignore the subdiagonal entries of
`T` leading to nondeterministic behavior.

This is pulled out from JuliaLang#41228, since this change should be less
controversial than the other changes there and this particular bug just
came up in ChainRules again.
staticfloat pushed a commit that referenced this pull request Dec 23, 2022
* fix equality of QRCompactWY (#41363)

Equality for `QRCompactWY` did not ignore the subdiagonal entries of
`T` leading to nondeterministic behavior.

This is pulled out from #41228, since this change should be less
controversial than the other changes there and this particular bug just
came up in ChainRules again.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix This change fixes an existing bug equality Issues relating to equality relations: ==, ===, isequal linear algebra Linear algebra
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants