Coder Social home page Coder Social logo

Comments (4)

gomezzz avatar gomezzz commented on August 25, 2024

Hi @jk4011 !

Thanks for the bug report. So, my first suspicion would be that your data generator returns data that is on the CPU while you set up the default device to be GPU with set_up_backend("torch")?

Could you by any chance post a minimal code example to help me reproduce the problem? Then I would have a more detailed look.

Thanks!

from torchquad.

jk4011 avatar jk4011 commented on August 25, 2024

Hello @gomezzz
Of course!

import torch
from torchquad import set_up_backend
from torchvision import datasets
from torch.utils.data import DataLoader

# this error only occurs when cuda is available
assert(torch.cuda.is_available())

set_up_backend('torch')

training_data = datasets.MNIST(
    root="data",
    train=True,
    download=True,
)

train_loader = DataLoader(training_data, batch_size=64, shuffle=True)

for (i, x) in enumerate(train_loader):
    pass

and the error is like below

Traceback (most recent call last):
  File "/home/wlsgur4011/torchlevy/tmp.py", line 19, in <module>
    for (i, x) in enumerate(train_loader):
  File "/home/wlsgur4011/.anaconda3/envs/torchlevy/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 681, in __next__
    data = self._next_data()
  File "/home/wlsgur4011/.anaconda3/envs/torchlevy/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 720, in _next_data
    index = self._next_index()  # may raise StopIteration
  File "/home/wlsgur4011/.anaconda3/envs/torchlevy/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 671, in _next_index
    return next(self._sampler_iter)  # may raise StopIteration
  File "/home/wlsgur4011/.anaconda3/envs/torchlevy/lib/python3.8/site-packages/torch/utils/data/sampler.py", line 247, in __iter__
    for idx in self.sampler:
  File "/home/wlsgur4011/.anaconda3/envs/torchlevy/lib/python3.8/site-packages/torch/utils/data/sampler.py", line 132, in __iter__
    yield from torch.randperm(n, generator=generator).tolist()
RuntimeError: Expected a 'cuda' device type for generator but found 'cpu'

from torchquad.

gomezzz avatar gomezzz commented on August 25, 2024

Hi @jk4011

Sorry for the late reply 🙏 Busy days

Just tried the snippet and indeed same happens for me. However, I think, the problem is due to a missing device specification.

See also this stackoverflow post

Below snippet fixes the problem (and a follow-up one related to MNIST being PIL images) for me:

import torch
from torchquad import set_up_backend
from torchvision import datasets
from torch.utils.data import DataLoader
from torchvision import transforms

# this error only occurs when cuda is available
assert torch.cuda.is_available()

set_up_backend("torch")

transform = transforms.Compose(
    [
        # you can add other transformations in this list
        transforms.ToTensor()
    ]
)

training_data = datasets.MNIST(
    root="data", train=True, download=True, transform=transform
)

train_loader = DataLoader(
    training_data,
    batch_size=64,
    shuffle=True,
    generator=torch.Generator(device="cuda"),
)

for (i, x) in enumerate(train_loader):
    pass

Does it fix it for you too, @jk4011 ? :)

from torchquad.

gomezzz avatar gomezzz commented on August 25, 2024

Should be fixed / not a problem in torchquad

from torchquad.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.