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

Misc fixes to contrib.epidemiology #2527

Merged
merged 4 commits into from
Jun 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions examples/contrib/epidemiology/sir.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def generate_data(args):


def infer(args, model):
parallel = args.num_chains > 1
energies = []

def hook_fn(kernel, *unused):
Expand All @@ -92,16 +93,17 @@ def hook_fn(kernel, *unused):
warmup_steps=args.warmup_steps,
num_samples=args.num_samples,
num_chains=args.num_chains,
mp_context="spawn" if parallel else None,
Copy link
Collaborator

Choose a reason for hiding this comment

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

unfortunately i've found this to be somewhat buggy (fair number of crashes)

Copy link
Member Author

@fritzo fritzo Jun 15, 2020

Choose a reason for hiding this comment

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

do you use "forkserver" instead?

Copy link
Collaborator

Choose a reason for hiding this comment

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

no i've used spawn. but i've tried to avoid it...

max_tree_depth=args.max_tree_depth,
arrowhead_mass=args.arrowhead_mass,
num_quant_bins=args.num_bins,
haar=args.haar,
haar_full_mass=args.haar_full_mass,
jit_compile=args.jit,
hook_fn=hook_fn)
hook_fn=None if parallel else hook_fn)

mcmc.summary()
if args.plot:
if args.plot and energies:
import matplotlib.pyplot as plt
plt.figure(figsize=(6, 3))
plt.plot(energies)
Expand Down
6 changes: 5 additions & 1 deletion pyro/infer/mcmc/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,12 @@ def model(data):
# If transforms is not explicitly provided, infer automatically using
# model args, kwargs.
if self.transforms is None:
# Try to initialize kernel.transforms using kernel.setup().
if getattr(self.kernel, "transforms", None) is None:
warmup_steps = 0
self.kernel.setup(warmup_steps, *args, **kwargs)
# Use `kernel.transforms` when available
if hasattr(self.kernel, 'transforms') and self.kernel.transforms is not None:
if getattr(self.kernel, "transforms", None) is not None:
Comment on lines +411 to +416
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 is required because manually calling initialize_model() below would ignore init_strategy and attempt to sample from ImproperUniform. Instead we let kernel.setup() call initialize_model() with proper arguments.

self.transforms = self.kernel.transforms
# Else, get transforms from model (e.g. in multiprocessing).
elif self.kernel.model:
Expand Down
6 changes: 3 additions & 3 deletions tests/contrib/epidemiology/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
{"jit_compile": True},
{"jit_compile": True, "haar_full_mass": 2},
{"jit_compile": True, "num_quant_bins": 2},
{"num_chains": 2},
{"num_chains": 2, "num_quant_bins": 2},
{"num_chains": 2, "jit_compile": True},
{"num_chains": 2, "mp_context": "spawn"},
{"num_chains": 2, "mp_context": "spawn", "num_quant_bins": 2},
{"num_chains": 2, "mp_context": "spawn", "jit_compile": True},
], ids=str)
def test_simple_sir_smoke(duration, forecast, options):
population = 100
Expand Down