-
Notifications
You must be signed in to change notification settings - Fork 21.3k
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
DataLoader with collate_fn that returns tensors in GPU memory raises warnings when deleted #98002
Comments
This comment was marked as off-topic.
This comment was marked as off-topic.
Hi @pat749 , thanks for the suggestion, but I still get all the same warnings on my system if I call |
This comment was marked as off-topic.
This comment was marked as off-topic.
Hi @pat749 , I think the link you posted was to a different notebook, but I tried implementing this code in a new Colab notebook and didn't get any warnings, even without calling |
This comment was marked as off-topic.
This comment was marked as off-topic.
First of all, it's better to keep Tensor in CPU within worker processes. Otherwise, each worker process will create a separate CUDA context. You can enable train_loader = torch.utils.data.DataLoader(
dataset=train_dataset,
batch_size=100,
num_workers=1,
prefetch_factor=2,
persistent_workers=True,
collate_fn=collate_gpu,
pin_memory=True,
)
for x, t in train_loader:
x.to(device)
t.to(device)
... |
Thanks for the suggestions of keeping batch preparation in CPU memory and moving tensors right before actual usage. However, I think the code examples does have some flaws.
|
馃悰 Describe the bug
I defined a
DataLoader
withcollate_fn
that returns tensors in GPU memory, withnum_workers=1
andprefetch_factor=2
so that as I iterate through theDataLoader
, the tensors it returns are already in GPU memory. When theDataLoader
is deleted, a lot of warnings are raised fromCUDAGuardImpl.h
. For example:Console output:
In reality I don't call
del train_loader
, but I initialisetrain_loader
inside a function, and when the function exits, the result is the same. (Weirdly, if I don't calldel train_loader
andtrain_loader
is not defined inside a function, then there are no warning messages at all).PS am I being silly? I would have assumed it would be a very common use case to want to pre-fetch data in GPU memory with a DataLoader rather than waiting in the main process for data to be copied to the GPU, but I can't seem to find many posts on this topic at all (one example is this thread, but it's 4 years old and the error message is different)
Versions
Versions/OS:
Python 3.7.6
Cuda 11.7
PyTorch 1.13.0+cu117
Windows 10
(sorry I don't fancy running a >600 line Python script downloaded from the internet, regardless of the author)
cc @ssnl @VitalyFedyunin @ejguan @NivekT @dzhulgakov
The text was updated successfully, but these errors were encountered: