Skip to content

Conversation

@tillahoffmann
Copy link
Contributor

@tillahoffmann tillahoffmann commented May 3, 2022

The PositiveDefiniteTransform is required to transform from an unconstrained space to positive definite matrices, e.g. to support testing the Wishart mode in #76690. It is a simple extension of the LowerCholeskyTransform.

I've also added a small test that ensures the generated data belong to the domain of the associated transform. Previously, the data generated for the inverse transform of the LowerCholeskyTransform wasn't part of the domain, and the test only passed because the comparison uses equal_nan=True.

cc @fritzo, @neerajprad

@facebook-github-bot
Copy link
Contributor

facebook-github-bot commented May 3, 2022

🔗 Helpful links

✅ No Failures (0 Pending)

As of commit c61fde6e3f (more details on the Dr. CI page):

Expand to see more

💚 💚 Looks good so far! There are no failures yet. 💚 💚


This comment was automatically generated by Dr. CI (expand for details).

Please report bugs/suggestions to the (internal) Dr. CI Users group.

Click here to manually regenerate this comment.

@bdhirsh bdhirsh requested review from lezcano and neerajprad May 4, 2022 21:02
@bdhirsh bdhirsh added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label May 4, 2022
Copy link
Collaborator

@lezcano lezcano left a comment

Choose a reason for hiding this comment

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

The implementation makes sense to me. I'll let others review whether this is a reasonable PR in the context of distributions (I am not involved in its maintenance).

@neerajprad
Copy link
Contributor

LGTM, thanks for adding this! cc @nonconvexopt - could you take a look as well?

@nonconvexopt
Copy link
Contributor

LGTM, thanks for adding this! cc @nonconvexopt - could you take a look as well?

@neerajprad Thank you for tagging me. It is good to know how PyTorch is improving.
@tillahoffmann I think it is essential implementation to the torch.distributions for future transformed distribution developement. Thank you for the contribution.

@tillahoffmann
Copy link
Contributor Author

I think this one is ready to be merged. Would you be able to take another look, @neerajprad? Thank you for the time reviewing my distribution-related PRs.

@facebook-github-bot
Copy link
Contributor

/easycla

As part of the transition to the PyTorch Foundation, this project now requires contributions be covered under the new CLA. See #85559 for additional details.

This comment will trigger a new check of this PR. If you are already covered, you will simply see a new "EasyCLA" check that passes. If you are not covered, a bot will leave a new comment with a link to sign.

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Oct 3, 2022

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: tillahoffmann / name: Till Hoffmann (395a36d044655b6c19b32b264a3aa2c438774d28, 519a419f9c9cad1672a5f98917abaa787f8c24ce, 61430a6fc650bf5ebfbe69f7cbe4fa29cd642413, 3027c02a8f497cdd7b51b3c33bce4f1699194384, c61fde6e3f8aee85ea30a1a5804488ee029b3eb0)

@tillahoffmann
Copy link
Contributor Author

Hi @neerajprad, I've now signed the CLA. Would you mind taking another look?

@lezcano
Copy link
Collaborator

lezcano commented Nov 30, 2022

@pytorchbot rebase

Copy link
Collaborator

@lezcano lezcano left a comment

Choose a reason for hiding this comment

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

The linalg part looks good. I'll let the people from distributions review the rest.

@pytorchmergebot
Copy link
Collaborator

@pytorchbot successfully started a rebase job. Check the current status here

@pytorchmergebot
Copy link
Collaborator

Successfully rebased positivedefinite onto refs/remotes/origin/viable/strict, please pull locally before adding more changes (for example, via git checkout positivedefinite && git pull --rebase)

@pytorch-bot
Copy link

pytorch-bot bot commented Nov 30, 2022

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/76777

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 378a0cd:
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@lezcano
Copy link
Collaborator

lezcano commented Nov 30, 2022

@fritzo, @neerajprad PTAL

Copy link
Collaborator

@fritzo fritzo left a comment

Choose a reason for hiding this comment

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

Thanks @tillahoffmann, everything looks good except my one comment on inheritance. After that's fixed LGTM!

