Skip to content

Commit 6a337d5

Browse files
authored
Add sphinx docs (#10)
1 parent 0b0e4ec commit 6a337d5

File tree

8 files changed

+282
-1
lines changed

8 files changed

+282
-1
lines changed

Makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.PHONY: all install docs lint test clean FORCE
2+
3+
all: docs test
4+
5+
install:
6+
pip install -e .[dev,test]
7+
8+
docs: FORCE
9+
make -C docs html
10+
11+
lint: FORCE
12+
flake8
13+
14+
test: lint FORCE
15+
pytest -vx test
16+
17+
clean: FORCE
18+
git clean -dfx -e pyroapi-egg.info
19+
20+
FORCE:

docs/Makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Minimal makefile for Sphinx documentation
2+
#
3+
4+
# You can set these variables from the command line.
5+
SPHINXOPTS =
6+
SPHINXBUILD = sphinx-build
7+
SOURCEDIR = source
8+
BUILDDIR = build
9+
10+
# Put it first so that "make" without argument is like "make help".
11+
help:
12+
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
13+
14+
.PHONY: help Makefile
15+
16+
# Catch-all target: route all unknown targets to Sphinx using the new
17+
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
18+
%: Makefile
19+
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

docs/make.bat

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
@ECHO OFF
2+
3+
pushd %~dp0
4+
5+
REM Command file for Sphinx documentation
6+
7+
if "%SPHINXBUILD%" == "" (
8+
set SPHINXBUILD=sphinx-build
9+
)
10+
set SOURCEDIR=source
11+
set BUILDDIR=build
12+
13+
if "%1" == "" goto help
14+
15+
%SPHINXBUILD% >NUL 2>NUL
16+
if errorlevel 9009 (
17+
echo.
18+
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
19+
echo.installed, then set the SPHINXBUILD environment variable to point
20+
echo.to the full path of the 'sphinx-build' executable. Alternatively you
21+
echo.may add the Sphinx directory to PATH.
22+
echo.
23+
echo.If you don't have Sphinx installed, grab it from
24+
echo.http://sphinx-doc.org/
25+
exit /b 1
26+
)
27+
28+
%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS%
29+
goto end
30+
31+
:help
32+
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS%
33+
34+
:end
35+
popd

docs/source/conf.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import os
2+
import sys
3+
4+
import sphinx_rtd_theme
5+
# Configuration file for the Sphinx documentation builder.
6+
#
7+
# This file only contains a selection of the most common options. For a full
8+
# list see the documentation:
9+
# http://www.sphinx-doc.org/en/master/config
10+
11+
# -- Path setup --------------------------------------------------------------
12+
13+
# If extensions (or modules to document with autodoc) are in another directory,
14+
# add these directories to sys.path here. If the directory is relative to the
15+
# documentation root, use os.path.abspath to make it absolute, like shown here.
16+
#
17+
sys.path.insert(0, os.path.abspath('../..'))
18+
19+
# -- Project information -----------------------------------------------------
20+
21+
project = u'Pyro API'
22+
copyright = u'2019, Uber Technologies, Inc'
23+
author = u'Uber AI Labs'
24+
25+
# The full version, including alpha/beta/rc tags
26+
release = u'0.0'
27+
28+
29+
# -- General configuration ---------------------------------------------------
30+
31+
# Add any Sphinx extension module names here, as strings. They can be
32+
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
33+
# ones.
34+
extensions = [
35+
'sphinx.ext.autodoc',
36+
'sphinx.ext.doctest',
37+
'sphinx.ext.intersphinx',
38+
'sphinx.ext.mathjax',
39+
'sphinx.ext.viewcode',
40+
]
41+
42+
# Disable documentation inheritance so as to avoid inheriting
43+
# docstrings in a different format, e.g. when the parent class
44+
# is a PyTorch class.
45+
46+
autodoc_inherit_docstrings = False
47+
48+
# Add any paths that contain templates here, relative to this directory.
49+
templates_path = ['_templates']
50+
51+
# The suffix(es) of source filenames.
52+
# You can specify multiple suffix as a list of string:
53+
#
54+
# source_suffix = ['.rst', '.md']
55+
source_suffix = '.rst'
56+
57+
# The master toctree document.
58+
master_doc = 'index'
59+
60+
# The language for content autogenerated by Sphinx. Refer to documentation
61+
# for a list of supported languages.
62+
#
63+
# This is also used if you do content translation via gettext catalogs.
64+
# Usually you set "language" from the command line for these cases.
65+
language = None
66+
67+
# List of patterns, relative to source directory, that match files and
68+
# directories to ignore when looking for source files.
69+
# This pattern also affects html_static_path and html_extra_path.
70+
exclude_patterns = []
71+
72+
# The name of the Pygments (syntax highlighting) style to use.
73+
pygments_style = 'sphinx'
74+
75+
76+
# do not prepend module name to functions
77+
add_module_names = False
78+
79+
# -- Options for HTML output -------------------------------------------------
80+
81+
# The theme to use for HTML and HTML Help pages. See the documentation for
82+
# a list of builtin themes.
83+
#
84+
html_theme = "sphinx_rtd_theme"
85+
html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]
86+
87+
# Add any paths that contain custom static files (such as style sheets) here,
88+
# relative to this directory. They are copied after the builtin static files,
89+
# so a file named "default.css" will overwrite the builtin "default.css".
90+
html_static_path = ['_static']

