<s> from functools import wraps <EOL> import numpy <EOL> from theano import scalar as scal , Constant <EOL> from theano . gof import local_optimizer <EOL> from theano . tensor import ( DimShuffle , get_scalar_constant_value , <EOL> NotScalarConstantError ) <EOL> from theano . sandbox . cuda . basic_ops import ( <EOL> GpuFromHost , HostFromGpu , host_from_gpu , GpuDimShuffle , GpuElemwise ) <EOL> _one = scal . constant ( numpy . asarray ( <NUM_LIT:1.0> , dtype = '<STR_LIT>' ) ) <EOL> def grab_cpu_scalar ( v , nd ) : <EOL> if v . owner is not None : <EOL> n = v . owner <EOL> if ( isinstance ( n . op , GpuDimShuffle ) and <EOL> n . op . new_order == ( '<STR_LIT:x>' , ) * nd ) : <EOL> return host_from_gpu ( n . inputs [ <NUM_LIT:0> ] ) <EOL> elif ( isinstance ( n . op , DimShuffle ) and <EOL> n . op . new_order == ( '<STR_LIT:x>' , ) * nd ) : <EOL> return n . inputs [ <NUM_LIT:0> ] <EOL> elif isinstance ( n . op , GpuFromHost ) : <EOL> return grab_cpu_scalar ( n . inputs [ <NUM_LIT:0> ] , nd = nd ) <EOL> else : <EOL> return None <EOL> else : <EOL> if ( isinstance ( v , Constant ) and <EOL> v . broadcastable == ( True , ) * nd ) : <EOL> return v . dimshuffle ( ( ) ) <EOL> def find_node ( v , cls , ignore_clients = False ) : <EOL> if v . owner is not None and ( ignore_clients or len ( v . clients ) == <NUM_LIT:1> ) : <EOL> if isinstance ( v . owner . op , cls ) : <EOL> return v . owner <EOL> elif ( isinstance ( v . owner . op , GpuFromHost ) and <EOL> v . owner . inputs [ <NUM_LIT:0> ] . owner is not None and <EOL> ( ignore_clients or len ( v . owner . inputs [ <NUM_LIT:0> ] . clients ) == <NUM_LIT:1> ) and <EOL> isinstance ( v . owner . inputs [ <NUM_LIT:0> ] . owner . op , HostFromGpu ) ) : <EOL> return find_node ( v . owner . inputs [ <NUM_LIT:0> ] . owner . inputs [ <NUM_LIT:0> ] , cls ) <EOL> else : <EOL> return None <EOL> def is_equal ( var , val ) : <EOL> try : <EOL> v = get_scalar_constant_value ( var ) <EOL> return v == val <EOL> except NotScalarConstantError : <EOL> return False <EOL> def alpha_merge ( cls , alpha_in , beta_in ) : <EOL> def wrapper ( maker ) : <EOL> @ local_optimizer ( [ GpuElemwise ] ) <EOL> @ wraps ( maker ) <EOL> def opt ( node ) : <EOL> if ( isinstance ( node . op , GpuElemwise ) and <EOL> node . op . scalar_op == scal . mul and <EOL> node . nin == <NUM_LIT:2> ) : <EOL> targ = find_node ( node . inputs [ <NUM_LIT:0> ] , cls ) <EOL> if targ is None : <EOL> targ = find_node ( node . inputs [ <NUM_LIT:1> ] , cls ) <EOL> if targ is None : <EOL> return <EOL> lr = grab_cpu_scalar ( node . inputs [ <NUM_LIT:0> ] , <EOL> nd = targ . outputs [ <NUM_LIT:0> ] . ndim ) <EOL> else : <EOL> lr = grab_cpu_scalar ( node . inputs [ <NUM_LIT:1> ] , <EOL> nd = targ . outputs [ <NUM_LIT:0> ] . ndim ) <EOL> if lr is None or targ is None : <EOL> return None <EOL> inputs = list ( targ . inputs ) <EOL> try : <EOL> c = get_scalar_constant_value ( lr ) <EOL> if c == <NUM_LIT:0> : <EOL> inputs [ alpha_in ] = lr <EOL> inputs [ beta_in ] = lr <EOL> elif c == <NUM_LIT:1> : <EOL> inputs [ alpha_in ] = targ . inputs [ alpha_in ] <EOL> inputs [ beta_in ] = targ . inputs [ beta_in ] <EOL> else : <EOL> inputs [ alpha_in ] = lr * targ . inputs [ alpha_in ] <EOL> inputs [ beta_in ] = lr * targ . inputs [ beta_in ] <EOL> except NotScalarConstantError : <EOL> inputs [ alpha_in ] = lr * targ . inputs [ alpha_in ] <EOL> inputs [ beta_in ] = lr * targ . inputs [ beta_in ] <EOL> return maker ( targ , * inputs ) <EOL> return opt <EOL> return wrapper <EOL> def output_merge ( cls , alpha_in , beta_in , out_in ) : <EOL> def wrapper ( maker ) : <EOL> @ local_optimizer ( [ GpuElemwise ] ) <EOL> @ wraps ( maker ) <EOL> def opt ( node ) : <EOL> if ( isinstance ( node . op , GpuElemwise ) and <EOL> node . op . scalar_op == scal . add and <EOL> node . nin == <NUM_LIT:2> ) : <EOL> targ = find_node ( node . inputs [ <NUM_LIT:0> ] , cls ) <EOL> W = node . inputs [ <NUM_LIT:1> ] <EOL> if targ is None : <EOL> targ = find_node ( node . inputs [ <NUM_LIT:1> ] , cls ) <EOL> W = node . inputs [ <NUM_LIT:0> ] <EOL> if targ is None : <EOL> return None <EOL> if not is_equal ( targ . inputs [ beta_in ] , <NUM_LIT:0.0> ) : <EOL> return None <EOL> if W . broadcastable != targ . inputs [ out_in ] . broadcastable : <EOL> return None <EOL> inputs = list ( targ . inputs ) <EOL> inputs [ out_in ] = W <EOL> inputs [ beta_in ] = _one . clone ( ) <EOL> return maker ( targ , * inputs ) <EOL> return opt <EOL> return wrapper </s>
