Created
July 27, 2018 22:49
-
-
Save MInner/9716950ac85b49821b56298117756451 to your computer and use it in GitHub Desktop.
Release GPU memory after tensorflow session is closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def run_release_gpu(func): | |
def parallel_wrapper(output_dict, *argv, **kwargs): | |
ret = func(*argv, **kwargs) | |
if ret is not None: | |
output_dict['ret'] = ret | |
def outer_wrapper(*argv, **kwargs): | |
same_process = kwargs.pop('same_process', False) | |
if same_process: | |
return func(*argv, **kwargs) | |
with multiprocessing.Manager() as manager: | |
output = manager.dict() | |
args = (output, ) + argv | |
p = multiprocessing.Process(target=parallel_wrapper, args=args, kwargs=kwargs) | |
p.start() | |
p.join() | |
ret_val = output.get('ret', None) | |
return ret_val | |
return outer_wrapper | |
@run_release_gpu | |
def run_computations(a, b, c): | |
with tf.Graph().as_default(): | |
with tf.Session() as sess: | |
pass | |
a = run_computations(1, 2, 3) |
It's being a while since I posted this, no idea whether it is still a viable workaround.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, thanks for sharing this on stack overflow. Unfortunately, it still seems very relevant as a work-around, but I can't seem to get it to work as Process needs to pickle the target func, which in this case is local, and thus cannot be pickled. Did you ever get this to work?