Hi there. I am trying to run a simple model by giving some input data to it. The model tensors are in cuda device. The way I am feeding the data to the model is by converting the cpu tensor to the cuda tensor by inline conversion right at the model input output = model(input.cuda()).
But after running this line I could see that the used memory of the gpu is increased (which is very normal). But after running the same line again, the newly created input gpu tensor (with the inline conversion) is again allocated to the gpu, which I guess is not normal.
In this case, is there any way to remove the previously created gpu tensor and deallocate gpu memory? Is there any way to find the variable handles which are created inline as shown in the example?
N.B. I know I could convert the cpu tensors to a gpu tensor and store in a variable to retain the variable handle (gpuTensor = cpuTensor.cuda()). And then when the work is done I could simply run del gpuTensor. That would deallocate the gpu memory. But the question is not how to remove a gpu tensor.
PyTorch will move the memory back to the cache once all references to the tensor are gone. How did you verify that new memory is allocated for the temp input?
I have used nvidia-smi and nvidia-htop.py to see the allocated memory. My basic understanding is: re-running a code block with the same variable alias should not occupy more memory as the previous tensor will lose reference to a variable. This should also be true for temporary allocated memory blocks.
nvidia-smi will show the GPU memory usage of all processes and thus also from the CUDA context etc., so is not trivial to map to tensor data.
As you can see in my example, the temp. tensor will use memory while it’s inside the function scope (seen via torch.cuda.memory_allocated()) and its memory will be released again in the for loop. The cache is allocated to 20MB and will be reused.