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

00b_How_to_use_numpy_arrays_in_fastai: AttributeError: attribute 'device' of 'torch._C._TensorBase' objects is not writable #500

Closed
jmp75 opened this issue May 4, 2022 · 2 comments
Labels
bug Something isn't working

Comments

@jmp75
Copy link
Contributor

jmp75 commented May 4, 2022

Running 00b_How_to_use_numpy_arrays_in_fastai.ipynb from source code as checked out today (below are commit hashs and repro version info). Reporting for the record; I'll see if I can find a fix and submit a PR.

Symptoms

dls      = DataLoaders(train_dl, valid_dl, device=default_device())
xb,yb = next(iter(dls.valid))

Call stack is:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [10], in <cell line: 6>()
      4 valid_dl = DataLoader(valid_ds, bs=128, num_workers=0)
      5 dls      = DataLoaders(train_dl, valid_dl, device=default_device())
----> 6 xb,yb = next(iter(dls.valid))
      7 print(xb, yb)
      8 print(f'shape: {str(len(train_ds)):10}   bs: {xb.shape}')

File ~/src/fastai/fastai/data/load.py:125, in DataLoader.__iter__(self)
    123 self.before_iter()
    124 self.__idxs=self.get_idxs() # called in context of main process (not workers/subprocesses)
--> 125 for b in _loaders[self.fake_l.num_workers==0](self.fake_l):
    126     # pin_memory causes tuples to be converted to lists, so convert them back to tuples
    127     if self.pin_memory and type(b) == list: b = tuple(b)
    128     if self.device is not None: b = to_device(b, self.device)

File ~/miniconda/envs/fai_pt/lib/python3.9/site-packages/torch/utils/data/dataloader.py:530, in _BaseDataLoaderIter.__next__(self)
    528 if self._sampler_iter is None:
    529     self._reset()
--> 530 data = self._next_data()
    531 self._num_yielded += 1
    532 if self._dataset_kind == _DatasetKind.Iterable and \
    533         self._IterableDataset_len_called is not None and \
    534         self._num_yielded > self._IterableDataset_len_called:

File ~/miniconda/envs/fai_pt/lib/python3.9/site-packages/torch/utils/data/dataloader.py:570, in _SingleProcessDataLoaderIter._next_data(self)
    568 def _next_data(self):
    569     index = self._next_index()  # may raise StopIteration
--> 570     data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
    571     if self._pin_memory:
    572         data = _utils.pin_memory.pin_memory(data)

File ~/miniconda/envs/fai_pt/lib/python3.9/site-packages/torch/utils/data/_utils/fetch.py:39, in _IterableDatasetFetcher.fetch(self, possibly_batched_index)
     37         raise StopIteration
     38 else:
---> 39     data = next(self.dataset_iter)
     40 return self.collate_fn(data)

File ~/src/fastai/fastai/data/load.py:136, in DataLoader.create_batches(self, samps)
    134 if self.dataset is not None: self.it = iter(self.dataset)
    135 res = filter(lambda o:o is not None, map(self.do_item, samps))
--> 136 yield from map(self.do_batch, self.chunkify(res))

File ~/miniconda/envs/fai_pt/lib/python3.9/site-packages/fastcore/basics.py:219, in chunked(it, chunk_sz, drop_last, n_chunks)
    217 if not isinstance(it, Iterator): it = iter(it)
    218 while True:
--> 219     res = list(itertools.islice(it, chunk_sz))
    220     if res and (len(res)==chunk_sz or not drop_last): yield res
    221     if len(res)<chunk_sz: return

File ~/src/fastai/fastai/data/load.py:151, in DataLoader.do_item(self, s)
    150 def do_item(self, s):
--> 151     try: return self.after_item(self.create_item(s))
    152     except SkipItemException: return None

File ~/src/fastai/fastai/data/load.py:158, in DataLoader.create_item(self, s)
    157 def create_item(self, s):
--> 158     if self.indexed: return self.dataset[s or 0]
    159     elif s is None:  return next(self.it)
    160     else: raise IndexError("Cannot index an iterable dataset numerically - must use `None`.")

File ~/src/tsai/tutorial_nbs/tsai/data/core.py:256, in TSDataset.__getitem__(self, idx)
    254     y = self._types[1](self.y[idx].compute(), device=self.device, dtype=self.dtype)
    255 else:
--> 256     y = self._types[1](self.y[idx], device=self.device, dtype=self.dtype)
    257 return (X, y)

File ~/src/fastai/fastai/torch_core.py:319, in TensorBase.__new__(cls, x, **kwargs)
    317 def __new__(cls, x, **kwargs):
    318     res = cast(tensor(x), cls)
--> 319     for k,v in kwargs.items(): setattr(res, k, v)
    320     return res

AttributeError: attribute 'device' of 'torch._C._TensorBase' objects is not writable

Environment

os             : Linux-5.10.0-13-amd64-x86_64-with-glibc2.31
python         : 3.9.12
tsai           : 0.3.2
fastai         : 2.6.4
fastcore       : 1.4.2
torch          : 1.11.0
device         : 1 gpu (['Quadro RTX 4000'])
cpu cores      : 16
RAM            : 62.45 GB
GPU memory     : [7.79] GB

To be a bit more precise since installed from source:

tsai:
commit 0b47c9d914c497f2c8128c184870c506a1d77d8b
Date:   Sat Apr 30 14:59:59 2022 +0200
fastai:
commit 2f2f9d9fa11e6da8527ef4852e72acd55cba259d
Date:   Sun May 1 00:01:46 2022 -0500
../fastcore:
commit cced1ec2b267d125cb3c49590475783f29a0343c
Date:   Thu Apr 21 14:09:35 2022 +1000
oguiza added a commit that referenced this issue May 13, 2022
@oguiza
Copy link
Contributor

oguiza commented May 13, 2022

Thanks for reporting this issue, @jmp75!
I've just fixed the issue and rerun the notebook. It should work now.
It'd be great if you try it in your environment. If it works, please, feel free to close the issue.

@oguiza oguiza added the bug Something isn't working label May 13, 2022
@jmp75
Copy link
Contributor Author

jmp75 commented Jun 21, 2022

Did a git pull origin main and indeed the issue is no more. Thank you!

@jmp75 jmp75 closed this as completed Jun 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants