-
Notifications
You must be signed in to change notification settings - Fork 26.3k
[ready] Add logdet and slogdet #5393
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
Conversation
|
@pytorchbot retest this please |
b8eb01e to
4395b8e
Compare
f3b2d0b to
f358631
Compare
b696396 to
2c53660
Compare
|
@ssnl I will have to use logdet for the implementation of the KL-divergence between two multivariate normal distributions. I would like to know how fast would this be compared to computing the determinant (using In fact, my question is motivated by this term: Would I be better off by doing
Thanks. |
|
putting speed aside, the gradient formulas permitting this would prob be more numerically stable |
|
Re @vishwakftw : @soumith is right. |
59b2ad9 to
43d60a8
Compare
|
@pytorchbot retest this please |
4ecd2fe to
338a869
Compare
a65f6f5 to
8ef991f
Compare
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
|
Hold on reviewing @zou3519 . I plan to move det computation from QR to LU factorization. It would be more consistent with things like numpy matlab and also won't be having problem with the inverse in backward. |
|
this comment is moved to top |
4759907 to
aba183d
Compare
|
@vishwakftw @samuela do one of you want to review this PR? Thanks in advance! :) |
vishwakftw
left a comment
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 think the code looks good. Thanks!!
2. Previously, det can return result with incorrect sign upon seeing symmetric
matrices. This is caused by the wrong assumption I had on SVD (when input is
symmetric U=V^T). This fixes it.
3. Moreover, after fixing 2 now QR is always needed for det forward. So I moved
SVD to backward call. Since this is a specific variant of SVD, it is named as
_svd_with_positive_UV_det, with derivative.yaml entry being svd_backward.
4. Updated/added backward functions for det, logdet and slogdet, which uses
_svd_with_positive_UV_det and svd_backward inside.
5. Optimized svd_backward:
a. Avoid unnecessary kernels when only sigma has gradient (this is the usual
case, and also true with *det backward functions).
b. Fix SVD double backward by avoiding a nan.
2. Fix an incorrect check for dim_args_idx in test_autograd.py 3. Add option to only test a subset of output values, specified by test_output_indices, for cases like slogdet where only the second output is differentiable. 4. Add better doc for the test generating list.
Add a scaling to random matrices so closeness checks are more robust
use svd only for non-invertible case, so don't need the special variant anymore
|
@vishwakftw @soumith Thanks!!! |

See commit messages for details of each commit. Notice that the last commit changes the QR based approach to LU based.
Some comments about switching from QR to LU:
geqrf) is generally more stable than LU (getrf).getrfgives specific information about matrix being singular or not. Furthermore, the backward (when det > 0) uses matrix inverse (getrf+getri), so usinggetrfin forward makes sense.getrf) to compute determinant.Therefore, in this PR, det is computed using
btrtifact(batchedgetrf). However, the backward still usesinverse(getrf+getri). So future work include:getriand cachegetrfresults to speed up backward.getrito support batched det methods.