-
Notifications
You must be signed in to change notification settings - Fork 267
Remove uses of OrderedDict #2046
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
Open
sid-kap
wants to merge
2
commits into
pyro-ppl:master
Choose a base branch
from
sid-kap:no-ordered-dict-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 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 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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,8 @@ | |
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from abc import ABC, abstractmethod | ||
from collections import OrderedDict, namedtuple | ||
from typing import Any, Callable, OrderedDict as OrderedDictType, Union | ||
from collections import defaultdict | ||
from typing import Any, Callable, Union | ||
|
||
import jax | ||
from jax import random | ||
|
@@ -61,7 +61,7 @@ def __init__(self, model: Callable, num_slp_samples: int, max_slps: int) -> None | |
|
||
def _find_slps( | ||
self, rng_key: jax.Array, *args: Any, **kwargs: Any | ||
) -> dict[str, OrderedDictType]: | ||
) -> dict[str, dict]: | ||
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. I'm not sure but it seems that we need to use OrderedDict in this module. |
||
""" | ||
Discover the straight-line programs (SLPs) in the model by sampling from the prior. | ||
This implementation assumes that all branching is done via discrete sampling sites | ||
|
@@ -80,11 +80,11 @@ def _find_slps( | |
|
||
return branching_traces | ||
|
||
def _get_branching_trace(self, tr: dict[str, Any]) -> OrderedDictType: | ||
def _get_branching_trace(self, tr: dict[str, Any]) -> dict: | ||
""" | ||
Extract the sites from the trace that are annotated with `infer={"branching": True}`. | ||
""" | ||
branching_trace = OrderedDict() | ||
branching_trace = {} | ||
for site in tr.values(): | ||
if ( | ||
site["type"] == "sample" | ||
|
@@ -109,7 +109,7 @@ def _get_branching_trace(self, tr: dict[str, Any]) -> OrderedDictType: | |
def _run_inference( | ||
self, | ||
rng_key: jax.Array, | ||
branching_trace: OrderedDictType, | ||
branching_trace: dict, | ||
*args: Any, | ||
**kwargs: Any, | ||
) -> RunInferenceResult: | ||
|
@@ -120,7 +120,7 @@ def _combine_inferences( | |
self, | ||
rng_key: jax.Array, | ||
inferences: dict[str, Any], | ||
branching_traces: dict[str, OrderedDictType], | ||
branching_traces: dict[str, dict], | ||
*args: Any, | ||
**kwargs: Any, | ||
) -> Union[DCCResult, SDVIResult]: | ||
|
@@ -139,7 +139,7 @@ def run( | |
rng_key, subkey = random.split(rng_key) | ||
branching_traces = self._find_slps(subkey, *args, **kwargs) | ||
|
||
inferences = dict() | ||
inferences = {} | ||
for key, bt in branching_traces.items(): | ||
rng_key, subkey = random.split(rng_key) | ||
inferences[key] = self._run_inference(subkey, bt, *args, **kwargs) | ||
|
@@ -209,7 +209,7 @@ def __init__( | |
def _run_inference( | ||
self, | ||
rng_key: jax.Array, | ||
branching_trace: OrderedDictType, | ||
branching_trace: dict, | ||
*args: Any, | ||
**kwargs: Any, | ||
) -> RunInferenceResult: | ||
|
@@ -227,7 +227,7 @@ def _combine_inferences( # type: ignore[override] | |
self, | ||
rng_key: jax.Array, | ||
samples: dict[str, Any], | ||
branching_traces: dict[str, OrderedDictType], | ||
branching_traces: dict[str, dict], | ||
*args: Any, | ||
**kwargs: Any, | ||
) -> DCCResult: | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
# Copyright Contributors to the Pyro project. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from typing import Any, Callable, OrderedDict as OrderedDictType | ||
from typing import Any, Callable | ||
|
||
import jax | ||
import jax.numpy as jnp | ||
|
@@ -98,7 +98,7 @@ def __init__( | |
def _run_inference( | ||
self, | ||
rng_key: jax.Array, | ||
branching_trace: OrderedDictType, | ||
branching_trace: dict, | ||
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. like above, maybe we need to use OrderedDict in this module. |
||
*args: Any, | ||
**kwargs: Any, | ||
) -> RunInferenceResult: | ||
|
@@ -121,7 +121,7 @@ def _combine_inferences( # type: ignore[override] | |
self, | ||
rng_key: jax.Array, | ||
guides: dict[str, tuple[AutoGuide, dict[str, Any]]], | ||
branching_traces: dict[str, OrderedDictType], | ||
branching_traces: dict[str, dict], | ||
*args: Any, | ||
**kwargs: Any, | ||
) -> SDVIResult: | ||
|
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
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
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.
Let's keep TraceT as OrderedDict