Skip to content
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

Type annotate broadcast, condition, do, enum messengers #3295

Merged
merged 7 commits into from
Nov 27, 2023

Conversation

ordabayevy
Copy link
Member

No description provided.

@ordabayevy ordabayevy added the WIP label Nov 20, 2023
pyro/poutine/broadcast_messenger.py Show resolved Hide resolved
@@ -76,7 +78,11 @@ def _pyro_sample(self, msg):
intervention = self.data[msg["name"]]
msg["name"] = msg["name"] + "__CF" # mangle old name

if isinstance(intervention, (numbers.Number, torch.Tensor)):
if isinstance(intervention, numbers.Number):
msg["value"] = torch.tensor(intervention)
Copy link
Member Author

Choose a reason for hiding this comment

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

It might be a good idea to constrain msg["value"] to be torch.Tensor because in many places it is expected to be a torch.Tensor by attempting to access torch.Tensor attributes.

Copy link
Member

Choose a reason for hiding this comment

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

@eb8680 can you make sure this is safe? We don't want to break any Chirho code.


# if this site has any ancestors, choose ancestors from diagonal approximation
thin_sample = fat_sample
if thin_sample.shape != target_shape:
index = [Ellipsis] + [slice(None)] * (len(thin_sample.shape) - 1)
index: List[Any] = [Ellipsis] + [slice(None)] * (len(thin_sample.shape) - 1)
Copy link
Member Author

Choose a reason for hiding this comment

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

This can be narrowed down once we annotate Vindex

Copy link
Member

@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.

Looks great, and like it was hard work getting this to check. typing_extensions sure makes things easy.

I just have a couple comments about change paranoia.

@@ -27,7 +28,7 @@ class TorchDistributionMixin(Distribution):
from :class:`TorchDistributionMixin`.
"""

def __call__(self, sample_shape: torch.Size = torch.Size()) -> torch.Tensor:
def __call__(self, sample_shape: Tuple[int] = torch.Size()) -> torch.Tensor:
Copy link
Member

Choose a reason for hiding this comment

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

Are you sure this is safe? There are a few places in Pyro where an actual torch.Size object is required, as it has special methods like .numel. Just checking 🙂

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, not sure about it. Changed it back and adjusted elsewhere to convert tuple arguments into torch.Size

pyro/poutine/broadcast_messenger.py Show resolved Hide resolved
@@ -76,7 +78,11 @@ def _pyro_sample(self, msg):
intervention = self.data[msg["name"]]
msg["name"] = msg["name"] + "__CF" # mangle old name

if isinstance(intervention, (numbers.Number, torch.Tensor)):
if isinstance(intervention, numbers.Number):
msg["value"] = torch.tensor(intervention)
Copy link
Member

Choose a reason for hiding this comment

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

@eb8680 can you make sure this is safe? We don't want to break any Chirho code.

Copy link
Member

@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, @ordabayevy is this ready to merge?

@ordabayevy ordabayevy merged commit c1ebdf3 into dev Nov 27, 2023
9 checks passed
@ordabayevy ordabayevy deleted the type-messengers branch November 27, 2023 03:40
@ordabayevy ordabayevy mentioned this pull request Feb 5, 2024
23 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants