-
-
Notifications
You must be signed in to change notification settings - Fork 986
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 pyro.primitives
& poutine.block_messenger
#3292
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,16 +4,30 @@ | |
from __future__ import annotations | ||
|
||
import functools | ||
from typing import TYPE_CHECKING, Callable, Dict, List, Optional, Set, Tuple, Union | ||
from typing import ( | ||
TYPE_CHECKING, | ||
Callable, | ||
Dict, | ||
List, | ||
Optional, | ||
Set, | ||
Tuple, | ||
TypeVar, | ||
Union, | ||
overload, | ||
) | ||
|
||
import torch | ||
from typing_extensions import TypedDict | ||
from typing_extensions import ParamSpec, TypedDict | ||
|
||
from pyro.params.param_store import ( # noqa: F401 | ||
_MODULE_NAMESPACE_DIVIDER, | ||
ParamStoreDict, | ||
) | ||
|
||
P = ParamSpec("P") | ||
T = TypeVar("T") | ||
|
||
if TYPE_CHECKING: | ||
from pyro.poutine.indep_messenger import CondIndepStackFrame | ||
from pyro.poutine.messenger import Messenger | ||
|
@@ -26,8 +40,8 @@ | |
|
||
|
||
class Message(TypedDict, total=False): | ||
type: Optional[str] | ||
name: str | ||
type: str | ||
name: Optional[str] | ||
fn: Callable | ||
is_observed: bool | ||
args: Tuple | ||
|
@@ -252,15 +266,31 @@ def apply_stack(initial_msg: Message) -> None: | |
cont(msg) | ||
|
||
|
||
def am_i_wrapped(): | ||
def am_i_wrapped() -> bool: | ||
""" | ||
Checks whether the current computation is wrapped in a poutine. | ||
:returns: bool | ||
""" | ||
return len(_PYRO_STACK) > 0 | ||
|
||
|
||
def effectful(fn: Optional[Callable] = None, type: Optional[str] = None) -> Callable: | ||
@overload | ||
def effectful( | ||
fn: None = ..., type: Optional[str] = ... | ||
) -> Callable[[Callable[P, T]], Callable[..., Union[T, torch.Tensor, None]]]: | ||
... | ||
|
||
|
||
@overload | ||
def effectful( | ||
fn: Callable[P, T] = ..., type: Optional[str] = ... | ||
) -> Callable[..., Union[T, torch.Tensor, None]]: | ||
... | ||
|
||
|
||
def effectful( | ||
fn: Optional[Callable[P, T]] = None, type: Optional[str] = None | ||
) -> Callable: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the best I could do at being specific with callable types. |
||
""" | ||
:param fn: function or callable that performs an effectful computation | ||
:param str type: the type label of the operation, e.g. `"sample"` | ||
|
@@ -277,32 +307,34 @@ def effectful(fn: Optional[Callable] = None, type: Optional[str] = None) -> Call | |
assert type != "message", "cannot use 'message' as keyword" | ||
|
||
@functools.wraps(fn) | ||
def _fn(*args, **kwargs): | ||
name = kwargs.pop("name", None) | ||
infer = kwargs.pop("infer", {}) | ||
|
||
value = kwargs.pop("obs", None) | ||
is_observed = value is not None | ||
def _fn( | ||
*args: P.args, | ||
name: Optional[str] = None, | ||
infer: Optional[Dict] = None, | ||
obs: Optional[torch.Tensor] = None, | ||
**kwargs: P.kwargs, | ||
) -> Union[T, torch.Tensor, None]: | ||
is_observed = obs is not None | ||
|
||
if not am_i_wrapped(): | ||
return fn(*args, **kwargs) | ||
else: | ||
msg = { | ||
"type": type, | ||
"name": name, | ||
"fn": fn, | ||
"is_observed": is_observed, | ||
"args": args, | ||
"kwargs": kwargs, | ||
"value": value, | ||
"scale": 1.0, | ||
"mask": None, | ||
"cond_indep_stack": (), | ||
"done": False, | ||
"stop": False, | ||
"continuation": None, | ||
"infer": infer, | ||
} | ||
msg = Message( | ||
type=type, | ||
name=name, | ||
fn=fn, | ||
is_observed=is_observed, | ||
args=args, | ||
kwargs=kwargs, | ||
value=obs, | ||
scale=1.0, | ||
mask=None, | ||
cond_indep_stack=(), | ||
done=False, | ||
stop=False, | ||
continuation=None, | ||
infer=infer if infer is not None else {}, | ||
) | ||
# apply the stack and return its return value | ||
apply_stack(msg) | ||
return msg["value"] | ||
|
@@ -321,22 +353,22 @@ def _inspect() -> Message: | |
:returns: A message with all effects applied. | ||
:rtype: dict | ||
""" | ||
msg: Message = { | ||
"type": "inspect", | ||
"name": "_pyro_inspect", | ||
"fn": lambda: True, | ||
"is_observed": False, | ||
"args": (), | ||
"kwargs": {}, | ||
"value": None, | ||
"infer": {"_do_not_trace": True}, | ||
"scale": 1.0, | ||
"mask": None, | ||
"cond_indep_stack": (), | ||
"done": False, | ||
"stop": False, | ||
"continuation": None, | ||
} | ||
msg = Message( | ||
type="inspect", | ||
name="_pyro_inspect", | ||
fn=lambda: True, | ||
is_observed=False, | ||
args=(), | ||
kwargs={}, | ||
value=None, | ||
infer={"_do_not_trace": True}, | ||
scale=1.0, | ||
mask=None, | ||
cond_indep_stack=(), | ||
done=False, | ||
stop=False, | ||
continuation=None, | ||
) | ||
apply_stack(msg) | ||
return msg | ||
|
||
|
Oops, something went wrong.
Oops, something went wrong.
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.
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.
In this file I type annotated only methods needed for
pyro.primitives