提交 c0f5f811 authored 作者: delallea's avatar delallea

Merge pull request #67 from nouiz/list_cache

Added "theano-cache list" to list the content of the theano cache
#!/usr/bin/env python #!/usr/bin/env python
import logging, os, sys import logging, os, sys
import theano
from theano import config from theano import config
from theano.gof.cc import get_module_cache from theano.gof.cc import get_module_cache
...@@ -25,10 +26,12 @@ elif sys.argv[1] in ('clear'): ...@@ -25,10 +26,12 @@ elif sys.argv[1] in ('clear'):
config.compiledir) config.compiledir)
_logger.debug('Remaining elements (%s): %s' % _logger.debug('Remaining elements (%s): %s' %
(len(items), ', '.join(items))) (len(items), ', '.join(items)))
elif sys.argv[1] in ('list'):
theano.gof.compiledir.print_compiledir_content()
else: else:
print 'command "%s" not recognized' % sys.argv[1] print 'command "%s" not recognized' % sys.argv[1]
print 'Type "theano-cache" to print the cache location' print 'Type "theano-cache" to print the cache location'
print 'Type "theano-cache clear" to erase the cache' print 'Type "theano-cache clear" to erase the cache'
print 'Type "theano-cache list" to print the cache content'
sys.exit(1) sys.exit(1)
import cPickle
import errno import errno
import os, sys import os
import platform import platform
import re import re
import theano
from theano.configparser import config, AddConfigVar, ConfigParam, StrParam from theano.configparser import config, AddConfigVar, ConfigParam, StrParam
def default_compiledirname(): def default_compiledirname():
platform_id = '-'.join([ platform_id = '-'.join([
platform.platform(), platform.platform(),
...@@ -25,7 +28,7 @@ def filter_compiledir(path): ...@@ -25,7 +28,7 @@ def filter_compiledir(path):
valid = True valid = True
if not os.access(path, os.R_OK | os.W_OK): if not os.access(path, os.R_OK | os.W_OK):
try: try:
os.makedirs(path, 0770) #read-write-execute for user and group os.makedirs(path, 0770) # read-write-execute for user and group
except OSError, e: except OSError, e:
# Maybe another parallel execution of theano was trying to create # Maybe another parallel execution of theano was trying to create
# the same directory at the same time. # the same directory at the same time.
...@@ -59,3 +62,43 @@ AddConfigVar('compiledir', ...@@ -59,3 +62,43 @@ AddConfigVar('compiledir',
default_compiledirname()), default_compiledirname()),
filter=filter_compiledir, filter=filter_compiledir,
allow_override=False)) allow_override=False))
def print_compiledir_content():
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
print "List compiled ops in this theano cache:", compiledir
print "sub directory/Op/Associated Type"
print
table = []
for dir in os.listdir(compiledir):
file = None
try:
try:
file = open(os.path.join(compiledir, dir, "key.pkl"))
keydata = cPickle.load(file)
ops = list(set([x for x in flatten(keydata.keys)
if isinstance(x, theano.gof.Op)]))
assert len(ops) == 1
types = list(set([x for x in flatten(keydata.keys)
if isinstance(x, theano.gof.Type)]))
table.append((dir, ops[0], types))
except IOError:
pass
finally:
if file is not None:
file.close()
table = sorted(table, key=lambda t: str(t[1]))
for dir, op, types in table:
print dir, op, types
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论