Skip to content

Commit

Permalink
Update to using pytorch-1.0-rc (#1431)
Browse files Browse the repository at this point in the history
  • Loading branch information
neerajprad authored and fritzo committed Nov 28, 2018
1 parent 43155bf commit 835aa31
Show file tree
Hide file tree
Showing 58 changed files with 870 additions and 1,197 deletions.
7 changes: 2 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ cache:

install:
- pip install -U pip
- if [[ $TRAVIS_PYTHON_VERSION == 2.7 ]]; then
pip install http://download.pytorch.org/whl/cpu/torch-0.4.0-cp27-cp27mu-linux_x86_64.whl;
else
pip install http://download.pytorch.org/whl/cpu/torch-0.4.0-cp36-cp36m-linux_x86_64.whl;
fi
- pip install torch_nightly==1.0.0.dev20181128 -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html
- pip install torchvision --no-dependencies
- pip install .[test]
- pip freeze

Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ test-cuda: lint FORCE
CUDA_TEST=1 PYRO_TENSOR_TYPE=torch.cuda.DoubleTensor pytest -vx --stage unit
CUDA_TEST=1 pytest -vx tests/test_examples.py::test_cuda

test-jit: FORCE
@echo See jit.log
pytest -v -n auto --tb=short --runxfail tests/infer/test_jit.py tests/test_examples.py::test_jit | tee jit.log
pytest -v -n auto --tb=short --runxfail tests/infer/mcmc/test_hmc.py tests/infer/mcmc/test_nuts.py \
-k JIT=True | tee -a jit.log

clean: FORCE
git clean -dfx -e pyro-egg.info

Expand Down
25 changes: 6 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,14 @@ Make sure that the models come from the same release version of the [Pyro source

For recent features you can install Pyro from source.

To install a compatible CPU version of PyTorch on OSX / Linux, you
could use the PyTorch install helper script.
To install a compatible version of PyTorch, use the PyTorch nightly
[build](https://pytorch.org/). We recommend pinning to the specific
nightly build below that has been well tested.

```
bash scripts/install_pytorch.sh
```

Alternatively, build PyTorch following instructions in the PyTorch
[README](https://github.com/pytorch/pytorch/blob/master/README.md).
```sh
git clone --recursive https://github.com/pytorch/pytorch
cd pytorch
git checkout 200fb22 # <---- a well-tested commit
```
On Linux:
```sh
python setup.py install
```
On OSX:
```sh
MACOSX_DEPLOYMENT_TARGET=10.9 CC=clang CXX=clang++ python setup.py install
build_ver=1.0.0.dev20181127 # <---- a well-tested PyTorch build
pip install torch_nightly==${build_ver} -f \
https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html
```

Finally install Pyro using pip or from source as shown below.
Expand Down
9 changes: 5 additions & 4 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ def setup(app):

# @jpchen's hack to get rtd builder to install latest pytorch
if 'READTHEDOCS' in os.environ:
os.system('curl -o install.sh https://raw.githubusercontent.com/uber/pyro/dev/scripts/install_pytorch.sh')
os.system('curl https://raw.githubusercontent.com/uber/pyro/dev/README.md > README.md')
os.system('bash install.sh')
os.system('rm -f install.sh')
os.system('pip install http://download.pytorch.org/whl/cpu/torch-0.4.1-cp27-cp27mu-linux_x86_64.whl')
# for pytorch 1.0 (currently fails with OOM
# https://readthedocs.org/projects/pyro-ppl/builds/8159615/
# os.system('pip install torch_nightly==1.0.0.dev20181127 -f '
# 'https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html')
22 changes: 0 additions & 22 deletions docs/source/distributions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,6 @@ AVFMultivariateNormal
:undoc-members:
:show-inheritance:

Binomial
--------

.. autoclass:: pyro.distributions.Binomial
:members:
:undoc-members:
:show-inheritance:

Delta
-----
.. autoclass:: pyro.distributions.Delta
Expand All @@ -85,20 +77,6 @@ GaussianScaleMixture
:undoc-members:
:show-inheritance:

HalfCauchy
----------
.. autoclass:: pyro.distributions.HalfCauchy
:members:
:undoc-members:
:show-inheritance:

LowRankMultivariateNormal
-------------------------
.. autoclass:: pyro.distributions.LowRankMultivariateNormal
:members:
:undoc-members:
:show-inheritance:

MaskedMixture
-------------
.. autoclass:: pyro.distributions.MaskedMixture
Expand Down
2 changes: 1 addition & 1 deletion docs/source/primitives.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ Primitives
.. autofunction:: pyro.validation_enabled
.. autofunction:: pyro.enable_validation

.. autofunction:: pyro.ops.jit.compile
.. autofunction:: pyro.ops.jit.trace
2 changes: 2 additions & 0 deletions examples/baseball.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,5 +333,7 @@ def main(args):
parser.add_argument("--num-chains", nargs='?', default=4, type=int)
parser.add_argument("--warmup-steps", nargs='?', default=100, type=int)
parser.add_argument("--rng_seed", nargs='?', default=0, type=int)
parser.add_argument('--jit', action='store_true', default=False,
help='use PyTorch jit')
args = parser.parse_args()
main(args)
3 changes: 2 additions & 1 deletion examples/eight_schools/mcmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def conditioned_model(model, sigma, y):


def main(args):
nuts_kernel = NUTS(conditioned_model, adapt_step_size=True)
nuts_kernel = NUTS(conditioned_model, jit_compile=args.jit,)
posterior = MCMC(nuts_kernel,
num_samples=args.num_samples,
warmup_steps=args.warmup_steps,
Expand All @@ -58,6 +58,7 @@ def main(args):
help='number of parallel MCMC chains (default: 1)')
parser.add_argument('--warmup-steps', type=int, default=1000,
help='number of MCMC samples for warmup (default: 1000)')
parser.add_argument('--jit', action='store_true', default=False)
args = parser.parse_args()

main(args)
4 changes: 2 additions & 2 deletions examples/hmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def main(args):
# We'll train on small minibatches.
logging.info('Step\tLoss')
for step in range(args.num_steps):
loss = svi.step(sequences, lengths, args, batch_size=args.batch_size)
loss = svi.step(sequences, lengths, args=args, batch_size=args.batch_size)
logging.info('{: >5d}\t{}'.format(step, loss / num_observations))

# We evaluate on the entire training dataset,
Expand All @@ -340,7 +340,7 @@ def main(args):
if args.truncate:
lengths.clamp_(max=args.truncate)
num_observations = float(lengths.sum())
test_loss = elbo.loss(model, guide, sequences, lengths, args, include_prior=False)
test_loss = elbo.loss(model, guide, sequences, lengths, args=args, include_prior=False)
logging.info('test loss = {}'.format(test_loss / num_observations))

# We expect models with higher capacity to perform better,
Expand Down
1 change: 0 additions & 1 deletion examples/vae/utils/mnist_cached.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

from pyro.contrib.examples.util import get_data_directory


# This file contains utilities for caching, transforming and splitting MNIST data
# efficiently. By default, a PyTorch DataLoader will apply the transform every epoch
# we avoid this by caching the data early on in MNISTCached class
Expand Down
2 changes: 1 addition & 1 deletion pyro/contrib/autoguide/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ def laplace_approximation(self, *args, **kwargs):
loc = pyro.param("{}_loc".format(self.prefix))
H = hessian(loss, loc.unconstrained())
cov = H.inverse()
scale_tril = cov.potrf(upper=False)
scale_tril = cov.cholesky()

# calculate scale_tril from self.guide()
scale_tril_name = "{}_scale_tril".format(self.prefix)
Expand Down
25 changes: 21 additions & 4 deletions pyro/contrib/examples/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,23 @@
import os
import sys

import torchvision
import torchvision.datasets as datasets
from torch.utils.data import DataLoader
from torchvision import transforms
from torchvision.datasets import MNIST

from pyro.distributions.torch_patch import patch_dependency


@patch_dependency('torchvision.datasets.MNIST', torchvision)
class _MNIST(getattr(MNIST, '_pyro_unpatched', MNIST)):
urls = [
"https://d2fefpcigoriu7.cloudfront.net/datasets/mnist/train-images-idx3-ubyte.gz",
"https://d2fefpcigoriu7.cloudfront.net/datasets/mnist/train-labels-idx1-ubyte.gz",
"https://d2fefpcigoriu7.cloudfront.net/datasets/mnist/t10k-images-idx3-ubyte.gz",
"https://d2fefpcigoriu7.cloudfront.net/datasets/mnist/t10k-labels-idx1-ubyte.gz",
]


def get_data_loader(dataset_name,
Expand All @@ -18,11 +32,14 @@ def get_data_loader(dataset_name,
dataset_transforms = []
trans = transforms.Compose([transforms.ToTensor()] + dataset_transforms)
dataset = getattr(datasets, dataset_name)
print("downloading data")
dset = dataset(root=data_dir,
train=is_training_set,
transform=trans,
download=True)
print("download complete.")
return DataLoader(
dataset(root=data_dir,
train=is_training_set,
transform=trans,
download=True),
dset,
batch_size=batch_size,
shuffle=shuffle
)
Expand Down
8 changes: 4 additions & 4 deletions pyro/contrib/gp/models/gplvm.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from __future__ import absolute_import, division, print_function

import torch
from torch.distributions import constraints
from torch.nn import Parameter

import pyro
from pyro.contrib.gp.util import Parameterized
import pyro.distributions as dist
import pyro.infer as infer
import pyro.optim as optim
from pyro.contrib.gp.util import Parameterized
from pyro.distributions.util import eye_like
from pyro.params import param_with_module_name


Expand Down Expand Up @@ -74,7 +74,7 @@ def __init__(self, base_model, name="GPLVM"):

C = self.X_loc.shape[1]
X_scale_tril_shape = self.X_loc.shape + (C,)
Id = torch.eye(C, out=self.X_loc.new_empty(C, C))
Id = eye_like(self.X_loc, C)
X_scale_tril = Id.expand(X_scale_tril_shape)
self.X_scale_tril = Parameter(X_scale_tril)
self.set_constraint("X_scale_tril", constraints.lower_cholesky)
Expand All @@ -87,7 +87,7 @@ def model(self):
# sample X from unit multivariate normal distribution
zero_loc = self.X_loc.new_zeros(self.X_loc.shape)
C = self.X_loc.shape[1]
Id = torch.eye(C, out=self.X_loc.new_empty(C, C))
Id = eye_like(self.X_loc, C)
X_name = param_with_module_name(self.name, "X")
X = pyro.sample(X_name, dist.MultivariateNormal(zero_loc, scale_tril=Id)
.independent(zero_loc.dim()-1))
Expand Down
6 changes: 3 additions & 3 deletions pyro/contrib/gp/models/gpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def model(self):
N = self.X.shape[0]
Kff = self.kernel(self.X)
Kff.view(-1)[::N + 1] += noise # add noise to diagonal
Lff = Kff.potrf(upper=False)
Lff = Kff.cholesky()

zero_loc = self.X.new_zeros(self.X.shape[0])
f_loc = zero_loc + self.mean_function(self.X)
Expand Down Expand Up @@ -129,7 +129,7 @@ def forward(self, Xnew, full_cov=False, noiseless=True):
N = self.X.shape[0]
Kff = self.kernel(self.X).contiguous()
Kff.view(-1)[::N + 1] += noise # add noise to the diagonal
Lff = Kff.potrf(upper=False)
Lff = Kff.cholesky()

y_residual = self.y - self.mean_function(self.X)
loc, cov = conditional(Xnew, self.X, self.kernel, y_residual, None, Lff,
Expand Down Expand Up @@ -185,7 +185,7 @@ def sample_next(xnew, outside_vars):
X, y, Kff = outside_vars["X"], outside_vars["y"], outside_vars["Kff"]

# Compute Cholesky decomposition of kernel matrix
Lff = Kff.potrf(upper=False)
Lff = Kff.cholesky()
y_residual = y - self.mean_function(X)

# Compute conditional mean and variance
Expand Down
6 changes: 3 additions & 3 deletions pyro/contrib/gp/models/sgpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def model(self):
M = Xu.shape[0]
Kuu = self.kernel(Xu).contiguous()
Kuu.view(-1)[::M + 1] += self.jitter # add jitter to the diagonal
Luu = Kuu.potrf(upper=False)
Luu = Kuu.cholesky()
Kuf = self.kernel(Xu, self.X)
W = Kuf.trtrs(Luu, upper=False)[0].t()

Expand Down Expand Up @@ -210,7 +210,7 @@ def forward(self, Xnew, full_cov=False, noiseless=True):

Kuu = self.kernel(Xu).contiguous()
Kuu.view(-1)[::M + 1] += self.jitter # add jitter to the diagonal
Luu = Kuu.potrf(upper=False)
Luu = Kuu.cholesky()
Kus = self.kernel(Xu, Xnew)
Kuf = self.kernel(Xu, self.X)

Expand All @@ -225,7 +225,7 @@ def forward(self, Xnew, full_cov=False, noiseless=True):
W_Dinv = W / D
K = W_Dinv.matmul(W.t()).contiguous()
K.view(-1)[::M + 1] += 1 # add identity matrix to K
L = K.potrf(upper=False)
L = K.cholesky()

# get y_residual and convert it into 2D tensor for packing
y_residual = self.y - self.mean_function(self.X)
Expand Down
7 changes: 4 additions & 3 deletions pyro/contrib/gp/models/vgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pyro.distributions as dist
from pyro.contrib.gp.models.model import GPModel
from pyro.contrib.gp.util import conditional
from pyro.distributions.util import eye_like
from pyro.params import param_with_module_name


Expand Down Expand Up @@ -74,7 +75,7 @@ def __init__(self, X, y, kernel, likelihood, mean_function=None,
self.f_loc = Parameter(f_loc)

f_scale_tril_shape = self.latent_shape + (N, N)
Id = torch.eye(N, out=self.X.new_empty(N, N))
Id = eye_like(self.X, N)
f_scale_tril = Id.expand(f_scale_tril_shape)
self.f_scale_tril = Parameter(f_scale_tril)
self.set_constraint("f_scale_tril", constraints.lower_cholesky)
Expand All @@ -90,13 +91,13 @@ def model(self):
N = self.X.shape[0]
Kff = self.kernel(self.X).contiguous()
Kff.view(-1)[::N + 1] += self.jitter # add jitter to the diagonal
Lff = Kff.potrf(upper=False)
Lff = Kff.cholesky()

zero_loc = self.X.new_zeros(f_loc.shape)
f_name = param_with_module_name(self.name, "f")

if self.whiten:
Id = torch.eye(N, out=self.X.new_empty(N, N))
Id = eye_like(self.X, N)
pyro.sample(f_name,
dist.MultivariateNormal(zero_loc, scale_tril=Id)
.independent(zero_loc.dim() - 1))
Expand Down
7 changes: 4 additions & 3 deletions pyro/contrib/gp/models/vsgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pyro.poutine as poutine
from pyro.contrib.gp.models.model import GPModel
from pyro.contrib.gp.util import conditional
from pyro.distributions.util import eye_like
from pyro.params import param_with_module_name


Expand Down Expand Up @@ -98,7 +99,7 @@ def __init__(self, X, y, kernel, Xu, likelihood, mean_function=None,
self.u_loc = Parameter(u_loc)

u_scale_tril_shape = self.latent_shape + (M, M)
Id = torch.eye(M, out=self.Xu.new_empty(M, M))
Id = eye_like(self.Xu, M)
u_scale_tril = Id.expand(u_scale_tril_shape)
self.u_scale_tril = Parameter(u_scale_tril)
self.set_constraint("u_scale_tril", constraints.lower_cholesky)
Expand All @@ -115,12 +116,12 @@ def model(self):
M = Xu.shape[0]
Kuu = self.kernel(Xu).contiguous()
Kuu.view(-1)[::M + 1] += self.jitter # add jitter to the diagonal
Luu = Kuu.potrf(upper=False)
Luu = Kuu.cholesky()

zero_loc = Xu.new_zeros(u_loc.shape)
u_name = param_with_module_name(self.name, "u")
if self.whiten:
Id = torch.eye(M, out=Xu.new_empty(M, M))
Id = eye_like(Xu, M)
pyro.sample(u_name,
dist.MultivariateNormal(zero_loc, scale_tril=Id)
.independent(zero_loc.dim() - 1))
Expand Down
2 changes: 1 addition & 1 deletion pyro/contrib/gp/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def conditional(Xnew, X, kernel, f_loc, f_scale_tril=None, Lff=None, full_cov=Fa
if Lff is None:
Kff = kernel(X).contiguous()
Kff.view(-1)[::N + 1] += jitter # add jitter to diagonal
Lff = Kff.potrf(upper=False)
Lff = Kff.cholesky()
Kfs = kernel(X, Xnew)

# convert f_loc_shape from latent_shape x N to N x latent_shape
Expand Down
Loading

0 comments on commit 835aa31

Please sign in to comment.