docs/source/dispatch.rst

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Dispatch
2+
========
3+
4+
It's easiest to see how to use pyroapi by example:
5+
6+
.. code-block:: python
7+
8+
from pyroapi import distributions as dist
9+
from pyroapi import infer, ops, optim, pyro, pyro_backend
10+
11+
# These model and guide are backend-agnostic.
12+
def model():
13+
locs = pyro.param("locs", ops.tensor([0.2, 0.3, 0.5]))
14+
p = ops.tensor([0.2, 0.3, 0.5])
15+
with pyro.plate("plate", len(data), dim=-1):
16+
x = pyro.sample("x", dist.Categorical(p))
17+
pyro.sample("obs", dist.Normal(locs[x], 1.), obs=data)
18+
19+
def guide():
20+
p = pyro.param("p", ops.tensor([0.5, 0.3, 0.2]))
21+
with pyro.plate("plate", len(data), dim=-1):
22+
pyro.sample("x", dist.Categorical(p))
23+
24+
# We can now set a backend at inference time.
25+
with pyro_backend("numpyro"):
26+
elbo = infer.Trace_ELBO(ignore_jit_warnings=True)
27+
adam = optim.Adam({"lr": 1e-6})
28+
inference = infer.SVI(model, guide, adam, elbo)
29+
for step in range(10):
30+
loss = inference.step(*args, **kwargs)
31+
print(f"step {step} loss = {loss}")
32+
33+
.. automodule:: pyroapi.dispatch
34+
.. autofunction:: pyroapi.dispatch.pyro_backend
35+
36+
Generic Modules
37+
---------------
38+
- pyro - The main pyro module.
39+
- distributions - Includes distributions.transforms and distributions.constraints.
40+
- handlers - Generalizing the original pyro.poutine.
41+
- infer - Inference algorithms.
42+
- optim - Optimization utilities.
43+
- ops - Basic tensor operations (like numpy or torch).

docs/source/index.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
.. pyroapi documentation master file, created by
2+
sphinx-quickstart on Fri Oct 18 13:54:39 2019.
3+
You can adapt this file completely to your liking, but it should at least
4+
contain the root `toctree` directive.
5+
6+
Pyro API
7+
========
8+
9+
The ``pyroapi`` package dynamically dispatches among multiple Pyro backends, including standard Pyro_, NumPyro_, Funsor_, and custom user-defined backends.
10+
This package includes both **dispatch** mechanisms for use in model and inference code, and **testing** utilities to help develop and test new Pyro backends.
11+
12+
.. _Pyro: https://pyro.ai
13+
.. _NumPyro: https://num.pyro.ai
14+
.. _Funsor: https://funsor.pyro.ai
15+
16+
.. toctree::
17+
:maxdepth: 2
18+
:caption: Contents:
19+
20+
dispatch
21+
testing
22+
23+
24+
Indices and tables
25+
==================
26+
27+
* :ref:`genindex`
28+
* :ref:`modindex`
29+
* :ref:`search`

docs/source/testing.rst

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Testing
2+
=======
3+
4+
The pyroapi package includes tests to ensure new backends conform to the standard API, indeed these tests serve as the formal API description.
5+
To add tests to your new backend say in ``project/test/`` follow these steps (or see the example_ in funsor):
6+
7+
.. _example: https://github.com/pyro-ppl/funsor/tree/master/test/pyroapi
8+
9+
1. Create a new directory ``project/test/pyroapi/``.
10+
11+
2. Create a file ``project/test/pyroapi/conftest.py`` and a hook to treat missing features as xfail:
12+
13+
.. code-block:: python
14+
15+
import pytest
16+
17+
18+
def pytest_runtest_call(item):
19+
try:
20+
item.runtest()
21+
except NotImplementedError as e:
22+
pytest.xfail(str(e))
23+
24+
3. Create a file ``project/test/pyroapi/test_pyroapi.py`` and define a ``backend`` fixture:
25+
26+
.. code-block:: python
27+
28+
import pytest
29+
from pyroapi import pyro_backend
30+
from pyroapi.tests import * # noqa F401
31+
32+
@pytest.yield_fixture
33+
def backend():
34+
with pyro_backend("my_backend"):
35+
yield
36+
37+
4. Test your backend with pytest
38+
39+
.. code-block:: bash
40+
41+
pytest -vx test/pyroapi

setup.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@
3737
'numpyro@https://api.github.com/repos/pyro-ppl/numpyro/tarball/master',
3838
'funsor@https://api.github.com/repos/pyro-ppl/funsor/tarball/master',
3939
],
40-
'dev': ['ipython'],
40+
'dev': [
41+
'sphinx>=2.0',
42+
'sphinx_rtd_theme',
43+
'ipython',
44+
],
4145
},
4246
long_description=long_description,
4347
long_description_content_type='text/markdown',

0 commit comments

Comments
 (0)