Refactored rational-quadratic spline transforms to run faster#44
Open
vsimkus wants to merge 1 commit intobayesiains:masterfrom
Open
Refactored rational-quadratic spline transforms to run faster#44vsimkus wants to merge 1 commit intobayesiains:masterfrom
vsimkus wants to merge 1 commit intobayesiains:masterfrom
Conversation
vsimkus
commented
Jun 10, 2021
| else: | ||
| raise RuntimeError("{} tails are not implemented.".format(tails)) | ||
|
|
||
| if torch.any(inside_interval_mask): |
Author
There was a problem hiding this comment.
Also, removed this check, as I suppose most of the time it will evaluate to true as you would expect some inputs to be in the domain. Let me know if you'd prefer it added back.
Author
There was a problem hiding this comment.
I see this was added in #25. With the new implementation there won't be any crashes with all-tail inputs either. (Except the computations will essentially be wasted, but I don't suppose we're expecting many calls with all-tails inputs.)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi,
I've noticed that the methods in
rational_quadratic.pycan be easily refactored to make them run ~25% faster.The main change in
unconstrained_rational_quadratic_splineis to avoid using masked select, which can be quite inefficient with dense masks, since it requires assembling all the "unmasked" elements into a new tensor. Instead, in order to do masked insert into a predefined zero tensor, it is generally cheaper to multiply the input tensor with a mask and add it to the target tensor, as I've done in this PR.I've also made a couple of changes in
rational_quadratic_splineabout computingwidths,heightsandcumwidhts,cumheightstensors. The refactored implementation removes the redundancy of some of the operations in the original implementation.The rational-quadratic spline flow as used in the NSF paper runs about 25% faster with these changes. I think some further improvements can be achieved if the
searchsortedis replaced withtorch.searchsortedwhen ran with the custom CUDA kernel as described in #19, but I haven't touched it since it would affect the other spline flows too.I suppose the other spline flow methods can be refactored in a similar way. If you'd prefer I can make the necessary changes to them too in this PR.
Best,
Vaidotas