<s> from __future__ import absolute_import , print_function , division <EOL> import copy <EOL> import sys <EOL> from theano . compat import DefaultOrderedDict <EOL> from theano . misc . ordered_set import OrderedSet <EOL> from six import StringIO , integer_types <EOL> from theano . gof import opt <EOL> from theano import config <EOL> class DB ( object ) : <EOL> def __hash__ ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _optimizer_idx = opt . _optimizer_idx [ <NUM_LIT:0> ] <EOL> opt . _optimizer_idx [ <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> return self . _optimizer_idx <EOL> def __init__ ( self ) : <EOL> self . __db__ = DefaultOrderedDict ( OrderedSet ) <EOL> self . _names = set ( ) <EOL> self . name = None <EOL> def register ( self , name , obj , * tags , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( obj , ( DB , opt . Optimizer , opt . LocalOptimizer ) ) : <EOL> raise TypeError ( '<STR_LIT>' , obj ) <EOL> if name in self . __db__ : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> obj , name ) <EOL> if kwargs : <EOL> assert "<STR_LIT>" in kwargs <EOL> assert kwargs [ "<STR_LIT>" ] is False <EOL> else : <EOL> if self . name is not None : <EOL> tags = tags + ( self . name , ) <EOL> obj . name = name <EOL> if obj . name in self . __db__ : <EOL> raise ValueError ( '''<STR_LIT>''' % ( obj . name , name ) ) <EOL> self . __db__ [ name ] = OrderedSet ( [ obj ] ) <EOL> self . _names . add ( name ) <EOL> self . __db__ [ obj . __class__ . __name__ ] . add ( obj ) <EOL> self . add_tags ( name , * tags ) <EOL> def add_tags ( self , name , * tags ) : <EOL> obj = self . __db__ [ name ] <EOL> assert len ( obj ) == <NUM_LIT:1> <EOL> obj = obj . copy ( ) . pop ( ) <EOL> for tag in tags : <EOL> if tag in self . _names : <EOL> raise ValueError ( '<STR_LIT>' , <EOL> obj , tag ) <EOL> self . __db__ [ tag ] . add ( obj ) <EOL> def remove_tags ( self , name , * tags ) : <EOL> obj = self . __db__ [ name ] <EOL> assert len ( obj ) == <NUM_LIT:1> <EOL> obj = obj . copy ( ) . pop ( ) <EOL> for tag in tags : <EOL> if tag in self . _names : <EOL> raise ValueError ( '<STR_LIT>' , <EOL> obj , tag ) <EOL> self . __db__ [ tag ] . remove ( obj ) <EOL> def __query__ ( self , q ) : <EOL> if not isinstance ( q , Query ) : <EOL> raise TypeError ( '<STR_LIT>' , q ) <EOL> variables = OrderedSet ( ) <EOL> for tag in q . include : <EOL> variables . update ( self . __db__ [ tag ] ) <EOL> for tag in q . require : <EOL> variables . intersection_update ( self . __db__ [ tag ] ) <EOL> for tag in q . exclude : <EOL> variables . difference_update ( self . __db__ [ tag ] ) <EOL> remove = OrderedSet ( ) <EOL> add = OrderedSet ( ) <EOL> for obj in variables : <EOL> if isinstance ( obj , DB ) : <EOL> def_sub_query = q <EOL> if q . extra_optimizations : <EOL> def_sub_query = copy . copy ( q ) <EOL> def_sub_query . extra_optimizations = [ ] <EOL> sq = q . subquery . get ( obj . name , def_sub_query ) <EOL> replacement = obj . query ( sq ) <EOL> replacement . name = obj . name <EOL> remove . add ( obj ) <EOL> add . add ( replacement ) <EOL> variables . difference_update ( remove ) <EOL> variables . update ( add ) <EOL> return variables <EOL> def query ( self , * tags , ** kwtags ) : <EOL> if len ( tags ) >= <NUM_LIT:1> and isinstance ( tags [ <NUM_LIT:0> ] , Query ) : <EOL> if len ( tags ) > <NUM_LIT:1> or kwtags : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> tags , kwtags ) <EOL> return self . __query__ ( tags [ <NUM_LIT:0> ] ) <EOL> include = [ tag [ <NUM_LIT:1> : ] for tag in tags if tag . startswith ( '<STR_LIT:+>' ) ] <EOL> require = [ tag [ <NUM_LIT:1> : ] for tag in tags if tag . startswith ( '<STR_LIT:&>' ) ] <EOL> exclude = [ tag [ <NUM_LIT:1> : ] for tag in tags if tag . startswith ( '<STR_LIT:->' ) ] <EOL> if len ( include ) + len ( require ) + len ( exclude ) < len ( tags ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" , tags ) <EOL> return self . __query__ ( Query ( include = include , <EOL> require = require , <EOL> exclude = exclude , <EOL> subquery = kwtags ) ) <EOL> def __getitem__ ( self , name ) : <EOL> variables = self . __db__ [ name ] <EOL> if not variables : <EOL> raise KeyError ( "<STR_LIT>" % name ) <EOL> elif len ( variables ) > <NUM_LIT:1> : <EOL> raise ValueError ( '<STR_LIT>' % <EOL> name ) <EOL> for variable in variables : <EOL> return variable <EOL> def __contains__ ( self , name ) : <EOL> return name in self . __db__ <EOL> def print_summary ( self , stream = sys . stdout ) : <EOL> print ( "<STR_LIT>" % ( self . __class__ . __name__ , id ( self ) ) , file = stream ) <EOL> print ( "<STR_LIT>" , self . _names , file = stream ) <EOL> print ( "<STR_LIT>" , self . __db__ , file = stream ) <EOL> class Query ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , include , require = None , exclude = None , <EOL> subquery = None , position_cutoff = None , <EOL> extra_optimizations = None ) : <EOL> self . include = OrderedSet ( include ) <EOL> self . require = require or OrderedSet ( ) <EOL> self . exclude = exclude or OrderedSet ( ) <EOL> self . subquery = subquery or { } <EOL> self . position_cutoff = position_cutoff <EOL> if extra_optimizations is None : <EOL> extra_optimizations = [ ] <EOL> self . extra_optimizations = extra_optimizations <EOL> if isinstance ( self . require , ( list , tuple ) ) : <EOL> self . require = OrderedSet ( self . require ) <EOL> if isinstance ( self . exclude , ( list , tuple ) ) : <EOL> self . exclude = OrderedSet ( self . exclude ) <EOL> def __str__ ( self ) : <EOL> return ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( self . include , self . exclude , self . require , self . subquery , <EOL> self . position_cutoff , self . extra_optimizations ) ) <EOL> def __setstate__ ( self , state ) : <EOL> self . __dict__ . update ( state ) <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . extra_optimizations = [ ] <EOL> def including ( self , * tags ) : <EOL> return Query ( self . include . union ( tags ) , <EOL> self . require , <EOL> self . exclude , <EOL> self . subquery , <EOL> self . position_cutoff , <EOL> self . extra_optimizations ) <EOL> def excluding ( self , * tags ) : <EOL> return Query ( self . include , <EOL> self . require , <EOL> self . exclude . union ( tags ) , <EOL> self . subquery , <EOL> self . position_cutoff , <EOL> self . extra_optimizations ) <EOL> def requiring ( self , * tags ) : <EOL> return Query ( self . include , <EOL> self . require . union ( tags ) , <EOL> self . exclude , <EOL> self . subquery , <EOL> self . position_cutoff , <EOL> self . extra_optimizations ) <EOL> def register ( self , * optimizations ) : <EOL> return Query ( self . include , <EOL> self . require , <EOL> self . exclude , <EOL> self . subquery , <EOL> self . position_cutoff , <EOL> self . extra_optimizations + list ( optimizations ) ) <EOL> class EquilibriumDB ( DB ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ignore_newtrees = True ) : <EOL> super ( EquilibriumDB , self ) . __init__ ( ) <EOL> self . ignore_newtrees = ignore_newtrees <EOL> self . __final__ = { } <EOL> self . __cleanup__ = { } <EOL> def register ( self , name , obj , * tags , ** kwtags ) : <EOL> final_opt = kwtags . pop ( '<STR_LIT>' , False ) <EOL> cleanup = kwtags . pop ( '<STR_LIT>' , False ) <EOL> assert not ( final_opt and cleanup ) <EOL> super ( EquilibriumDB , self ) . register ( name , obj , * tags , ** kwtags ) <EOL> self . __final__ [ name ] = final_opt <EOL> self . __cleanup__ [ name ] = cleanup <EOL> def query ( self , * tags , ** kwtags ) : <EOL> _opts = super ( EquilibriumDB , self ) . query ( * tags , ** kwtags ) <EOL> final_opts = [ o for o in _opts if self . __final__ . get ( o . name , False ) ] <EOL> cleanup_opts = [ o for o in _opts if self . __cleanup__ . get ( o . name , <EOL> False ) ] <EOL> opts = [ o for o in _opts <EOL> if o not in final_opts and o not in cleanup_opts ] <EOL> if len ( final_opts ) == <NUM_LIT:0> : <EOL> final_opts = None <EOL> if len ( cleanup_opts ) == <NUM_LIT:0> : <EOL> cleanup_opts = None <EOL> return opt . EquilibriumOptimizer ( <EOL> opts , <EOL> max_use_ratio = config . optdb . max_use_ratio , <EOL> ignore_newtrees = self . ignore_newtrees , <EOL> failure_callback = opt . NavigatorOptimizer . warn_inplace , <EOL> final_optimizers = final_opts , <EOL> cleanup_optimizers = cleanup_opts ) <EOL> class SequenceDB ( DB ) : <EOL> """<STR_LIT>""" <EOL> seq_opt = opt . SeqOptimizer <EOL> def __init__ ( self , failure_callback = opt . SeqOptimizer . warn ) : <EOL> super ( SequenceDB , self ) . __init__ ( ) <EOL> self . __position__ = { } <EOL> self . failure_callback = failure_callback <EOL> def register ( self , name , obj , position , * tags ) : <EOL> super ( SequenceDB , self ) . register ( name , obj , * tags ) <EOL> assert isinstance ( position , ( integer_types , float ) ) <EOL> self . __position__ [ name ] = position <EOL> def query ( self , * tags , ** kwtags ) : <EOL> """<STR_LIT>""" <EOL> opts = super ( SequenceDB , self ) . query ( * tags , ** kwtags ) <EOL> position_cutoff = kwtags . pop ( '<STR_LIT>' , <EOL> config . optdb . position_cutoff ) <EOL> position_dict = self . __position__ <EOL> if len ( tags ) >= <NUM_LIT:1> and isinstance ( tags [ <NUM_LIT:0> ] , Query ) : <EOL> assert len ( tags ) == <NUM_LIT:1> <EOL> if getattr ( tags [ <NUM_LIT:0> ] , '<STR_LIT>' , None ) : <EOL> position_cutoff = tags [ <NUM_LIT:0> ] . position_cutoff <EOL> if len ( tags [ <NUM_LIT:0> ] . extra_optimizations ) > <NUM_LIT:0> : <EOL> position_dict = position_dict . copy ( ) <EOL> for extra_opt in tags [ <NUM_LIT:0> ] . extra_optimizations : <EOL> opt , position = extra_opt <EOL> opt . name = "<STR_LIT>" % ( opt . __class__ , id ( opt ) ) <EOL> if position < position_cutoff : <EOL> opts . add ( opt ) <EOL> position_dict [ opt . name ] = position <EOL> opts = [ o for o in opts if position_dict [ o . name ] < position_cutoff ] <EOL> opts . sort ( key = lambda obj : ( position_dict [ obj . name ] , obj . name ) ) <EOL> kwargs = { } <EOL> if self . failure_callback : <EOL> kwargs [ "<STR_LIT>" ] = self . failure_callback <EOL> ret = self . seq_opt ( opts , ** kwargs ) <EOL> if hasattr ( tags [ <NUM_LIT:0> ] , '<STR_LIT:name>' ) : <EOL> ret . name = tags [ <NUM_LIT:0> ] . name <EOL> return ret <EOL> def print_summary ( self , stream = sys . stdout ) : <EOL> print ( self . __class__ . __name__ + "<STR_LIT>" % id ( self ) , file = stream ) <EOL> positions = list ( self . __position__ . items ( ) ) <EOL> def c ( a , b ) : <EOL> return ( ( a [ <NUM_LIT:1> ] > b [ <NUM_LIT:1> ] ) - ( a [ <NUM_LIT:1> ] < b [ <NUM_LIT:1> ] ) ) <EOL> positions . sort ( c ) <EOL> print ( "<STR_LIT>" , positions , file = stream ) <EOL> print ( "<STR_LIT>" , self . _names , file = stream ) <EOL> print ( "<STR_LIT>" , self . __db__ , file = stream ) <EOL> def __str__ ( self ) : <EOL> sio = StringIO ( ) <EOL> self . print_summary ( sio ) <EOL> return sio . getvalue ( ) <EOL> class LocalGroupDB ( SequenceDB ) : <EOL> """<STR_LIT>""" <EOL> seq_opt = opt . LocalOptGroup <EOL> def __init__ ( self , failure_callback = opt . SeqOptimizer . warn ) : <EOL> super ( LocalGroupDB , self ) . __init__ ( ) <EOL> self . failure_callback = None <EOL> class ProxyDB ( DB ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , db ) : <EOL> assert isinstance ( db , DB ) , "<STR_LIT>" <EOL> self . db = db <EOL> def query ( self , * tags , ** kwtags ) : <EOL> return self . db . query ( * tags , ** kwtags ) </s>
