sbi: RuntimeError Expected all tensors to be on the same device at append_simulations

I made sure that input tensors to append_simulations be on cuda, but still have an error.

>>>theta.is_cuda 
True
>>>x.is_cuda
True

device = "cuda"

inference = SNPE(prior, density_estimator, device)

inference = inference.append_simulations(theta, x)  # error appeares here
density_estimator = inference.train()
raise RuntimeError(rte, message)
RuntimeError: (RuntimeError('Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0! (when checking arugment for argument mat1 in method wrapper_addmm)'), '')

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 15 (9 by maintainers)

Most upvoted comments

net = inf.append_simulations(theta, x)

shouldn’t that read append_simulations(theta_, x_)?

The following works for me:

import torch
from sbi.inference import SNPE
from sbi.utils import BoxUniform

num_dim = 3
prior = BoxUniform(low=-2*torch.ones(num_dim), high=2*torch.ones(num_dim), device="cuda:0")

def simulator(parameter_set):
    return 1.0 + parameter_set + torch.randn(parameter_set.shape) * 0.1

theta = prior.sample((300,)).to("cpu")
x = simulator(theta)

theta_ = theta.to("cuda:0")
x_ = x.to("cuda:0")

inf = SNPE(prior=prior, device="cuda:0")

net = inf.append_simulations(theta_, x_)

Maybe the issue got fixed along the way? Feel free to reopen if the issue persists on the latest version of sbi.