William Lepley
William Lepley

Reputation: 1

Not understanding CUDA resources and keep running out of memory

I think I'm running Tensor PyTorch.

I am new to python, and trying to use it experimenting with convolutional Neural networks and processing larger images. But I keep running into this error, even if I Request smaller image outputs. I just signed up for Colab Pro. While it is certainly faster, it still errors out with the CUDA. I would reallocate memory if I knew how, but I don't. Are there any other other way to access/manage GPU memory??

File "/usr/local/lib/python3.7/dist-packages/torch/_tensor.py", line 255, in backward torch.autograd.backward(self, gradient, retain_graph, create_graph, inputs=inputs) File "/usr/local/lib/python3.7/dist-packages/torch/autograd/init.py", line 149, in backward allow_unreachable=True, accumulate_grad=True) # allow_unreachable flag RuntimeError: CUDA out of memory. Tried to allocate 114.00 MiB (GPU 0; 15.78 GiB total capacity; 13.27 GiB already allocated; 4.75 MiB free; 14.43 GiB reserved in total by PyTorch) VGG-19 Architecture Detected Successfully loaded models/vgg19-d01eb7cb.pth conv1_1: 64 3 3 3 conv1_2: 64 64 3 3 conv2_1: 128 64 3 3 conv2_2:

Upvotes: 0

Views: 1058

Answers (1)

SarthakJain
SarthakJain

Reputation: 1706

I have shown below ways to manage GPU memory in pytorch, but often these ways are not suggested ways to deal with CUDA Errors like yours.

The reason you get this error has nothing to do with the size of your output but by the size of your input. You either have way too big images coming into your network in which you may need to use transforms.Resize() or your batch size is way to big, so you are calling for a huge parralel computation and thus need to lower that number in the dataloader.

The ways to remove a tensor from gpu memory can be done by using

a = torch.tensor(1)
del a

# Though not suggested and not rlly needed to be called explicitly

torch.cuda.empty_cache()

The ways to allocate a tensor to cuda memory is to simply move the tensor to device using

a = torch.tensor(1)
a = a.cuda()

# OR

device = torch.device("cuda")
a = a.to(device)

Sarthak Jain

Upvotes: 0

Related Questions