hughperkins / pytorch

Python wrappers for torch and lua
BSD 2-Clause "Simplified" License
431 stars 70 forks source link

How to delete PyTorch objects correctly from memory within Python? #19

Open cjmcmurtrie opened 8 years ago

cjmcmurtrie commented 8 years ago

I'm having an issue with properly deleting PyTorch objects from memory. With this dummy class:

require 'nn'
require 'cunn'
require 'cutorch'

local Test = torch.class('Test')

function Test:__init()
    self.tensor = torch.CudaTensor(10000, 10000)
end

If I instantiate from Python:

>>> from PyTorchHelpers import load_lua_class
>>> Test = load_lua_class('modules/test.lua', 'Test')
>>> test = Test()

Then delete the object:

>>> del test

CUDA memory is not freed up.

Is there a clean way to delete a PyTorch object from memory?

hughperkins commented 8 years ago

Ah, yes, this is probably connected to https://github.com/hughperkins/pytorch/issues/10#issuecomment-248637508 It's kind of non-trivial to fix, but probably should be fixed... Probably need some kind of gc command perhaps. If memory was freed iff one called a new hypothetical PyTorch.gc() command, to what extent could that be acceptable-ish?