question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] RuntimeError: cannot pin 'torch.cuda.FloatTensor' only dense CPU tensors can be pinned

See original GitHub issue

Describe the bug When trying to train the NBEATS model on GPU, I get the error:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-21-cd62ef13c2ee> in <module>
     10     ts_mad,
     11     ts_bcn
---> 12 ], verbose=True)

~/Environments/base/lib/python3.7/site-packages/darts/utils/torch.py in decorator(self, *args, **kwargs)
     63         with fork_rng():
     64             manual_seed(self._random_instance.randint(0, high=MAX_TORCH_SEED_VALUE))
---> 65             return decorated(self, *args, **kwargs)
     66     return decorator

~/Environments/base/lib/python3.7/site-packages/darts/models/torch_forecasting_model.py in fit(self, series, past_covariates, future_covariates, val_series, val_past_covariates, val_future_covariates, verbose, epochs)
    429         logger.info('Train dataset contains {} samples.'.format(len(train_dataset)))
    430 
--> 431         self.fit_from_dataset(train_dataset, val_dataset, verbose, epochs)
    432 
    433     @random_method

~/Environments/base/lib/python3.7/site-packages/darts/utils/torch.py in decorator(self, *args, **kwargs)
     63         with fork_rng():
     64             manual_seed(self._random_instance.randint(0, high=MAX_TORCH_SEED_VALUE))
---> 65             return decorated(self, *args, **kwargs)
     66     return decorator

~/Environments/base/lib/python3.7/site-packages/darts/models/torch_forecasting_model.py in fit_from_dataset(self, train_dataset, val_dataset, verbose, epochs)
    517 
    518         # Train model
--> 519         self._train(train_loader, val_loader, tb_writer, verbose, train_num_epochs)
    520 
    521         # Close tensorboard writer

~/Environments/base/lib/python3.7/site-packages/darts/models/torch_forecasting_model.py in _train(self, train_loader, val_loader, tb_writer, verbose, epochs)
    790             total_loss = 0
    791 
--> 792             for batch_idx, train_batch in enumerate(train_loader):
    793                 self.model.train()
    794                 output = self._produce_train_output(train_batch[:-1])

~/Environments/base/lib/python3.7/site-packages/torch/utils/data/dataloader.py in __next__(self)
    515             if self._sampler_iter is None:
    516                 self._reset()
--> 517             data = self._next_data()
    518             self._num_yielded += 1
    519             if self._dataset_kind == _DatasetKind.Iterable and \

~/Environments/base/lib/python3.7/site-packages/torch/utils/data/dataloader.py in _next_data(self)
    557         data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
    558         if self._pin_memory:
--> 559             data = _utils.pin_memory.pin_memory(data)
    560         return data
    561 

~/Environments/base/lib/python3.7/site-packages/torch/utils/data/_utils/pin_memory.py in pin_memory(data)
     53         return type(data)(*(pin_memory(sample) for sample in data))
     54     elif isinstance(data, container_abcs.Sequence):
---> 55         return [pin_memory(sample) for sample in data]
     56     elif hasattr(data, "pin_memory"):
     57         return data.pin_memory()

~/Environments/base/lib/python3.7/site-packages/torch/utils/data/_utils/pin_memory.py in <listcomp>(.0)
     53         return type(data)(*(pin_memory(sample) for sample in data))
     54     elif isinstance(data, container_abcs.Sequence):
---> 55         return [pin_memory(sample) for sample in data]
     56     elif hasattr(data, "pin_memory"):
     57         return data.pin_memory()

~/Environments/base/lib/python3.7/site-packages/torch/utils/data/_utils/pin_memory.py in pin_memory(data)
     45 def pin_memory(data):
     46     if isinstance(data, torch.Tensor):
---> 47         return data.pin_memory()
     48     elif isinstance(data, string_classes):
     49         return data

RuntimeError: cannot pin 'torch.cuda.FloatTensor' only dense CPU tensors can be pinned

To Reproduce

ts = TimeSeries.from_series(series.dropna().astype('float32'), freq='D')

model = NBEATSModel(
    input_chunk_length=365, 
    output_chunk_length=51,
    torch_device_str='cuda',
    force_reset=True
)

model.fit(ts, verbose=True)

System (please complete the following information):

  • Python version: 3.7.7
  • darts version: 0.10.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
hrzncommented, Aug 19, 2021

The bug has been solved in v0.10.1. Thanks again for reporting.

0reactions
Ayesha-Ancommented, Dec 19, 2022

Running semantic segmentation using UNET

