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

Concatenate torch docstrings at end of distribution wrappers #3246

Merged
merged 4 commits into from
Jul 25, 2023
Merged
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: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ scrub: FORCE
find tutorial -name "*.ipynb" | xargs python tutorial/source/cleannb.py

doctest: FORCE
python -m pytest -p tests.doctest_fixtures --doctest-modules -o filterwarnings=ignore pyro
# We skip testing pyro.distributions.torch wrapper classes because
# they include torch docstrings which are tested upstream.
python -m pytest -p tests.doctest_fixtures --doctest-modules -o filterwarnings=ignore pyro --ignore=pyro/distributions/torch.py

perf-test: FORCE
bash scripts/perf_test.sh ${ref}
Expand Down
1 change: 1 addition & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"sphinx.ext.graphviz", #
"sphinx.ext.autodoc",
"sphinx.ext.doctest",
'sphinx.ext.napoleon',
]

# Disable documentation inheritance so as to avoid inheriting
Expand Down
2 changes: 2 additions & 0 deletions pyro/contrib/forecast/forecaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ def model(self, zero_data, covariates):
@property
def time_plate(self):
"""
Helper to create a ``pyro.plate`` over time.

:returns: A plate named "time" with size ``covariates.size(-2)`` and
``dim=-1``. This is available only during model execution.
:rtype: :class:`~pyro.plate`
Expand Down
15 changes: 14 additions & 1 deletion pyro/distributions/torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# SPDX-License-Identifier: Apache-2.0

import math
import re
import textwrap

import torch

Expand Down Expand Up @@ -334,6 +336,16 @@ def support(self):
return constraints.interval(self._unbroadcasted_low, self._unbroadcasted_high)


def _cat_docstrings(*docstrings):
result = "\n".join(textwrap.dedent(s.lstrip("\n")) for s in docstrings)
result = re.sub("\n\n+", "\n\n", result)
# Drop torch-specific lines.
result = "".join(
line for line in result.splitlines(keepends=True) if "xdoctest" not in line
)
return result


# Programmatically load all distributions from PyTorch.
__all__ = []
for _name, _Dist in torch.distributions.__dict__.items():
Expand All @@ -354,10 +366,11 @@ def support(self):
_PyroDist.__doc__ = """
Wraps :class:`{}.{}` with
:class:`~pyro.distributions.torch_distribution.TorchDistributionMixin`.

""".format(
_Dist.__module__, _Dist.__name__
)

_PyroDist.__doc__ = _cat_docstrings(_PyroDist.__doc__, _Dist.__doc__)
__all__.append(_name)


Expand Down
Loading