How to empty_cache by passing gpu-tensor as a parameter to a function?
In this case, It works!
x = torch.tensor([1., 2.]).cuda()
print(torch.cuda.memory_allocated()) # 512
del x
print(torch.cuda.memory_allocated()) # 0
x = torch.tensor([1., 2.]).cuda()
print(torch.cuda.memory_allocated()) # 512
cleaner(x)
print(torch.cuda.memory_allocated()) # still 512 ...
def cleaner(x):
del x
The cleaner method will delete the x variable from the local scope, not the global one.
To remove the global x, you could use the global keyword:
x = torch.tensor([1., 2.]).cuda()
print(torch.cuda.memory_allocated()) # 512
del x
print(torch.cuda.memory_allocated()) # 0
def cleaner():
global x
del x
x = torch.tensor([1., 2.]).cuda()
print(torch.cuda.memory_allocated()) # 512
cleaner()
print(torch.cuda.memory_allocated()) # 0