-
Notifications
You must be signed in to change notification settings - Fork 95
Add rules for det and logdet of Cholesky
#613
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
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)) | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
| s = conj!((2 * y) ./ _diag_view(C.factors)) | |
| function det_Cholesky_pullback(ȳ) | |
| ΔC = Tangent{typeof(C)}(; factors=Diagonal(ȳ .* s)) | |
| diagF = _diag_view(C.factors) | |
| function det_Cholesky_pullback(ȳ) | |
| ΔC = Tangent{typeof(C)}(; factors=Diagonal(2(ȳ * conj(y)) ./ conj.(diagF))) |
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 the determinant is 0 (can happen if check=false is passed to cholesky), this will inject NaNs, even if the cotangent is 0. Since we try to treat cotangents as strong zeros, it would be nice to handle this case by ensuring that such NaNs end up as zeros.
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.
There's one fewer allocation, and we don't need to assume that s is mutable.
Seems like an improvement to me, thanks! 🙂
There's a whitespace missing in the first line of your suggestion it seem:
| s = conj!((2 * y) ./ _diag_view(C.factors)) | |
| function det_Cholesky_pullback(ȳ) | |
| ΔC = Tangent{typeof(C)}(; factors=Diagonal(ȳ .* s)) | |
| diagF = _diag_view(C.factors) | |
| function det_Cholesky_pullback(ȳ) | |
| ΔC = Tangent{typeof(C)}(; factors=Diagonal(2(ȳ * conj(y)) ./ conj.(diagF))) |
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 the determinant is 0
It would happen if y = 0 (the determinant) but also if ȳ = 0. Should we care about the last case as well? Or is it correct to return NaN there?
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.
Ah, never mind, of course, at least one element of diagF is zero iffy = 0. I.e., we only have to care about y = 0.
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.
Maybe even a bit clearer (without having to know about precedence of operators):
| s = conj!((2 * y) ./ _diag_view(C.factors)) | |
| function det_Cholesky_pullback(ȳ) | |
| ΔC = Tangent{typeof(C)}(; factors=Diagonal(ȳ .* s)) | |
| diagF = _diag_view(C.factors) | |
| function det_Cholesky_pullback(ȳ) | |
| ΔC = Tangent{typeof(C)}(; factors=Diagonal( (2 * (ȳ * conj(y))) ./ conj.(diagF))) |
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 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 comment
The reason will be displayed to describe this comment to others. Learn more.
@sethaxen I updated the PR and added tests for singular matrices.
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.