@tillahoffmann
Copy link
Contributor Author

Thanks for the review, @fritzo. I couldn't find the comment on inheritance, unfortunately. Which line was that on?

Copy link
Collaborator

@fritzo fritzo Dec 2, 2022

Choose a reason for hiding this comment

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

Please avoid inheriting from LowerCholeskyTransform here. Instead either own an instance or use the global instance. I think something like this should work:

class PositiveDefiniteTransform(Transform):
    def __init__(self, cache_size=0):
        super.__init__(cache_size=cache_size)
        self._lower_cholesky = LowerCholeskyTransform(cache_size=cache_size)
    def _call(self, x):
        x = self._lower_cholesky(x)
        return x @ x.mT
    def _inverse(self, y):
        y = torch.linalg.cholesky(y)
        return self._lower_cholesky.inv(y)

Copy link
Collaborator

Choose a reason for hiding this comment

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

just for my understanding, why is this prefered over the current approach?

Copy link
Collaborator

@fritzo fritzo Dec 3, 2022

Choose a reason for hiding this comment

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

In practice, downstream users may dispatch on transforms via singledispatch, multipledispatch, or isinstance, which could yield incorrect behavior due to the false match. This could also lead to a missed typechecking error, where a LowerCholeskyTransform is required, a PositiveDefiniteTransform is provided, and the typechecker erroneously thinks that's fine.

In theory, the two classes do not satisfy a subtyping relation, an is-a relationship: PositiveDefiniteTransform is not a special case of LowerCholeskyTransform.

(sorry I had tried to explain in my earlier review, but apparently forgot to save the comment)

Copy link
Contributor Author

@tillahoffmann tillahoffmann Dec 5, 2022

Choose a reason for hiding this comment

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

I've removed the inheritance. Overriding __init__ also required overriding with_cache (see below), and I decided to instantiate a new LowerCholeskyTransform for each transform. The overhead seems minimal, but happy to make adjustments as you see fit. I couldn't find the global instance of LowerCholeskyTransform.

if type(self).__init__ is Transform.__init__:
return type(self)(cache_size=cache_size)
raise NotImplementedError("{}.with_cache is not implemented".format(type(self)))

Copy link
Collaborator

@fritzo fritzo left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for resolving the inheritance issue!

@fritzo
Copy link
Collaborator

fritzo commented Dec 5, 2022

@lezcano would you merge this please? (i believe i lost merge privileges)

@lezcano
Copy link
Collaborator

lezcano commented Dec 5, 2022

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Dec 5, 2022
@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot
Copy link
Collaborator

Merge failed

Reason: The following mandatory check(s) failed (Rule Distributions):

Dig deeper by viewing the failures on hud

Details for Dev Infra team Raised by workflow job

@soumith
Copy link
Contributor

soumith commented Dec 8, 2022

@pytorchbot rebase

@pytorchmergebot
Copy link
Collaborator

@pytorchbot successfully started a rebase job. Check the current status here

@pytorchmergebot
Copy link
Collaborator

Successfully rebased positivedefinite onto refs/remotes/origin/viable/strict, please pull locally before adding more changes (for example, via git checkout positivedefinite && git pull --rebase)

@soumith soumith removed the request for review from neerajprad December 8, 2022 06:19
@soumith
Copy link
Contributor

soumith commented Dec 8, 2022

@pytorchbot merge

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

kulinseth pushed a commit to kulinseth/pytorch that referenced this pull request Dec 10, 2022
The `PositiveDefiniteTransform` is required to transform from an unconstrained space to positive definite matrices, e.g. to support testing the Wishart mode in pytorch#76690. It is a simple extension of the `LowerCholeskyTransform`.

I've also added a small test that ensures the generated data belong to the domain of the associated transform. Previously, the data generated for the inverse transform of the `LowerCholeskyTransform` wasn't part of the domain, and the test only passed because the comparison uses `equal_nan=True`.

Pull Request resolved: pytorch#76777
Approved by: https://github.com/lezcano, https://github.com/fritzo, https://github.com/soumith
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request cla signed Merged open source triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants