<s> from __future__ import print_function <EOL> from functools import partial <EOL> import sys <EOL> import time <EOL> import inspect <EOL> import theano <EOL> from theano import config <EOL> from theano . compat import OrderedDict <EOL> from theano . gof import graph <EOL> class AlreadyThere ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class ReplacementDidntRemovedError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class Feature ( object ) : <EOL> """<STR_LIT>""" <EOL> def on_attach ( self , function_graph ) : <EOL> """<STR_LIT>""" <EOL> def on_detach ( self , function_graph ) : <EOL> """<STR_LIT>""" <EOL> def on_import ( self , function_graph , node , reason ) : <EOL> """<STR_LIT>""" <EOL> def on_prune ( self , function_graph , node , reason ) : <EOL> """<STR_LIT>""" <EOL> def on_change_input ( self , function_graph , node , i , r , new_r , reason = None ) : <EOL> """<STR_LIT>""" <EOL> def orderings ( self , function_graph ) : <EOL> """<STR_LIT>""" <EOL> return OrderedDict ( ) <EOL> class Bookkeeper ( Feature ) : <EOL> def on_attach ( self , fgraph ) : <EOL> for node in graph . io_toposort ( fgraph . inputs , fgraph . outputs ) : <EOL> self . on_import ( fgraph , node , "<STR_LIT>" ) <EOL> def on_detach ( self , fgraph ) : <EOL> for node in graph . io_toposort ( fgraph . inputs , fgraph . outputs ) : <EOL> self . on_prune ( fgraph , node , '<STR_LIT>' ) <EOL> class GetCheckpoint : <EOL> def __init__ ( self , history , fgraph ) : <EOL> self . h = history <EOL> self . fgraph = fgraph <EOL> self . nb = <NUM_LIT:0> <EOL> def __call__ ( self ) : <EOL> self . h . history [ self . fgraph ] = [ ] <EOL> self . nb += <NUM_LIT:1> <EOL> return self . nb <EOL> class LambdExtract : <EOL> def __init__ ( self , fgraph , node , i , r , reason = None ) : <EOL> self . fgraph = fgraph <EOL> self . node = node <EOL> self . i = i <EOL> self . r = r <EOL> self . reason = reason <EOL> def __call__ ( self ) : <EOL> return self . fgraph . change_input ( self . node , self . i , self . r , <EOL> reason = ( "<STR_LIT>" , self . reason ) ) <EOL> class History ( Feature ) : <EOL> """<STR_LIT>""" <EOL> pickle_rm_attr = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> def __init__ ( self ) : <EOL> self . history = { } <EOL> def on_attach ( self , fgraph ) : <EOL> if hasattr ( fgraph , '<STR_LIT>' ) or hasattr ( fgraph , '<STR_LIT>' ) : <EOL> raise AlreadyThere ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . history [ fgraph ] = [ ] <EOL> fgraph . checkpoint = GetCheckpoint ( self , fgraph ) <EOL> fgraph . revert = partial ( self . revert , fgraph ) <EOL> def unpickle ( self , fgraph ) : <EOL> fgraph . checkpoint = GetCheckpoint ( self , fgraph ) <EOL> fgraph . revert = partial ( self . revert , fgraph ) <EOL> def on_detach ( self , fgraph ) : <EOL> del fgraph . checkpoint <EOL> del fgraph . revert <EOL> del self . history [ fgraph ] <EOL> def on_change_input ( self , fgraph , node , i , r , new_r , reason = None ) : <EOL> if self . history [ fgraph ] is None : <EOL> return <EOL> h = self . history [ fgraph ] <EOL> h . append ( LambdExtract ( fgraph , node , i , r , reason ) ) <EOL> def revert ( self , fgraph , checkpoint ) : <EOL> """<STR_LIT>""" <EOL> h = self . history [ fgraph ] <EOL> self . history [ fgraph ] = None <EOL> assert fgraph . checkpoint . nb == checkpoint <EOL> while h : <EOL> f = h . pop ( ) <EOL> f ( ) <EOL> self . history [ fgraph ] = h <EOL> class Validator ( Feature ) : <EOL> pickle_rm_attr = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> def on_attach ( self , fgraph ) : <EOL> for attr in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if hasattr ( fgraph , attr ) : <EOL> raise AlreadyThere ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> fgraph . validate = partial ( self . validate_ , fgraph ) <EOL> fgraph . consistent = partial ( self . consistent_ , fgraph ) <EOL> def unpickle ( self , fgraph ) : <EOL> fgraph . validate = partial ( self . validate_ , fgraph ) <EOL> fgraph . consistent = partial ( self . consistent_ , fgraph ) <EOL> def on_detach ( self , fgraph ) : <EOL> del fgraph . validate <EOL> del fgraph . consistent <EOL> def validate_ ( self , fgraph ) : <EOL> t0 = time . time ( ) <EOL> try : <EOL> ret = fgraph . execute_callbacks ( '<STR_LIT>' ) <EOL> except Exception as e : <EOL> cf = inspect . currentframe ( ) <EOL> uf = cf . f_back <EOL> uf_info = inspect . getframeinfo ( uf ) <EOL> if uf_info . function == '<STR_LIT>' : <EOL> raise <EOL> else : <EOL> verbose = uf . f_locals . get ( '<STR_LIT>' , False ) <EOL> if verbose : <EOL> r = uf . f_locals . get ( '<STR_LIT:r>' , "<STR_LIT>" ) <EOL> reason = uf_info . function <EOL> print ( "<STR_LIT>" % <EOL> ( r , reason , e ) ) <EOL> raise <EOL> t1 = time . time ( ) <EOL> if fgraph . profile : <EOL> fgraph . profile . validate_time += t1 - t0 <EOL> return ret <EOL> def consistent_ ( self , fgraph ) : <EOL> try : <EOL> fgraph . validate ( ) <EOL> return True <EOL> except Exception : <EOL> return False <EOL> class ReplaceValidate ( History , Validator ) : <EOL> pickle_rm_attr = ( [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ] + <EOL> History . pickle_rm_attr + Validator . pickle_rm_attr ) <EOL> def on_attach ( self , fgraph ) : <EOL> for attr in ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) : <EOL> if hasattr ( fgraph , attr ) : <EOL> raise AlreadyThere ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . _nodes_removed = set ( ) <EOL> self . fail_validate = False <EOL> History . on_attach ( self , fgraph ) <EOL> Validator . on_attach ( self , fgraph ) <EOL> self . unpickle ( fgraph ) <EOL> def unpickle ( self , fgraph ) : <EOL> History . unpickle ( self , fgraph ) <EOL> Validator . unpickle ( self , fgraph ) <EOL> fgraph . replace_validate = partial ( self . replace_validate , fgraph ) <EOL> fgraph . replace_all_validate = partial ( self . replace_all_validate , <EOL> fgraph ) <EOL> fgraph . replace_all_validate_remove = partial ( <EOL> self . replace_all_validate_remove , fgraph ) <EOL> def on_detach ( self , fgraph ) : <EOL> History . on_detach ( self , fgraph ) <EOL> Validator . on_detach ( self , fgraph ) <EOL> del self . _nodes_removed <EOL> del fgraph . replace_validate <EOL> del fgraph . replace_all_validate <EOL> del fgraph . replace_all_validate_remove <EOL> def replace_validate ( self , fgraph , r , new_r , reason = None ) : <EOL> self . replace_all_validate ( fgraph , [ ( r , new_r ) ] , reason = reason ) <EOL> def replace_all_validate ( self , fgraph , replacements , <EOL> reason = None , verbose = None ) : <EOL> chk = fgraph . checkpoint ( ) <EOL> if verbose is None : <EOL> verbose = config . optimizer_verbose <EOL> for r , new_r in replacements : <EOL> try : <EOL> fgraph . replace ( r , new_r , reason = reason , verbose = False ) <EOL> except Exception as e : <EOL> msg = str ( e ) <EOL> s1 = '<STR_LIT>' <EOL> s2 = '<STR_LIT>' <EOL> s3 = '<STR_LIT>' <EOL> if s3 in msg : <EOL> e . args += ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , ) <EOL> raise <EOL> elif ( s1 not in msg and s2 not in msg ) : <EOL> out = sys . stderr <EOL> print ( "<STR_LIT>" , <EOL> type ( e ) , e , reason , file = out ) <EOL> fgraph . revert ( chk ) <EOL> raise <EOL> try : <EOL> fgraph . validate ( ) <EOL> except Exception as e : <EOL> fgraph . revert ( chk ) <EOL> if verbose : <EOL> print ( "<STR_LIT>" % ( r , reason , e ) ) <EOL> raise <EOL> if verbose : <EOL> print ( reason , r , new_r ) <EOL> return chk <EOL> def replace_all_validate_remove ( self , fgraph , replacements , <EOL> remove , reason = None , warn = True ) : <EOL> """<STR_LIT>""" <EOL> chk = fgraph . replace_all_validate ( replacements , reason ) <EOL> self . _nodes_removed . update ( remove ) <EOL> for rm in remove : <EOL> if rm in fgraph . apply_nodes or rm in fgraph . variables : <EOL> fgraph . revert ( chk ) <EOL> if warn : <EOL> out = sys . stderr <EOL> print ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> file = out ) <EOL> print ( reason , replacements , file = out ) <EOL> raise ReplacementDidntRemovedError ( ) <EOL> def __getstate__ ( self ) : <EOL> d = self . __dict__ . copy ( ) <EOL> if "<STR_LIT>" in d : <EOL> del d [ "<STR_LIT>" ] <EOL> return d <EOL> def on_import ( self , fgraph , node , reason ) : <EOL> if node in self . _nodes_removed : <EOL> self . fail_validate = True <EOL> def validate ( self , fgraph ) : <EOL> if self . fail_validate : <EOL> self . fail_validate = False <EOL> raise theano . gof . InconsistencyError ( "<STR_LIT>" ) <EOL> class NodeFinder ( Bookkeeper ) : <EOL> def __init__ ( self ) : <EOL> self . fgraph = None <EOL> self . d = { } <EOL> def on_attach ( self , fgraph ) : <EOL> if self . fgraph is not None : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if hasattr ( fgraph , '<STR_LIT>' ) : <EOL> raise AlreadyThere ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . fgraph = fgraph <EOL> fgraph . get_nodes = partial ( self . query , fgraph ) <EOL> Bookkeeper . on_attach ( self , fgraph ) <EOL> def on_detach ( self , fgraph ) : <EOL> if self . fgraph is not fgraph : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . fgraph = None <EOL> del fgraph . get_nodes <EOL> Bookkeeper . on_detach ( self , fgraph ) <EOL> def on_import ( self , fgraph , node , reason ) : <EOL> try : <EOL> self . d . setdefault ( node . op , [ ] ) . append ( node ) <EOL> except TypeError : <EOL> return <EOL> except Exception as e : <EOL> print ( '<STR_LIT>' , type ( node ) , type ( node . op ) , file = sys . stderr ) <EOL> try : <EOL> print ( '<STR_LIT>' , hash ( node . op ) , file = sys . stderr ) <EOL> except Exception : <EOL> print ( '<STR_LIT>' , file = sys . stderr ) <EOL> raise e <EOL> def on_prune ( self , fgraph , node , reason ) : <EOL> try : <EOL> nodes = self . d [ node . op ] <EOL> except TypeError : <EOL> return <EOL> nodes . remove ( node ) <EOL> if not nodes : <EOL> del self . d [ node . op ] <EOL> def query ( self , fgraph , op ) : <EOL> try : <EOL> all = self . d . get ( op , [ ] ) <EOL> except TypeError : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % op ) <EOL> all = list ( all ) <EOL> return all <EOL> class PrintListener ( Feature ) : <EOL> def __init__ ( self , active = True ) : <EOL> self . active = active <EOL> def on_attach ( self , fgraph ) : <EOL> if self . active : <EOL> print ( "<STR_LIT>" , fgraph ) <EOL> def on_detach ( self , fgraph ) : <EOL> if self . active : <EOL> print ( "<STR_LIT>" , fgraph ) <EOL> def on_import ( self , fgraph , node , reason ) : <EOL> if self . active : <EOL> print ( "<STR_LIT>" % ( node , reason ) ) <EOL> def on_prune ( self , fgraph , node , reason ) : <EOL> if self . active : <EOL> print ( "<STR_LIT>" % ( node , reason ) ) <EOL> def on_change_input ( self , fgraph , node , i , r , new_r , reason = None ) : <EOL> if self . active : <EOL> print ( "<STR_LIT>" % ( <EOL> node , i , r , new_r ) ) <EOL> class PreserveNames ( Feature ) : <EOL> """<STR_LIT>""" <EOL> def on_change_input ( self , fgraph , node , i , r , new_r , reason = None ) : <EOL> if r . name is not None and new_r . name is None : <EOL> new_r . name = r . name <EOL> class PreserveVariableAttributes ( Feature ) : <EOL> """<STR_LIT>""" <EOL> def on_change_input ( self , fgraph , node , i , r , new_r , reason = None ) : <EOL> if r . name is not None and new_r . name is None : <EOL> new_r . name = r . name <EOL> if getattr ( r . tag , '<STR_LIT>' , False ) and getattr ( <EOL> new_r . tag , '<STR_LIT>' , False ) is False : <EOL> new_r . tag . nan_guard_mode_check = r . tag . nan_guard_mode_check <EOL> class NoOutputFromInplace ( Feature ) : <EOL> def __init__ ( self , first_output_idx = <NUM_LIT:0> , last_output_idx = None ) : <EOL> self . first_idx = first_output_idx <EOL> self . last_idx = last_output_idx <EOL> def validate ( self , fgraph ) : <EOL> if not hasattr ( fgraph , '<STR_LIT>' ) : <EOL> return True <EOL> outputs_to_validate = list ( fgraph . outputs ) [ self . first_idx : <EOL> self . last_idx ] <EOL> for out in outputs_to_validate : <EOL> if out . owner is None : <EOL> continue <EOL> node = out . owner <EOL> op = node . op <EOL> out_idx = node . outputs . index ( out ) <EOL> if hasattr ( op , '<STR_LIT>' ) and out_idx in op . destroy_map : <EOL> raise theano . gof . InconsistencyError ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , out , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ) </s>
