提交 e7c3d879 authored 作者: Frederic's avatar Frederic

Add theano-cache cleanup that delete the old keys with ndarray from the cache.

上级 124829f7
......@@ -28,6 +28,8 @@ elif sys.argv[1] in ('clear'):
(len(items), ', '.join(items)))
elif sys.argv[1] in ('list'):
theano.gof.compiledir.print_compiledir_content()
elif sys.argv[1] in ('cleanup'):
theano.gof.compiledir.cleanup()
elif sys.argv[1] == 'unlock':
theano.gof.compilelock.force_unlock()
print 'Lock successfully removed!'
......@@ -37,5 +39,6 @@ else:
print 'Type "theano-cache clear" to erase the cache'
print 'Type "theano-cache list" to print the cache content'
print 'Type "theano-cache unlock" to unlock the cache directory'
print 'Type "theano-cache cleanup" to delete old key format'
sys.exit(1)
......@@ -3,9 +3,12 @@ import errno
import os
import platform
import re
import shutil
import sys
import textwrap
import numpy
import theano
from theano.configparser import config, AddConfigVar, ConfigParam, StrParam
......@@ -106,18 +109,56 @@ AddConfigVar('compiledir',
allow_override=False))
def flatten(a):
if isinstance(a, (tuple, list, set)):
l = []
for item in a:
l.extend(flatten(item))
return l
else:
return [a]
def cleanup():
""" Delete old keys from the compiledir
We define old key as key that have an ndarray in them.
Now we use an hash in the keys of the constant data.
If there is no key left for a compiled module, we delete the module.
"""
compiledir = theano.config.compiledir
for directory in os.listdir(compiledir):
file = None
try:
try:
filename = os.path.join(compiledir, directory, "key.pkl")
file = open(filename, 'rb')
#print file
try:
keydata = cPickle.load(file)
for key in list(keydata.keys):
for obj in flatten(key):
if isinstance(obj, numpy.ndarray):
keydata.remove_key(key)
break
if len(keydata.keys) == 0:
shutil.rmtree(os.path.join(compiledir, directory))
pass
except EOFError:
print ("ERROR while reading this key file '%s'."
" Delete its directory" % filename)
except IOError:
pass
finally:
if file is not None:
file.close()
def print_compiledir_content():
max_key_file_size = 1 * 1024 * 1024 # 1M
def flatten(a):
if isinstance(a, (tuple, list, set)):
l = []
for item in a:
l.extend(flatten(item))
return l
else:
return [a]
compiledir = theano.config.compiledir
table = []
more_than_one_ops = 0
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论