RuntimeError Traceback (most recent call last) Input In [18], in <cell line: 2>() 1 model = create_model(params) ----> 2 model = train_and_validate(model, train_data, test_data, params)

Input In [16], in train_and_validate(model, train_dataset, val_dataset, params) 17 optimizer = torch.optim.Adam(model.parameters(), lr=params[“lr”]) 18 for epoch in range(1, params[“epochs”] + 1): —> 19 train(train_loader, model, criterion, optimizer, epoch, params) 20 validate(val_loader, model, criterion, epoch, params) 21 return model

Input In [13], in train(train_loader, model, criterion, optimizer, epoch, params) 3 model.train() 4 stream = tqdm(train_loader) ----> 5 for i, (images, target) in enumerate(stream, start=1): 6 images = images.to(params[“device”], non_blocking=True) 7 target = target.to(params[“device”], non_blocking=True)

File ~\Anaconda3\envs\py38\lib\site-packages\tqdm\std.py:1195, in tqdm.iter(self) 1192 time = self._time 1194 try: -> 1195 for obj in iterable: 1196 yield obj 1197 # Update and possibly print the progressbar. 1198 # Note: does not call self.update(1) for speed optimisation.

File ~\Anaconda3\envs\py38\lib\site-packages\torch\utils\data\dataloader.py:681, in _BaseDataLoaderIter.next(self) 678 if self._sampler_iter is None: 679 # TODO(https://github.com/pytorch/pytorch/issues/76750) 680 self._reset() # type: ignore[call-arg] –> 681 data = self._next_data() 682 self._num_yielded += 1 683 if self._dataset_kind == _DatasetKind.Iterable and
684 self._IterableDataset_len_called is not None and
685 self._num_yielded > self._IterableDataset_len_called:

File ~\Anaconda3\envs\py38\lib\site-packages\torch\utils\data\dataloader.py:723, in _SingleProcessDataLoaderIter._next_data(self) 721 data = self._dataset_fetcher.fetch(index) # may raise StopIteration 722 if self._pin_memory: –> 723 data = _utils.pin_memory.pin_memory(data, self._pin_memory_device) 724 return data

File ~\Anaconda3\envs\py38\lib\site-packages\torch\utils\data_utils\pin_memory.py:65, in pin_memory(data, device) 63 elif isinstance(data, collections.abc.Sequence): 64 try: —> 65 return type(data)([pin_memory(sample, device) for sample in data]) # type: ignore[call-arg] 66 except TypeError: 67 # The sequence type may not support __init__(iterable) (e.g., range). 68 return [pin_memory(sample, device) for sample in data]

File ~\Anaconda3\envs\py38\lib\site-packages\torch\utils\data_utils\pin_memory.py:65, in <listcomp>(.0) 63 elif isinstance(data, collections.abc.Sequence): 64 try: —> 65 return type(data)([pin_memory(sample, device) for sample in data]) # type: ignore[call-arg] 66 except TypeError: 67 # The sequence type may not support __init__(iterable) (e.g., range). 68 return [pin_memory(sample, device) for sample in data]

File ~\Anaconda3\envs\py38\lib\site-packages\torch\utils\data_utils\pin_memory.py:50, in pin_memory(data, device) 48 def pin_memory(data, device=None): 49 if isinstance(data, torch.Tensor): —> 50 return data.pin_memory(device) 51 elif isinstance(data, string_classes): 52 return data

RuntimeError: cannot pin ‘torch.cuda.FloatTensor’ only dense CPU tensors can be pinned

torch 1.12.1 python 3.8

Read more comments on GitHub >

github_iconTop Results From Across the Web

What are dense CPU tensor?
RuntimeError : cannot pin 'torch.cuda.ByteTensor' only dense CPU tensors can be pinned. ptrblck September 11, 2019, 1:10pm #2.
Read more >
Pytorch. How does pin_memory work in Dataloader?
According to the documentation: pin_memory (bool, optional) – If True, the data loader will copy tensors into CUDA pinned memory before ...
Read more >
Using Trainer with LayoutXLM for classification - Beginners
RuntimeError : cannot pin 'torch.cuda.LongTensor' only dense CPU tensors can be pinned. When doing that I get the following error… :
Read more >
pytorch cuda入门_doubleslow
return data.pin_memory() RuntimeError: cannot pin 'torch.cuda.FloatTensor' only dense CPU tensors can be pinned.
Read more >
Pytorch生僻bug记录「NoneType、Cannot re-initialize
RuntimeError : cannot pin 'torch.cuda.FloatTensor' only dense CPU tensors can be pinned 原因:是pin_memory的问题解决方案:初始化dataloader的时候 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found