Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions pyro/poutine/subsample_messenger.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ def _subsample(
"""
Helper function for plate. See its docstrings for details.
"""
if size is not None and not torch._C._get_tracing_state() and size < 0:
raise ValueError(
"size must be a positive integer, got {}".format(size)
)
if size is None:
assert subsample_size is None
assert subsample is None
Expand Down
12 changes: 12 additions & 0 deletions tests/poutine/test_poutines.py
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,18 @@ def model():
assert len(_DIM_ALLOCATOR._stack) == 0, "stack was not cleaned on error"


@pytest.mark.parametrize("size", [-1, -10])
def test_plate_error_on_negative_size(size):
def model():
with pyro.plate("foo", size):
pass

assert len(_DIM_ALLOCATOR._stack) == 0
with pytest.raises(ValueError, match="size must be a positive integer"):
poutine.trace(model)()
assert len(_DIM_ALLOCATOR._stack) == 0, "stack was not cleaned on error"


@pytest.mark.parametrize(
"graph_type, expected", [("flat", set()), ("dense", {"x", "y"})]
)
Expand Down
Loading