-
Notifications
You must be signed in to change notification settings - Fork 89
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
Add rules for det
and logdet
of Cholesky
#613
Changes from 2 commits
54995d6
ac246e7
40e0890
e7e929b
92385c9
6d11837
c4f0d7a
1482a95
d831cd4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -551,3 +551,24 @@ function rrule(::typeof(getproperty), F::T, x::Symbol) where {T <: Cholesky} | |||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||
return getproperty(F, x), getproperty_cholesky_pullback | ||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
# `det` and `logdet` for `Cholesky` | ||||||||||||||||||||||||||||||||||||||
function rrule(::typeof(det), C::Cholesky) | ||||||||||||||||||||||||||||||||||||||
y = det(C) | ||||||||||||||||||||||||||||||||||||||
s = conj!((2 * y) ./ _diag_view(C.factors)) | ||||||||||||||||||||||||||||||||||||||
function det_Cholesky_pullback(ȳ) | ||||||||||||||||||||||||||||||||||||||
ΔC = Tangent{typeof(C)}(; factors=Diagonal(ȳ .* s)) | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would something like this be better? There's one fewer allocation, and we don't need to assume that
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the determinant is 0 (can happen if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Seems like an improvement to me, thanks! 🙂 There's a whitespace missing in the first line of your suggestion it seem:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It would happen if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, never mind, of course, at least one element of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe even a bit clearer (without having to know about precedence of operators):
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess something like the following could work? # compute `x / conj(y)`, handling `x = y = 0`
function _x_divide_conj_y(x, y)
z = x / conj(y)
# in our case `iszero(x)` implies `iszero(y)`
return iszero(x) ? zero(z) : z
end
function rrule(::typeof(det), C::Cholesky)
y = det(C)
diagF = _diag_view(C.factors)
function det_Cholesky_pullback(ȳ)
ΔC = Tangent{typeof(C)}(; factors=Diagonal(_x_divide_conj_y.(2 * ȳ * conj(y), diagF))) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sethaxen I updated the PR and added tests for singular matrices. |
||||||||||||||||||||||||||||||||||||||
return NoTangent(), ΔC | ||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||
return y, det_Cholesky_pullback | ||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
function rrule(::typeof(logdet), C::Cholesky) | ||||||||||||||||||||||||||||||||||||||
y = logdet(C) | ||||||||||||||||||||||||||||||||||||||
s = conj!((2 * one(eltype(C))) ./ _diag_view(C.factors)) | ||||||||||||||||||||||||||||||||||||||
function logdet_Cholesky_pullback(ȳ) | ||||||||||||||||||||||||||||||||||||||
ΔC = Tangent{typeof(C)}(; factors=Diagonal(ȳ .* s)) | ||||||||||||||||||||||||||||||||||||||
devmotion marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||
return NoTangent(), ΔC | ||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||
return y, logdet_Cholesky_pullback | ||||||||||||||||||||||||||||||||||||||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if these changes could be split out of this PR then we could meged this much faster
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reverted these changes.