<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import logging <EOL> import itertools <EOL> import operator <EOL> import sys <EOL> import time <EOL> import traceback <EOL> import warnings <EOL> import numpy <EOL> import numpy as N <EOL> from six . moves import xrange <EOL> import theano <EOL> from theano import gof <EOL> from theano . compat import izip <EOL> from six import integer_types , iteritems <EOL> from six . moves import reduce <EOL> from theano . gof import opt , InconsistencyError , TopoOptimizer , graph <EOL> from theano . gof import Variable , Constant <EOL> from theano . gof . utils import MethodNotDefined <EOL> from theano . gradient import DisconnectedType <EOL> from theano . configparser import config <EOL> from theano . tensor . elemwise import Elemwise , DimShuffle <EOL> from theano . tensor . subtensor import ( get_idx_list , get_canonical_form_slice , <EOL> Subtensor , IncSubtensor , make_constant , <EOL> AdvancedIncSubtensor1 , <EOL> AdvancedIncSubtensor , <EOL> AdvancedSubtensor1 , <EOL> advanced_subtensor , <EOL> advanced_subtensor1 , <EOL> advanced_inc_subtensor1 ) <EOL> from theano import scalar <EOL> from theano . scalar import basic <EOL> from theano . tensor import basic as T <EOL> from theano import compile <EOL> from theano . compile . ops import Shape_i <EOL> from theano . tensor . type import ( values_eq_approx_remove_inf , <EOL> values_eq_approx_remove_nan , <EOL> values_eq_approx_remove_inf_nan ) <EOL> from theano . gof . opt import ( Optimizer , pre_constant_merge , <EOL> pre_greedy_local_optimizer ) <EOL> from theano . gof import toolbox <EOL> from theano . tensor . basic import get_scalar_constant_value , ShapeError , NotScalarConstantError <EOL> from six import StringIO <EOL> _logger = logging . getLogger ( '<STR_LIT>' ) <EOL> def copy_stack_trace ( from_var , to_var ) : <EOL> """<STR_LIT>""" <EOL> tr = [ ] <EOL> if type ( from_var ) is list : <EOL> for v in from_var : <EOL> tr += getattr ( v . tag , '<STR_LIT>' , [ ] ) <EOL> else : <EOL> tr = getattr ( from_var . tag , '<STR_LIT>' , [ ] ) <EOL> if type ( to_var ) is list : <EOL> for v in to_var : <EOL> v . tag . trace = getattr ( v . tag , '<STR_LIT>' , [ ] ) + tr <EOL> else : <EOL> to_var . tag . trace = getattr ( to_var . tag , '<STR_LIT>' , [ ] ) + tr <EOL> def out2in ( * local_opts , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> name = ( kwargs and kwargs . pop ( '<STR_LIT:name>' , None ) ) <EOL> if len ( local_opts ) > <NUM_LIT:1> : <EOL> local_opts = opt . LocalOptGroup ( * local_opts ) <EOL> else : <EOL> local_opts , = local_opts <EOL> if not name : <EOL> name = local_opts . __name__ <EOL> ret = opt . TopoOptimizer ( local_opts , <EOL> order = '<STR_LIT>' , <EOL> failure_callback = TopoOptimizer . warn_inplace , <EOL> ** kwargs ) <EOL> if name : <EOL> ret . __name__ = name <EOL> return ret <EOL> def in2out ( * local_opts , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> name = ( kwargs and kwargs . pop ( '<STR_LIT:name>' , None ) ) <EOL> if len ( local_opts ) > <NUM_LIT:1> : <EOL> local_opts = opt . LocalOptGroup ( * local_opts ) <EOL> else : <EOL> local_opts , = local_opts <EOL> if not name : <EOL> name = local_opts . __name__ <EOL> ret = opt . TopoOptimizer ( local_opts , <EOL> order = '<STR_LIT>' , <EOL> failure_callback = TopoOptimizer . warn_inplace , <EOL> ** kwargs ) <EOL> if name : <EOL> ret . __name__ = name <EOL> return ret <EOL> def _fill_chain ( new_out , orig_inputs ) : <EOL> for i in orig_inputs : <EOL> new_out = T . fill ( i , new_out ) <EOL> return [ new_out ] <EOL> def encompasses_broadcastable ( b1 , b2 ) : <EOL> """<STR_LIT>""" <EOL> if len ( b1 ) < len ( b2 ) : <EOL> return False <EOL> b1 = b1 [ - len ( b2 ) : ] <EOL> return not any ( v1 and not v2 for v1 , v2 in zip ( b1 , b2 ) ) <EOL> def merge_broadcastables ( broadcastables ) : <EOL> return [ all ( bcast ) for bcast in zip ( * broadcastables ) ] <EOL> def scalarconsts_rest ( inputs ) : <EOL> """<STR_LIT>""" <EOL> consts = [ ] <EOL> origconsts = [ ] <EOL> nonconsts = [ ] <EOL> for i in inputs : <EOL> try : <EOL> v = get_scalar_constant_value ( i ) <EOL> consts . append ( v ) <EOL> origconsts . append ( i ) <EOL> except NotScalarConstantError : <EOL> nonconsts . append ( i ) <EOL> return consts , origconsts , nonconsts <EOL> def broadcast_like ( value , template , fgraph , dtype = None ) : <EOL> """<STR_LIT>""" <EOL> value = T . as_tensor_variable ( value ) <EOL> if value . type == template . type : <EOL> return value <EOL> if template not in fgraph . variables : <EOL> raise NotImplementedError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if hasattr ( fgraph , '<STR_LIT>' ) : <EOL> new_shape = fgraph . shape_feature . shape_of [ template ] <EOL> else : <EOL> new_shape = template . shape <EOL> if dtype is None : <EOL> dtype = template . dtype <EOL> rval = T . alloc ( T . cast ( value , dtype ) , * new_shape ) <EOL> if rval . broadcastable != template . broadcastable : <EOL> rval = T . unbroadcast ( rval , * [ i for i in xrange ( rval . ndim ) <EOL> if rval . broadcastable [ i ] and <EOL> not template . broadcastable [ i ] ] ) <EOL> assert rval . type . dtype == dtype <EOL> if rval . type . broadcastable != template . broadcastable : <EOL> raise AssertionError ( "<STR_LIT>" + <EOL> str ( rval . type . broadcastable ) + <EOL> "<STR_LIT>" + <EOL> str ( template . broadcastable ) ) <EOL> return rval <EOL> def inplace_elemwise_optimizer_op ( OP ) : <EOL> """<STR_LIT>""" <EOL> @ gof . inplace_optimizer <EOL> def inplace_elemwise_optimizer ( fgraph ) : <EOL> """<STR_LIT>""" <EOL> check_each_change = config . tensor . insert_inplace_optimizer_validate_nb <EOL> if check_each_change == - <NUM_LIT:1> : <EOL> if len ( fgraph . apply_nodes ) > <NUM_LIT> : <EOL> check_each_change = <NUM_LIT:10> <EOL> else : <EOL> check_each_change = <NUM_LIT:1> <EOL> nb_change_no_validate = <NUM_LIT:0> <EOL> chk = fgraph . checkpoint ( ) <EOL> if fgraph . update_mapping : <EOL> update_outs = [ fgraph . outputs [ i ] for i in fgraph . update_mapping ] <EOL> else : <EOL> update_outs = [ ] <EOL> for node in list ( graph . io_toposort ( fgraph . inputs , fgraph . outputs ) ) : <EOL> op = node . op <EOL> if not type ( op ) == OP : <EOL> continue <EOL> if ( check_each_change != <NUM_LIT:1> and <EOL> all ( [ getattr ( o . type , '<STR_LIT>' , - <NUM_LIT:1> ) == <NUM_LIT:0> <EOL> for o in node . outputs ] ) ) : <EOL> continue <EOL> baseline = op . inplace_pattern <EOL> protected_inputs = [ <EOL> f . protected for f in node . fgraph . _features if <EOL> isinstance ( f , theano . compile . function_module . Supervisor ) ] <EOL> protected_inputs = sum ( protected_inputs , [ ] ) <EOL> protected_inputs . extend ( fgraph . outputs ) <EOL> candidate_outputs = [ i for i in xrange ( len ( node . outputs ) ) <EOL> if i not in baseline ] <EOL> candidate_inputs = [ i for i in xrange ( len ( node . inputs ) ) <EOL> if i not in baseline . values ( ) and <EOL> not isinstance ( node . inputs [ i ] , Constant ) and <EOL> not fgraph . destroyers ( node . inputs [ i ] ) and <EOL> node . inputs [ i ] not in protected_inputs ] <EOL> verbose = False <EOL> raised_warning = not verbose <EOL> for candidate_output in candidate_outputs : <EOL> candidate_out_var = node . outputs [ candidate_output ] <EOL> sorted_candidate_inputs = candidate_inputs <EOL> if candidate_out_var in update_outs : <EOL> updated_inputs = [ ] <EOL> for i , f_out in enumerate ( fgraph . outputs ) : <EOL> if ( f_out is candidate_out_var and i in fgraph . update_mapping ) : <EOL> updated_inp_idx = fgraph . update_mapping [ i ] <EOL> updated_inputs . append ( fgraph . inputs [ updated_inp_idx ] ) <EOL> updated_vars = [ ] <EOL> vars_from_inplace = [ ] <EOL> other_vars = [ ] <EOL> for inp_idx in candidate_inputs : <EOL> inp = node . inputs [ inp_idx ] <EOL> if inp in updated_inputs : <EOL> updated_vars . append ( inp_idx ) <EOL> elif ( hasattr ( fgraph , '<STR_LIT>' ) and <EOL> inp . owner and <EOL> any ( [ fgraph . destroy_handler . root_destroyer . get ( up_inp , None ) is inp . owner <EOL> for up_inp in updated_inputs ] ) ) : <EOL> vars_from_inplace . append ( inp_idx ) <EOL> else : <EOL> other_vars . append ( inp_idx ) <EOL> sorted_candidate_inputs = ( updated_vars + <EOL> vars_from_inplace + other_vars ) <EOL> for candidate_input in sorted_candidate_inputs : <EOL> if node . inputs [ candidate_input ] . type != node . outputs [ <EOL> candidate_output ] . type : <EOL> continue <EOL> inplace_pattern = dict ( baseline ) <EOL> inplace_pattern [ candidate_output ] = candidate_input <EOL> try : <EOL> if hasattr ( op . scalar_op , "<STR_LIT>" ) : <EOL> new_scal = op . scalar_op . make_new_inplace ( <EOL> scalar . transfer_type ( <EOL> * [ inplace_pattern . get ( i , o . dtype ) <EOL> for i , o in enumerate ( node . outputs ) ] ) ) <EOL> else : <EOL> new_scal = op . scalar_op . __class__ ( <EOL> scalar . transfer_type ( <EOL> * [ inplace_pattern . get ( i , None ) <EOL> for i in xrange ( len ( node . outputs ) ) ] ) ) <EOL> new_outputs = OP ( new_scal , inplace_pattern ) ( <EOL> * node . inputs , ** dict ( return_list = True ) ) <EOL> new_node = new_outputs [ <NUM_LIT:0> ] . owner <EOL> for r , new_r in zip ( node . outputs , new_outputs ) : <EOL> fgraph . replace ( r , new_r , <EOL> reason = "<STR_LIT>" ) <EOL> nb_change_no_validate += <NUM_LIT:1> <EOL> if nb_change_no_validate >= check_each_change : <EOL> fgraph . validate ( ) <EOL> chk = fgraph . checkpoint ( ) <EOL> nb_change_no_validate = <NUM_LIT:0> <EOL> except ( ValueError , InconsistencyError ) as e : <EOL> if check_each_change != <NUM_LIT:1> and not raised_warning : <EOL> print ( ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> file = sys . stderr ) <EOL> print ( e , file = sys . stderr ) <EOL> raised_warning = True <EOL> fgraph . revert ( chk ) <EOL> continue <EOL> candidate_inputs . remove ( candidate_input ) <EOL> node = new_node <EOL> baseline = inplace_pattern <EOL> break <EOL> if nb_change_no_validate > <NUM_LIT:0> : <EOL> try : <EOL> fgraph . validate ( ) <EOL> except Exception : <EOL> if not raised_warning : <EOL> print ( ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> file = sys . stderr ) <EOL> fgraph . revert ( chk ) <EOL> return inplace_elemwise_optimizer <EOL> inplace_elemwise_optimizer = inplace_elemwise_optimizer_op ( T . Elemwise ) <EOL> compile . optdb . register ( '<STR_LIT>' , inplace_elemwise_optimizer , <NUM_LIT> , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> def register_canonicalize ( lopt , * tags , ** kwargs ) : <EOL> if type ( lopt ) == str : <EOL> def register ( inner_lopt ) : <EOL> return register_canonicalize ( inner_lopt , lopt , * tags , ** kwargs ) <EOL> return register <EOL> else : <EOL> name = kwargs . pop ( '<STR_LIT:name>' , None ) or lopt . __name__ <EOL> compile . optdb [ '<STR_LIT>' ] . register ( name , lopt , '<STR_LIT>' , <EOL> * tags , ** kwargs ) <EOL> return lopt <EOL> def register_stabilize ( lopt , * tags , ** kwargs ) : <EOL> if type ( lopt ) == str : <EOL> def register ( inner_lopt ) : <EOL> return register_stabilize ( inner_lopt , lopt , * tags , ** kwargs ) <EOL> return register <EOL> else : <EOL> name = kwargs . pop ( '<STR_LIT:name>' , None ) or lopt . __name__ <EOL> compile . optdb [ '<STR_LIT>' ] . register ( name , lopt , '<STR_LIT>' , <EOL> * tags , ** kwargs ) <EOL> return lopt <EOL> def register_specialize ( lopt , * tags , ** kwargs ) : <EOL> if type ( lopt ) == str : <EOL> def register ( inner_lopt ) : <EOL> return register_specialize ( inner_lopt , lopt , * tags , ** kwargs ) <EOL> return register <EOL> else : <EOL> name = kwargs . pop ( '<STR_LIT:name>' , None ) or lopt . __name__ <EOL> compile . optdb [ '<STR_LIT>' ] . register ( name , lopt , '<STR_LIT>' , <EOL> * tags , ** kwargs ) <EOL> return lopt <EOL> def register_uncanonicalize ( lopt , * tags , ** kwargs ) : <EOL> if type ( lopt ) == str : <EOL> def register ( inner_lopt ) : <EOL> return register_uncanonicalize ( inner_lopt , lopt , * tags , ** kwargs ) <EOL> return register <EOL> else : <EOL> name = ( kwargs and kwargs . pop ( '<STR_LIT:name>' ) ) or lopt . __name__ <EOL> compile . optdb [ '<STR_LIT>' ] . register ( name , lopt , '<STR_LIT>' , * tags ) <EOL> return lopt <EOL> def register_specialize_device ( lopt , * tags , ** kwargs ) : <EOL> if type ( lopt ) == str : <EOL> def register ( inner_lopt ) : <EOL> return register_specialize_device ( inner_lopt , lopt , * tags , ** kwargs ) <EOL> return register <EOL> else : <EOL> name = ( kwargs and kwargs . pop ( '<STR_LIT:name>' ) ) or lopt . __name__ <EOL> compile . optdb [ '<STR_LIT>' ] . register ( name , lopt , '<STR_LIT>' , * tags ) <EOL> return lopt <EOL> @ register_canonicalize <EOL> @ register_stabilize <EOL> @ gof . local_optimizer ( [ T . Dot ] ) <EOL> def local_0_dot_x ( node ) : <EOL> if not isinstance ( node . op , T . Dot ) : <EOL> return False <EOL> x = node . inputs [ <NUM_LIT:0> ] <EOL> y = node . inputs [ <NUM_LIT:1> ] <EOL> replace = False <EOL> try : <EOL> if get_scalar_constant_value ( x ) == <NUM_LIT:0> : <EOL> replace = True <EOL> except NotScalarConstantError : <EOL> pass <EOL> try : <EOL> if get_scalar_constant_value ( y ) == <NUM_LIT:0> : <EOL> replace = True <EOL> except NotScalarConstantError : <EOL> pass <EOL> if replace : <EOL> constant_zero = T . constant ( <NUM_LIT:0> , dtype = node . outputs [ <NUM_LIT:0> ] . type . dtype ) <EOL> if x . ndim == <NUM_LIT:2> and y . ndim == <NUM_LIT:2> : <EOL> constant_zero = assert_ ( constant_zero , <EOL> T . eq ( x . shape [ <NUM_LIT:1> ] , y . shape [ <NUM_LIT:0> ] ) ) <EOL> return [ T . alloc ( constant_zero , x . shape [ <NUM_LIT:0> ] , y . shape [ <NUM_LIT:1> ] ) ] <EOL> elif x . ndim == <NUM_LIT:1> and y . ndim == <NUM_LIT:2> : <EOL> constant_zero = assert_ ( constant_zero , <EOL> T . eq ( x . shape [ <NUM_LIT:0> ] , y . shape [ <NUM_LIT:0> ] ) ) <EOL> return [ T . alloc ( constant_zero , y . shape [ <NUM_LIT:1> ] ) ] <EOL> elif x . ndim == <NUM_LIT:2> and y . ndim == <NUM_LIT:1> : <EOL> constant_zero = assert_ ( constant_zero , <EOL> T . eq ( x . shape [ <NUM_LIT:1> ] , y . shape [ <NUM_LIT:0> ] ) ) <EOL> return [ T . alloc ( constant_zero , x . shape [ <NUM_LIT:0> ] ) ] <EOL> elif x . ndim == <NUM_LIT:1> and y . ndim == <NUM_LIT:1> : <EOL> constant_zero = assert_ ( constant_zero , <EOL> T . eq ( x . shape [ <NUM_LIT:0> ] , y . shape [ <NUM_LIT:0> ] ) ) <EOL> return [ constant_zero ] <EOL> else : <EOL> _logger . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> x . type , y . type ) <EOL> def apply_local_dimshuffle_lift ( var ) : <EOL> if not var . owner : <EOL> return var <EOL> new = local_dimshuffle_lift . transform ( var . owner ) <EOL> if new : <EOL> return new [ <NUM_LIT:0> ] <EOL> return var <EOL> @ gof . local_optimizer ( [ DimShuffle ] ) <EOL> def local_dimshuffle_lift ( node ) : <EOL> """<STR_LIT>""" <EOL> op = node . op <EOL> if not isinstance ( op , DimShuffle ) : <EOL> return False <EOL> input = node . inputs [ <NUM_LIT:0> ] <EOL> inode = input . owner <EOL> if inode and isinstance ( inode . op , Elemwise ) and ( len ( input . clients ) == <NUM_LIT:1> ) : <EOL> new_inputs = [ ] <EOL> for inp in inode . inputs : <EOL> new_inp = op . __class__ ( inp . type . broadcastable , <EOL> op . new_order , <EOL> op . inplace ) ( inp ) <EOL> new_inputs . append ( apply_local_dimshuffle_lift ( new_inp ) ) <EOL> copy_stack_trace ( node . outputs [ <NUM_LIT:0> ] , new_inputs ) <EOL> ret = inode . op ( * new_inputs , ** dict ( return_list = True ) ) <EOL> return ret <EOL> if inode and isinstance ( inode . op , DimShuffle ) : <EOL> new_order = [ x == '<STR_LIT:x>' and '<STR_LIT:x>' or inode . op . new_order [ x ] for x in <EOL> op . new_order ] <EOL> inplace = op . inplace and inode . op . inplace <EOL> iinput = inode . inputs [ <NUM_LIT:0> ] <EOL> if ( new_order == list ( range ( len ( new_order ) ) ) and <EOL> len ( new_order ) == iinput . type . ndim ) : <EOL> return [ iinput ] <EOL> else : <EOL> ret = op . __class__ ( iinput . type . broadcastable , new_order , <EOL> inplace ) ( iinput ) <EOL> ret = apply_local_dimshuffle_lift ( ret ) <EOL> copy_stack_trace ( node . outputs [ <NUM_LIT:0> ] , ret ) <EOL> return [ ret ] <EOL> @ register_canonicalize <EOL> @ gof . local_optimizer ( [ T . DimShuffle ] ) <EOL> def local_lift_transpose_through_dot ( node ) : <EOL> """<STR_LIT>""" <EOL> if not ( isinstance ( node . op , T . DimShuffle ) and node . op . new_order == ( <NUM_LIT:1> , <NUM_LIT:0> ) ) : <EOL> return False <EOL> if not ( node . inputs [ <NUM_LIT:0> ] . owner and <EOL> isinstance ( node . inputs [ <NUM_LIT:0> ] . owner . op , T . Dot ) ) : <EOL> return False <EOL> x , y = node . inputs [ <NUM_LIT:0> ] . owner . inputs <EOL> if x . ndim == y . ndim == <NUM_LIT:2> : <EOL> ret = [ T . dot ( y . T , x . T ) ] <EOL> copy_stack_trace ( node . inputs [ <NUM_LIT:0> ] , ret ) <EOL> return ret <EOL> @ gof . local_optimizer ( [ DimShuffle ] ) <EOL> def dimshuffle_as_view ( node ) : <EOL> op = node . op <EOL> if not isinstance ( op , DimShuffle ) or op . inplace : <EOL> return False <EOL> new_op = op . __class__ ( op . input_broadcastable , op . new_order , inplace = True ) <EOL> v = new_op ( * node . inputs ) <EOL> copy_stack_trace ( node . outputs [ <NUM_LIT:0> ] , v ) <EOL> return [ v ] <EOL> compile . optdb . register ( '<STR_LIT>' , <EOL> TopoOptimizer ( <EOL> dimshuffle_as_view , <EOL> failure_callback = TopoOptimizer . warn_inplace ) , <EOL> <NUM_LIT> , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> register_canonicalize ( local_dimshuffle_lift ) <EOL> register_specialize ( local_dimshuffle_lift ) <EOL> @ register_canonicalize <EOL> @ gof . local_optimizer ( [ T . DimShuffle ] ) <EOL> def local_dimshuffle_no_inplace_at_canonicalize ( node ) : <EOL> if isinstance ( node . op , T . DimShuffle ) and node . op . inplace : <EOL> return [ T . DimShuffle ( node . op . input_broadcastable , <EOL> node . op . new_order , inplace = False ) ( node . inputs [ <NUM_LIT:0> ] ) ] <EOL> @ register_canonicalize <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( [ T . TensorFromScalar ] ) <EOL> def local_tensor_scalar_tensor ( node ) : <EOL> '''<STR_LIT>''' <EOL> if isinstance ( node . op , T . TensorFromScalar ) : <EOL> s = node . inputs [ <NUM_LIT:0> ] <EOL> if s . owner and isinstance ( s . owner . op , T . ScalarFromTensor ) : <EOL> t = s . owner . inputs [ <NUM_LIT:0> ] <EOL> return [ t ] <EOL> @ register_canonicalize <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( [ T . ScalarFromTensor ] ) <EOL> def local_scalar_tensor_scalar ( node ) : <EOL> '''<STR_LIT>''' <EOL> if isinstance ( node . op , T . ScalarFromTensor ) : <EOL> t = node . inputs [ <NUM_LIT:0> ] <EOL> if t . owner and isinstance ( t . owner . op , T . TensorFromScalar ) : <EOL> s = t . owner . inputs [ <NUM_LIT:0> ] <EOL> return [ s ] <EOL> class MakeVector ( T . Op ) : <EOL> """<STR_LIT>""" <EOL> __props__ = ( "<STR_LIT>" , ) <EOL> def __init__ ( self , dtype = '<STR_LIT>' ) : <EOL> self . dtype = dtype <EOL> def make_node ( self , * inputs ) : <EOL> inputs = list ( map ( T . as_tensor_variable , inputs ) ) <EOL> if ( not all ( a . type == inputs [ <NUM_LIT:0> ] . type for a in inputs ) or <EOL> ( len ( inputs ) > <NUM_LIT:0> and inputs [ <NUM_LIT:0> ] . dtype != self . dtype ) ) : <EOL> dtype = theano . scalar . upcast ( self . dtype , * [ i . dtype for i in inputs ] ) <EOL> assert dtype == self . dtype , ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if not all ( self . dtype == T . cast ( i , dtype = dtype ) . dtype <EOL> for i in inputs ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( self . dtype , str ( [ i . dtype for i in inputs ] ) ) ) <EOL> inputs = [ T . cast ( i , dtype = dtype ) for i in inputs ] <EOL> assert all ( self . dtype == a . dtype for a in inputs ) <EOL> assert all ( a . ndim == <NUM_LIT:0> for a in inputs ) <EOL> if inputs : <EOL> dtype = inputs [ <NUM_LIT:0> ] . type . dtype <EOL> else : <EOL> dtype = self . dtype <EOL> bcastable = False <EOL> otype = T . TensorType ( broadcastable = ( bcastable , ) , dtype = dtype ) <EOL> return T . Apply ( self , inputs , [ otype ( ) ] ) <EOL> def perform ( self , node , inputs , out_ ) : <EOL> out , = out_ <EOL> if ( out [ <NUM_LIT:0> ] is None ) or ( out [ <NUM_LIT:0> ] . size != len ( inputs ) ) : <EOL> out [ <NUM_LIT:0> ] = theano . _asarray ( inputs , dtype = node . outputs [ <NUM_LIT:0> ] . dtype ) <EOL> else : <EOL> out [ <NUM_LIT:0> ] [ ... ] = inputs <EOL> def c_code_cache_version ( self ) : <EOL> return ( <NUM_LIT:2> , ) <EOL> def c_code ( self , node , name , inp , out_ , sub ) : <EOL> out , = out_ <EOL> out_shape = len ( inp ) <EOL> out_num = numpy . dtype ( node . outputs [ <NUM_LIT:0> ] . dtype ) . num <EOL> out_dtype = node . outputs [ <NUM_LIT:0> ] . type . dtype_specs ( ) [ <NUM_LIT:1> ] <EOL> if len ( inp ) > <NUM_LIT:0> : <EOL> assert self . dtype == node . inputs [ <NUM_LIT:0> ] . dtype <EOL> out_num = '<STR_LIT>' % inp [ <NUM_LIT:0> ] <EOL> ret = """<STR_LIT>""" % locals ( ) <EOL> for idx , i in enumerate ( inp ) : <EOL> ret += """<STR_LIT>""" % locals ( ) <EOL> return ret <EOL> def infer_shape ( self , node , ishapes ) : <EOL> return [ ( len ( ishapes ) , ) ] <EOL> def grad ( self , inputs , output_gradients ) : <EOL> if '<STR_LIT:int>' in self . dtype : <EOL> return [ ipt . zeros_like ( ) . astype ( theano . config . floatX ) <EOL> for ipt in inputs ] <EOL> grads = [ ] <EOL> for i , inp in enumerate ( inputs ) : <EOL> grads . append ( output_gradients [ <NUM_LIT:0> ] [ i ] ) <EOL> return grads <EOL> def R_op ( self , inputs , eval_points ) : <EOL> if None in eval_points : <EOL> return [ None ] <EOL> return self . make_node ( * eval_points ) . outputs <EOL> make_vector = MakeVector ( ) <EOL> class MakeVectorPrinter : <EOL> def process ( self , r , pstate ) : <EOL> if r . owner is None : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> elif isinstance ( r . owner . op , MakeVector ) : <EOL> return "<STR_LIT>" % "<STR_LIT:U+002CU+0020>" . join ( <EOL> pstate . pprinter . process ( input , pstate . clone ( precedence = <NUM_LIT:1000> ) ) <EOL> for input in r . owner . inputs ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> T . pprint . assign ( lambda pstate , r : r . owner and <EOL> isinstance ( r . owner . op , MakeVector ) , MakeVectorPrinter ( ) ) <EOL> class ShapeFeature ( object ) : <EOL> """<STR_LIT>""" <EOL> def get_node_infer_shape ( self , node ) : <EOL> try : <EOL> shape_infer = node . op . infer_shape <EOL> except AttributeError : <EOL> shape_infer = self . default_infer_shape <EOL> try : <EOL> o_shapes = shape_infer ( node , <EOL> [ self . shape_of [ r ] for r in node . inputs ] ) <EOL> except ShapeError : <EOL> o_shapes = self . default_infer_shape ( node , [ self . shape_of [ r ] for <EOL> r in node . inputs ] ) <EOL> except NotImplementedError as e : <EOL> raise NotImplementedError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % e ) <EOL> except Exception as e : <EOL> msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) % ( <EOL> node . op , [ self . shape_of [ r ] for r in node . inputs ] , <EOL> type ( e ) , str ( e ) , traceback . format_exc ( ) ) <EOL> if config . on_shape_error == "<STR_LIT>" : <EOL> raise Exception ( msg ) <EOL> else : <EOL> _logger . warning ( msg ) <EOL> o_shapes = self . default_infer_shape ( <EOL> node , [ self . shape_of [ r ] for r in node . inputs ] ) <EOL> return o_shapes <EOL> def get_shape ( self , var , idx ) : <EOL> """<STR_LIT>""" <EOL> r = self . shape_of [ var ] [ idx ] <EOL> if ( r . owner and <EOL> isinstance ( r . owner . op , Shape_i ) and <EOL> r . owner . inputs [ <NUM_LIT:0> ] not in var . fgraph . variables ) : <EOL> assert var . owner <EOL> node = var . owner <EOL> for i in node . inputs : <EOL> if getattr ( i , '<STR_LIT>' , None ) > <NUM_LIT:0> : <EOL> self . get_shape ( i , <NUM_LIT:0> ) <EOL> o_shapes = self . get_node_infer_shape ( node ) <EOL> assert len ( o_shapes ) == len ( node . outputs ) <EOL> for new_shps , out in zip ( o_shapes , node . outputs ) : <EOL> if not hasattr ( out , '<STR_LIT>' ) : <EOL> continue <EOL> merged_shps = list ( self . shape_of [ out ] ) <EOL> changed = False <EOL> for i in range ( out . ndim ) : <EOL> n_r = merged_shps [ i ] <EOL> if ( n_r . owner and <EOL> isinstance ( n_r . owner . op , Shape_i ) and <EOL> n_r . owner . inputs [ <NUM_LIT:0> ] not in var . fgraph . variables ) : <EOL> changed = True <EOL> merged_shps [ i ] = new_shps [ i ] <EOL> if changed : <EOL> self . set_shape ( out , merged_shps , override = True ) <EOL> r = self . shape_of [ var ] [ idx ] <EOL> return r <EOL> def shape_ir ( self , i , r ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( r . type , "<STR_LIT>" ) and r . type . broadcastable [ i ] : <EOL> return self . lscalar_one <EOL> else : <EOL> s = Shape_i ( i ) ( r ) <EOL> try : <EOL> s = get_scalar_constant_value ( s ) <EOL> except NotScalarConstantError : <EOL> pass <EOL> return s <EOL> def shape_tuple ( self , r ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( r , '<STR_LIT>' ) : <EOL> return None <EOL> return tuple ( [ self . shape_ir ( i , r ) for i in xrange ( r . ndim ) ] ) <EOL> def default_infer_shape ( self , node , i_shapes ) : <EOL> """<STR_LIT>""" <EOL> rval = [ ] <EOL> for r in node . outputs : <EOL> try : <EOL> rval . append ( self . shape_tuple ( r ) ) <EOL> except AttributeError : <EOL> rval . append ( None ) <EOL> return rval <EOL> def unpack ( self , s_i ) : <EOL> """<STR_LIT>""" <EOL> assert s_i is not None <EOL> if s_i == <NUM_LIT:1> : <EOL> return self . lscalar_one <EOL> if ( type ( s_i ) in integer_types or <EOL> isinstance ( s_i , numpy . integer ) or <EOL> ( isinstance ( s_i , numpy . ndarray ) and s_i . ndim == <NUM_LIT:0> ) ) : <EOL> assert s_i >= <NUM_LIT:0> <EOL> return T . constant ( s_i , dtype = '<STR_LIT>' ) <EOL> if type ( s_i ) in ( tuple , list ) : <EOL> raise NotImplementedError ( s_i ) <EOL> if ( s_i . owner and <EOL> isinstance ( s_i . owner . op , Subtensor ) and <EOL> s_i . owner . inputs [ <NUM_LIT:0> ] . owner and <EOL> isinstance ( s_i . owner . inputs [ <NUM_LIT:0> ] . owner . op , T . Shape ) ) : <EOL> assert s_i . ndim == <NUM_LIT:0> <EOL> assert len ( s_i . owner . op . idx_list ) == <NUM_LIT:1> <EOL> idx = get_idx_list ( s_i . owner . inputs , s_i . owner . op . idx_list ) <EOL> assert len ( idx ) == <NUM_LIT:1> <EOL> idx = idx [ <NUM_LIT:0> ] <EOL> try : <EOL> i = get_scalar_constant_value ( idx ) <EOL> except NotScalarConstantError : <EOL> pass <EOL> else : <EOL> x = s_i . owner . inputs [ <NUM_LIT:0> ] . owner . inputs [ <NUM_LIT:0> ] <EOL> s_i = self . shape_of [ x ] [ i ] <EOL> if s_i . type . dtype [ : <NUM_LIT:3> ] in ( '<STR_LIT:int>' , '<STR_LIT>' ) : <EOL> if getattr ( s_i . type , '<STR_LIT>' , <NUM_LIT:0> ) : <EOL> raise TypeError ( '<STR_LIT>' , s_i ) <EOL> return s_i <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' , <EOL> s_i , type ( s_i ) , getattr ( s_i , '<STR_LIT:type>' , None ) ) <EOL> def set_shape ( self , r , s , override = False ) : <EOL> """<STR_LIT>""" <EOL> if not override : <EOL> assert r not in self . shape_of , '<STR_LIT>' <EOL> if s is None : <EOL> self . shape_of [ r ] = s <EOL> else : <EOL> if not isinstance ( s , ( tuple , list ) ) : <EOL> raise TypeError ( '<STR_LIT>' , ( r , s ) ) <EOL> if r . ndim != len ( s ) : <EOL> sio = StringIO ( ) <EOL> theano . printing . debugprint ( r , file = sio , print_type = True ) <EOL> raise AssertionError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( <EOL> len ( s ) , r . ndim , sio . getvalue ( ) ) ) <EOL> shape_vars = [ ] <EOL> for i in xrange ( r . ndim ) : <EOL> if ( hasattr ( r . type , '<STR_LIT>' ) and <EOL> r . type . broadcastable [ i ] ) : <EOL> shape_vars . append ( self . lscalar_one ) <EOL> else : <EOL> shape_vars . append ( self . unpack ( s [ i ] ) ) <EOL> assert all ( [ not hasattr ( r . type , "<STR_LIT>" ) or <EOL> not r . type . broadcastable [ i ] or <EOL> self . lscalar_one . equals ( shape_vars [ i ] ) or <EOL> self . lscalar_one . equals ( <EOL> T . extract_constant ( shape_vars [ i ] ) ) <EOL> for i in xrange ( r . ndim ) ] ) <EOL> self . shape_of [ r ] = tuple ( shape_vars ) <EOL> for sv in shape_vars : <EOL> self . shape_of_reverse_index . setdefault ( sv , set ( ) ) . add ( r ) <EOL> def update_shape ( self , r , other_r ) : <EOL> """<STR_LIT>""" <EOL> assert other_r in self . shape_of , ( '<STR_LIT>' , other_r ) <EOL> other_shape = self . shape_of [ other_r ] <EOL> if other_shape is None : <EOL> return <EOL> if r in self . shape_of : <EOL> r_shape = self . shape_of [ r ] <EOL> else : <EOL> self . set_shape ( r , other_shape ) <EOL> return <EOL> if ( other_r . owner and r . owner and <EOL> other_r . owner . inputs == r . owner . inputs and <EOL> other_r . owner . op == r . owner . op ) : <EOL> return <EOL> merged_shape = [ ] <EOL> for i , ps in enumerate ( other_shape ) : <EOL> if r_shape is None and other_shape : <EOL> merged_shape . append ( other_shape [ i ] ) <EOL> elif ( ps . owner and <EOL> isinstance ( getattr ( ps . owner , '<STR_LIT>' , None ) , Shape_i ) and <EOL> ps . owner . op . i == i and <EOL> ps . owner . inputs [ <NUM_LIT:0> ] in ( r , other_r ) ) : <EOL> merged_shape . append ( r_shape [ i ] ) <EOL> elif isinstance ( r_shape [ i ] , ( Constant , int ) ) : <EOL> merged_shape . append ( r_shape [ i ] ) <EOL> elif isinstance ( other_shape [ i ] , ( Constant , int ) ) : <EOL> merged_shape . append ( other_shape [ i ] ) <EOL> elif other_shape [ i ] == r_shape [ i ] : <EOL> merged_shape . append ( r_shape [ i ] ) <EOL> elif r_shape [ i ] in theano . gof . graph . ancestors ( [ other_shape [ i ] ] ) : <EOL> merged_shape . append ( r_shape [ i ] ) <EOL> else : <EOL> merged_shape . append ( other_shape [ i ] ) <EOL> assert all ( [ ( not hasattr ( r . type , "<STR_LIT>" ) or <EOL> not r . type . broadcastable [ i ] and <EOL> not other_r . type . broadcastable [ i ] ) or <EOL> self . lscalar_one . equals ( merged_shape [ i ] ) or <EOL> self . lscalar_one . equals ( <EOL> T . extract_constant ( merged_shape [ i ] ) ) <EOL> for i in xrange ( r . ndim ) ] ) <EOL> self . shape_of [ r ] = tuple ( merged_shape ) <EOL> for sv in self . shape_of [ r ] : <EOL> self . shape_of_reverse_index . setdefault ( sv , set ( ) ) . add ( r ) <EOL> def set_shape_i ( self , r , i , s_i ) : <EOL> '''<STR_LIT>''' <EOL> assert r in self . shape_of <EOL> prev_shape = self . shape_of [ r ] <EOL> new_shape = [ ] <EOL> for j , s_j in enumerate ( prev_shape ) : <EOL> if j == i : <EOL> new_shape . append ( self . unpack ( s_i ) ) <EOL> else : <EOL> new_shape . append ( s_j ) <EOL> assert all ( [ not hasattr ( r . type , "<STR_LIT>" ) or <EOL> not r . type . broadcastable [ idx ] or <EOL> self . lscalar_one . equals ( new_shape [ idx ] ) or <EOL> self . lscalar_one . equals ( T . extract_constant ( new_shape [ idx ] ) ) <EOL> for idx in xrange ( r . ndim ) ] ) <EOL> self . shape_of [ r ] = tuple ( new_shape ) <EOL> for sv in self . shape_of [ r ] : <EOL> self . shape_of_reverse_index . setdefault ( sv , set ( ) ) . add ( r ) <EOL> def init_r ( self , r ) : <EOL> '''<STR_LIT>''' <EOL> if r not in self . shape_of : <EOL> try : <EOL> self . set_shape ( r , self . shape_tuple ( r ) ) <EOL> except AttributeError : <EOL> self . set_shape ( r , None ) <EOL> def make_vector_shape ( self , r ) : <EOL> return make_vector ( * self . shape_of [ r ] ) <EOL> def on_attach ( self , fgraph ) : <EOL> assert not hasattr ( fgraph , '<STR_LIT>' ) <EOL> fgraph . shape_feature = self <EOL> self . lscalar_one = T . constant ( <NUM_LIT:1> , dtype = '<STR_LIT>' ) <EOL> assert self . lscalar_one . type == T . lscalar <EOL> self . shape_of = { } <EOL> self . scheduled = { } <EOL> self . shape_of_reverse_index = { } <EOL> for node in fgraph . toposort ( ) : <EOL> self . on_import ( fgraph , node , reason = '<STR_LIT>' ) <EOL> def on_import ( self , fgraph , node , reason ) : <EOL> if node . outputs [ <NUM_LIT:0> ] in self . shape_of : <EOL> for r in node . outputs + node . inputs : <EOL> assert r in self . shape_of <EOL> return <EOL> for i , r in enumerate ( node . inputs ) : <EOL> self . init_r ( r ) <EOL> o_shapes = self . get_node_infer_shape ( node ) <EOL> if len ( o_shapes ) != len ( node . outputs ) : <EOL> raise Exception ( <EOL> ( '<STR_LIT>' + <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' ) % ( str ( node . op ) , <EOL> len ( o_shapes ) , <EOL> len ( node . outputs ) ) ) <EOL> for sh_idx , sh in enumerate ( o_shapes ) : <EOL> if sh is None : <EOL> continue <EOL> if not isinstance ( sh , ( list , tuple ) ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( str ( node ) , str ( o_shapes ) ) ) <EOL> new_shape = [ ] <EOL> for i , d in enumerate ( sh ) : <EOL> if getattr ( d , '<STR_LIT>' , '<STR_LIT>' ) != '<STR_LIT>' : <EOL> assert d . dtype in theano . tensor . discrete_dtypes , ( node , d . dtype ) <EOL> assert str ( d . dtype ) != '<STR_LIT>' , node <EOL> new_shape += sh [ len ( new_shape ) : i + <NUM_LIT:1> ] <EOL> new_shape [ i ] = theano . tensor . cast ( d , '<STR_LIT>' ) <EOL> if new_shape : <EOL> new_shape += sh [ len ( new_shape ) : ] <EOL> o_shapes [ sh_idx ] = tuple ( new_shape ) <EOL> for r , s in izip ( node . outputs , o_shapes ) : <EOL> self . set_shape ( r , s ) <EOL> def on_change_input ( self , fgraph , node , i , r , new_r , reason ) : <EOL> if new_r not in self . shape_of : <EOL> self . init_r ( new_r ) <EOL> self . update_shape ( new_r , r ) <EOL> for ( shpnode , idx ) in ( r . clients + [ ( node , i ) ] ) : <EOL> if isinstance ( getattr ( shpnode , '<STR_LIT>' , None ) , Shape_i ) : <EOL> idx = shpnode . op . i <EOL> repl = self . shape_of [ new_r ] [ idx ] <EOL> if repl . owner is shpnode : <EOL> continue <EOL> if ( repl . owner and <EOL> repl . owner . inputs [ <NUM_LIT:0> ] is shpnode . inputs [ <NUM_LIT:0> ] and <EOL> isinstance ( repl . owner . op , Shape_i ) and <EOL> repl . owner . op . i == shpnode . op . i ) : <EOL> continue <EOL> if shpnode . outputs [ <NUM_LIT:0> ] in theano . gof . graph . ancestors ( [ repl ] ) : <EOL> raise InconsistencyError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( node , i , r , new_r ) ) <EOL> self . scheduled [ shpnode ] = new_r <EOL> unscheduled = [ k for k , v in self . scheduled . items ( ) if v == r ] <EOL> for k in unscheduled : <EOL> del self . scheduled [ k ] <EOL> for v in self . shape_of_reverse_index . get ( r , [ ] ) : <EOL> for ii , svi in enumerate ( self . shape_of . get ( v , [ ] ) ) : <EOL> if svi == r : <EOL> self . set_shape_i ( v , ii , new_r ) <EOL> self . shape_of_reverse_index [ r ] = set ( ) <EOL> def same_shape ( self , x , y , dim_x = None , dim_y = None ) : <EOL> """<STR_LIT>""" <EOL> sx = self . shape_of [ x ] <EOL> sy = self . shape_of [ y ] <EOL> if sx is None or sy is None : <EOL> return False <EOL> if dim_x is not None : <EOL> sx = [ sx [ dim_x ] ] <EOL> if dim_y is not None : <EOL> sy = [ sy [ dim_y ] ] <EOL> assert len ( sx ) == len ( sy ) <EOL> for dx , dy in zip ( sx , sy ) : <EOL> if dx is dy : <EOL> continue <EOL> if not dx . owner or not dy . owner : <EOL> return False <EOL> if ( not isinstance ( dx . owner . op , Shape_i ) or <EOL> not isinstance ( dy . owner . op , Shape_i ) ) : <EOL> return False <EOL> opx = dx . owner . op <EOL> opy = dy . owner . op <EOL> if not ( opx . i == opy . i ) : <EOL> return False <EOL> if dx . owner . inputs [ <NUM_LIT:0> ] == dy . owner . inputs [ <NUM_LIT:0> ] : <EOL> continue <EOL> from theano . scan_module . scan_utils import equal_computations <EOL> if not equal_computations ( [ dx ] , [ dy ] ) : <EOL> return False <EOL> return True <EOL> class ShapeOptimizer ( Optimizer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> Optimizer . __init__ ( self ) <EOL> def add_requirements ( self , fgraph ) : <EOL> fgraph . attach_feature ( ShapeFeature ( ) ) <EOL> def apply ( self , fgraph ) : <EOL> pass <EOL> theano . compile . mode . optdb . register ( '<STR_LIT>' , ShapeOptimizer ( ) , <EOL> <NUM_LIT:0.1> , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def local_elemwise_alloc_op ( ElemwiseOP , AllocOP , DimShuffleOP ) : <EOL> def local_elemwise_alloc ( node ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( node . op , ElemwiseOP ) : <EOL> return False <EOL> if len ( node . outputs ) > <NUM_LIT:1> : <EOL> assert all ( [ o . type . broadcastable == <EOL> node . outputs [ <NUM_LIT:0> ] . type . broadcastable for o in <EOL> node . outputs [ <NUM_LIT:1> : ] ] ) <EOL> if not any ( [ i . type . broadcastable == <EOL> node . outputs [ <NUM_LIT:0> ] . type . broadcastable for i in node . inputs ] ) : <EOL> return False <EOL> def dimshuffled_alloc ( i ) : <EOL> return ( isinstance ( i . owner . op , DimShuffleOP ) and <EOL> i . owner . inputs [ <NUM_LIT:0> ] . owner and <EOL> isinstance ( i . owner . inputs [ <NUM_LIT:0> ] . owner . op , AllocOP ) ) <EOL> if not any ( [ i . owner and ( isinstance ( i . owner . op , AllocOP ) or <EOL> dimshuffled_alloc ( i ) ) for i in node . inputs ] ) : <EOL> return False <EOL> assert_op_idx = - <NUM_LIT:1> <EOL> for idx , i in enumerate ( node . inputs ) : <EOL> if i . type . broadcastable == node . outputs [ <NUM_LIT:0> ] . type . broadcastable : <EOL> if not ( i . owner and ( isinstance ( i . owner . op , AllocOP ) or <EOL> dimshuffled_alloc ( i ) ) ) : <EOL> assert_op_idx = idx <EOL> break <EOL> if assert_op_idx < <NUM_LIT:0> : <EOL> l2 = [ i for i in node . inputs <EOL> if ( i . owner and ( isinstance ( i . owner . op , AllocOP ) or <EOL> dimshuffled_alloc ( i ) ) ) ] <EOL> if len ( l2 ) > <NUM_LIT:1> : <EOL> l = [ idx for idx , i in enumerate ( node . inputs ) <EOL> if i . broadcastable == node . outputs [ <NUM_LIT:0> ] . broadcastable ] <EOL> assert_op_idx = l [ <NUM_LIT:0> ] <EOL> else : <EOL> return False <EOL> assert_op = node . inputs [ assert_op_idx ] <EOL> cmp_op = assert_op <EOL> new_i = [ ] <EOL> same_shape = node . fgraph . shape_feature . same_shape <EOL> for i in node . inputs : <EOL> if ( i . owner and isinstance ( i . owner . op , AllocOP ) and <EOL> i . owner . inputs [ <NUM_LIT:0> ] . type != i . owner . outputs [ <NUM_LIT:0> ] . type ) : <EOL> assert i . type . ndim == cmp_op . ndim <EOL> get_shape = node . fgraph . shape_feature . get_shape <EOL> if theano . config . experimental . local_alloc_elemwise_assert : <EOL> cond = [ ] <EOL> for idx in xrange ( i . type . ndim ) : <EOL> if ( not i . type . broadcastable [ idx ] and <EOL> not same_shape ( i , cmp_op , idx , idx ) ) : <EOL> i_shp = get_shape ( i , idx ) <EOL> cmp_shp = get_shape ( cmp_op , idx ) <EOL> cond . append ( T . eq ( i_shp , cmp_shp ) ) <EOL> if cond : <EOL> assert_op = assert_ ( assert_op , * cond ) <EOL> new_i . append ( i . owner . inputs [ <NUM_LIT:0> ] ) <EOL> elif i . owner and dimshuffled_alloc ( i ) : <EOL> assert i . type . ndim == cmp_op . type . ndim <EOL> if theano . config . experimental . local_alloc_elemwise_assert : <EOL> assert_cond = [ T . eq ( i . shape [ idx ] , cmp_op . shape [ idx ] ) <EOL> for idx in xrange ( i . type . ndim ) <EOL> if not i . type . broadcastable [ idx ] and <EOL> not same_shape ( i , cmp_op , idx , idx ) ] <EOL> if assert_cond : <EOL> assert_op = assert_ ( assert_op , * assert_cond ) <EOL> alloc_input = i . owner . inputs [ <NUM_LIT:0> ] . owner . inputs [ <NUM_LIT:0> ] <EOL> if alloc_input . ndim != i . owner . inputs [ <NUM_LIT:0> ] . ndim : <EOL> nb_dim_to_add = i . owner . inputs [ <NUM_LIT:0> ] . ndim - alloc_input . ndim <EOL> alloc_input = alloc_input . dimshuffle ( <EOL> [ '<STR_LIT:x>' ] * nb_dim_to_add + <EOL> list ( range ( alloc_input . ndim ) ) ) <EOL> r_i = i . owner . op ( alloc_input ) <EOL> copy_stack_trace ( i , r_i ) <EOL> new_i . append ( r_i ) <EOL> else : <EOL> new_i . append ( i ) <EOL> new_i [ assert_op_idx ] = assert_op <EOL> ret = node . op ( * new_i , return_list = True ) <EOL> copy_stack_trace ( node . outputs , ret ) <EOL> return ret <EOL> return local_elemwise_alloc <EOL> local_elemwise_alloc = register_specialize ( <EOL> gof . local_optimizer ( [ T . Elemwise ] ) ( <EOL> local_elemwise_alloc_op ( T . Elemwise , T . Alloc , T . DimShuffle ) ) , <EOL> '<STR_LIT>' ) <EOL> @ gof . local_optimizer ( [ T . Elemwise ] ) <EOL> def local_fill_sink ( node ) : <EOL> """<STR_LIT>""" <EOL> if ( not hasattr ( node , '<STR_LIT>' ) or <EOL> not isinstance ( node . op , T . Elemwise ) or <EOL> node . op == T . fill ) : <EOL> return False <EOL> models = [ ] <EOL> inputs = [ ] <EOL> for input in node . inputs : <EOL> if input . owner and input . owner . op == T . fill : <EOL> models . append ( input . owner . inputs [ <NUM_LIT:0> ] ) <EOL> inputs . append ( input . owner . inputs [ <NUM_LIT:1> ] ) <EOL> else : <EOL> inputs . append ( input ) <EOL> if not models : <EOL> return False <EOL> c = node . op ( * inputs ) <EOL> for model in models : <EOL> if model . type != c . type : <EOL> c = T . fill ( model , c ) <EOL> replacements = { node . outputs [ <NUM_LIT:0> ] : c } <EOL> for client , cl_idx in node . outputs [ <NUM_LIT:0> ] . clients : <EOL> if ( hasattr ( client , '<STR_LIT>' ) and <EOL> isinstance ( client . op , T . Elemwise ) and <EOL> not client . op == T . fill ) : <EOL> client_inputs = client . inputs [ : ] <EOL> client_inputs [ cl_idx ] = c <EOL> new_client = client . op ( * client_inputs ) <EOL> new_client . owner . outputs [ <NUM_LIT:0> ] . clients = client . outputs [ <NUM_LIT:0> ] . clients <EOL> r = local_fill_sink . transform ( new_client . owner ) <EOL> if not r : <EOL> continue <EOL> replacements . update ( r ) <EOL> return replacements <EOL> register_canonicalize ( local_fill_sink ) <EOL> @ register_specialize <EOL> @ register_stabilize <EOL> @ gof . local_optimizer ( [ T . fill ] ) <EOL> def local_fill_to_alloc ( node ) : <EOL> """<STR_LIT>""" <EOL> if node . op == T . fill : <EOL> r , v = node . inputs <EOL> if v . type == node . outputs [ <NUM_LIT:0> ] . type : <EOL> rval = [ v ] <EOL> elif v . type . broadcastable == node . outputs [ <NUM_LIT:0> ] . type . broadcastable : <EOL> rval = [ T . cast ( v , node . outputs [ <NUM_LIT:0> ] . type . dtype ) ] <EOL> elif r . type . broadcastable == node . outputs [ <NUM_LIT:0> ] . type . broadcastable : <EOL> o = broadcast_like ( v , r , node . fgraph , dtype = v . dtype ) <EOL> copy_stack_trace ( node . outputs [ <NUM_LIT:0> ] , o ) <EOL> rval = [ o ] <EOL> else : <EOL> return <EOL> assert rval [ <NUM_LIT:0> ] . type == node . outputs [ <NUM_LIT:0> ] . type , ( <EOL> '<STR_LIT>' , rval [ <NUM_LIT:0> ] . type , '<STR_LIT>' , node . outputs [ <NUM_LIT:0> ] . type , '<STR_LIT>' , <EOL> node , ) <EOL> return rval <EOL> compile . optdb . register ( '<STR_LIT>' , <EOL> in2out ( local_fill_to_alloc ) , <EOL> <NUM_LIT> , '<STR_LIT>' ) <EOL> compile . optdb . register ( '<STR_LIT>' , <EOL> in2out ( local_elemwise_alloc ) , <EOL> <NUM_LIT> , '<STR_LIT>' ) <EOL> @ register_canonicalize ( "<STR_LIT>" ) <EOL> @ gof . local_optimizer ( [ T . fill ] ) <EOL> def local_useless_fill ( node ) : <EOL> """<STR_LIT>""" <EOL> if node . op == T . fill : <EOL> r , v = node . inputs <EOL> if v . type == node . outputs [ <NUM_LIT:0> ] . type : <EOL> return [ v ] <EOL> @ register_specialize <EOL> @ register_stabilize <EOL> @ register_canonicalize <EOL> @ gof . local_optimizer ( [ T . alloc ] ) <EOL> def local_useless_alloc ( node ) : <EOL> """<STR_LIT>""" <EOL> if node . op == T . alloc : <EOL> if node . inputs [ <NUM_LIT:0> ] . type == node . outputs [ <NUM_LIT:0> ] . type : <EOL> return [ node . inputs [ <NUM_LIT:0> ] ] <EOL> @ gof . local_optimizer ( [ T . AllocEmpty ] ) <EOL> def local_alloc_empty_to_zeros ( node ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( node . op , T . AllocEmpty ) : <EOL> return [ T . zeros ( node . inputs , dtype = node . outputs [ <NUM_LIT:0> ] . dtype ) ] <EOL> compile . optdb . register ( '<STR_LIT>' , <EOL> in2out ( local_alloc_empty_to_zeros ) , <EOL> <NUM_LIT> , <EOL> '<STR_LIT>' , ) <EOL> @ register_specialize <EOL> @ register_canonicalize <EOL> @ gof . local_optimizer ( [ T . shape ] ) <EOL> def local_shape_to_shape_i ( node ) : <EOL> if node . op == T . shape : <EOL> if not hasattr ( node . fgraph , '<STR_LIT>' ) : <EOL> return <EOL> shape_feature = node . fgraph . shape_feature <EOL> ret = shape_feature . make_vector_shape ( node . inputs [ <NUM_LIT:0> ] ) <EOL> copy_stack_trace ( node . outputs [ <NUM_LIT:0> ] , ret ) <EOL> return [ ret ] <EOL> @ register_specialize <EOL> @ register_canonicalize <EOL> @ gof . local_optimizer ( None ) <EOL> def local_track_shape_i ( node ) : <EOL> try : <EOL> shape_feature = node . fgraph . shape_feature <EOL> except AttributeError : <EOL> return <EOL> if node in shape_feature . scheduled : <EOL> assert isinstance ( node . op , Shape_i ) <EOL> replacement = shape_feature . scheduled [ node ] <EOL> return [ shape_feature . shape_of [ replacement ] [ node . op . i ] ] <EOL> @ register_specialize <EOL> @ register_canonicalize ( '<STR_LIT>' ) <EOL> @ gof . local_optimizer ( [ Subtensor , AdvancedSubtensor1 ] ) <EOL> def local_subtensor_make_vector ( node ) : <EOL> """<STR_LIT>""" <EOL> x = node . inputs [ <NUM_LIT:0> ] <EOL> if not x . owner or x . owner . op != make_vector : <EOL> return <EOL> if isinstance ( node . op , Subtensor ) : <EOL> try : <EOL> idx , = node . op . idx_list <EOL> except Exception : <EOL> raise <EOL> if isinstance ( idx , ( scalar . Scalar , T . TensorType ) ) : <EOL> old_idx , idx = idx , node . inputs [ <NUM_LIT:1> ] <EOL> assert idx . type == old_idx <EOL> elif isinstance ( node . op , AdvancedSubtensor1 ) : <EOL> idx = node . inputs [ <NUM_LIT:1> ] <EOL> else : <EOL> return <EOL> if isinstance ( idx , ( int , numpy . integer ) ) : <EOL> return [ x . owner . inputs [ idx ] ] <EOL> elif isinstance ( idx , Variable ) : <EOL> if idx . ndim == <NUM_LIT:0> : <EOL> try : <EOL> v = get_scalar_constant_value ( idx ) <EOL> if isinstance ( v , numpy . integer ) : <EOL> v = int ( v ) <EOL> try : <EOL> ret = [ x . owner . inputs [ v ] ] <EOL> except IndexError : <EOL> raise NotScalarConstantError ( "<STR_LIT>" ) <EOL> return ret <EOL> except NotScalarConstantError : <EOL> pass <EOL> elif idx . ndim == <NUM_LIT:1> and isinstance ( idx , T . Constant ) : <EOL> values = list ( map ( int , list ( idx . value ) ) ) <EOL> ret = [ make_vector ( * [ x . owner . inputs [ v ] for v in values ] ) ] <EOL> copy_stack_trace ( node . outputs [ <NUM_LIT:0> ] , ret ) <EOL> return ret <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> elif isinstance ( idx , slice ) : <EOL> try : <EOL> const_slice = node . op . get_constant_idx ( node . inputs , <EOL> allow_partial = False ) [ <NUM_LIT:0> ] <EOL> ret = make_vector ( * x . owner . inputs [ const_slice ] ) <EOL> copy_stack_trace ( node . outputs , ret ) <EOL> return [ ret ] <EOL> except NotScalarConstantError : <EOL> pass <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> @ register_canonicalize ( '<STR_LIT>' ) <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( [ T . Elemwise ] ) <EOL> def local_useless_elemwise ( node ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( node . op , T . Elemwise ) : <EOL> def zeros_like ( node , in_idx ) : <EOL> return [ T . fill ( node . inputs [ in_idx ] , <EOL> T . constant ( <NUM_LIT:0.0> , dtype = node . outputs [ <NUM_LIT:0> ] . type . dtype ) ) ] <EOL> def ones_like ( node , in_idx ) : <EOL> return [ T . fill ( node . inputs [ in_idx ] , <EOL> T . constant ( <NUM_LIT:1.0> , dtype = node . outputs [ <NUM_LIT:0> ] . type . dtype ) ) ] <EOL> if node . op . scalar_op == theano . scalar . eq and len ( node . inputs ) == <NUM_LIT:2> : <EOL> if node . inputs [ <NUM_LIT:0> ] == node . inputs [ <NUM_LIT:1> ] : <EOL> ret = [ T . fill ( node . inputs [ <NUM_LIT:0> ] , <EOL> T . constant ( <NUM_LIT:1.0> , <EOL> dtype = node . outputs [ <NUM_LIT:0> ] . type . dtype ) ) ] <EOL> copy_stack_trace ( node . outputs [ <NUM_LIT:0> ] , ret ) <EOL> return ret <EOL> elif node . op . scalar_op == theano . scalar . neq and len ( node . inputs ) == <NUM_LIT:2> : <EOL> if node . inputs [ <NUM_LIT:0> ] == node . inputs [ <NUM_LIT:1> ] : <EOL> ret = [ T . fill ( node . inputs [ <NUM_LIT:0> ] , <EOL> T . constant ( <NUM_LIT:0.0> , <EOL> dtype = node . outputs [ <NUM_LIT:0> ] . type . dtype ) ) ] <EOL> copy_stack_trace ( node . outputs [ <NUM_LIT:0> ] , ret ) <EOL> return ret <EOL> elif node . op . scalar_op == theano . scalar . mul and len ( node . inputs ) == <NUM_LIT:1> : <EOL> return [ node . inputs [ <NUM_LIT:0> ] ] <EOL> elif node . op . scalar_op == theano . scalar . add and len ( node . inputs ) == <NUM_LIT:1> : <EOL> return [ node . inputs [ <NUM_LIT:0> ] ] <EOL> elif ( node . op . scalar_op == theano . scalar . identity and <EOL> len ( node . inputs ) == <NUM_LIT:1> ) : <EOL> return [ node . inputs [ <NUM_LIT:0> ] ] <EOL> elif ( isinstance ( node . op . scalar_op , scalar . AND ) and <EOL> len ( node . inputs ) == <NUM_LIT:2> ) : <EOL> if isinstance ( node . inputs [ <NUM_LIT:0> ] , T . TensorConstant ) : <EOL> const_val = T . extract_constant ( node . inputs [ <NUM_LIT:0> ] ) <EOL> if not isinstance ( const_val , Variable ) : <EOL> if const_val == <NUM_LIT:0> : <EOL> return zeros_like ( node , <NUM_LIT:1> ) <EOL> else : <EOL> return [ node . inputs [ <NUM_LIT:1> ] ] <EOL> if isinstance ( node . inputs [ <NUM_LIT:1> ] , T . TensorConstant ) : <EOL> const_val = T . extract_constant ( node . inputs [ <NUM_LIT:1> ] ) <EOL> if not isinstance ( const_val , Variable ) : <EOL> if const_val == <NUM_LIT:0> : <EOL> return zeros_like ( node , <NUM_LIT:0> ) <EOL> else : <EOL> return [ node . inputs [ <NUM_LIT:0> ] ] <EOL> elif ( isinstance ( node . op . scalar_op , scalar . OR ) and <EOL> len ( node . inputs ) == <NUM_LIT:2> ) : <EOL> if isinstance ( node . inputs [ <NUM_LIT:0> ] , T . TensorConstant ) : <EOL> const_val = T . extract_constant ( node . inputs [ <NUM_LIT:0> ] ) <EOL> if not isinstance ( const_val , Variable ) : <EOL> if const_val == <NUM_LIT:0> : <EOL> return [ node . inputs [ <NUM_LIT:1> ] ] <EOL> else : <EOL> return ones_like ( node , <NUM_LIT:1> ) <EOL> if isinstance ( node . inputs [ <NUM_LIT:1> ] , T . TensorConstant ) : <EOL> const_val = T . extract_constant ( node . inputs [ <NUM_LIT:1> ] ) <EOL> if not isinstance ( const_val , Variable ) : <EOL> if const_val == <NUM_LIT:0> : <EOL> return [ node . inputs [ <NUM_LIT:0> ] ] <EOL> else : <EOL> return ones_like ( node , <NUM_LIT:0> ) <EOL> elif ( isinstance ( node . op . scalar_op , scalar . XOR ) and <EOL> len ( node . inputs ) == <NUM_LIT:2> ) : <EOL> if node . inputs [ <NUM_LIT:0> ] is node . inputs [ <NUM_LIT:1> ] : <EOL> return zeros_like ( node , <NUM_LIT:0> ) <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( [ T . Elemwise ] ) <EOL> def local_alloc_unary ( node ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( node . op , T . Elemwise ) and len ( node . inputs ) == <NUM_LIT:1> : <EOL> a = node . inputs [ <NUM_LIT:0> ] <EOL> if a . owner and isinstance ( a . owner . op , T . Alloc ) : <EOL> x = a . owner . inputs [ <NUM_LIT:0> ] <EOL> shp = a . owner . inputs [ <NUM_LIT:1> : ] <EOL> v = node . op ( x ) <EOL> copy_stack_trace ( node . outputs [ <NUM_LIT:0> ] , v ) <EOL> ret = T . alloc ( T . cast ( v , node . outputs [ <NUM_LIT:0> ] . dtype ) , * shp ) <EOL> copy_stack_trace ( [ node . outputs [ <NUM_LIT:0> ] , a ] , ret ) <EOL> return [ ret ] <EOL> @ register_canonicalize <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( [ T . Elemwise ] ) <EOL> def local_cast_cast ( node ) : <EOL> """<STR_LIT>""" <EOL> if ( not isinstance ( node . op , T . Elemwise ) or <EOL> not isinstance ( node . op . scalar_op , scalar . Cast ) ) : <EOL> return <EOL> x = node . inputs [ <NUM_LIT:0> ] <EOL> if ( not x . owner or <EOL> not isinstance ( x . owner . op , T . Elemwise ) or <EOL> not isinstance ( x . owner . op . scalar_op , scalar . Cast ) ) : <EOL> return <EOL> if node . op . scalar_op . o_type == x . owner . op . scalar_op . o_type : <EOL> return [ x ] <EOL> @ register_canonicalize <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( [ T . Elemwise ] ) <EOL> def local_func_inv ( node ) : <EOL> """<STR_LIT>""" <EOL> inv_pairs = ( <EOL> ( basic . Deg2Rad , basic . Rad2Deg ) , <EOL> ( basic . Cosh , basic . ArcCosh ) , <EOL> ( basic . Tanh , basic . ArcTanh ) , <EOL> ( basic . Sinh , basic . ArcSinh ) , <EOL> ( basic . Conj , basic . Conj ) , <EOL> ( basic . Neg , basic . Neg ) , <EOL> ( basic . Inv , basic . Inv ) , <EOL> ) <EOL> x = node . inputs [ <NUM_LIT:0> ] <EOL> if not isinstance ( node . op , T . Elemwise ) : <EOL> return <EOL> if ( not x . owner or not isinstance ( x . owner . op , T . Elemwise ) ) : <EOL> return <EOL> prev_op = x . owner . op . scalar_op <EOL> node_op = node . op . scalar_op <EOL> for inv_pair in inv_pairs : <EOL> if is_inverse_pair ( node_op , prev_op , inv_pair ) : <EOL> return x . owner . inputs <EOL> return <EOL> def is_inverse_pair ( node_op , prev_op , inv_pair ) : <EOL> """<STR_LIT>""" <EOL> node_is_op0 = isinstance ( node_op , inv_pair [ <NUM_LIT:0> ] ) <EOL> node_is_op1 = isinstance ( node_op , inv_pair [ <NUM_LIT:1> ] ) <EOL> prev_is_op0 = isinstance ( prev_op , inv_pair [ <NUM_LIT:0> ] ) <EOL> prev_is_op1 = isinstance ( prev_op , inv_pair [ <NUM_LIT:1> ] ) <EOL> return ( node_is_op0 and prev_is_op1 ) or ( node_is_op1 and prev_is_op0 ) <EOL> class Assert ( T . Op ) : <EOL> """<STR_LIT>""" <EOL> __props__ = ( '<STR_LIT>' , ) <EOL> view_map = { <NUM_LIT:0> : [ <NUM_LIT:0> ] } <EOL> check_input = False <EOL> def __init__ ( self , msg = "<STR_LIT>" ) : <EOL> self . msg = msg <EOL> def __setstate__ ( self , attrs ) : <EOL> self . __dict__ . update ( attrs ) <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . msg = "<STR_LIT>" <EOL> def make_node ( self , value , * conds ) : <EOL> if not isinstance ( value , Variable ) : <EOL> value = T . as_tensor_variable ( value ) <EOL> cond = [ T . as_tensor_variable ( c ) for c in conds ] <EOL> assert numpy . all ( [ c . type . ndim == <NUM_LIT:0> for c in cond ] ) <EOL> return gof . Apply ( self , [ value ] + cond , [ value . type ( ) ] ) <EOL> def perform ( self , node , inputs , out_ ) : <EOL> out , = out_ <EOL> v = inputs [ <NUM_LIT:0> ] <EOL> out [ <NUM_LIT:0> ] = v <EOL> assert numpy . all ( inputs [ <NUM_LIT:1> : ] ) , self . msg <EOL> def grad ( self , input , output_gradients ) : <EOL> return output_gradients + [ DisconnectedType ( ) ( ) ] * ( len ( input ) - <NUM_LIT:1> ) <EOL> def connection_pattern ( self , node ) : <EOL> return [ [ <NUM_LIT:1> ] ] + [ [ <NUM_LIT:0> ] ] * ( len ( node . inputs ) - <NUM_LIT:1> ) <EOL> def c_code ( self , node , name , inames , onames , sub ) : <EOL> value = inames [ <NUM_LIT:0> ] <EOL> out = onames [ <NUM_LIT:0> ] <EOL> check = [ ] <EOL> fail = sub [ '<STR_LIT>' ] <EOL> msg = self . msg . replace ( '<STR_LIT:">' , '<STR_LIT>' ) . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> for idx in xrange ( len ( inames ) - <NUM_LIT:1> ) : <EOL> i = inames [ idx + <NUM_LIT:1> ] <EOL> dtype = node . inputs [ idx + <NUM_LIT:1> ] . dtype <EOL> check . append ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % locals ( ) ) <EOL> check = "<STR_LIT:\n>" . join ( check ) <EOL> return """<STR_LIT>""" % locals ( ) <EOL> def c_code_cache_version ( self ) : <EOL> return ( <NUM_LIT:3> , <NUM_LIT:0> ) <EOL> def infer_shape ( self , node , input_shapes ) : <EOL> return [ input_shapes [ <NUM_LIT:0> ] ] <EOL> assert_ = Assert ( ) <EOL> assert_op = assert_ <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( [ Assert ] ) <EOL> def local_remove_useless_assert ( node ) : <EOL> if isinstance ( node . op , Assert ) : <EOL> cond = [ ] <EOL> for c in node . inputs [ <NUM_LIT:1> : ] : <EOL> try : <EOL> const = get_scalar_constant_value ( c ) <EOL> if <NUM_LIT:0> != const . ndim or const == <NUM_LIT:0> : <EOL> cond . append ( c ) <EOL> except NotScalarConstantError : <EOL> cond . append ( c ) <EOL> if len ( cond ) == <NUM_LIT:0> : <EOL> return [ node . inputs [ <NUM_LIT:0> ] ] <EOL> if len ( cond ) != len ( node . inputs ) - <NUM_LIT:1> : <EOL> ret = assert_ ( node . inputs [ <NUM_LIT:0> ] , * cond ) <EOL> copy_stack_trace ( node . outputs [ <NUM_LIT:0> ] , ret ) <EOL> return [ ret ] <EOL> @ gof . local_optimizer ( [ Assert ] ) <EOL> def local_remove_all_assert ( node ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( node . op , Assert ) : <EOL> return <EOL> return [ node . inputs [ <NUM_LIT:0> ] ] <EOL> compile . optdb [ '<STR_LIT>' ] . register ( '<STR_LIT>' , <EOL> local_remove_all_assert , <EOL> '<STR_LIT>' , <EOL> use_db_name_as_tag = False ) <EOL> compile . optdb [ '<STR_LIT>' ] . register ( '<STR_LIT>' , <EOL> local_remove_all_assert , <EOL> '<STR_LIT>' , <EOL> use_db_name_as_tag = False ) <EOL> compile . optdb [ '<STR_LIT>' ] . register ( '<STR_LIT>' , <EOL> local_remove_all_assert , <EOL> '<STR_LIT>' , <EOL> use_db_name_as_tag = False ) <EOL> @ register_canonicalize <EOL> @ gof . local_optimizer ( [ T . Elemwise ] ) <EOL> def local_upcast_elemwise_constant_inputs ( node ) : <EOL> """<STR_LIT>""" <EOL> if len ( node . outputs ) > <NUM_LIT:1> : <EOL> return <EOL> try : <EOL> shape_i = node . fgraph . shape_feature . shape_i <EOL> except AttributeError : <EOL> shape_i = None <EOL> if isinstance ( node . op , T . Elemwise ) : <EOL> scalar_op = node . op . scalar_op <EOL> if ( getattr ( scalar_op , '<STR_LIT>' , None ) <EOL> in ( T . scal . upgrade_to_float , T . scal . upcast_out ) ) : <EOL> output_dtype = node . outputs [ <NUM_LIT:0> ] . type . dtype <EOL> new_inputs = [ ] <EOL> for i in node . inputs : <EOL> if i . type . dtype == output_dtype : <EOL> new_inputs . append ( i ) <EOL> else : <EOL> try : <EOL> cval_i = get_scalar_constant_value ( i , elemwise = False ) <EOL> if all ( i . broadcastable ) : <EOL> new_inputs . append ( T . shape_padleft ( <EOL> T . cast ( cval_i , output_dtype ) , <EOL> i . ndim ) ) <EOL> else : <EOL> if shape_i is None : <EOL> return <EOL> new_inputs . append ( <EOL> T . alloc ( T . cast ( cval_i , output_dtype ) , <EOL> * [ shape_i ( d ) ( i ) <EOL> for d in xrange ( i . ndim ) ] ) ) <EOL> except NotScalarConstantError : <EOL> if isinstance ( i , T . TensorConstant ) : <EOL> new_inputs . append ( T . cast ( i , output_dtype ) ) <EOL> else : <EOL> new_inputs . append ( i ) <EOL> if new_inputs != node . inputs : <EOL> rval = [ node . op ( * new_inputs ) ] <EOL> if rval [ <NUM_LIT:0> ] . type != node . outputs [ <NUM_LIT:0> ] . type : <EOL> return <EOL> copy_stack_trace ( node . outputs [ <NUM_LIT:0> ] , rval ) <EOL> return rval <EOL> @ register_canonicalize <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( [ IncSubtensor ] ) <EOL> def local_useless_inc_subtensor ( node ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( node . op , IncSubtensor ) : <EOL> return <EOL> if node . op . set_instead_of_inc is False : <EOL> try : <EOL> c = get_scalar_constant_value ( node . inputs [ <NUM_LIT:0> ] ) <EOL> if c != <NUM_LIT:0> : <EOL> return <EOL> except NotScalarConstantError : <EOL> return <EOL> if ( node . inputs [ <NUM_LIT:0> ] . ndim != node . inputs [ <NUM_LIT:1> ] . ndim or <EOL> node . inputs [ <NUM_LIT:0> ] . broadcastable != node . inputs [ <NUM_LIT:1> ] . broadcastable ) : <EOL> return <EOL> idx_cst = get_idx_list ( node . inputs [ <NUM_LIT:1> : ] , node . op . idx_list ) <EOL> if all ( isinstance ( e , slice ) and e . start is None and <EOL> e . stop is None and ( e . step is None or T . extract_constant ( e . step ) == - <NUM_LIT:1> ) <EOL> for e in idx_cst ) : <EOL> if not hasattr ( node . fgraph , '<STR_LIT>' ) : <EOL> return <EOL> if not node . fgraph . shape_feature . same_shape ( node . inputs [ <NUM_LIT:0> ] , <EOL> node . inputs [ <NUM_LIT:1> ] ) : <EOL> return <EOL> if all ( e . step is None <EOL> for e in node . op . idx_list ) : <EOL> return [ node . inputs [ <NUM_LIT:1> ] ] <EOL> ret = Subtensor ( node . op . idx_list ) ( * node . inputs [ <NUM_LIT:1> : ] ) <EOL> copy_stack_trace ( node . outputs , ret ) <EOL> return [ ret ] <EOL> @ register_canonicalize <EOL> @ gof . local_optimizer ( [ AdvancedIncSubtensor1 ] ) <EOL> def local_set_to_inc_subtensor ( node ) : <EOL> """<STR_LIT>""" <EOL> if ( isinstance ( node . op , AdvancedIncSubtensor1 ) and <EOL> node . op . set_instead_of_inc and <EOL> node . inputs [ <NUM_LIT:1> ] . owner and <EOL> isinstance ( node . inputs [ <NUM_LIT:1> ] . owner . op , Elemwise ) and <EOL> isinstance ( node . inputs [ <NUM_LIT:1> ] . owner . op . scalar_op , scalar . Add ) ) : <EOL> addn = node . inputs [ <NUM_LIT:1> ] . owner <EOL> subn = None <EOL> other = None <EOL> if ( addn . inputs [ <NUM_LIT:0> ] . owner and <EOL> isinstance ( addn . inputs [ <NUM_LIT:0> ] . owner . op , AdvancedSubtensor1 ) ) : <EOL> subn = addn . inputs [ <NUM_LIT:0> ] . owner <EOL> other = addn . inputs [ <NUM_LIT:1> ] <EOL> elif ( addn . inputs [ <NUM_LIT:1> ] . owner and <EOL> isinstance ( addn . inputs [ <NUM_LIT:1> ] . owner . op , AdvancedSubtensor1 ) ) : <EOL> subn = addn . inputs [ <NUM_LIT:1> ] . owner <EOL> other = addn . inputs [ <NUM_LIT:0> ] <EOL> else : <EOL> return <EOL> if ( subn . inputs [ <NUM_LIT:1> ] != node . inputs [ <NUM_LIT:2> ] or <EOL> subn . inputs [ <NUM_LIT:0> ] != node . inputs [ <NUM_LIT:0> ] ) : <EOL> return <EOL> ret = advanced_inc_subtensor1 ( node . inputs [ <NUM_LIT:0> ] , other , node . inputs [ <NUM_LIT:2> ] ) <EOL> copy_stack_trace ( node . outputs , ret ) <EOL> return [ ret ] <EOL> @ register_canonicalize <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( [ Subtensor ] ) <EOL> def local_useless_slice ( node ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( node . op , Subtensor ) : <EOL> slices = get_idx_list ( node . inputs , node . op . idx_list ) <EOL> last_slice = len ( slices ) <EOL> for s in slices [ : : - <NUM_LIT:1> ] : <EOL> if ( isinstance ( s , slice ) and s . start is None and s . stop is None and <EOL> ( s . step is None or T . extract_constant ( s . step ) == <NUM_LIT:1> ) ) : <EOL> last_slice -= <NUM_LIT:1> <EOL> else : <EOL> break <EOL> if last_slice < len ( slices ) : <EOL> subtens = Subtensor ( slices [ : last_slice ] ) <EOL> sl_ins = Subtensor . collapse ( slices [ : last_slice ] , <EOL> lambda x : isinstance ( x , T . Variable ) ) <EOL> out = subtens ( node . inputs [ <NUM_LIT:0> ] , * sl_ins ) <EOL> copy_stack_trace ( node . outputs , out ) <EOL> return [ out ] <EOL> @ register_canonicalize <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( [ Subtensor , AdvancedSubtensor1 ] ) <EOL> def local_useless_subtensor ( node ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( node . fgraph , '<STR_LIT>' ) : <EOL> return <EOL> shape_of = node . fgraph . shape_feature . shape_of <EOL> if isinstance ( node . op , Subtensor ) : <EOL> cdata = node . op . get_constant_idx ( node . inputs , allow_partial = True , <EOL> only_process_constants = True ) <EOL> for pos , idx in enumerate ( cdata ) : <EOL> if not isinstance ( idx , slice ) : <EOL> return False <EOL> if idx . start is not None and idx . start != <NUM_LIT:0> : <EOL> return False <EOL> if idx . step is not None and idx . step != <NUM_LIT:1> : <EOL> return False <EOL> for pos , idx in enumerate ( cdata ) : <EOL> length_pos = shape_of [ node . inputs [ <NUM_LIT:0> ] ] [ pos ] <EOL> if isinstance ( idx . stop , ( int , numpy . integer ) ) : <EOL> length_pos_data = sys . maxsize <EOL> try : <EOL> length_pos_data = get_scalar_constant_value ( length_pos ) <EOL> except NotScalarConstantError : <EOL> pass <EOL> if idx . stop < length_pos_data : <EOL> return False <EOL> elif isinstance ( idx . stop , gof . Variable ) : <EOL> length_pos_shape_i = idx . stop <EOL> if ( length_pos_shape_i . owner and <EOL> isinstance ( length_pos_shape_i . owner . op , <EOL> T . ScalarFromTensor ) ) : <EOL> length_pos_shape_i = length_pos_shape_i . owner . inputs [ <NUM_LIT:0> ] <EOL> elif ( length_pos . owner and <EOL> isinstance ( length_pos . owner . op , T . TensorFromScalar ) ) : <EOL> length_pos = length_pos . owner . inputs [ <NUM_LIT:0> ] <EOL> else : <EOL> return False <EOL> assert str ( length_pos . type . dtype ) == "<STR_LIT>" <EOL> assert str ( length_pos_shape_i . type . dtype ) in [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] <EOL> if length_pos_shape_i != length_pos : <EOL> return False <EOL> elif idx . stop is None : <EOL> pass <EOL> else : <EOL> return False <EOL> elif isinstance ( node . op , AdvancedSubtensor1 ) : <EOL> try : <EOL> length = get_scalar_constant_value ( shape_of [ node . inputs [ <NUM_LIT:0> ] ] [ <NUM_LIT:0> ] ) <EOL> except NotScalarConstantError : <EOL> return False <EOL> idx = node . inputs [ <NUM_LIT:1> ] <EOL> if isinstance ( idx , T . Constant ) : <EOL> idx = idx . value <EOL> if len ( idx ) != length : <EOL> return False <EOL> if numpy . any ( idx != numpy . arange ( length ) ) : <EOL> return False <EOL> elif idx . owner is not None and isinstance ( idx . owner . op , T . ARange ) : <EOL> try : <EOL> start , stop , step = map ( get_scalar_constant_value , <EOL> idx . owner . inputs ) <EOL> except NotScalarConstantError : <EOL> return False <EOL> if start != <NUM_LIT:0> : <EOL> return False <EOL> if stop != length : <EOL> return False <EOL> if step != <NUM_LIT:1> : <EOL> return False <EOL> else : <EOL> return False <EOL> else : <EOL> return False <EOL> return [ node . inputs [ <NUM_LIT:0> ] ] <EOL> @ register_canonicalize ( '<STR_LIT>' ) <EOL> @ gof . local_optimizer ( [ Subtensor ] ) <EOL> def local_subtensor_lift ( node ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( node . op , Subtensor ) : <EOL> u = node . inputs [ <NUM_LIT:0> ] <EOL> if not u . owner or len ( u . clients ) > <NUM_LIT:1> : <EOL> return False <EOL> if isinstance ( u . owner . op , T . Elemwise ) and len ( u . owner . inputs ) == <NUM_LIT:1> : <EOL> idx = node . inputs [ <NUM_LIT:1> : ] <EOL> x_idx = node . op ( u . owner . inputs [ <NUM_LIT:0> ] , * idx ) <EOL> copy_stack_trace ( node . outputs , x_idx ) <EOL> ret = u . owner . op ( x_idx ) <EOL> copy_stack_trace ( [ node . outputs [ <NUM_LIT:0> ] , node . inputs [ <NUM_LIT:0> ] ] , ret ) <EOL> return [ ret ] <EOL> if isinstance ( u . owner . op , T . Elemwise ) : <EOL> new_inputs = [ ] <EOL> if all ( [ sum ( i . type . broadcastable ) == <NUM_LIT:0> for i in u . owner . inputs ] ) : <EOL> idx = node . inputs [ <NUM_LIT:1> : ] <EOL> new_inputs = [ node . op ( i , * idx ) for i in u . owner . inputs ] <EOL> copy_stack_trace ( node . outputs [ <NUM_LIT:0> ] , new_inputs ) <EOL> ret = u . owner . op ( * new_inputs ) <EOL> copy_stack_trace ( [ node . outputs [ <NUM_LIT:0> ] , node . inputs [ <NUM_LIT:0> ] ] , ret ) <EOL> return [ ret ] <EOL> elif all ( [ sum ( i . type . broadcastable ) in [ i . ndim , <NUM_LIT:0> ] <EOL> for i in u . owner . inputs ] ) : <EOL> idx = node . inputs [ <NUM_LIT:1> : ] <EOL> new_inputs = [ ] <EOL> for i in u . owner . inputs : <EOL> if sum ( i . type . broadcastable ) == <NUM_LIT:0> : <EOL> new_inputs . append ( node . op ( i , * idx ) ) <EOL> else : <EOL> if node . outputs [ <NUM_LIT:0> ] . ndim == i . ndim : <EOL> new_inputs . append ( i ) <EOL> else : <EOL> new_inputs . append ( <EOL> i . dimshuffle ( [ '<STR_LIT:x>' ] * node . outputs [ <NUM_LIT:0> ] . ndim ) ) <EOL> copy_stack_trace ( node . outputs [ <NUM_LIT:0> ] , new_inputs ) <EOL> ret = u . owner . op ( * new_inputs ) <EOL> copy_stack_trace ( [ node . outputs [ <NUM_LIT:0> ] , node . inputs [ <NUM_LIT:0> ] ] , ret ) <EOL> return [ ret ] <EOL> if isinstance ( u . owner . op , T . Rebroadcast ) : <EOL> assert len ( u . owner . inputs ) == <NUM_LIT:1> <EOL> new_axis = [ ] <EOL> j = <NUM_LIT:0> <EOL> for ( i , x ) in enumerate ( node . op . idx_list ) : <EOL> if isinstance ( x , slice ) : <EOL> new_axis += [ ( j , u . broadcastable [ i ] ) ] <EOL> j += <NUM_LIT:1> <EOL> for i in xrange ( len ( node . op . idx_list ) , len ( u . broadcastable ) ) : <EOL> new_axis += [ ( j , u . broadcastable [ i ] ) ] <EOL> j += <NUM_LIT:1> <EOL> subt_x = node . op ( u . owner . inputs [ <NUM_LIT:0> ] , * node . inputs [ <NUM_LIT:1> : ] ) <EOL> copy_stack_trace ( node . outputs [ <NUM_LIT:0> ] , subt_x ) <EOL> rbcast_subt_x = T . Rebroadcast ( * new_axis ) ( subt_x ) <EOL> copy_stack_trace ( [ node . outputs [ <NUM_LIT:0> ] , node . inputs [ <NUM_LIT:0> ] ] , rbcast_subt_x ) <EOL> return [ rbcast_subt_x ] <EOL> def merge_two_slices ( slice1 , len1 , slice2 , len2 ) : <EOL> """<STR_LIT>""" <EOL> list_opt = [ local_abs_merge , local_mul_switch_sink , <EOL> local_upcast_elemwise_constant_inputs , <EOL> local_useless_switch , constant_folding ] <EOL> if type ( slice1 ) is not slice : <EOL> raise ValueError ( ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , slice1 ) <EOL> sl1 , reverse1 = get_canonical_form_slice ( slice1 , len1 ) <EOL> sl2 , reverse2 = get_canonical_form_slice ( slice2 , len2 ) <EOL> if type ( sl2 ) is not slice : <EOL> if reverse1 is None : <EOL> val = sl1 . start + sl2 * sl1 . step <EOL> val = T . switch ( T . le ( len2 , <NUM_LIT:0> ) , len1 + <NUM_LIT:1> , val ) <EOL> val = T . switch ( T . ge ( sl2 , len2 ) , len1 + <NUM_LIT:1> , val ) <EOL> val = T . switch ( T . lt ( sl2 , <NUM_LIT:0> ) , - len1 - <NUM_LIT:1> , val ) <EOL> if sl1 . step : <EOL> val = T . switch ( T . eq ( sl1 . step , <NUM_LIT:0> ) , len1 + <NUM_LIT:1> , val ) <EOL> return val <EOL> else : <EOL> p_val = sl1 . start + sl2 * sl1 . step <EOL> n_val = sl1 . stop - <NUM_LIT:1> - sl2 * sl1 . step <EOL> if config . warn . subtensor_merge_bug : <EOL> _logger . warn ( ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> val = T . switch ( T . lt ( reverse1 , <NUM_LIT:0> ) , n_val , p_val ) <EOL> val = T . switch ( T . le ( len2 , <NUM_LIT:0> ) , len1 + <NUM_LIT:1> , val ) <EOL> val = T . switch ( T . ge ( sl2 , len2 ) , len1 + <NUM_LIT:1> , val ) <EOL> val = T . switch ( T . lt ( sl2 , <NUM_LIT:0> ) , - len1 - <NUM_LIT:1> , val ) <EOL> if sl1 . step : <EOL> val = T . switch ( T . eq ( sl1 . step , <NUM_LIT:0> ) , len1 + <NUM_LIT:1> , val ) <EOL> return val <EOL> else : <EOL> flen = sl2 . stop - sl2 . start <EOL> p_step = sl1 . step * sl2 . step <EOL> n_step = sl1 . step * sl2 . step * - <NUM_LIT:1> <EOL> pp_start = T . minimum ( sl1 . start + sl2 . start * sl1 . step , sl1 . stop ) <EOL> pp_stop = T . minimum ( sl1 . start + sl2 . stop * sl1 . step , sl1 . stop ) <EOL> pn_stop = sl1 . start + ( sl2 . start - <NUM_LIT:1> ) * sl1 . step <EOL> pn_stop = T . switch ( T . and_ ( T . lt ( pn_stop , <NUM_LIT:0> ) , <EOL> T . gt ( flen , <NUM_LIT:0> ) ) , <EOL> - len1 - <NUM_LIT:1> , <EOL> T . minimum ( pn_stop , sl1 . stop ) ) <EOL> pn_start = sl1 . start + ( sl2 . stop - <NUM_LIT:1> ) * sl1 . step <EOL> pn_start = T . minimum ( pn_start , sl1 . stop ) <EOL> pn_start = T . maximum ( pn_start , <NUM_LIT:0> ) <EOL> np_stop = sl1 . stop - sl2 . stop * sl1 . step - <NUM_LIT:1> <EOL> np_stop = T . switch ( T . and_ ( T . lt ( np_stop , <NUM_LIT:0> ) , <EOL> T . gt ( flen , <NUM_LIT:0> ) ) , <EOL> - len1 - <NUM_LIT:1> , <EOL> T . maximum ( sl1 . start - <NUM_LIT:1> , np_stop ) ) <EOL> np_start = T . maximum ( sl1 . start , sl1 . stop - sl2 . start * sl1 . step - <NUM_LIT:1> ) <EOL> nn_start = T . maximum ( sl1 . start , <EOL> ( sl1 . stop - <NUM_LIT:1> ) - ( sl2 . stop - <NUM_LIT:1> ) * sl1 . step ) <EOL> nn_stop = T . maximum ( sl1 . start , sl1 . stop - sl2 . start * sl1 . step ) <EOL> start = T . switch ( T . lt ( reverse2 * reverse1 , <NUM_LIT:0> ) , <EOL> T . switch ( T . lt ( reverse1 , <NUM_LIT:0> ) , np_start , pn_start ) , <EOL> T . switch ( T . lt ( reverse1 , <NUM_LIT:0> ) , nn_start , <EOL> pp_start ) ) <EOL> stop = T . switch ( T . lt ( reverse2 * reverse1 , <NUM_LIT:0> ) , <EOL> T . switch ( T . lt ( reverse1 , <NUM_LIT:0> ) , np_stop , pn_stop ) , <EOL> T . switch ( T . lt ( reverse1 , <NUM_LIT:0> ) , nn_stop , pp_stop ) ) <EOL> step = T . switch ( T . lt ( reverse2 * reverse1 , <NUM_LIT:0> ) , n_step , p_step ) <EOL> start = T . switch ( T . le ( flen , <NUM_LIT:0> ) , <NUM_LIT:0> , start ) <EOL> stop = T . switch ( T . le ( flen , <NUM_LIT:0> ) , <NUM_LIT:0> , stop ) <EOL> start = pre_greedy_local_optimizer ( list_opt , start ) <EOL> stop = pre_greedy_local_optimizer ( list_opt , stop ) <EOL> step = pre_greedy_local_optimizer ( list_opt , step ) <EOL> start = pre_greedy_local_optimizer ( list_opt , start ) <EOL> stop = pre_greedy_local_optimizer ( list_opt , stop ) <EOL> step = pre_greedy_local_optimizer ( list_opt , step ) <EOL> start , stop , step = pre_constant_merge ( [ start , stop , step ] ) <EOL> return slice ( start , stop , step ) <EOL> @ register_canonicalize <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( [ Subtensor ] ) <EOL> def local_subtensor_merge ( node ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( node . op , Subtensor ) : <EOL> u = node . inputs [ <NUM_LIT:0> ] <EOL> if u . owner and isinstance ( u . owner . op , Subtensor ) : <EOL> x = u . owner . inputs [ <NUM_LIT:0> ] <EOL> slices1 = get_idx_list ( u . owner . inputs , u . owner . op . idx_list ) <EOL> slices2 = get_idx_list ( node . inputs , node . op . idx_list ) <EOL> try : <EOL> xshape = node . fgraph . shape_feature . shape_of [ x ] <EOL> ushape = node . fgraph . shape_feature . shape_of [ u ] <EOL> except AttributeError : <EOL> xshape = x . shape <EOL> ushape = u . shape <EOL> merged_slices = [ ] <EOL> pos_2 = <NUM_LIT:0> <EOL> pos_1 = <NUM_LIT:0> <EOL> while ( pos_1 < len ( slices1 ) ) and ( pos_2 < len ( slices2 ) ) : <EOL> slice1 = slices1 [ pos_1 ] <EOL> if type ( slice1 ) is slice : <EOL> merged_slices . append ( <EOL> merge_two_slices ( slice1 , <EOL> xshape [ pos_1 ] , <EOL> slices2 [ pos_2 ] , <EOL> ushape [ pos_2 ] ) ) <EOL> pos_2 += <NUM_LIT:1> <EOL> else : <EOL> merged_slices . append ( slice1 ) <EOL> pos_1 += <NUM_LIT:1> <EOL> if pos_2 < len ( slices2 ) : <EOL> merged_slices += slices2 [ pos_2 : ] <EOL> else : <EOL> merged_slices += slices1 [ pos_1 : ] <EOL> merged_slices = make_constant ( merged_slices ) <EOL> subtens = Subtensor ( merged_slices ) <EOL> sl_ins = Subtensor . collapse ( <EOL> merged_slices , <EOL> lambda x : isinstance ( x , T . Variable ) ) <EOL> out = subtens ( x , * sl_ins ) <EOL> copy_stack_trace ( [ node . outputs [ <NUM_LIT:0> ] , node . inputs [ <NUM_LIT:0> ] ] , out ) <EOL> return [ out ] <EOL> @ register_canonicalize <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( [ Subtensor ] ) <EOL> def local_subtensor_of_alloc ( node ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( node . op , Subtensor ) : <EOL> return False <EOL> u = node . inputs [ <NUM_LIT:0> ] <EOL> if u . owner is None : <EOL> return False <EOL> if not isinstance ( u . owner . op , T . Alloc ) : <EOL> return False <EOL> slices = get_idx_list ( node . inputs , node . op . idx_list ) <EOL> val = u . owner . inputs [ <NUM_LIT:0> ] <EOL> dims = u . owner . inputs [ <NUM_LIT:1> : ] <EOL> assert len ( slices ) <= len ( dims ) <EOL> n_added_dims = u . ndim - val . ndim <EOL> nw_dims = [ ] <EOL> val_slices = [ ] <EOL> for i , ( sl , dim ) in enumerate ( zip ( slices , dims ) ) : <EOL> if i >= n_added_dims : <EOL> if ( val . type . ndim > ( i - n_added_dims ) and <EOL> val . type . broadcastable [ i - n_added_dims ] ) : <EOL> val_slices . append ( slice ( None ) ) <EOL> else : <EOL> val_slices . append ( sl ) <EOL> csl , _ = get_canonical_form_slice ( sl , dim ) <EOL> if type ( csl ) is not slice : <EOL> pass <EOL> else : <EOL> nw_dim = csl . stop - csl . start <EOL> if csl . step != <NUM_LIT:1> : <EOL> nw_dim = T . ceil_intdiv ( nw_dim , csl . step ) <EOL> nw_dims += [ nw_dim ] <EOL> nw_val = val [ tuple ( val_slices ) ] <EOL> nw_dims += dims [ len ( slices ) : ] <EOL> if nw_val . ndim > len ( nw_dims ) : <EOL> return False <EOL> rval = T . alloc ( nw_val , * nw_dims ) <EOL> if type ( rval ) not in ( list , tuple ) : <EOL> rval = [ rval ] <EOL> if rval [ <NUM_LIT:0> ] . type != node . outputs [ <NUM_LIT:0> ] . type : <EOL> rval [ <NUM_LIT:0> ] = theano . tensor . unbroadcast ( <EOL> rval [ <NUM_LIT:0> ] , <EOL> * [ i for i , ( b1 , b2 ) in enumerate ( zip ( rval [ <NUM_LIT:0> ] . broadcastable , <EOL> node . outputs [ <NUM_LIT:0> ] . broadcastable ) ) <EOL> if b1 and not b2 ] ) <EOL> return rval <EOL> @ register_canonicalize <EOL> @ register_stabilize <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( [ Subtensor ] ) <EOL> def local_subtensor_of_dot ( node ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( node . op , Subtensor ) : <EOL> return <EOL> if ( not node . inputs [ <NUM_LIT:0> ] . owner or <EOL> not isinstance ( node . inputs [ <NUM_LIT:0> ] . owner . op , T . Dot ) ) : <EOL> return <EOL> if len ( node . inputs [ <NUM_LIT:0> ] . clients ) > <NUM_LIT:1> : <EOL> return <EOL> a = node . inputs [ <NUM_LIT:0> ] . owner . inputs [ <NUM_LIT:0> ] <EOL> b = node . inputs [ <NUM_LIT:0> ] . owner . inputs [ <NUM_LIT:1> ] <EOL> idx_list = get_idx_list ( node . inputs , node . op . idx_list ) <EOL> num_a_indices = min ( a . ndim - <NUM_LIT:1> , len ( idx_list ) ) <EOL> a_indices = idx_list [ : num_a_indices ] <EOL> b_indices = idx_list [ num_a_indices : ] <EOL> if b . ndim > <NUM_LIT:1> and len ( b_indices ) >= b . ndim - <NUM_LIT:1> : <EOL> b_indices = ( b_indices [ : b . ndim - <NUM_LIT:2> ] + <EOL> ( slice ( None , None , None ) , ) + b_indices [ b . ndim - <NUM_LIT:2> : ] ) <EOL> a_sub = a . __getitem__ ( tuple ( a_indices ) ) <EOL> b_sub = b . __getitem__ ( tuple ( b_indices ) ) if b_indices else b <EOL> copy_stack_trace ( node . outputs [ <NUM_LIT:0> ] , [ a_sub , b_sub ] ) <EOL> r = T . dot ( a_sub , b_sub ) <EOL> copy_stack_trace ( [ node . outputs [ <NUM_LIT:0> ] , node . inputs [ <NUM_LIT:0> ] ] , r ) <EOL> return [ r ] <EOL> @ register_canonicalize <EOL> @ gof . local_optimizer ( [ T . add ] ) <EOL> def local_IncSubtensor_serialize ( node ) : <EOL> """<STR_LIT>""" <EOL> def movable ( i ) : <EOL> return ( i . owner and <EOL> isinstance ( i . owner . op , ( IncSubtensor , <EOL> AdvancedIncSubtensor1 , <EOL> AdvancedIncSubtensor , ) ) and <EOL> i . type == o_type and <EOL> len ( i . clients ) == <NUM_LIT:1> and <EOL> not i . owner . op . set_instead_of_inc ) <EOL> if node . op == T . add : <EOL> o_type = node . outputs [ <NUM_LIT:0> ] . type <EOL> movable_inputs = [ i for i in node . inputs if movable ( i ) ] <EOL> if movable_inputs : <EOL> new_inputs = ( [ i for i in node . inputs if not movable ( i ) ] + <EOL> [ mi . owner . inputs [ <NUM_LIT:0> ] for mi in movable_inputs ] ) <EOL> new_add = T . add ( * new_inputs ) <EOL> copy_stack_trace ( node . outputs [ <NUM_LIT:0> ] , new_add ) <EOL> tip = new_add <EOL> for mi in movable_inputs : <EOL> assert tip . type == o_type <EOL> assert tip . type == mi . owner . inputs [ <NUM_LIT:0> ] . type <EOL> tip = mi . owner . op ( tip , * mi . owner . inputs [ <NUM_LIT:1> : ] ) <EOL> copy_stack_trace ( node . outputs + mi . owner . outputs , tip ) <EOL> return [ tip ] <EOL> compile . optdb . register ( '<STR_LIT>' , <EOL> in2out ( local_IncSubtensor_serialize ) , <EOL> <NUM_LIT> , '<STR_LIT>' ) <EOL> @ gof . local_optimizer ( [ IncSubtensor ] , inplace = True ) <EOL> def local_inplace_setsubtensor ( node ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( node . op , IncSubtensor ) and not node . op . inplace : <EOL> dta = node . op . destroyhandler_tolerate_aliased <EOL> new_op = node . op . __class__ ( <EOL> node . op . idx_list , inplace = True , <EOL> set_instead_of_inc = node . op . set_instead_of_inc , <EOL> destroyhandler_tolerate_aliased = dta ) <EOL> new_node = new_op ( * node . inputs ) <EOL> val = getattr ( node . outputs [ <NUM_LIT:0> ] . tag , '<STR_LIT>' , True ) <EOL> new_node . tag . nan_guard_mode_check = val <EOL> copy_stack_trace ( node . outputs , new_node ) <EOL> return [ new_node ] <EOL> return False <EOL> compile . optdb . register ( '<STR_LIT>' , <EOL> TopoOptimizer ( <EOL> local_inplace_setsubtensor , <EOL> failure_callback = TopoOptimizer . warn_inplace ) , <EOL> <NUM_LIT> , '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ gof . local_optimizer ( [ AdvancedIncSubtensor1 ] , inplace = True ) <EOL> def local_inplace_incsubtensor1 ( node ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( node . op , AdvancedIncSubtensor1 ) and not node . op . inplace : <EOL> new_op = node . op . clone_inplace ( ) <EOL> new_node = new_op ( * node . inputs ) <EOL> copy_stack_trace ( node . outputs , new_node ) <EOL> return [ new_node ] <EOL> return False <EOL> compile . optdb . register ( '<STR_LIT>' , <EOL> TopoOptimizer ( <EOL> local_inplace_incsubtensor1 , <EOL> failure_callback = TopoOptimizer . warn_inplace ) , <EOL> <NUM_LIT> , '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ register_canonicalize ( "<STR_LIT>" ) <EOL> @ register_stabilize ( "<STR_LIT>" ) <EOL> @ gof . local_optimizer ( [ IncSubtensor , <EOL> AdvancedIncSubtensor , <EOL> AdvancedIncSubtensor1 ] ) <EOL> def local_incsubtensor_of_zeros ( node ) : <EOL> """<STR_LIT>""" <EOL> if ( isinstance ( node . op , ( IncSubtensor , <EOL> AdvancedIncSubtensor , <EOL> AdvancedIncSubtensor1 ) ) and <EOL> not node . op . set_instead_of_inc ) : <EOL> x = node . inputs [ <NUM_LIT:0> ] <EOL> y = node . inputs [ <NUM_LIT:1> ] <EOL> replace = False <EOL> try : <EOL> if get_scalar_constant_value ( y ) == <NUM_LIT:0> : <EOL> replace = True <EOL> except NotScalarConstantError : <EOL> pass <EOL> if replace : <EOL> return [ x ] <EOL> else : <EOL> return False <EOL> @ register_canonicalize ( '<STR_LIT>' ) <EOL> @ register_stabilize ( '<STR_LIT>' ) <EOL> @ gof . local_optimizer ( [ IncSubtensor ] ) <EOL> def local_setsubtensor_of_constants ( node ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( node . op , IncSubtensor ) and node . op . set_instead_of_inc : <EOL> x = node . inputs [ <NUM_LIT:0> ] <EOL> y = node . inputs [ <NUM_LIT:1> ] <EOL> replace_x = None <EOL> replace_y = None <EOL> try : <EOL> replace_x = get_scalar_constant_value ( x ) <EOL> except NotScalarConstantError : <EOL> pass <EOL> try : <EOL> replace_y = get_scalar_constant_value ( y ) <EOL> except NotScalarConstantError : <EOL> pass <EOL> if ( replace_x is not None and <EOL> replace_y is not None and <EOL> replace_x == replace_y ) : <EOL> return [ x ] <EOL> else : <EOL> return False <EOL> @ register_canonicalize <EOL> @ register_stabilize <EOL> @ gof . local_optimizer ( [ AdvancedSubtensor1 ] ) <EOL> def local_adv_sub1_adv_inc_sub1 ( node ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( node . op , AdvancedSubtensor1 ) : <EOL> return <EOL> inp = node . inputs [ <NUM_LIT:0> ] <EOL> if ( not inp . owner or <EOL> not isinstance ( inp . owner . op , AdvancedIncSubtensor1 ) ) : <EOL> return <EOL> idx = node . inputs [ <NUM_LIT:1> ] <EOL> idx2 = inp . owner . inputs [ <NUM_LIT:2> ] <EOL> x = inp . owner . inputs [ <NUM_LIT:0> ] <EOL> y = inp . owner . inputs [ <NUM_LIT:1> ] <EOL> if idx is not idx2 : <EOL> return <EOL> if ( not inp . owner . op . set_instead_of_inc and <EOL> T . extract_constant ( x ) != <NUM_LIT:0> ) : <EOL> return <EOL> cond = [ T . all ( T . and_ ( T . lt ( idx , x . shape [ <NUM_LIT:0> ] ) , T . ge ( idx , - x . shape [ <NUM_LIT:0> ] ) ) ) ] <EOL> if not node . fgraph . shape_feature . same_shape ( idx , y , <NUM_LIT:0> , <NUM_LIT:0> ) : <EOL> cond . append ( T . eq ( idx . shape [ <NUM_LIT:0> ] , y . shape [ <NUM_LIT:0> ] ) ) <EOL> y = Assert ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ( y , * cond ) <EOL> if y . dtype == node . outputs [ <NUM_LIT:0> ] . dtype : <EOL> return [ y ] <EOL> r = T . cast ( y , node . outputs [ <NUM_LIT:0> ] . dtype ) <EOL> copy_stack_trace ( y , r ) <EOL> return [ r ] <EOL> @ register_specialize <EOL> @ register_stabilize <EOL> @ register_canonicalize <EOL> @ gof . local_optimizer ( [ IncSubtensor , <EOL> AdvancedIncSubtensor , <EOL> AdvancedIncSubtensor1 ] ) <EOL> def local_useless_inc_subtensor_alloc ( node ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( node . op , ( IncSubtensor , <EOL> AdvancedIncSubtensor , <EOL> AdvancedIncSubtensor1 ) ) : <EOL> x = node . inputs [ <NUM_LIT:0> ] <EOL> y = node . inputs [ <NUM_LIT:1> ] <EOL> i = node . inputs [ <NUM_LIT:2> : ] <EOL> if y . owner is not None and isinstance ( y . owner . op , T . Alloc ) : <EOL> z = y . owner . inputs [ <NUM_LIT:0> ] <EOL> try : <EOL> shape_feature = node . fgraph . shape_feature <EOL> except AttributeError : <EOL> return False <EOL> shape_of = shape_feature . shape_of <EOL> same_shape = shape_feature . same_shape <EOL> if isinstance ( node . op , IncSubtensor ) : <EOL> xi = Subtensor ( node . op . idx_list ) ( x , * i ) <EOL> elif isinstance ( node . op , AdvancedIncSubtensor ) : <EOL> xi = advanced_subtensor ( x , * i ) <EOL> elif isinstance ( node . op , AdvancedIncSubtensor1 ) : <EOL> xi = advanced_subtensor1 ( x , * i ) <EOL> else : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> reason = '<STR_LIT>' <EOL> if xi not in shape_of : <EOL> shape_feature . on_import ( node . fgraph , xi . owner , <EOL> '<STR_LIT>' % reason ) <EOL> if xi . ndim > y . ndim : <EOL> y = T . shape_padleft ( y , xi . ndim - y . ndim ) <EOL> if y not in shape_of : <EOL> shape_feature . on_import ( node . fgraph , y . owner , <EOL> '<STR_LIT>' % reason ) <EOL> z_broad = ( ( True , ) * ( xi . ndim - z . ndim ) + z . broadcastable ) <EOL> cond = [ <EOL> T . or_ ( T . eq ( y . shape [ k ] , <NUM_LIT:1> ) , T . eq ( y . shape [ k ] , xi . shape [ k ] ) ) <EOL> for k in xrange ( xi . ndim ) <EOL> if ( z_broad [ k ] and <EOL> not same_shape ( xi , y , dim_x = k , dim_y = k ) and <EOL> shape_of [ y ] [ k ] != <NUM_LIT:1> ) ] <EOL> if len ( cond ) > <NUM_LIT:0> : <EOL> msg = '<STR_LIT>' <EOL> z = Assert ( msg ) ( z , * cond ) <EOL> r = node . op ( x , z , * i ) <EOL> copy_stack_trace ( node . outputs , r ) <EOL> return [ r ] <EOL> @ register_canonicalize <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( [ T . Rebroadcast ] ) <EOL> def local_useless_rebroadcast ( node ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( node . op , T . Rebroadcast ) : <EOL> x = node . inputs [ <NUM_LIT:0> ] <EOL> if numpy . all ( x . broadcastable == node . outputs [ <NUM_LIT:0> ] . broadcastable ) : <EOL> return [ x ] <EOL> else : <EOL> new_axis = { } <EOL> for dim , bc in list ( node . op . axis . items ( ) ) : <EOL> if x . broadcastable [ dim ] != bc : <EOL> new_axis [ dim ] = bc <EOL> if new_axis == node . op . axis : <EOL> return <EOL> else : <EOL> r = T . Rebroadcast ( * list ( new_axis . items ( ) ) ) ( x ) <EOL> copy_stack_trace ( node . outputs , r ) <EOL> return [ r ] <EOL> @ register_canonicalize <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( [ T . Rebroadcast ] ) <EOL> def local_rebroadcast_lift ( node ) : <EOL> """<STR_LIT>""" <EOL> op = node . op <EOL> if not isinstance ( op , T . Rebroadcast ) : <EOL> return False <EOL> input = node . inputs [ <NUM_LIT:0> ] <EOL> inode = input . owner <EOL> if inode and isinstance ( inode . op , Elemwise ) and len ( inode . inputs ) == <NUM_LIT:1> : <EOL> if hasattr ( input , '<STR_LIT>' ) and len ( input . clients ) == <NUM_LIT:1> : <EOL> rebroadcasted = T . Rebroadcast ( * list ( op . axis . items ( ) ) ) ( <EOL> inode . inputs [ <NUM_LIT:0> ] ) <EOL> copy_stack_trace ( node . outputs , rebroadcasted ) <EOL> rval = inode . op . make_node ( rebroadcasted ) . outputs <EOL> copy_stack_trace ( node . outputs + node . inputs , rval ) <EOL> return rval <EOL> if inode and isinstance ( inode . op , T . Rebroadcast ) : <EOL> axis = inode . op . axis . copy ( ) <EOL> axis . update ( op . axis ) <EOL> iinput = inode . inputs [ <NUM_LIT:0> ] <EOL> rval = [ T . Rebroadcast ( * list ( axis . items ( ) ) ) ( iinput ) ] <EOL> copy_stack_trace ( node . outputs + node . inputs , rval ) <EOL> return rval <EOL> def apply_rebroadcast_opt ( rval ) : <EOL> """<STR_LIT>""" <EOL> changed = True <EOL> while changed and rval . owner : <EOL> changed = False <EOL> rval2 = theano . tensor . opt . local_useless_rebroadcast . transform ( <EOL> rval . owner ) <EOL> if rval2 : <EOL> assert len ( rval2 ) == <NUM_LIT:1> <EOL> rval = rval2 [ <NUM_LIT:0> ] <EOL> changed = True <EOL> if rval . owner : <EOL> rval2 = theano . tensor . opt . local_rebroadcast_lift . transform ( <EOL> rval . owner ) <EOL> if rval2 : <EOL> assert len ( rval2 ) == <NUM_LIT:1> <EOL> rval = rval2 [ <NUM_LIT:0> ] <EOL> changed = True <EOL> return rval <EOL> @ register_specialize <EOL> @ register_canonicalize <EOL> @ gof . local_optimizer ( [ T . Join ] ) <EOL> def local_join_1 ( node ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( node . op , T . Join ) : <EOL> return <EOL> tensors = node . inputs [ <NUM_LIT:1> : ] <EOL> if len ( tensors ) == <NUM_LIT:1> : <EOL> return [ tensors [ <NUM_LIT:0> ] ] <EOL> @ register_specialize <EOL> @ register_canonicalize <EOL> @ gof . local_optimizer ( [ T . Join ] ) <EOL> def local_join_empty ( node ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( node . op , T . Join ) : <EOL> return <EOL> new_inputs = [ ] <EOL> try : <EOL> join_idx = get_scalar_constant_value ( node . inputs [ <NUM_LIT:0> ] ) <EOL> except NotScalarConstantError : <EOL> return <EOL> for idx in xrange ( <NUM_LIT:1> , len ( node . inputs ) ) : <EOL> inp = node . inputs [ idx ] <EOL> if isinstance ( inp , theano . Constant ) and inp . data . shape [ join_idx ] == <NUM_LIT:0> : <EOL> continue <EOL> new_inputs . append ( inp ) <EOL> if len ( new_inputs ) < len ( node . inputs ) - <NUM_LIT:1> : <EOL> if len ( new_inputs ) == <NUM_LIT:0> : <EOL> return <EOL> ret = T . join ( node . inputs [ <NUM_LIT:0> ] , * new_inputs ) <EOL> o = node . outputs [ <NUM_LIT:0> ] <EOL> if ret . dtype != o . dtype : <EOL> return <EOL> copy_stack_trace ( node . outputs , ret ) <EOL> if ret . type != o . type : <EOL> assert ret . dtype == o . dtype <EOL> assert ret . ndim == o . ndim <EOL> ret = T . patternbroadcast ( ret , node . outputs [ <NUM_LIT:0> ] . broadcastable ) <EOL> copy_stack_trace ( node . outputs , ret ) <EOL> return [ ret ] <EOL> @ register_specialize <EOL> @ register_canonicalize <EOL> @ gof . local_optimizer ( [ T . Join ] ) <EOL> def local_join_make_vector ( node ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( node . op , T . Join ) or node . outputs [ <NUM_LIT:0> ] . ndim != <NUM_LIT:1> : <EOL> return <EOL> new_inputs = [ node . inputs [ <NUM_LIT:1> ] ] <EOL> for idx in xrange ( <NUM_LIT:2> , len ( node . inputs ) ) : <EOL> inp = node . inputs [ idx ] <EOL> if ( inp . owner and <EOL> isinstance ( inp . owner . op , MakeVector ) and <EOL> new_inputs [ - <NUM_LIT:1> ] . owner and <EOL> isinstance ( new_inputs [ - <NUM_LIT:1> ] . owner . op , MakeVector ) and <EOL> inp . owner . op == new_inputs [ - <NUM_LIT:1> ] . owner . op ) : <EOL> inps = new_inputs [ - <NUM_LIT:1> ] . owner . inputs + inp . owner . inputs <EOL> new_inputs [ - <NUM_LIT:1> ] = inp . owner . op ( * inps ) <EOL> copy_stack_trace ( node . outputs , new_inputs [ - <NUM_LIT:1> ] ) <EOL> else : <EOL> new_inputs . append ( inp ) <EOL> if len ( new_inputs ) < len ( node . inputs ) - <NUM_LIT:1> : <EOL> ret = T . join ( node . inputs [ <NUM_LIT:0> ] , * new_inputs ) <EOL> copy_stack_trace ( node . outputs , ret ) <EOL> return [ ret ] <EOL> @ register_canonicalize ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( [ T . Elemwise ] ) <EOL> def local_useless_switch ( node ) : <EOL> """<STR_LIT>""" <EOL> if ( isinstance ( node . op , T . Elemwise ) and <EOL> isinstance ( node . op . scalar_op , scalar . basic . Switch ) ) : <EOL> cond = T . extract_constant ( node . inputs [ <NUM_LIT:0> ] , elemwise = False ) <EOL> if type ( cond ) is numpy . ndarray and cond . ndim == <NUM_LIT:0> : <EOL> if cond == <NUM_LIT:0> : <EOL> correct_out = node . inputs [ <NUM_LIT:2> ] <EOL> else : <EOL> correct_out = node . inputs [ <NUM_LIT:1> ] <EOL> if correct_out . ndim != node . outputs [ <NUM_LIT:0> ] . ndim : <EOL> return False <EOL> if correct_out . dtype != node . outputs [ <NUM_LIT:0> ] . dtype : <EOL> out = T . cast ( correct_out , node . outputs [ <NUM_LIT:0> ] . dtype ) <EOL> else : <EOL> out = correct_out <EOL> if out . type . broadcastable != node . outputs [ <NUM_LIT:0> ] . type . broadcastable : <EOL> out = T . alloc ( out , * [ node . outputs [ <NUM_LIT:0> ] . shape [ i ] for i <EOL> in xrange ( out . ndim ) ] ) <EOL> else : <EOL> out = out <EOL> copy_stack_trace ( node . outputs + correct_out , out ) <EOL> return [ out ] <EOL> if node . inputs [ <NUM_LIT:1> ] is node . inputs [ <NUM_LIT:2> ] : <EOL> if cond . type == node . inputs [ <NUM_LIT:1> ] . type : <EOL> return [ node . inputs [ <NUM_LIT:1> ] ] <EOL> ret = T . fill ( cond , node . inputs [ <NUM_LIT:1> ] ) <EOL> copy_stack_trace ( node . outputs + node . inputs [ <NUM_LIT:1> ] , ret ) <EOL> return [ ret ] <EOL> left = node . inputs [ <NUM_LIT:1> ] <EOL> right = node . inputs [ <NUM_LIT:2> ] <EOL> cond_var = node . inputs [ <NUM_LIT:0> ] <EOL> if cond_var . owner and isinstance ( cond_var . owner . op , T . Elemwise ) and isinstance ( cond_var . owner . op . scalar_op , scalar . LE ) and cond_var . owner . inputs [ <NUM_LIT:0> ] . owner and isinstance ( cond_var . owner . inputs [ <NUM_LIT:0> ] . owner . op , Shape_i ) and T . extract_constant ( cond_var . owner . inputs [ <NUM_LIT:1> ] ) == <NUM_LIT:0> and T . extract_constant ( left ) == <NUM_LIT:0> and right is cond_var . owner . inputs [ <NUM_LIT:0> ] : <EOL> assert right . type == node . outputs [ <NUM_LIT:0> ] . type <EOL> return [ right ] <EOL> return False <EOL> return False <EOL> @ register_specialize <EOL> @ register_canonicalize <EOL> @ gof . local_optimizer ( [ T . mul ] ) <EOL> def local_mul_switch_sink ( node ) : <EOL> """<STR_LIT>""" <EOL> if node . op != T . mul : <EOL> return False <EOL> for idx , i in enumerate ( node . inputs ) : <EOL> if i . owner and i . owner . op == T . switch : <EOL> switch = i . owner <EOL> try : <EOL> if ( get_scalar_constant_value ( <EOL> switch . inputs [ <NUM_LIT:1> ] , only_process_constants = True ) == <NUM_LIT:0.> ) : <EOL> listmul = node . inputs [ : idx ] + node . inputs [ idx + <NUM_LIT:1> : ] <EOL> fmul = T . mul ( * ( listmul + [ switch . inputs [ <NUM_LIT:2> ] ] ) ) <EOL> copy_stack_trace ( node . outputs , fmul ) <EOL> fct = [ T . switch ( switch . inputs [ <NUM_LIT:0> ] , <NUM_LIT:0> , <EOL> fmul ) ] <EOL> fct [ <NUM_LIT:0> ] . tag . values_eq_approx = values_eq_approx_remove_nan <EOL> copy_stack_trace ( node . outputs + switch . outputs , fct ) <EOL> return fct <EOL> except NotScalarConstantError : <EOL> pass <EOL> try : <EOL> if ( get_scalar_constant_value ( <EOL> switch . inputs [ <NUM_LIT:2> ] , only_process_constants = True ) == <NUM_LIT:0.> ) : <EOL> listmul = node . inputs [ : idx ] + node . inputs [ idx + <NUM_LIT:1> : ] <EOL> fmul = T . mul ( * ( listmul + [ switch . inputs [ <NUM_LIT:1> ] ] ) ) <EOL> copy_stack_trace ( node . outputs , fmul ) <EOL> fct = [ T . switch ( switch . inputs [ <NUM_LIT:0> ] , <EOL> fmul , <NUM_LIT:0> ) ] <EOL> fct [ <NUM_LIT:0> ] . tag . values_eq_approx = values_eq_approx_remove_nan <EOL> copy_stack_trace ( node . outputs + switch . outputs , fct ) <EOL> return fct <EOL> except NotScalarConstantError : <EOL> pass <EOL> return False <EOL> @ register_canonicalize <EOL> @ gof . local_optimizer ( [ T . true_div , T . int_div ] ) <EOL> def local_div_switch_sink ( node ) : <EOL> """<STR_LIT>""" <EOL> if ( node . op != T . true_div and node . op != T . int_div ) : <EOL> return False <EOL> op = node . op <EOL> if node . inputs [ <NUM_LIT:0> ] . owner and node . inputs [ <NUM_LIT:0> ] . owner . op == T . switch : <EOL> switch = node . inputs [ <NUM_LIT:0> ] . owner <EOL> try : <EOL> if get_scalar_constant_value ( switch . inputs [ <NUM_LIT:1> ] ) == <NUM_LIT:0.> : <EOL> fdiv = op ( switch . inputs [ <NUM_LIT:2> ] , node . inputs [ <NUM_LIT:1> ] ) <EOL> copy_stack_trace ( node . outputs , fdiv ) <EOL> fct = [ T . switch ( switch . inputs [ <NUM_LIT:0> ] , <NUM_LIT:0> , <EOL> fdiv ) ] <EOL> fct [ <NUM_LIT:0> ] . tag . values_eq_approx = values_eq_approx_remove_nan <EOL> copy_stack_trace ( node . outputs + switch . outputs , fct ) <EOL> return fct <EOL> except NotScalarConstantError : <EOL> pass <EOL> try : <EOL> if get_scalar_constant_value ( switch . inputs [ <NUM_LIT:2> ] ) == <NUM_LIT:0.> : <EOL> fdiv = op ( switch . inputs [ <NUM_LIT:1> ] , node . inputs [ <NUM_LIT:1> ] ) <EOL> copy_stack_trace ( node . outputs , fdiv ) <EOL> fct = [ T . switch ( switch . inputs [ <NUM_LIT:0> ] , <EOL> fdiv , <NUM_LIT:0> ) ] <EOL> fct [ <NUM_LIT:0> ] . tag . values_eq_approx = values_eq_approx_remove_nan <EOL> copy_stack_trace ( node . outputs + switch . outputs , fct ) <EOL> return fct <EOL> except NotScalarConstantError : <EOL> pass <EOL> return False <EOL> @ register_canonicalize <EOL> @ register_stabilize <EOL> @ gof . local_optimizer ( [ T . Tile ] ) <EOL> def local_useless_tile ( node ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( node . op , T . Tile ) : <EOL> try : <EOL> a = T . get_scalar_constant_value ( node . inputs [ <NUM_LIT:1> ] ) <EOL> if a == <NUM_LIT:1> : <EOL> try : <EOL> l = T . get_vector_length ( node . inputs [ <NUM_LIT:1> ] ) <EOL> if l == node . inputs [ <NUM_LIT:0> ] . ndim : <EOL> return [ node . inputs [ <NUM_LIT:0> ] ] <EOL> elif l < node . inputs [ <NUM_LIT:0> ] . ndim : <EOL> return <EOL> return [ node . inputs [ <NUM_LIT:0> ] ] <EOL> else : <EOL> return <EOL> x_nd = node . inputs [ <NUM_LIT:0> ] . ndim <EOL> broad = [ '<STR_LIT:x>' ] * ( l - x_nd ) + xrange ( x_nd ) <EOL> ret = node . inputs [ <NUM_LIT:0> ] . dimshuffle ( broad ) <EOL> copy_stack_trace ( node . outputs + node . inputs [ <NUM_LIT:0> ] , ret ) <EOL> return [ ret ] <EOL> except ValueError : <EOL> return <EOL> except NotScalarConstantError : <EOL> return <EOL> @ register_canonicalize <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( [ T . Split ] ) <EOL> def local_useless_split ( node ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( node . op , T . Split ) : <EOL> if node . op . len_splits == <NUM_LIT:1> : <EOL> x , axis , splits = node . inputs <EOL> out = assert_op ( x , T . eq ( splits . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) ) <EOL> out = assert_op ( out , T . eq ( x . shape [ axis ] , splits [ <NUM_LIT:0> ] ) ) <EOL> copy_stack_trace ( node . outputs , out ) <EOL> return [ out ] <EOL> @ register_canonicalize <EOL> @ register_stabilize <EOL> @ gof . local_optimizer ( [ T . Flatten ] ) <EOL> def local_flatten_lift ( node ) : <EOL> """<STR_LIT>""" <EOL> if ( isinstance ( node . op , T . Flatten ) and <EOL> node . inputs [ <NUM_LIT:0> ] . owner and <EOL> isinstance ( node . inputs [ <NUM_LIT:0> ] . owner . op , T . Elemwise ) and <EOL> len ( node . inputs [ <NUM_LIT:0> ] . owner . inputs ) == <NUM_LIT:1> ) : <EOL> f = node . op ( node . inputs [ <NUM_LIT:0> ] . owner . inputs [ <NUM_LIT:0> ] ) <EOL> e = node . inputs [ <NUM_LIT:0> ] . owner . op ( f ) <EOL> return [ e ] <EOL> def local_reshape_chain ( op ) : <EOL> @ gof . local_optimizer ( [ op ] ) <EOL> def f ( node ) : <EOL> """<STR_LIT>""" <EOL> if not opt . check_chain ( node , op , op ) : <EOL> return False <EOL> rval = node . op ( node . inputs [ <NUM_LIT:0> ] . owner . inputs [ <NUM_LIT:0> ] , node . inputs [ <NUM_LIT:1> ] ) <EOL> if rval . broadcastable == node . outputs [ <NUM_LIT:0> ] . broadcastable : <EOL> return [ rval ] <EOL> else : <EOL> return False <EOL> return f <EOL> register_canonicalize ( local_reshape_chain ( T . Reshape ) , <EOL> name = '<STR_LIT>' ) <EOL> @ register_canonicalize <EOL> @ register_stabilize <EOL> @ gof . local_optimizer ( [ T . Reshape ] ) <EOL> def local_useless_reshape ( node ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( node . op , T . Reshape ) : <EOL> if ( node . inputs [ <NUM_LIT:0> ] . ndim == <NUM_LIT:1> and node . outputs [ <NUM_LIT:0> ] . ndim == <NUM_LIT:1> and <EOL> node . inputs [ <NUM_LIT:0> ] . broadcastable == <EOL> node . outputs [ <NUM_LIT:0> ] . broadcastable ) : <EOL> return [ node . inputs [ <NUM_LIT:0> ] ] <EOL> @ register_canonicalize <EOL> @ register_stabilize <EOL> @ gof . local_optimizer ( [ T . Reshape ] ) <EOL> def local_reshape_lift ( node ) : <EOL> """<STR_LIT>""" <EOL> if ( isinstance ( node . op , T . Reshape ) and <EOL> node . inputs [ <NUM_LIT:0> ] . owner and <EOL> isinstance ( node . inputs [ <NUM_LIT:0> ] . owner . op , T . Elemwise ) and <EOL> len ( node . inputs [ <NUM_LIT:0> ] . owner . inputs ) == <NUM_LIT:1> ) : <EOL> r = node . op ( node . inputs [ <NUM_LIT:0> ] . owner . inputs [ <NUM_LIT:0> ] , node . inputs [ <NUM_LIT:1> ] ) <EOL> e = node . inputs [ <NUM_LIT:0> ] . owner . op ( r ) <EOL> if e . type != node . outputs [ <NUM_LIT:0> ] . type : <EOL> e = T . patternbroadcast ( e , node . outputs [ <NUM_LIT:0> ] . broadcastable ) <EOL> return [ e ] <EOL> if <NUM_LIT:0> : <EOL> @ register_canonicalize <EOL> @ gof . local_optimizer ( [ T . Reshape ] ) <EOL> def local_scalar_reshape ( node ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( node . op , T . Reshape ) : <EOL> x , shp = node . inputs <EOL> if x . ndim == <NUM_LIT:0> and T . get_vector_length ( shp ) == <NUM_LIT:0> : <EOL> return [ x ] <EOL> if <NUM_LIT:0> : <EOL> @ register_canonicalize <EOL> @ gof . local_optimizer ( [ T . Sum ] ) <EOL> def local_sum_over_empty ( node ) : <EOL> if isinstance ( node . op , T . Sum ) : <EOL> if not hasattr ( node . fgraph , '<STR_LIT>' ) : <EOL> return <EOL> y , = node . outputs <EOL> y_shape = node . fgraph . shape_feature . shape_of [ y ] <EOL> def tmp ( thing ) : <EOL> try : <EOL> return T . get_scalar_constant_value ( thing ) <EOL> except ( TypeError , ValueError ) as e : <EOL> print ( e , thing . owner . inputs [ <NUM_LIT:0> ] ) <EOL> return None <EOL> print ( '<STR_LIT>' , [ tmp ( s ) for s in y_shape ] ) <EOL> @ gof . local_optimizer ( [ T . Elemwise ] ) <EOL> def local_fill_cut ( node ) : <EOL> """<STR_LIT>""" <EOL> if node . op != T . Elemwise : <EOL> return False <EOL> output = node . outputs [ <NUM_LIT:0> ] <EOL> try : <EOL> reference = [ input <EOL> for input in node . inputs <EOL> if input . type == output . type and <EOL> ( not input . owner or input . owner . op != T . fill ) ] [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> return False <EOL> new_inputs = [ ] <EOL> new = False <EOL> for input in node . inputs : <EOL> if input . owner and input . owner . op == T . fill : <EOL> model , filling = input . owner . inputs <EOL> if encompasses_broadcastable ( reference . type . broadcastable , <EOL> filling . type . broadcastable ) : <EOL> new_inputs . append ( filling ) <EOL> new = True <EOL> continue <EOL> new_inputs . append ( input ) <EOL> if not new : <EOL> return False <EOL> rval = node . op ( * new_inputs ) <EOL> if isinstance ( rval , gof . Variable ) : <EOL> return rval . owner . outputs <EOL> else : <EOL> return rval [ <NUM_LIT:0> ] . owner . outputs <EOL> register_canonicalize ( local_fill_cut ) <EOL> register_canonicalize ( gof . OpRemove ( T . tensor_copy ) , name = '<STR_LIT>' ) <EOL> class Canonizer ( gof . LocalOptimizer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , main , inverse , reciprocal , calculate , <EOL> use_reciprocal = True ) : <EOL> self . main = main <EOL> self . inverse = inverse <EOL> self . reciprocal = reciprocal <EOL> self . calculate = calculate <EOL> self . use_reciprocal = use_reciprocal <EOL> self . external_simplifiers = [ ] <EOL> def add_simplifier ( self , simplifier , reason ) : <EOL> self . external_simplifiers . append ( ( reason , simplifier ) ) <EOL> def tracks ( self ) : <EOL> return [ self . main , self . inverse , self . reciprocal ] <EOL> def get_num_denum ( self , input ) : <EOL> """<STR_LIT>""" <EOL> if <NUM_LIT:0> : <EOL> if len ( input . clients ) > <NUM_LIT:1> : <EOL> return [ input ] , [ ] <EOL> if input . owner is None or input . owner . op not in [ <EOL> self . main , self . inverse , self . reciprocal ] : <EOL> if input . owner and isinstance ( input . owner . op , T . DimShuffle ) : <EOL> dsn = input . owner <EOL> dsop = dsn . op <EOL> dsi0 = dsn . inputs [ <NUM_LIT:0> ] <EOL> compatible_order = ( ( '<STR_LIT:x>' , ) * <EOL> ( input . type . ndim - dsi0 . type . ndim ) + <EOL> tuple ( range ( dsi0 . type . ndim ) ) ) <EOL> if dsop . new_order == compatible_order : <EOL> return self . get_num_denum ( input . owner . inputs [ <NUM_LIT:0> ] ) <EOL> else : <EOL> return [ input ] , [ ] <EOL> else : <EOL> return [ input ] , [ ] <EOL> num = [ ] <EOL> denum = [ ] <EOL> parent = input . owner <EOL> pairs = [ self . get_num_denum ( input2 ) for input2 in parent . inputs ] <EOL> if parent . op == self . main : <EOL> num = reduce ( list . __iadd__ , map ( operator . itemgetter ( <NUM_LIT:0> ) , pairs ) ) <EOL> denum = reduce ( list . __iadd__ , map ( operator . itemgetter ( <NUM_LIT:1> ) , pairs ) ) <EOL> elif parent . op == self . inverse : <EOL> num = pairs [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] + pairs [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] <EOL> denum = pairs [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] + pairs [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> elif parent . op == self . reciprocal : <EOL> num = pairs [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> denum = pairs [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> return num , denum <EOL> def merge_num_denum ( self , num , denum ) : <EOL> """<STR_LIT>""" <EOL> ln , ld = len ( num ) , len ( denum ) <EOL> if not ln and not ld : <EOL> return T . as_tensor_variable ( self . calculate ( [ ] , [ ] ) ) <EOL> if not ln : <EOL> if self . use_reciprocal : <EOL> return self . reciprocal ( self . merge_num_denum ( denum , [ ] ) ) <EOL> else : <EOL> ln = [ self . calculate ( [ ] , [ ] , aslist = False ) ] <EOL> if not ld : <EOL> if ln == <NUM_LIT:1> : <EOL> assert isinstance ( num [ <NUM_LIT:0> ] , gof . Variable ) <EOL> return num [ <NUM_LIT:0> ] <EOL> else : <EOL> return self . main ( * num ) <EOL> return self . inverse ( self . merge_num_denum ( num , [ ] ) , <EOL> self . merge_num_denum ( denum , [ ] ) ) <EOL> @ staticmethod <EOL> def get_constant ( v ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( v , Variable ) : <EOL> try : <EOL> return get_scalar_constant_value ( v , only_process_constants = True ) <EOL> except NotScalarConstantError : <EOL> return None <EOL> else : <EOL> return v <EOL> def simplify ( self , num , denum , out_type ) : <EOL> """<STR_LIT>""" <EOL> rval = self . simplify_constants ( * self . simplify_factors ( num , denum ) , <EOL> out_type = out_type ) <EOL> for reason , simplifier in self . external_simplifiers : <EOL> rval = simplifier ( * rval ) <EOL> return rval <EOL> def simplify_factors ( self , num , denum ) : <EOL> """<STR_LIT>""" <EOL> for v in list ( num ) : <EOL> if v in denum : <EOL> num . remove ( v ) <EOL> denum . remove ( v ) <EOL> return num , denum <EOL> def simplify_constants ( self , orig_num , orig_denum , out_type = None ) : <EOL> """<STR_LIT>""" <EOL> num , denum = list ( orig_num ) , list ( orig_denum ) <EOL> numct , denumct = [ ] , [ ] <EOL> for v in orig_num : <EOL> ct = self . get_constant ( v ) <EOL> if ct is not None : <EOL> num . remove ( v ) <EOL> numct . append ( ct ) <EOL> for v in orig_denum : <EOL> ct = self . get_constant ( v ) <EOL> if ct is not None : <EOL> denum . remove ( v ) <EOL> denumct . append ( ct ) <EOL> if self . use_reciprocal or num : <EOL> ct = self . calculate ( numct , denumct , aslist = True , <EOL> out_type = out_type ) <EOL> else : <EOL> ct = [ self . calculate ( numct , denumct , aslist = False , <EOL> out_type = out_type ) ] <EOL> ct = [ T . constant ( c , dtype = out_type . dtype ) for c in ct ] <EOL> if orig_num and len ( numct ) == <NUM_LIT:1> and len ( denumct ) == <NUM_LIT:0> and ct : <EOL> assert len ( ct ) == <NUM_LIT:1> <EOL> first_num_ct = self . get_constant ( orig_num [ <NUM_LIT:0> ] ) <EOL> if first_num_ct is not None and ct [ <NUM_LIT:0> ] . type . values_eq ( ct [ <NUM_LIT:0> ] . data , <EOL> first_num_ct ) : <EOL> return orig_num , orig_denum <EOL> return ct + num , denum <EOL> def transform ( self , node ) : <EOL> op = node . op <EOL> if op not in [ self . main , self . inverse , self . reciprocal ] : <EOL> return False <EOL> assert len ( node . outputs ) == <NUM_LIT:1> <EOL> out = node . outputs [ <NUM_LIT:0> ] <EOL> for c , c_idx in out . clients : <EOL> if c == '<STR_LIT>' : <EOL> continue <EOL> while ( isinstance ( getattr ( c , '<STR_LIT>' , None ) , DimShuffle ) and <EOL> len ( c . outputs [ <NUM_LIT:0> ] . clients ) <= <NUM_LIT:1> ) : <EOL> c = c . outputs [ <NUM_LIT:0> ] . clients [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> if getattr ( c , '<STR_LIT>' , '<STR_LIT>' ) in [ self . main , self . inverse , <EOL> self . reciprocal ] : <EOL> return False <EOL> orig_num , orig_denum = self . get_num_denum ( node . outputs [ <NUM_LIT:0> ] ) <EOL> num , denum = self . simplify ( list ( orig_num ) , list ( orig_denum ) , out . type ) <EOL> def same ( x , y ) : <EOL> return len ( x ) == len ( y ) and all ( N . all ( xe == ye ) for xe , ye in <EOL> zip ( x , y ) ) <EOL> if same ( orig_num , num ) and same ( orig_denum , denum ) : <EOL> return False <EOL> new = self . merge_num_denum ( num , denum ) <EOL> if new . type . dtype != out . type . dtype : <EOL> elem_op = T . Elemwise ( scalar . Identity ( scalar . specific_out ( <EOL> getattr ( scalar , out . type . dtype ) ) ) ) <EOL> new = elem_op ( new ) <EOL> assert ( new . type == out . type ) == ( not ( new . type != out . type ) ) <EOL> if not ( new . type == out . type ) : <EOL> new = _fill_chain ( new , node . inputs ) [ <NUM_LIT:0> ] <EOL> if new . type == out . type : <EOL> new . tag . values_eq_approx = values_eq_approx_remove_inf_nan <EOL> return [ new ] <EOL> else : <EOL> _logger . warning ( '<STR_LIT:U+0020>' . join ( ( '<STR_LIT>' , <EOL> new , '<STR_LIT:U+002C>' , out , '<STR_LIT>' , <EOL> new . type , '<STR_LIT:U+002C>' , out . type ) ) ) <EOL> return False <EOL> def __str__ ( self ) : <EOL> return getattr ( self , '<STR_LIT:name>' , '<STR_LIT>' % ( <EOL> self . main , self . inverse , self . reciprocal ) ) <EOL> def mul_calculate ( num , denum , aslist = False , out_type = None ) : <EOL> if not num and not denum : <EOL> if aslist : <EOL> return [ ] <EOL> else : <EOL> return N . int8 ( <NUM_LIT:1> ) <EOL> if out_type is None : <EOL> out_dtype = scalar . upcast ( * [ v . dtype for v in ( num + denum ) ] ) <EOL> else : <EOL> out_dtype = out_type . dtype <EOL> one = theano . _asarray ( <NUM_LIT:1> , dtype = out_dtype ) <EOL> v = reduce ( N . multiply , num , one ) / reduce ( N . multiply , denum , one ) <EOL> if aslist : <EOL> if N . all ( v == <NUM_LIT:1> ) : <EOL> return [ ] <EOL> else : <EOL> return [ v ] <EOL> return v <EOL> local_mul_canonizer = Canonizer ( T . mul , T . true_div , T . inv , mul_calculate , False ) <EOL> register_canonicalize ( local_mul_canonizer , name = '<STR_LIT>' ) <EOL> @ gof . local_optimizer ( [ T . neg ] ) <EOL> def local_neg_to_mul ( node ) : <EOL> if node . op == T . neg : <EOL> return [ T . mul ( numpy . array ( - <NUM_LIT:1> , dtype = node . inputs [ <NUM_LIT:0> ] . dtype ) , <EOL> node . inputs [ <NUM_LIT:0> ] ) ] <EOL> register_canonicalize ( local_neg_to_mul ) <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( [ T . Sum , T . elemwise . Prod ] ) <EOL> def local_sum_prod_mul_by_scalar ( node ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( node . op , ( T . Sum , T . elemwise . Prod ) ) : <EOL> node_inps , = node . inputs <EOL> if node_inps . owner and node_inps . owner . op == T . mul : <EOL> terms = node_inps . owner . inputs <EOL> scalars = [ t . dimshuffle ( ) for t in terms if <EOL> numpy . all ( t . type . broadcastable ) ] <EOL> if len ( scalars ) == <NUM_LIT:0> : <EOL> return <EOL> non_scalars = [ t for t in terms if not numpy . all ( t . broadcastable ) ] <EOL> if len ( non_scalars ) == <NUM_LIT:0> : <EOL> new_op_input_nb_elements = <NUM_LIT:1> <EOL> new_op_output = <NUM_LIT:1> <EOL> elif len ( non_scalars ) == <NUM_LIT:1> : <EOL> new_op_input_nb_elements = non_scalars [ <NUM_LIT:0> ] . size <EOL> new_op_output = node . op ( non_scalars [ <NUM_LIT:0> ] ) <EOL> else : <EOL> new_op_input = T . mul ( * non_scalars ) <EOL> new_op_input_nb_elements = new_op_input . size <EOL> new_op_output = node . op ( new_op_input ) <EOL> if ( isinstance ( node . op , T . elemwise . Prod ) and <EOL> new_op_input_nb_elements != <NUM_LIT:1> ) : <EOL> scalars = [ s ** new_op_input_nb_elements for s in scalars ] <EOL> mul_inputs = scalars <EOL> if new_op_input_nb_elements != <NUM_LIT:1> : <EOL> mul_inputs . append ( new_op_output ) <EOL> if len ( mul_inputs ) == <NUM_LIT:1> : <EOL> return mul_inputs <EOL> else : <EOL> return [ T . mul ( * mul_inputs ) ] <EOL> if isinstance ( node . op , T . Sum ) and node_inps . owner and node_inps . owner . op == T . neg : <EOL> return [ T . neg ( node . op ( node_inps . owner . inputs [ <NUM_LIT:0> ] ) ) ] <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( [ T . Elemwise ] ) <EOL> def local_elemwise_sub_zeros ( node ) : <EOL> """<STR_LIT>""" <EOL> if ( isinstance ( node . op , T . Elemwise ) and <EOL> node . op . scalar_op . nin == <NUM_LIT:2> and <EOL> node . op . scalar_op == scalar . sub and <EOL> node . inputs [ <NUM_LIT:0> ] == node . inputs [ <NUM_LIT:1> ] ) : <EOL> return [ T . zeros_like ( node . inputs [ <NUM_LIT:0> ] ) ] <EOL> @ register_specialize <EOL> @ register_stabilize <EOL> @ register_canonicalize <EOL> @ gof . local_optimizer ( [ T . Elemwise ] ) <EOL> def local_useless_elemwise_comparison ( node ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( node . op , T . Elemwise ) : <EOL> return <EOL> if node . op . scalar_op . nin != <NUM_LIT:2> : <EOL> return <EOL> if isinstance ( node . op . scalar_op , ( scalar . LT , scalar . GT ) ) and node . inputs [ <NUM_LIT:0> ] is node . inputs [ <NUM_LIT:1> ] : <EOL> return [ T . zeros_like ( node . inputs [ <NUM_LIT:0> ] , dtype = node . outputs [ <NUM_LIT:0> ] . dtype ) ] <EOL> if isinstance ( node . op . scalar_op , ( scalar . LE , scalar . GE ) ) and node . inputs [ <NUM_LIT:0> ] is node . inputs [ <NUM_LIT:1> ] : <EOL> return [ T . ones_like ( node . inputs [ <NUM_LIT:0> ] , dtype = node . outputs [ <NUM_LIT:0> ] . dtype ) ] <EOL> if isinstance ( node . op . scalar_op , ( scalar . Minimum , scalar . Maximum ) ) and node . inputs [ <NUM_LIT:0> ] is node . inputs [ <NUM_LIT:1> ] : <EOL> return [ node . inputs [ <NUM_LIT:0> ] ] <EOL> if isinstance ( node . op . scalar_op , scalar . LT ) and node . inputs [ <NUM_LIT:0> ] . owner and isinstance ( node . inputs [ <NUM_LIT:0> ] . owner . op , Shape_i ) and T . extract_constant ( node . inputs [ <NUM_LIT:1> ] , only_process_constants = True ) == <NUM_LIT:0> : <EOL> return [ T . zeros_like ( node . inputs [ <NUM_LIT:0> ] , dtype = node . outputs [ <NUM_LIT:0> ] . dtype ) ] <EOL> if isinstance ( node . op . scalar_op , scalar . GE ) and node . inputs [ <NUM_LIT:0> ] . owner and isinstance ( node . inputs [ <NUM_LIT:0> ] . owner . op , Shape_i ) and T . extract_constant ( node . inputs [ <NUM_LIT:1> ] , only_process_constants = True ) == <NUM_LIT:0> : <EOL> return [ T . ones_like ( node . inputs [ <NUM_LIT:0> ] , dtype = node . outputs [ <NUM_LIT:0> ] . dtype ) ] <EOL> if isinstance ( node . op . scalar_op , scalar . Maximum ) and node . inputs [ <NUM_LIT:0> ] . owner and isinstance ( node . inputs [ <NUM_LIT:0> ] . owner . op , Shape_i ) and T . extract_constant ( node . inputs [ <NUM_LIT:1> ] , only_process_constants = True ) == <NUM_LIT:0> : <EOL> return [ node . inputs [ <NUM_LIT:0> ] ] <EOL> if isinstance ( node . op . scalar_op , scalar . Maximum ) and T . extract_constant ( node . inputs [ <NUM_LIT:0> ] , only_process_constants = True ) == <NUM_LIT:0> and node . inputs [ <NUM_LIT:1> ] . owner and isinstance ( node . inputs [ <NUM_LIT:1> ] . owner . op , Shape_i ) : <EOL> return [ node . inputs [ <NUM_LIT:1> ] ] <EOL> if isinstance ( node . op . scalar_op , scalar . Minimum ) and node . inputs [ <NUM_LIT:0> ] . owner and isinstance ( node . inputs [ <NUM_LIT:0> ] . owner . op , Shape_i ) and T . extract_constant ( node . inputs [ <NUM_LIT:1> ] , only_process_constants = True ) == <NUM_LIT:0> : <EOL> return [ T . zeros_like ( node . inputs [ <NUM_LIT:0> ] , dtype = node . outputs [ <NUM_LIT:0> ] . dtype ) ] <EOL> if isinstance ( node . op . scalar_op , scalar . Minimum ) and T . extract_constant ( node . inputs [ <NUM_LIT:0> ] , only_process_constants = True ) == <NUM_LIT:0> and node . inputs [ <NUM_LIT:1> ] . owner and isinstance ( node . inputs [ <NUM_LIT:1> ] . owner . op , Shape_i ) : <EOL> return [ T . zeros_like ( node . inputs [ <NUM_LIT:1> ] , dtype = node . outputs [ <NUM_LIT:0> ] . dtype ) ] <EOL> if isinstance ( node . op . scalar_op , scalar . LT ) and node . inputs [ <NUM_LIT:0> ] . owner and isinstance ( node . inputs [ <NUM_LIT:0> ] . owner . op , Elemwise ) and isinstance ( node . inputs [ <NUM_LIT:0> ] . owner . op . scalar_op , scalar . Add ) and all ( [ isinstance ( var . owner and var . owner . op , Shape_i ) <EOL> for var in node . inputs [ <NUM_LIT:0> ] . owner . inputs ] ) and T . extract_constant ( node . inputs [ <NUM_LIT:1> ] , only_process_constants = True ) == <NUM_LIT:0> : <EOL> return [ T . zeros_like ( node . inputs [ <NUM_LIT:0> ] , dtype = node . outputs [ <NUM_LIT:0> ] . dtype ) ] <EOL> if isinstance ( node . op . scalar_op , scalar . GE ) and node . inputs [ <NUM_LIT:0> ] . owner and isinstance ( node . inputs [ <NUM_LIT:0> ] . owner . op , Elemwise ) and isinstance ( node . inputs [ <NUM_LIT:0> ] . owner . op . scalar_op , scalar . Add ) and all ( [ isinstance ( var . owner and var . owner . op , Shape_i ) <EOL> for var in node . inputs [ <NUM_LIT:0> ] . owner . inputs ] ) and T . extract_constant ( node . inputs [ <NUM_LIT:1> ] , only_process_constants = True ) == <NUM_LIT:0> : <EOL> return [ T . ones_like ( node . inputs [ <NUM_LIT:0> ] , dtype = node . outputs [ <NUM_LIT:0> ] . dtype ) ] <EOL> return <EOL> @ register_canonicalize <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( [ T . Sum , T . elemwise . Prod ] ) <EOL> def local_sum_prod_div_dimshuffle ( node ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( node . op , ( T . Sum , T . elemwise . Prod ) ) : <EOL> axis = node . op . axis <EOL> if axis is None : <EOL> axis = list ( range ( node . inputs [ <NUM_LIT:0> ] . ndim ) ) <EOL> node_input = node . inputs [ <NUM_LIT:0> ] <EOL> if node_input . owner and node_input . owner . op == T . true_div : <EOL> numerator , denominator = node_input . owner . inputs <EOL> if ( config . warn . sum_div_dimshuffle_bug and <EOL> isinstance ( node . op , T . Sum ) and <EOL> numerator . owner and <EOL> isinstance ( numerator . owner . op , T . DimShuffle ) ) : <EOL> new_order = numerator . owner . op . new_order <EOL> compatible_dims = True <EOL> for ax in axis : <EOL> if len ( new_order ) <= ax or new_order [ ax ] != '<STR_LIT:x>' : <EOL> compatible_dims = False <EOL> break <EOL> if compatible_dims : <EOL> _logger . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if denominator . owner and isinstance ( denominator . owner . op , <EOL> T . DimShuffle ) : <EOL> dimshuffle_input = denominator . owner . inputs [ <NUM_LIT:0> ] <EOL> dimshuffle_order = denominator . owner . op . new_order <EOL> compatible_dims = [ ] <EOL> incompatible_dims = [ ] <EOL> for ax in axis : <EOL> if ( ax < len ( dimshuffle_order ) and <EOL> dimshuffle_order [ ax ] == '<STR_LIT:x>' ) : <EOL> compatible_dims . append ( ax ) <EOL> else : <EOL> incompatible_dims . append ( ax ) <EOL> reordered_incompatible_dims = [ ] <EOL> for ic_ax in incompatible_dims : <EOL> reordered_incompatible_dims . append ( <EOL> ic_ax - sum ( <EOL> [ <NUM_LIT:1> for c_ax in compatible_dims if c_ax < ic_ax ] ) ) <EOL> if len ( compatible_dims ) > <NUM_LIT:0> : <EOL> optimized_dimshuffle_order = list ( <EOL> ax for i , ax in enumerate ( dimshuffle_order ) <EOL> if ( i not in axis ) or ( ax != '<STR_LIT:x>' ) ) <EOL> while ( len ( optimized_dimshuffle_order ) > <NUM_LIT:0> and <EOL> optimized_dimshuffle_order [ <NUM_LIT:0> ] == '<STR_LIT:x>' ) : <EOL> del optimized_dimshuffle_order [ <NUM_LIT:0> ] <EOL> if all ( i == e for i , e in <EOL> enumerate ( optimized_dimshuffle_order ) ) : <EOL> optimized_dimshuffle = dimshuffle_input <EOL> else : <EOL> optimized_dimshuffle = T . DimShuffle ( <EOL> dimshuffle_input . type . broadcastable , <EOL> optimized_dimshuffle_order ) ( dimshuffle_input ) <EOL> if ( config . warn . sum_div_dimshuffle_bug and <EOL> isinstance ( node . op , T . Sum ) ) : <EOL> _logger . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if isinstance ( node . op , T . Sum ) : <EOL> op_on_compatible_dims = T . sum ( <EOL> numerator , axis = compatible_dims ) <EOL> rval = T . true_div ( <EOL> op_on_compatible_dims , <EOL> optimized_dimshuffle ) <EOL> if len ( reordered_incompatible_dims ) > <NUM_LIT:0> : <EOL> rval = T . sum ( rval , <EOL> axis = reordered_incompatible_dims ) <EOL> elif isinstance ( node . op , T . elemwise . Prod ) : <EOL> op_on_compatible_dims = T . prod ( <EOL> numerator , axis = compatible_dims ) <EOL> dtype = numerator . dtype <EOL> rval = T . true_div ( <EOL> op_on_compatible_dims , <EOL> ( optimized_dimshuffle ** <EOL> T . prod ( [ numerator . shape [ ax ] . astype ( dtype ) <EOL> for ax in compatible_dims ] ) ) ) <EOL> if len ( reordered_incompatible_dims ) > <NUM_LIT:0> : <EOL> rval = T . prod ( rval , <EOL> axis = reordered_incompatible_dims ) <EOL> return [ rval ] <EOL> @ register_canonicalize <EOL> @ gof . local_optimizer ( [ T . Sum , T . elemwise . Prod ] ) <EOL> def local_sum_prod_all_to_none ( node ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( node . op , T . Sum ) or isinstance ( node . op , T . elemwise . Prod ) : <EOL> opt_type = T . Sum if isinstance ( node . op , T . Sum ) else T . elemwise . Prod <EOL> if node . op . axis is None : <EOL> return <EOL> if set ( node . op . axis ) == set ( range ( node . inputs [ <NUM_LIT:0> ] . type . ndim ) ) : <EOL> return [ opt_type ( axis = None , dtype = node . op . dtype ) ( node . inputs [ <NUM_LIT:0> ] ) ] <EOL> @ register_canonicalize <EOL> @ gof . local_optimizer ( [ T . Sum , T . elemwise . Prod ] ) <EOL> def local_op_of_op ( node ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( node . op , T . elemwise . Prod ) or isinstance ( node . op , T . Sum ) : <EOL> opt_type = T . Sum if isinstance ( node . op , T . Sum ) else T . elemwise . Prod <EOL> node_inps , = node . inputs <EOL> out_dtype = node . op . dtype <EOL> if len ( node_inps . clients ) == <NUM_LIT:1> : <EOL> if ( node_inps . owner and <EOL> ( isinstance ( node_inps . owner . op , node . op . __class__ ) ) ) : <EOL> if node_inps . owner . op . axis is None or node . op . axis is None : <EOL> return [ opt_type ( None , dtype = out_dtype ) ( <EOL> node_inps . owner . inputs [ <NUM_LIT:0> ] ) ] <EOL> newaxis = list ( tuple ( node_inps . owner . op . axis ) ) <EOL> for i in node . op . axis : <EOL> new_i = i <EOL> for ii in node_inps . owner . op . axis : <EOL> if new_i >= ii : <EOL> new_i += <NUM_LIT:1> <EOL> assert new_i not in newaxis <EOL> newaxis . append ( new_i ) <EOL> assert len ( newaxis ) == len ( list ( node_inps . owner . op . axis ) + <EOL> list ( node . op . axis ) ) <EOL> alldims = list ( range ( node_inps . owner . inputs [ <NUM_LIT:0> ] . type . ndim ) ) <EOL> alldims = [ d for i , d in enumerate ( alldims ) if i <EOL> in node_inps . owner . op . axis ] <EOL> alldims = [ d for i , d in enumerate ( alldims ) <EOL> if i in node . op . axis ] <EOL> newaxis_old = [ i for i in <EOL> xrange ( node_inps . owner . inputs [ <NUM_LIT:0> ] . type . ndim ) <EOL> if i not in alldims ] <EOL> if ( theano . config . warn . sum_sum_bug and <EOL> newaxis != newaxis_old and <EOL> len ( newaxis ) == len ( newaxis_old ) ) : <EOL> _logger . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> combined = opt_type ( newaxis , dtype = out_dtype ) <EOL> return [ combined ( node_inps . owner . inputs [ <NUM_LIT:0> ] ) ] <EOL> ALL_REDUCE = [ T . elemwise . CAReduce , T . elemwise . All , T . elemwise . Any , <EOL> T . elemwise . Sum , T . elemwise . Prod , <EOL> T . elemwise . ProdWithoutZeros ] <EOL> @ register_canonicalize <EOL> @ register_uncanonicalize <EOL> @ gof . local_optimizer ( ALL_REDUCE ) <EOL> def local_reduce_join ( node ) : <EOL> """<STR_LIT>""" <EOL> if ( isinstance ( node . op , T . CAReduce ) and <EOL> node . inputs [ <NUM_LIT:0> ] . owner and <EOL> isinstance ( node . inputs [ <NUM_LIT:0> ] . owner . op , T . Join ) ) : <EOL> join = node . inputs [ <NUM_LIT:0> ] . owner <EOL> if T . extract_constant ( join . inputs [ <NUM_LIT:0> ] ) != <NUM_LIT:0> : <EOL> return <EOL> if isinstance ( node . op . scalar_op , ( scalar . Maximum , scalar . Minimum ) ) : <EOL> if len ( join . inputs ) != <NUM_LIT:3> : <EOL> return <EOL> elif not isinstance ( node . op . scalar_op , ( scalar . Add , scalar . Mul ) ) : <EOL> return <EOL> elif len ( join . inputs ) <= <NUM_LIT:2> : <EOL> return <EOL> new_inp = [ ] <EOL> for inp in join . inputs [ <NUM_LIT:1> : ] : <EOL> inp = inp . owner <EOL> if not inp : <EOL> return <EOL> if ( not isinstance ( inp . op , DimShuffle ) or <EOL> inp . op . new_order != ( '<STR_LIT:x>' , ) + <EOL> tuple ( range ( inp . inputs [ <NUM_LIT:0> ] . ndim ) ) ) : <EOL> return <EOL> new_inp . append ( inp . inputs [ <NUM_LIT:0> ] ) <EOL> ret = Elemwise ( node . op . scalar_op ) ( * new_inp ) <EOL> if ret . dtype != node . outputs [ <NUM_LIT:0> ] . dtype : <EOL> return <EOL> reduce_axis = node . op . axis <EOL> if reduce_axis is None : <EOL> reduce_axis = tuple ( xrange ( node . inputs [ <NUM_LIT:0> ] . ndim ) ) <EOL> if len ( reduce_axis ) != <NUM_LIT:1> or <NUM_LIT:0> not in reduce_axis : <EOL> if theano . config . warn . reduce_join : <EOL> warnings . warn ( ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> return <EOL> try : <EOL> join_axis = get_scalar_constant_value ( join . inputs [ <NUM_LIT:0> ] ) <EOL> if join_axis != reduce_axis [ <NUM_LIT:0> ] : <EOL> return <EOL> except NotScalarConstantError : <EOL> return <EOL> return [ ret ] <EOL> @ register_canonicalize ( '<STR_LIT>' ) <EOL> @ gof . local_optimizer ( ALL_REDUCE ) <EOL> def local_cut_useless_reduce ( node ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( node . op , T . CAReduce ) : <EOL> summed , = node . inputs <EOL> if summed . type == node . outputs [ <NUM_LIT:0> ] . type : <EOL> return [ summed ] <EOL> @ register_uncanonicalize <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( ALL_REDUCE ) <EOL> def local_reduce_broadcastable ( node ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( node . op , T . CAReduce ) : <EOL> reduced , = node . inputs <EOL> odtype = node . outputs [ <NUM_LIT:0> ] . dtype <EOL> if node . op . axis is None : <EOL> if all ( reduced . broadcastable ) : <EOL> return [ reduced . dimshuffle ( ) . astype ( odtype ) ] <EOL> else : <EOL> axis = list ( node . op . axis ) <EOL> cuttable = [ a for a in axis if reduced . broadcastable [ a ] ] <EOL> if cuttable : <EOL> new_axis = [ ] <EOL> pattern = [ ] <EOL> ii = <NUM_LIT:0> <EOL> for p in xrange ( reduced . ndim ) : <EOL> if p not in cuttable : <EOL> if p in axis : <EOL> new_axis . append ( ii ) <EOL> pattern . append ( p ) <EOL> ii += <NUM_LIT:1> <EOL> new_reduced = reduced . dimshuffle ( * pattern ) <EOL> if new_axis : <EOL> if type ( node . op ) == theano . tensor . elemwise . CAReduce : <EOL> new_op = node . op . __class__ ( node . op . scalar_op , <EOL> axis = new_axis ) <EOL> else : <EOL> new_op = node . op . __class__ ( axis = new_axis ) <EOL> return [ new_op ( new_reduced ) ] <EOL> else : <EOL> return [ new_reduced . astype ( odtype ) ] <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( [ T . Sum , T . elemwise . Prod ] ) <EOL> def local_opt_alloc ( node ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( node . op , T . Sum ) or isinstance ( node . op , T . elemwise . Prod ) : <EOL> node_inps , = node . inputs <EOL> if node_inps . owner and isinstance ( node_inps . owner . op , T . Alloc ) : <EOL> input = node_inps . owner . inputs [ <NUM_LIT:0> ] <EOL> shapes = node_inps . owner . inputs [ <NUM_LIT:1> : ] <EOL> if ( node . op . axis is None or <EOL> node . op . axis == tuple ( range ( input . ndim ) ) ) : <EOL> try : <EOL> val = get_scalar_constant_value ( input ) <EOL> assert val . size == <NUM_LIT:1> <EOL> if isinstance ( node . op , T . Sum ) : <EOL> val = val . reshape ( <NUM_LIT:1> ) [ <NUM_LIT:0> ] * T . mul ( * shapes ) <EOL> else : <EOL> val = val . reshape ( <NUM_LIT:1> ) [ <NUM_LIT:0> ] ** T . mul ( * shapes ) <EOL> return [ T . cast ( val , dtype = node . outputs [ <NUM_LIT:0> ] . dtype ) ] <EOL> except NotScalarConstantError : <EOL> pass <EOL> else : <EOL> try : <EOL> val = get_scalar_constant_value ( input ) <EOL> assert val . size == <NUM_LIT:1> <EOL> val = val . reshape ( <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> to_prod = [ shapes [ i ] for i in xrange ( len ( shapes ) ) <EOL> if i in node . op . axis ] <EOL> if to_prod : <EOL> if isinstance ( node . op , T . Sum ) : <EOL> val *= T . mul ( * to_prod ) <EOL> else : <EOL> val = val ** T . mul ( * to_prod ) <EOL> return [ T . alloc ( T . cast ( val , dtype = node . outputs [ <NUM_LIT:0> ] . dtype ) , <EOL> * [ shapes [ i ] for i in xrange ( len ( shapes ) ) <EOL> if i not in node . op . axis ] ) ] <EOL> except NotScalarConstantError : <EOL> pass <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( [ T . neg ] ) <EOL> def local_neg_neg ( node ) : <EOL> if node . op == T . neg : <EOL> if node . inputs [ <NUM_LIT:0> ] . owner and node . inputs [ <NUM_LIT:0> ] . owner . op == T . neg : <EOL> return [ node . inputs [ <NUM_LIT:0> ] . owner . inputs [ <NUM_LIT:0> ] ] <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( [ T . neg ] ) <EOL> def local_neg_div_neg ( node ) : <EOL> """<STR_LIT>""" <EOL> if node . op == T . neg : <EOL> if node . inputs [ <NUM_LIT:0> ] . owner and node . inputs [ <NUM_LIT:0> ] . owner . op == T . true_div : <EOL> frac = node . inputs [ <NUM_LIT:0> ] <EOL> num , denom = frac . owner . inputs <EOL> if num . owner and num . owner . op == T . neg : <EOL> if len ( frac . clients ) == <NUM_LIT:1> : <EOL> new_num = num . owner . inputs [ <NUM_LIT:0> ] <EOL> return [ T . true_div ( new_num , denom ) ] <EOL> elif numpy . all ( num . broadcastable ) and isinstance ( num , Constant ) : <EOL> if len ( frac . clients ) == <NUM_LIT:1> : <EOL> new_num = - num . data <EOL> return [ T . true_div ( new_num , denom ) ] <EOL> @ gof . local_optimizer ( [ T . mul ] ) <EOL> def local_mul_zero ( node ) : <EOL> """<STR_LIT>""" <EOL> if node . op == T . mul : <EOL> otype = node . outputs [ <NUM_LIT:0> ] . type <EOL> for i in node . inputs : <EOL> try : <EOL> value = get_scalar_constant_value ( i ) <EOL> except NotScalarConstantError : <EOL> continue <EOL> if value == <NUM_LIT:0> : <EOL> return _fill_chain ( theano . _asarray ( <NUM_LIT:0> , dtype = otype . dtype ) , <EOL> node . inputs ) <EOL> register_canonicalize ( local_mul_zero ) <EOL> @ gof . local_optimizer ( [ T . true_div ] ) <EOL> def local_div_to_inv ( node ) : <EOL> if node . op == T . true_div and N . all ( <EOL> local_mul_canonizer . get_constant ( node . inputs [ <NUM_LIT:0> ] ) == <NUM_LIT:1.0> ) : <EOL> out = node . outputs [ <NUM_LIT:0> ] <EOL> new_out = T . inv ( local_mul_canonizer . merge_num_denum ( node . inputs [ <NUM_LIT:1> : ] , <EOL> [ ] ) ) <EOL> if new_out . dtype != out . dtype : <EOL> new_out = T . cast ( new_out , dtype = out . dtype ) <EOL> if new_out . type != out . type : <EOL> new_out = broadcast_like ( new_out , out , node . fgraph ) <EOL> return [ new_out ] <EOL> else : <EOL> return False <EOL> register_specialize ( local_div_to_inv ) <EOL> @ gof . local_optimizer ( [ T . inv ] ) <EOL> def local_inv_canon ( node ) : <EOL> if node . op == T . inv : <EOL> return [ T . pow ( node . inputs [ <NUM_LIT:0> ] , - <NUM_LIT:1.0> ) ] <EOL> else : <EOL> return False <EOL> register_canonicalize ( local_inv_canon ) <EOL> @ gof . local_optimizer ( [ T . pow ] ) <EOL> def local_pow_canonicalize ( node ) : <EOL> if node . op == T . pow : <EOL> cst = local_mul_canonizer . get_constant ( node . inputs [ <NUM_LIT:1> ] ) <EOL> if cst == <NUM_LIT:0> : <EOL> return [ broadcast_like ( <NUM_LIT:1> , node . outputs [ <NUM_LIT:0> ] , node . fgraph ) ] <EOL> if cst == <NUM_LIT:1> : <EOL> return [ broadcast_like ( node . inputs [ <NUM_LIT:0> ] , node . outputs [ <NUM_LIT:0> ] , node . fgraph ) ] <EOL> else : <EOL> return False <EOL> register_canonicalize ( local_pow_canonicalize ) <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( [ T . mul ] ) <EOL> def local_mul_to_sqr ( node ) : <EOL> """<STR_LIT>""" <EOL> if node . op == T . mul : <EOL> if len ( node . inputs ) == <NUM_LIT:2> : <EOL> if node . inputs [ <NUM_LIT:0> ] is node . inputs [ <NUM_LIT:1> ] : <EOL> return [ T . sqr ( node . inputs [ <NUM_LIT:0> ] ) ] <EOL> @ register_canonicalize <EOL> @ gof . local_optimizer ( [ T . int_div ] ) <EOL> def local_intdiv_by_one ( node ) : <EOL> """<STR_LIT>""" <EOL> if node . op in [ T . int_div ] : <EOL> if isinstance ( node . inputs [ <NUM_LIT:1> ] , T . TensorConstant ) and numpy . all ( node . inputs [ <NUM_LIT:1> ] . value == <NUM_LIT:1> ) : <EOL> return [ node . inputs [ <NUM_LIT:0> ] . astype ( node . outputs [ <NUM_LIT:0> ] . dtype ) ] <EOL> @ gof . local_optimizer ( [ T . pow ] ) <EOL> def local_pow_specialize ( node ) : <EOL> if node . op == T . pow : <EOL> odtype = node . outputs [ <NUM_LIT:0> ] . dtype <EOL> xsym = node . inputs [ <NUM_LIT:0> ] <EOL> ysym = node . inputs [ <NUM_LIT:1> ] <EOL> y = local_mul_canonizer . get_constant ( ysym ) <EOL> if ( y is not None ) and encompasses_broadcastable ( xsym . type . broadcastable , <EOL> ysym . type . broadcastable ) : <EOL> rval = None <EOL> if N . all ( y == <NUM_LIT:2> ) : <EOL> rval = [ T . sqr ( xsym ) ] <EOL> if N . all ( y == <NUM_LIT:1> ) : <EOL> rval = [ xsym ] <EOL> if N . all ( y == <NUM_LIT:0> ) : <EOL> rval = [ T . fill ( xsym , numpy . asarray ( <NUM_LIT:1> , dtype = odtype ) ) ] <EOL> if N . all ( y == <NUM_LIT:0.5> ) : <EOL> rval = [ T . sqrt ( xsym ) ] <EOL> if N . all ( y == - <NUM_LIT:0.5> ) : <EOL> rval = [ T . inv ( T . sqrt ( xsym ) ) ] <EOL> if N . all ( y == - <NUM_LIT:1> ) : <EOL> rval = [ T . inv ( xsym ) ] <EOL> if N . all ( y == - <NUM_LIT:2> ) : <EOL> rval = [ T . inv ( T . sqr ( xsym ) ) ] <EOL> if rval : <EOL> rval [ <NUM_LIT:0> ] = T . cast ( rval [ <NUM_LIT:0> ] , odtype ) <EOL> assert rval [ <NUM_LIT:0> ] . type == node . outputs [ <NUM_LIT:0> ] . type , ( <EOL> rval , node . outputs ) <EOL> return rval <EOL> else : <EOL> return False <EOL> register_specialize ( local_pow_specialize ) <EOL> @ register_specialize_device <EOL> @ gof . local_optimizer ( [ T . pow ] ) <EOL> def local_pow_specialize_device ( node ) : <EOL> """<STR_LIT>""" <EOL> if node . op == T . pow : <EOL> odtype = node . outputs [ <NUM_LIT:0> ] . dtype <EOL> xsym = node . inputs [ <NUM_LIT:0> ] <EOL> ysym = node . inputs [ <NUM_LIT:1> ] <EOL> y = local_mul_canonizer . get_constant ( ysym ) <EOL> if isinstance ( y , numpy . ndarray ) : <EOL> assert y . size == <NUM_LIT:1> <EOL> try : <EOL> y = y [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> pass <EOL> if ( y is not None ) and encompasses_broadcastable ( xsym . type . broadcastable , <EOL> ysym . type . broadcastable ) : <EOL> rval = None <EOL> if abs ( y ) == int ( abs ( y ) ) and abs ( y ) <= <NUM_LIT> : <EOL> pow2 = [ xsym ] <EOL> pow2_scal = [ theano . scalar . get_scalar_type ( xsym . dtype ) ( ) ] <EOL> y_to_do = abs ( y ) <EOL> for i in xrange ( int ( numpy . log2 ( y_to_do ) ) ) : <EOL> pow2 . append ( T . sqr ( pow2 [ i ] ) ) <EOL> pow2_scal . append ( theano . scalar . sqr ( pow2_scal [ i ] ) ) <EOL> rval1 = None <EOL> rval1_scal = None <EOL> while y_to_do > <NUM_LIT:0> : <EOL> log_to_do = int ( numpy . log2 ( y_to_do ) ) <EOL> if rval1 : <EOL> rval1 *= pow2 [ log_to_do ] <EOL> rval1_scal *= pow2_scal [ log_to_do ] <EOL> else : <EOL> rval1 = pow2 [ log_to_do ] <EOL> rval1_scal = pow2_scal [ log_to_do ] <EOL> y_to_do -= <NUM_LIT:2> ** log_to_do <EOL> if abs ( y ) > <NUM_LIT:2> : <EOL> rval1 = Elemwise ( <EOL> theano . scalar . Composite ( <EOL> [ pow2_scal [ <NUM_LIT:0> ] ] , [ rval1_scal ] ) ) . make_node ( xsym ) <EOL> if y < <NUM_LIT:0> : <EOL> rval = [ T . inv ( rval1 ) ] <EOL> else : <EOL> rval = [ rval1 ] <EOL> if rval : <EOL> rval [ <NUM_LIT:0> ] = T . cast ( rval [ <NUM_LIT:0> ] , odtype ) <EOL> assert rval [ <NUM_LIT:0> ] . type == node . outputs [ <NUM_LIT:0> ] . type , ( <EOL> rval , node . outputs ) <EOL> return rval <EOL> @ gof . local_optimizer ( [ T . mul ] ) <EOL> def local_mul_specialize ( node ) : <EOL> """<STR_LIT>""" <EOL> if node . op == T . mul : <EOL> neg = False <EOL> new_inputs = [ ] <EOL> nb_neg_node = <NUM_LIT:0> <EOL> nb_cst = <NUM_LIT:0> <EOL> for input in node . inputs : <EOL> while input . owner and input . owner . op == T . neg : <EOL> neg ^= True <EOL> input = input . owner . inputs [ <NUM_LIT:0> ] <EOL> nb_neg_node += <NUM_LIT:1> <EOL> y = local_mul_canonizer . get_constant ( input ) <EOL> if y == <NUM_LIT:1.0> : <EOL> nb_cst += <NUM_LIT:1> <EOL> elif y == - <NUM_LIT:1.0> : <EOL> nb_cst += <NUM_LIT:1> <EOL> neg ^= True <EOL> elif y == <NUM_LIT:0.0> : <EOL> return [ broadcast_like ( <NUM_LIT:0> , node . outputs [ <NUM_LIT:0> ] , node . fgraph ) ] <EOL> else : <EOL> new_inputs . append ( input ) <EOL> if new_inputs != node . inputs : <EOL> if new_inputs : <EOL> if len ( new_inputs ) == <NUM_LIT:1> : <EOL> if neg : <EOL> rval = - new_inputs [ <NUM_LIT:0> ] <EOL> else : <EOL> rval = new_inputs [ <NUM_LIT:0> ] <EOL> else : <EOL> if ( neg and <EOL> nb_neg_node == <NUM_LIT:0> and <EOL> nb_cst == <NUM_LIT:1> ) : <EOL> return <EOL> elif neg : <EOL> m1 = numpy . asarray ( - <NUM_LIT:1> , dtype = node . outputs [ <NUM_LIT:0> ] . dtype ) <EOL> new_inputs = [ m1 ] + new_inputs <EOL> rval = T . mul ( * new_inputs ) <EOL> return [ broadcast_like ( rval , node . outputs [ <NUM_LIT:0> ] , node . fgraph ) ] <EOL> else : <EOL> if neg : <EOL> return [ broadcast_like ( - <NUM_LIT:1> , node . outputs [ <NUM_LIT:0> ] , node . fgraph ) ] <EOL> else : <EOL> return [ broadcast_like ( <NUM_LIT:1> , node . outputs [ <NUM_LIT:0> ] , node . fgraph ) ] <EOL> register_specialize ( local_mul_specialize ) <EOL> @ gof . local_optimizer ( [ T . add ] ) <EOL> def local_add_specialize ( node ) : <EOL> def fill_chain ( v ) : <EOL> out = _fill_chain ( v , node . inputs ) <EOL> return out <EOL> if node . op == T . add : <EOL> new_inputs = [ ] <EOL> for input in node . inputs : <EOL> try : <EOL> y = get_scalar_constant_value ( input ) <EOL> except NotScalarConstantError : <EOL> y = input <EOL> if numpy . all ( y == <NUM_LIT:0.0> ) : <EOL> continue <EOL> new_inputs . append ( input ) <EOL> if len ( new_inputs ) < len ( node . inputs ) : <EOL> dtype = node . outputs [ <NUM_LIT:0> ] . type . dtype <EOL> if len ( new_inputs ) == <NUM_LIT:0> : <EOL> ndim = node . outputs [ <NUM_LIT:0> ] . type . ndim <EOL> cst = T . constant ( numpy . zeros ( ( <NUM_LIT:1> , ) * ndim , dtype = dtype ) ) <EOL> assert cst . type . broadcastable == ( True , ) * ndim <EOL> return fill_chain ( cst ) <EOL> if len ( new_inputs ) == <NUM_LIT:1> : <EOL> ret = fill_chain ( new_inputs [ <NUM_LIT:0> ] ) <EOL> else : <EOL> ret = fill_chain ( T . add ( * new_inputs ) ) <EOL> if ret [ <NUM_LIT:0> ] . dtype != dtype : <EOL> ret = [ T . cast ( ret [ <NUM_LIT:0> ] , dtype ) ] <EOL> return ret <EOL> else : <EOL> return False <EOL> register_specialize ( local_add_specialize ) <EOL> mul_canonizer = in2out ( gof . LocalOptGroup ( local_mul_canonizer , local_fill_cut , <EOL> local_fill_sink ) , <EOL> name = '<STR_LIT>' ) <EOL> def check_for_x_over_absX ( numerators , denominators ) : <EOL> """<STR_LIT>""" <EOL> for den in list ( denominators ) : <EOL> if ( den . owner and den . owner . op == T . abs_ and <EOL> den . owner . inputs [ <NUM_LIT:0> ] in numerators ) : <EOL> if den . owner . inputs [ <NUM_LIT:0> ] . type . dtype . startswith ( '<STR_LIT>' ) : <EOL> pass <EOL> else : <EOL> denominators . remove ( den ) <EOL> numerators . remove ( den . owner . inputs [ <NUM_LIT:0> ] ) <EOL> numerators . append ( T . sgn ( den . owner . inputs [ <NUM_LIT:0> ] ) ) <EOL> return numerators , denominators <EOL> local_mul_canonizer . add_simplifier ( check_for_x_over_absX , '<STR_LIT>' ) <EOL> @ register_canonicalize <EOL> @ gof . local_optimizer ( [ T . abs_ ] ) <EOL> def local_abs_lift ( node ) : <EOL> """<STR_LIT>""" <EOL> if node . op == T . abs_ and node . inputs [ <NUM_LIT:0> ] . owner : <EOL> assert node . nin == <NUM_LIT:1> <EOL> if node . inputs [ <NUM_LIT:0> ] . owner . op == T . mul : <EOL> return [ T . mul ( * [ T . abs_ ( i ) for i in node . inputs [ <NUM_LIT:0> ] . owner . inputs ] ) ] <EOL> if node . inputs [ <NUM_LIT:0> ] . owner . op == T . true_div : <EOL> i = node . inputs [ <NUM_LIT:0> ] . owner . inputs <EOL> return [ T . true_div ( T . abs_ ( i [ <NUM_LIT:0> ] ) , T . abs_ ( i [ <NUM_LIT:1> ] ) ) ] <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( [ T . mul , T . true_div ] ) <EOL> def local_abs_merge ( node ) : <EOL> """<STR_LIT>""" <EOL> if node . op == T . mul and sum ( [ i . owner . op == T . abs_ for i in node . inputs <EOL> if i . owner ] ) > <NUM_LIT:1> : <EOL> inputs = [ ] <EOL> for i in node . inputs : <EOL> if i . owner and i . owner . op == T . abs_ : <EOL> inputs . append ( i . owner . inputs [ <NUM_LIT:0> ] ) <EOL> elif isinstance ( i , Constant ) : <EOL> try : <EOL> const = get_scalar_constant_value ( i ) <EOL> except NotScalarConstantError : <EOL> return False <EOL> if not ( const >= <NUM_LIT:0> ) . all ( ) : <EOL> return False <EOL> inputs . append ( i ) <EOL> else : <EOL> return False <EOL> return [ T . abs_ ( T . mul ( * inputs ) ) ] <EOL> if node . op == T . true_div and sum ( [ i . owner . op == T . abs_ for i in <EOL> node . inputs if i . owner ] ) == <NUM_LIT:2> : <EOL> return [ T . abs_ ( T . true_div ( node . inputs [ <NUM_LIT:0> ] . owner . inputs [ <NUM_LIT:0> ] , <EOL> node . inputs [ <NUM_LIT:1> ] . owner . inputs [ <NUM_LIT:0> ] ) ) ] <EOL> @ register_stabilize <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( [ T . log ] ) <EOL> def local_log1p ( node ) : <EOL> if node . op == T . log : <EOL> log_arg , = node . inputs <EOL> if log_arg . owner and log_arg . owner . op == T . add : <EOL> scalars , scalar_inputs , nonconsts = scalarconsts_rest ( <EOL> log_arg . owner . inputs ) <EOL> if scalars and numpy . allclose ( numpy . sum ( scalars ) , <NUM_LIT:1> ) : <EOL> if not nonconsts : <EOL> pass <EOL> if len ( nonconsts ) == <NUM_LIT:1> : <EOL> return _fill_chain ( T . log1p ( nonconsts [ <NUM_LIT:0> ] ) , scalar_inputs ) <EOL> else : <EOL> return _fill_chain ( T . log1p ( T . add ( * nonconsts ) ) , <EOL> scalar_inputs ) <EOL> @ register_stabilize <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( [ T . log ] ) <EOL> def local_log_add ( node ) : <EOL> if node . op == T . log : <EOL> z = node . inputs [ <NUM_LIT:0> ] <EOL> if z . owner and z . owner . op == T . add : <EOL> zi = z . owner . inputs <EOL> if len ( zi ) != <NUM_LIT:2> : <EOL> return <EOL> pre_exp = [ x . owner . inputs [ <NUM_LIT:0> ] for x in zi <EOL> if x . owner and x . owner . op == T . exp ] <EOL> if len ( pre_exp ) == len ( zi ) : <EOL> max_pre = T . maximum ( * pre_exp ) <EOL> ret = max_pre + T . log1p ( T . exp ( T . add ( * [ p - max_pre <EOL> for p in pre_exp ] ) ) ) <EOL> ret . tag . values_eq_approx = values_eq_approx_remove_inf <EOL> return [ ret ] <EOL> def add_calculate ( num , denum , aslist = False , out_type = None ) : <EOL> if out_type is None : <EOL> zero = <NUM_LIT:0.0> <EOL> else : <EOL> zero = theano . _asarray ( <NUM_LIT:0> , dtype = out_type . dtype ) <EOL> v = reduce ( N . add , num , zero ) - reduce ( N . add , denum , zero ) <EOL> if aslist : <EOL> if N . all ( v == <NUM_LIT:0> ) : <EOL> return [ ] <EOL> else : <EOL> return [ v ] <EOL> return v <EOL> local_add_canonizer = Canonizer ( T . add , T . sub , T . neg , add_calculate ) <EOL> add_canonizer = in2out ( gof . LocalOptGroup ( local_add_canonizer , local_fill_cut , <EOL> local_fill_sink ) , <EOL> name = '<STR_LIT>' ) <EOL> register_canonicalize ( local_add_canonizer , name = '<STR_LIT>' ) <EOL> def distribute_greedy ( pos_pairs , neg_pairs , num , denum , <EOL> out_type , minscore = <NUM_LIT:0> ) : <EOL> div_cost = <NUM_LIT> <EOL> score = len ( num ) + div_cost * len ( denum ) <EOL> new_pos_pairs = list ( itertools . starmap ( local_mul_canonizer . simplify , <EOL> [ ( n + num , d + denum , out_type ) for ( n , d ) <EOL> in pos_pairs ] ) ) <EOL> new_neg_pairs = list ( itertools . starmap ( local_mul_canonizer . simplify , <EOL> [ ( n + num , d + denum , out_type ) for ( n , d ) <EOL> in neg_pairs ] ) ) <EOL> for ( n , d ) , ( nn , dd ) in zip ( pos_pairs + neg_pairs , new_pos_pairs + <EOL> new_neg_pairs ) : <EOL> score += len ( n ) + div_cost * len ( d ) - len ( nn ) - div_cost * len ( dd ) <EOL> if score <= minscore : <EOL> return False , pos_pairs , neg_pairs <EOL> return True , new_pos_pairs , new_neg_pairs <EOL> def attempt_distribution ( factor , num , denum , out_type ) : <EOL> pos , neg = local_add_canonizer . get_num_denum ( factor ) <EOL> if len ( pos ) == <NUM_LIT:1> and not neg : <EOL> return False , factor , num , denum <EOL> pos_pairs = list ( map ( local_mul_canonizer . get_num_denum , pos ) ) <EOL> neg_pairs = list ( map ( local_mul_canonizer . get_num_denum , neg ) ) <EOL> change = False <EOL> for n in list ( num ) : <EOL> success , pos_pairs , neg_pairs = distribute_greedy ( pos_pairs , <EOL> neg_pairs , [ n ] , [ ] , out_type ) <EOL> if success : <EOL> change = True <EOL> num . remove ( n ) <EOL> for d in list ( denum ) : <EOL> success , pos_pairs , neg_pairs = distribute_greedy ( pos_pairs , <EOL> neg_pairs , [ ] , [ d ] , out_type ) <EOL> if success : <EOL> change = True <EOL> denum . remove ( d ) <EOL> if not change : <EOL> return change , factor , num , denum <EOL> else : <EOL> return change , local_add_canonizer . merge_num_denum ( <EOL> list ( itertools . starmap ( local_mul_canonizer . merge_num_denum , <EOL> pos_pairs ) ) , <EOL> list ( itertools . starmap ( local_mul_canonizer . merge_num_denum , <EOL> neg_pairs ) ) ) , num , denum <EOL> @ register_canonicalize <EOL> @ register_stabilize <EOL> @ gof . local_optimizer ( [ T . mul , T . true_div , T . inv ] ) <EOL> def local_greedy_distributor ( node ) : <EOL> """<STR_LIT>""" <EOL> out = node . outputs [ <NUM_LIT:0> ] <EOL> num , denum = local_mul_canonizer . get_num_denum ( out ) <EOL> if len ( num ) == <NUM_LIT:1> and not denum : <EOL> return False <EOL> new_num , new_denum = [ ] , [ ] <EOL> change = False <EOL> out_type = out . type <EOL> for candidate in list ( num ) : <EOL> if candidate not in num : <EOL> continue <EOL> num . remove ( candidate ) <EOL> _change , candidate , num , denum = attempt_distribution ( candidate , <EOL> num , denum , out_type ) <EOL> change |= _change <EOL> new_num . append ( candidate ) <EOL> for candidate in list ( denum ) : <EOL> if candidate not in denum : <EOL> continue <EOL> denum . remove ( candidate ) <EOL> _change , candidate , denum , num = attempt_distribution ( candidate , <EOL> denum , num , out_type ) <EOL> change |= _change <EOL> new_denum . append ( candidate ) <EOL> if not change : <EOL> return False <EOL> new_num += num <EOL> new_denum += denum <EOL> rval = local_mul_canonizer . merge_num_denum ( new_num , new_denum ) <EOL> if not ( rval . type == out . type ) : <EOL> return False <EOL> return [ rval ] <EOL> @ gof . local_optimizer ( None ) <EOL> def constant_folding ( node ) : <EOL> for input in node . inputs : <EOL> if not isinstance ( input , Constant ) : <EOL> return False <EOL> if not node . op . do_constant_folding ( node ) : <EOL> return False <EOL> storage_map = dict ( [ ( i , [ i . data ] ) for i in node . inputs ] ) <EOL> compute_map = dict ( [ ( i , [ True ] ) for i in node . inputs ] ) <EOL> for o in node . outputs : <EOL> storage_map [ o ] = [ None ] <EOL> compute_map [ o ] = [ False ] <EOL> if ( hasattr ( node . op , '<STR_LIT>' ) and <EOL> node . op . python_constant_folding ( node ) ) : <EOL> old_value = getattr ( node . op , '<STR_LIT>' , False ) <EOL> try : <EOL> node . op . _op_use_c_code = False <EOL> thunk = node . op . make_thunk ( node , <EOL> storage_map , <EOL> compute_map , <EOL> [ ] ) <EOL> finally : <EOL> node . op . _op_use_c_code = old_value <EOL> else : <EOL> thunk = node . op . make_thunk ( node , storage_map , compute_map , <EOL> no_recycling = [ ] ) <EOL> required = thunk ( ) <EOL> assert not required <EOL> rval = [ ] <EOL> for output in node . outputs : <EOL> assert compute_map [ output ] [ <NUM_LIT:0> ] , ( output , storage_map [ output ] [ <NUM_LIT:0> ] ) <EOL> try : <EOL> constant = output . type . Constant <EOL> except AttributeError : <EOL> constant = Constant <EOL> v = constant ( output . type , storage_map [ output ] [ <NUM_LIT:0> ] ) <EOL> copy_stack_trace ( output , v ) <EOL> rval . append ( v ) <EOL> return rval <EOL> topo_constant_folding = in2out ( constant_folding , ignore_newtrees = True , <EOL> name = "<STR_LIT>" ) <EOL> register_canonicalize ( topo_constant_folding , '<STR_LIT>' , final_opt = True ) <EOL> register_stabilize ( topo_constant_folding , '<STR_LIT>' , final_opt = True ) <EOL> register_specialize ( topo_constant_folding , '<STR_LIT>' , final_opt = True ) <EOL> def get_clients ( node ) : <EOL> """<STR_LIT>""" <EOL> return [ c for c , i in node . outputs [ <NUM_LIT:0> ] . clients <EOL> if c != "<STR_LIT>" ] <EOL> def get_clients2 ( node ) : <EOL> """<STR_LIT>""" <EOL> l = [ ] <EOL> for c , i in node . outputs [ <NUM_LIT:0> ] . clients : <EOL> if c != "<STR_LIT>" : <EOL> for var in c . outputs : <EOL> l . extend ( [ cc for cc , ii in var . clients if cc != "<STR_LIT>" ] ) <EOL> return l <EOL> local_one_plus_erf = gof . PatternSub ( ( T . add , <EOL> <NUM_LIT:1> , <EOL> ( T . erf , '<STR_LIT:x>' ) ) , <EOL> ( T . erfc , ( T . neg , '<STR_LIT:x>' ) ) , <EOL> allow_multiple_clients = True , <EOL> name = '<STR_LIT>' , <EOL> tracks = [ T . erf ] , <EOL> get_nodes = get_clients ) <EOL> register_canonicalize ( local_one_plus_erf ) <EOL> register_stabilize ( local_one_plus_erf ) <EOL> register_specialize ( local_one_plus_erf ) <EOL> local_one_minus_erf = gof . PatternSub ( ( T . sub , <EOL> <NUM_LIT:1> , <EOL> ( T . erf , '<STR_LIT:x>' ) ) , <EOL> ( T . erfc , '<STR_LIT:x>' ) , <EOL> allow_multiple_clients = True , <EOL> name = '<STR_LIT>' , ) <EOL> register_canonicalize ( local_one_minus_erf ) <EOL> register_stabilize ( local_one_minus_erf ) <EOL> register_specialize ( local_one_minus_erf ) <EOL> local_one_minus_erf2 = gof . PatternSub ( ( T . add , <EOL> <NUM_LIT:1> , <EOL> ( T . mul , - <NUM_LIT:1> , ( T . erf , '<STR_LIT:x>' ) ) ) , <EOL> ( T . erfc , '<STR_LIT:x>' ) , <EOL> allow_multiple_clients = True , <EOL> name = '<STR_LIT>' ) <EOL> register_canonicalize ( local_one_minus_erf2 ) <EOL> register_stabilize ( local_one_minus_erf2 ) <EOL> register_specialize ( local_one_minus_erf2 ) <EOL> local_one_plus_neg_erf = gof . PatternSub ( ( T . add , <EOL> <NUM_LIT:1> , <EOL> ( T . neg , ( T . erf , '<STR_LIT:x>' ) ) ) , <EOL> ( T . erfc , '<STR_LIT:x>' ) , <EOL> allow_multiple_clients = True , <EOL> name = '<STR_LIT>' , <EOL> tracks = [ T . erf ] , <EOL> get_nodes = get_clients2 ) <EOL> register_canonicalize ( local_one_plus_neg_erf ) <EOL> register_stabilize ( local_one_plus_neg_erf ) <EOL> register_specialize ( local_one_plus_neg_erf ) <EOL> local_erf_minus_one = gof . PatternSub ( ( T . add , <EOL> - <NUM_LIT:1> , <EOL> ( T . erf , '<STR_LIT:x>' ) ) , <EOL> ( T . neg , ( T . erfc , '<STR_LIT:x>' ) ) , <EOL> allow_multiple_clients = True , <EOL> name = '<STR_LIT>' , <EOL> tracks = [ T . erf ] , <EOL> get_nodes = get_clients ) <EOL> register_canonicalize ( local_erf_minus_one ) <EOL> register_stabilize ( local_erf_minus_one ) <EOL> register_specialize ( local_erf_minus_one ) <EOL> local_one_minus_erfc = gof . PatternSub ( ( T . sub , <EOL> <NUM_LIT:1> , <EOL> ( T . erfc , '<STR_LIT:x>' ) ) , <EOL> ( T . erf , '<STR_LIT:x>' ) , <EOL> allow_multiple_clients = True , <EOL> name = '<STR_LIT>' , <EOL> tracks = [ T . erfc ] , <EOL> get_nodes = get_clients ) <EOL> register_canonicalize ( local_one_minus_erfc ) <EOL> register_stabilize ( local_one_minus_erfc ) <EOL> register_specialize ( local_one_minus_erfc ) <EOL> local_one_minus_erfc2 = gof . PatternSub ( ( T . add , <EOL> <NUM_LIT:1> , <EOL> ( T . neg , ( T . erfc , '<STR_LIT:x>' ) ) ) , <EOL> ( T . erf , '<STR_LIT:x>' ) , <EOL> allow_multiple_clients = True , <EOL> name = '<STR_LIT>' , <EOL> tracks = [ T . erfc ] , <EOL> get_nodes = get_clients2 ) <EOL> register_canonicalize ( local_one_minus_erfc2 ) <EOL> register_stabilize ( local_one_minus_erfc2 ) <EOL> register_specialize ( local_one_minus_erfc2 ) <EOL> local_one_minus_erfc3 = gof . PatternSub ( ( T . add , <EOL> <NUM_LIT:1> , <EOL> ( T . mul , - <NUM_LIT:1> , ( T . erfc , '<STR_LIT:x>' ) ) ) , <EOL> ( T . erf , '<STR_LIT:x>' ) , <EOL> allow_multiple_clients = True , <EOL> name = '<STR_LIT>' , <EOL> tracks = [ T . erfc ] , <EOL> get_nodes = get_clients2 ) <EOL> register_canonicalize ( local_one_minus_erfc3 ) <EOL> register_stabilize ( local_one_minus_erfc3 ) <EOL> register_specialize ( local_one_minus_erfc3 ) <EOL> local_one_add_neg_erfc = gof . PatternSub ( ( T . add , <EOL> <NUM_LIT:1> , <EOL> ( T . neg , ( T . erfc , '<STR_LIT:x>' ) ) ) , <EOL> ( T . erf , '<STR_LIT:x>' ) , <EOL> allow_multiple_clients = True , <EOL> name = '<STR_LIT>' , <EOL> tracks = [ T . erfc ] , <EOL> get_nodes = get_clients2 ) <EOL> register_canonicalize ( local_one_add_neg_erfc ) <EOL> register_stabilize ( local_one_add_neg_erfc ) <EOL> register_specialize ( local_one_add_neg_erfc ) <EOL> local_erf_neg_minus_one = gof . PatternSub ( ( T . add , <EOL> - <NUM_LIT:1> , <EOL> ( T . erfc , ( T . neg , '<STR_LIT:x>' ) ) ) , <EOL> ( T . erf , '<STR_LIT:x>' ) , <EOL> allow_multiple_clients = True , <EOL> name = '<STR_LIT>' , <EOL> tracks = [ T . erfc ] , <EOL> get_nodes = get_clients ) <EOL> register_canonicalize ( local_erf_neg_minus_one ) <EOL> register_stabilize ( local_erf_neg_minus_one ) <EOL> register_specialize ( local_erf_neg_minus_one ) <EOL> local_erf_neg_minus_one2 = gof . PatternSub ( ( T . add , <EOL> - <NUM_LIT:1> , <EOL> ( T . erfc , ( T . mul , - <NUM_LIT:1> , '<STR_LIT:x>' ) ) ) , <EOL> ( T . erf , '<STR_LIT:x>' ) , <EOL> allow_multiple_clients = True , <EOL> name = '<STR_LIT>' , <EOL> tracks = [ T . erfc ] , <EOL> get_nodes = get_clients ) <EOL> register_canonicalize ( local_erf_neg_minus_one2 ) <EOL> register_stabilize ( local_erf_neg_minus_one2 ) <EOL> register_specialize ( local_erf_neg_minus_one2 ) <EOL> @ register_stabilize <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( [ T . log ] ) <EOL> def local_log_erfc ( node ) : <EOL> if node . op != T . log : <EOL> return False <EOL> if not node . inputs [ <NUM_LIT:0> ] . owner or node . inputs [ <NUM_LIT:0> ] . owner . op != T . erfc : <EOL> return False <EOL> if hasattr ( node . tag , '<STR_LIT>' ) : <EOL> return False <EOL> node . tag . local_log_erfc_applied = True <EOL> x = node . inputs [ <NUM_LIT:0> ] . owner . inputs [ <NUM_LIT:0> ] <EOL> stab_value = ( - x ** <NUM_LIT:2> - T . log ( x ) - <NUM_LIT> * T . log ( numpy . pi ) + <EOL> T . log ( <NUM_LIT:1> - <NUM_LIT:1> / ( <NUM_LIT:2> * x ** <NUM_LIT:2> ) + <NUM_LIT:3> / ( <NUM_LIT:4> * x ** <NUM_LIT:4> ) - <EOL> <NUM_LIT:15> / ( <NUM_LIT:8> * x ** <NUM_LIT:6> ) ) ) <EOL> if ( node . outputs [ <NUM_LIT:0> ] . dtype == '<STR_LIT>' or <EOL> node . outputs [ <NUM_LIT:0> ] . dtype == '<STR_LIT>' ) : <EOL> threshold = <NUM_LIT> <EOL> elif node . outputs [ <NUM_LIT:0> ] . dtype == '<STR_LIT>' : <EOL> threshold = <NUM_LIT> <EOL> ret = T . switch ( x < threshold , node . outputs [ <NUM_LIT:0> ] , stab_value ) <EOL> ret . tag . values_eq_approx = values_eq_approx_remove_inf <EOL> return [ ret ] <EOL> @ register_stabilize <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( [ T . true_div ] ) <EOL> def local_grad_log_erfc_neg ( node ) : <EOL> if node . op != T . true_div : <EOL> return False <EOL> if not node . inputs [ <NUM_LIT:1> ] . owner or node . inputs [ <NUM_LIT:1> ] . owner . op != T . erfc : <EOL> return False <EOL> erfc = node . inputs [ <NUM_LIT:1> ] <EOL> erfc_x = erfc . owner . inputs [ <NUM_LIT:0> ] <EOL> if not node . inputs [ <NUM_LIT:0> ] . owner : <EOL> return False <EOL> if node . inputs [ <NUM_LIT:0> ] . owner . op != T . mul : <EOL> mul = None <EOL> y = [ ] <EOL> if not node . inputs [ <NUM_LIT:0> ] . owner or node . inputs [ <NUM_LIT:0> ] . owner . op != T . exp : <EOL> return False <EOL> exp = node . inputs [ <NUM_LIT:0> ] <EOL> else : <EOL> mul = node . inputs [ <NUM_LIT:0> ] <EOL> exp = None <EOL> for idx , inp in enumerate ( mul . owner . inputs ) : <EOL> if inp . owner and inp . owner . op == T . exp : <EOL> exp = inp <EOL> break <EOL> if len ( mul . owner . inputs ) == <NUM_LIT:2> : <EOL> y = [ mul . owner . inputs [ <NUM_LIT:1> - idx ] ] <EOL> else : <EOL> y = mul . owner . inputs [ : ] <EOL> del y [ idx ] <EOL> del mul <EOL> if not exp . owner . inputs [ <NUM_LIT:0> ] . owner : <EOL> return False <EOL> if exp . owner . inputs [ <NUM_LIT:0> ] . owner . op == T . neg : <EOL> neg = exp . owner . inputs [ <NUM_LIT:0> ] <EOL> if ( not neg . owner . inputs [ <NUM_LIT:0> ] . owner or <EOL> neg . owner . inputs [ <NUM_LIT:0> ] . owner . op != T . sqr ) : <EOL> return False <EOL> sqr = neg . owner . inputs [ <NUM_LIT:0> ] <EOL> x = sqr . owner . inputs [ <NUM_LIT:0> ] <EOL> elif exp . owner . inputs [ <NUM_LIT:0> ] . owner . op == T . mul : <EOL> mul_neg = exp . owner . inputs [ <NUM_LIT:0> ] <EOL> def check_input ( inputs ) : <EOL> new_inputs = [ ] <EOL> for i in inputs : <EOL> if i . owner and i . owner . op == T . mul : <EOL> new_inputs . extend ( check_input ( i . owner . inputs ) ) <EOL> else : <EOL> new_inputs . append ( i ) <EOL> return new_inputs <EOL> mul_inputs = check_input ( mul_neg . owner . inputs ) <EOL> for i in xrange ( len ( mul_inputs ) ) : <EOL> if isinstance ( i , Constant ) : <EOL> if i == <NUM_LIT:0> : <EOL> break <EOL> else : <EOL> tmp = mul_inputs [ <NUM_LIT:0> ] <EOL> mul_inputs [ <NUM_LIT:0> ] = mul_inputs [ i ] <EOL> mul_inputs [ i ] = tmp <EOL> break <EOL> mul_neg = T . mul ( * mul_inputs ) <EOL> try : <EOL> cst2 = get_scalar_constant_value ( mul_neg . owner . inputs [ <NUM_LIT:0> ] ) <EOL> except NotScalarConstantError : <EOL> return False <EOL> if len ( mul_neg . owner . inputs ) == <NUM_LIT:2> : <EOL> if ( not mul_neg . owner . inputs [ <NUM_LIT:1> ] . owner or <EOL> mul_neg . owner . inputs [ <NUM_LIT:1> ] . owner . op != T . sqr ) : <EOL> return False <EOL> sqr = mul_neg . owner . inputs [ <NUM_LIT:1> ] <EOL> x = sqr . owner . inputs [ <NUM_LIT:0> ] <EOL> elif len ( mul_neg . owner . inputs ) == <NUM_LIT:3> : <EOL> if mul_neg . owner . inputs [ <NUM_LIT:1> ] is not mul_neg . owner . inputs [ <NUM_LIT:2> ] : <EOL> return False <EOL> x = mul_neg . owner . inputs [ <NUM_LIT:1> ] <EOL> else : <EOL> return False <EOL> if cst2 != - <NUM_LIT:1> : <EOL> if ( not erfc_x . owner or erfc_x . owner . op != T . mul or <EOL> len ( erfc_x . owner . inputs ) != <NUM_LIT:2> ) : <EOL> return False <EOL> if erfc_x . owner . inputs [ <NUM_LIT:1> ] is not mul_neg . owner . inputs [ <NUM_LIT:1> ] : <EOL> return False <EOL> x = erfc_x <EOL> try : <EOL> cst = get_scalar_constant_value ( erfc_x . owner . inputs [ <NUM_LIT:0> ] ) <EOL> except NotScalarConstantError : <EOL> return False <EOL> if cst2 != - cst * <NUM_LIT:2> : <EOL> return False <EOL> elif erfc_x is not x : <EOL> return False <EOL> else : <EOL> return False <EOL> if hasattr ( node . tag , '<STR_LIT>' ) : <EOL> return False <EOL> true_div_no_mul = T . true_div ( exp , erfc ) <EOL> true_div_no_mul . owner . tag . local_grad_log_erfc_neg = True <EOL> stab_value = ( x * T . pow ( <NUM_LIT:1> - <NUM_LIT:1> / ( <NUM_LIT:2> * ( x ** <NUM_LIT:2> ) ) + <EOL> <NUM_LIT:3> / ( <NUM_LIT:4> * ( x ** <NUM_LIT:4> ) ) - <NUM_LIT:15> / ( <NUM_LIT:8> * ( x ** <NUM_LIT:6> ) ) , - <NUM_LIT:1> ) * <EOL> T . cast ( T . sqrt ( numpy . pi ) , dtype = x . dtype ) ) <EOL> if x . dtype == '<STR_LIT>' or x . dtype == '<STR_LIT>' : <EOL> threshold = <NUM_LIT> <EOL> elif x . dtype == '<STR_LIT>' : <EOL> threshold = <NUM_LIT> <EOL> ret = T . switch ( x < threshold , true_div_no_mul , stab_value ) <EOL> if y : <EOL> ret = T . mul ( ret , * y ) <EOL> ret . tag . values_eq_approx = values_eq_approx_remove_inf_nan <EOL> return [ ret ] <EOL> """<STR_LIT>""" <EOL> def local_elemwise_fusion_op ( OP , max_input_fct = lambda node : <NUM_LIT:32> , <EOL> maker = None ) : <EOL> """<STR_LIT>""" <EOL> if maker is None : <EOL> def maker ( node , scalar_op ) : <EOL> return OP ( scalar_op ) <EOL> def local_fuse ( node ) : <EOL> """<STR_LIT>""" <EOL> if type ( node . op ) is not OP : <EOL> return False <EOL> inputs = [ ] <EOL> s_inputs = [ ] <EOL> s_g = [ ] <EOL> max_nb_input = max_input_fct ( node ) <EOL> new_nb_input = len ( node . inputs ) <EOL> fused = False <EOL> for i in node . inputs : <EOL> do_fusion = False <EOL> catch = False <EOL> tmp_input = [ ] <EOL> tmp_scalar = [ ] <EOL> if ( i . owner and <EOL> isinstance ( i . owner . op , OP ) and <EOL> len ( set ( [ n for n , idx in i . clients ] ) ) == <NUM_LIT:1> and <EOL> i . owner . outputs [ <NUM_LIT:0> ] . broadcastable == <EOL> node . outputs [ <NUM_LIT:0> ] . broadcastable ) : <EOL> do_fusion = True <EOL> try : <EOL> tmp_s_input = [ ] <EOL> for ii in i . owner . inputs : <EOL> if ii in inputs : <EOL> tmp_s_input . append ( s_inputs [ inputs . index ( ii ) ] ) <EOL> elif ii in tmp_input : <EOL> tmp_s_input . append ( tmp_scalar [ tmp_input . index ( ii ) ] ) <EOL> else : <EOL> tmp = scalar . get_scalar_type ( ii . dtype ) . make_variable ( ) <EOL> try : <EOL> tv = gof . op . get_test_value ( ii ) <EOL> if tv . size > <NUM_LIT:0> : <EOL> tmp . tag . test_value = tv . flatten ( ) [ <NUM_LIT:0> ] <EOL> else : <EOL> tmp . tag . test_value = tv <EOL> except AttributeError : <EOL> pass <EOL> tmp_s_input . append ( tmp ) <EOL> tmp_input . append ( ii ) <EOL> tmp_scalar . append ( tmp_s_input [ - <NUM_LIT:1> ] ) <EOL> s_op = i . owner . op . scalar_op ( * tmp_s_input , <EOL> return_list = True ) <EOL> i . owner . op . scalar_op . c_code ( s_op [ <NUM_LIT:0> ] . owner , <EOL> "<STR_LIT>" , <EOL> [ "<STR_LIT:x>" for x in i . owner . inputs ] , <EOL> [ "<STR_LIT:z>" for z in i . owner . outputs ] , <EOL> { } ) <EOL> except MethodNotDefined : <EOL> catch = True <EOL> except NotImplementedError : <EOL> catch = True <EOL> if catch : <EOL> _logger . info ( ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) % <EOL> str ( i . owner . op . scalar_op ) ) <EOL> do_fusion = False <EOL> new_nb_input_ = new_nb_input + len ( tmp_input ) - <NUM_LIT:1> <EOL> for x in tmp_input : <EOL> if x in node . inputs : <EOL> new_nb_input_ -= <NUM_LIT:1> <EOL> if do_fusion and ( new_nb_input_ <= max_nb_input ) : <EOL> fused = True <EOL> new_nb_input = new_nb_input_ <EOL> inputs . extend ( tmp_input ) <EOL> s_inputs . extend ( tmp_scalar ) <EOL> s_g . extend ( s_op ) <EOL> else : <EOL> if inputs . count ( i ) == node . inputs . count ( i ) : <EOL> s = s_inputs [ inputs . index ( i ) ] <EOL> else : <EOL> s = scalar . get_scalar_type ( i . dtype ) . make_variable ( ) <EOL> try : <EOL> if theano . config . compute_test_value != '<STR_LIT>' : <EOL> v = gof . op . get_test_value ( i ) <EOL> if v . size > <NUM_LIT:0> : <EOL> s . tag . test_value = v . flatten ( ) [ <NUM_LIT:0> ] <EOL> except AttributeError : <EOL> pass <EOL> inputs . append ( i ) <EOL> s_inputs . append ( s ) <EOL> s_g . append ( s ) <EOL> if not fused : <EOL> return False <EOL> if new_nb_input != len ( inputs ) or len ( s_inputs ) != len ( inputs ) : <EOL> raise Exception ( """<STR_LIT>""" ) <EOL> s_new_out = node . op . scalar_op ( * s_g , return_list = True ) <EOL> try : <EOL> s_new_out [ <NUM_LIT:0> ] . owner . op . c_code ( s_new_out [ <NUM_LIT:0> ] . owner , <EOL> "<STR_LIT>" , <EOL> [ "<STR_LIT:x>" for x in s_g ] , <EOL> [ "<STR_LIT:z>" for x in s_new_out ] , { } ) <EOL> except MethodNotDefined : <EOL> _logger . info ( ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) % str ( <EOL> s_new_out [ <NUM_LIT:0> ] . owner . op ) ) <EOL> return False <EOL> except NotImplementedError : <EOL> _logger . info ( ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) % str ( s_new_out [ <NUM_LIT:0> ] . owner . op ) ) <EOL> return False <EOL> C = scalar . Composite ( s_inputs , s_new_out ) <EOL> n = maker ( node , C ) ( * inputs ) . owner <EOL> assert len ( n . outputs ) == <NUM_LIT:1> <EOL> assert node . outputs [ <NUM_LIT:0> ] . dtype == n . outputs [ <NUM_LIT:0> ] . dtype <EOL> if len ( n . inputs ) > max_nb_input : <EOL> _logger . info ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return False <EOL> while True : <EOL> ret = local_fuse ( n ) <EOL> if ret is not False and ret is not None : <EOL> assert len ( ret ) == len ( n . outputs ) <EOL> assert len ( ret ) == <NUM_LIT:1> <EOL> n = ret [ <NUM_LIT:0> ] . owner <EOL> else : <EOL> break <EOL> return n . outputs <EOL> return local_fuse <EOL> def elemwise_max_input_fct ( node ) : <EOL> if not theano . config . cxx : <EOL> return <NUM_LIT> <EOL> return <NUM_LIT> <EOL> local_elemwise_fusion = local_elemwise_fusion_op ( T . Elemwise , <EOL> elemwise_max_input_fct ) <EOL> class FusionOptimizer ( Optimizer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , local_optimizer ) : <EOL> Optimizer . __init__ ( self ) <EOL> self . optimizer = local_optimizer <EOL> def add_requirements ( self , fgraph ) : <EOL> fgraph . attach_feature ( toolbox . ReplaceValidate ( ) ) <EOL> def apply ( self , fgraph ) : <EOL> did_something = True <EOL> nb_iter = <NUM_LIT:0> <EOL> nb_replacement = <NUM_LIT:0> <EOL> nb_inconsistency_replace = <NUM_LIT:0> <EOL> time_toposort = <NUM_LIT:0> <EOL> if fgraph . profile : <EOL> validate_before = fgraph . profile . validate_time <EOL> callbacks_before = fgraph . execute_callbacks_times . copy ( ) <EOL> callback_before = fgraph . execute_callbacks_time <EOL> while did_something : <EOL> t0 = time . time ( ) <EOL> nodelist = list ( fgraph . toposort ( ) ) <EOL> time_toposort += time . time ( ) - t0 <EOL> nodelist . reverse ( ) <EOL> did_something = False <EOL> for node in nodelist : <EOL> if node in fgraph . apply_nodes : <EOL> new_outputs = self . optimizer ( node ) <EOL> if new_outputs : <EOL> assert len ( new_outputs ) == len ( node . outputs ) <EOL> try : <EOL> fgraph . replace_all_validate ( <EOL> list ( zip ( node . outputs , new_outputs ) ) , <EOL> reason = self . __class__ . __name__ ) <EOL> did_something = True <EOL> nb_replacement += <NUM_LIT:1> <EOL> except InconsistencyError : <EOL> nb_inconsistency_replace += <NUM_LIT:1> <EOL> pass <EOL> nb_iter += <NUM_LIT:1> <EOL> if fgraph . profile : <EOL> validate_time = fgraph . profile . validate_time - validate_before <EOL> callback_time = fgraph . execute_callbacks_time - callback_before <EOL> callbacks_time = { } <EOL> for k , v in iteritems ( fgraph . execute_callbacks_times ) : <EOL> if k in callbacks_before : <EOL> callbacks_time [ k ] = v - callbacks_before [ k ] <EOL> else : <EOL> callbacks_time [ k ] = v <EOL> else : <EOL> validate_time = None <EOL> callback_time = None <EOL> callbacks_time = { } <EOL> return ( self , nb_iter , nb_replacement , <EOL> nb_inconsistency_replace , <EOL> validate_time , callback_time , callbacks_time , <EOL> time_toposort ) <EOL> @ staticmethod <EOL> def print_profile ( stream , prof , level = <NUM_LIT:0> ) : <EOL> blanc = ( '<STR_LIT:U+0020>' * level ) <EOL> print ( blanc , "<STR_LIT>" , file = stream ) <EOL> print ( blanc , "<STR_LIT>" , prof [ <NUM_LIT:1> ] , file = stream ) <EOL> print ( blanc , "<STR_LIT>" , prof [ <NUM_LIT:2> ] , file = stream ) <EOL> print ( blanc , "<STR_LIT>" , prof [ <NUM_LIT:3> ] , file = stream ) <EOL> print ( blanc , "<STR_LIT>" , prof [ <NUM_LIT:4> ] , file = stream ) <EOL> print ( blanc , "<STR_LIT>" , prof [ <NUM_LIT:5> ] , file = stream ) <EOL> if prof [ <NUM_LIT:5> ] > <NUM_LIT:1> : <EOL> print ( blanc , "<STR_LIT>" , file = stream ) <EOL> for i in sorted ( iteritems ( prof [ <NUM_LIT:6> ] ) , key = lambda a : a [ <NUM_LIT:1> ] ) : <EOL> if i [ <NUM_LIT:1> ] > <NUM_LIT:0> : <EOL> print ( i ) <EOL> print ( blanc , "<STR_LIT>" , prof [ <NUM_LIT:7> ] , file = stream ) <EOL> def local_add_mul_fusion ( node ) : <EOL> """<STR_LIT>""" <EOL> if ( not isinstance ( node . op , Elemwise ) or <EOL> not isinstance ( node . op . scalar_op , ( scalar . Add , scalar . Mul ) ) ) : <EOL> return False <EOL> s_op = node . op . scalar_op . __class__ <EOL> for inp in node . inputs : <EOL> if ( inp . owner and <EOL> isinstance ( inp . owner . op , Elemwise ) and <EOL> isinstance ( inp . owner . op . scalar_op , s_op ) ) : <EOL> l = list ( node . inputs ) <EOL> l . remove ( inp ) <EOL> output_node = node . op ( * ( l + inp . owner . inputs ) ) <EOL> copy_stack_trace ( node . outputs [ <NUM_LIT:0> ] , output_node ) <EOL> return [ output_node ] <EOL> if config . tensor . local_elemwise_fusion : <EOL> _logger . debug ( "<STR_LIT>" ) <EOL> fuse_seqopt = gof . SequenceDB ( ) <EOL> fuse_seqopt . register ( '<STR_LIT>' , <EOL> FusionOptimizer ( local_add_mul_fusion ) , <EOL> <NUM_LIT:0> , '<STR_LIT>' , '<STR_LIT>' ) <EOL> fuse_seqopt . register ( '<STR_LIT>' , <EOL> FusionOptimizer ( local_elemwise_fusion ) , <EOL> <NUM_LIT:1> , '<STR_LIT>' , '<STR_LIT>' ) <EOL> compile . optdb . register ( '<STR_LIT>' , <EOL> fuse_seqopt , <NUM_LIT> , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> else : <EOL> _logger . debug ( "<STR_LIT>" ) <EOL> compile . optdb . register ( '<STR_LIT>' , <EOL> FusionOptimizer ( local_elemwise_fusion ) , <NUM_LIT> , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> register_canonicalize ( gof . OpRemove ( theano . gradient . consider_constant_ ) , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> name = '<STR_LIT>' ) <EOL> register_canonicalize ( gof . OpRemove ( theano . gradient . zero_grad_ ) , <EOL> '<STR_LIT>' , '<STR_LIT>' , name = '<STR_LIT>' ) <EOL> register_canonicalize ( gof . OpRemove ( theano . gradient . disconnected_grad_ ) , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> name = '<STR_LIT>' ) <EOL> @ register_canonicalize <EOL> @ gof . local_optimizer ( [ theano . gradient . GradClip ] ) <EOL> def local_grad_clip ( node ) : <EOL> if isinstance ( node . op , theano . gradient . GradClip ) : <EOL> return node . inputs <EOL> @ register_canonicalize <EOL> @ register_stabilize <EOL> @ register_specialize <EOL> @ gof . local_optimizer ( [ T . Alloc ] ) <EOL> def local_merge_alloc ( node ) : <EOL> if not isinstance ( node . op , T . Alloc ) : <EOL> return False <EOL> if not node . inputs [ <NUM_LIT:0> ] . owner or not isinstance ( <EOL> node . inputs [ <NUM_LIT:0> ] . owner . op , T . Alloc ) : <EOL> return False <EOL> inputs_outer = node . inputs <EOL> inputs_inner = node . inputs [ <NUM_LIT:0> ] . owner . inputs <EOL> dims_outer = inputs_outer [ <NUM_LIT:1> : ] <EOL> dims_inner = inputs_inner [ <NUM_LIT:1> : ] <EOL> dims_outer_rev = dims_outer [ : : - <NUM_LIT:1> ] <EOL> dims_inner_rev = dims_inner [ : : - <NUM_LIT:1> ] <EOL> i = <NUM_LIT:0> <EOL> for dim_inner , dim_outer in zip ( dims_inner_rev , dims_outer_rev ) : <EOL> if dim_inner != dim_outer : <EOL> if isinstance ( dim_inner , Constant ) and dim_inner . data == <NUM_LIT:1> : <EOL> pass <EOL> else : <EOL> dims_outer [ - <NUM_LIT:1> - i ] = Assert ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) ( <EOL> dim_outer , T . eq ( dim_outer , dim_inner ) ) <EOL> i += <NUM_LIT:1> <EOL> return [ T . alloc ( inputs_inner [ <NUM_LIT:0> ] , * dims_outer ) ] </s>
