<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , print_function , division <EOL> import warnings <EOL> import numpy <EOL> from six import integer_types <EOL> from six . moves import xrange <EOL> from theano import Op , Apply , shared , config , Variable <EOL> from theano import gradient , function <EOL> from theano import tensor <EOL> from theano . tensor import ( TensorType , as_tensor_variable , get_vector_length , <EOL> cast , opt , scal ) <EOL> from theano . tensor import sqrt , log , sin , cos , join , prod <EOL> from theano . compile import optdb <EOL> from theano . gof import local_optimizer <EOL> from . import multinomial <EOL> import theano . sandbox . cuda <EOL> from theano . sandbox . cuda import GpuOp <EOL> from theano . sandbox . gpuarray . basic_ops import GpuKernelBase , Kernel <EOL> from theano . sandbox . gpuarray . type import GpuArrayType <EOL> from theano . sandbox . gpuarray . fp16_help import write_w <EOL> from theano . sandbox . gpuarray . opt import ( register_opt as register_gpua , <EOL> host_from_gpu as host_from_gpua ) <EOL> if theano . sandbox . cuda . cuda_available : <EOL> from theano . sandbox . cuda import ( CudaNdarrayType , <EOL> float32_shared_constructor ) <EOL> def matVecModM ( A , s , m ) : <EOL> assert A . dtype == '<STR_LIT>' <EOL> return numpy . int32 ( numpy . sum ( ( A * s ) % m , <NUM_LIT:1> ) % m ) <EOL> def multMatVect ( v , A , m1 , B , m2 ) : <EOL> """<STR_LIT>""" <EOL> if multMatVect . dot_modulo is None : <EOL> A_sym = tensor . lmatrix ( '<STR_LIT:A>' ) <EOL> s_sym = tensor . ivector ( '<STR_LIT:s>' ) <EOL> m_sym = tensor . iscalar ( '<STR_LIT:m>' ) <EOL> A2_sym = tensor . lmatrix ( '<STR_LIT>' ) <EOL> s2_sym = tensor . ivector ( '<STR_LIT>' ) <EOL> m2_sym = tensor . iscalar ( '<STR_LIT>' ) <EOL> o = DotModulo ( ) ( A_sym , s_sym , m_sym , A2_sym , s2_sym , m2_sym ) <EOL> multMatVect . dot_modulo = function ( <EOL> [ A_sym , s_sym , m_sym , A2_sym , s2_sym , m2_sym ] , o , profile = False ) <EOL> f = multMatVect . dot_modulo <EOL> f . input_storage [ <NUM_LIT:0> ] . storage [ <NUM_LIT:0> ] = A <EOL> f . input_storage [ <NUM_LIT:1> ] . storage [ <NUM_LIT:0> ] = v [ : <NUM_LIT:3> ] <EOL> f . input_storage [ <NUM_LIT:2> ] . storage [ <NUM_LIT:0> ] = m1 <EOL> f . input_storage [ <NUM_LIT:3> ] . storage [ <NUM_LIT:0> ] = B <EOL> f . input_storage [ <NUM_LIT:4> ] . storage [ <NUM_LIT:0> ] = v [ <NUM_LIT:3> : ] <EOL> f . input_storage [ <NUM_LIT:5> ] . storage [ <NUM_LIT:0> ] = m2 <EOL> f . fn ( ) <EOL> r = f . output_storage [ <NUM_LIT:0> ] . storage [ <NUM_LIT:0> ] <EOL> return r <EOL> multMatVect . dot_modulo = None <EOL> class DotModulo ( Op ) : <EOL> """<STR_LIT>""" <EOL> __props__ = ( ) <EOL> def make_node ( self , A , s , m , A2 , s2 , m2 ) : <EOL> return Apply ( self , [ A , s , m , A2 , s2 , m2 ] , [ s . type ( ) ] ) <EOL> def perform ( self , node , inputs , outputs ) : <EOL> ( A , s , m , A2 , s2 , m2 ) = inputs <EOL> ( out , ) = outputs <EOL> o1 = matVecModM ( A , s , m ) <EOL> o2 = matVecModM ( A2 , s2 , m2 ) <EOL> out [ <NUM_LIT:0> ] = numpy . concatenate ( ( o1 , o2 ) ) <EOL> def c_code_cache_version ( self ) : <EOL> return ( <NUM_LIT:6> , ) <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> ( _A , _s , _m , _A2 , _s2 , _m2 ) = inputs <EOL> ( _z , ) = outputs <EOL> return """<STR_LIT>""" % dict ( locals ( ) , ** sub ) <EOL> M1 = numpy . asarray ( numpy . int32 ( <NUM_LIT> ) ) <EOL> M2 = numpy . asarray ( numpy . int32 ( <NUM_LIT> ) ) <EOL> MASK12 = numpy . int32 ( <NUM_LIT> ) <EOL> MASK13 = numpy . int32 ( <NUM_LIT> ) <EOL> MASK2 = numpy . int32 ( <NUM_LIT> ) <EOL> MULT2 = numpy . int32 ( <NUM_LIT> ) <EOL> NORM = <NUM_LIT> <EOL> A1p72 = numpy . asarray ( [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] , <EOL> dtype = '<STR_LIT>' ) <EOL> A2p72 = numpy . asarray ( [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] , <EOL> dtype = '<STR_LIT>' ) <EOL> A1p134 = numpy . asarray ( <EOL> [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] , <EOL> dtype = '<STR_LIT>' ) <EOL> A2p134 = numpy . asarray ( <EOL> [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] , <EOL> dtype = '<STR_LIT>' ) <EOL> np_int32_vals = [ numpy . int32 ( i ) for i in ( <NUM_LIT:0> , <NUM_LIT:7> , <NUM_LIT:9> , <NUM_LIT:15> , <NUM_LIT:16> , <NUM_LIT> , <NUM_LIT> ) ] <EOL> def ff_2p134 ( rstate ) : <EOL> return multMatVect ( rstate , A1p134 , M1 , A2p134 , M2 ) <EOL> def ff_2p72 ( rstate ) : <EOL> return multMatVect ( rstate , A1p72 , M1 , A2p72 , M2 ) <EOL> def mrg_next_value ( rstate , new_rstate ) : <EOL> x11 , x12 , x13 , x21 , x22 , x23 = rstate <EOL> assert type ( x11 ) == numpy . int32 <EOL> i0 , i7 , i9 , i15 , i16 , i22 , i24 = np_int32_vals <EOL> y1 = ( ( ( x12 & MASK12 ) << i22 ) + ( x12 >> i9 ) + <EOL> ( ( x13 & MASK13 ) << i7 ) + ( x13 >> i24 ) ) <EOL> assert type ( y1 ) == numpy . int32 <EOL> if ( y1 < <NUM_LIT:0> or y1 >= M1 ) : <EOL> y1 -= M1 <EOL> y1 += x13 <EOL> if ( y1 < <NUM_LIT:0> or y1 >= M1 ) : <EOL> y1 -= M1 <EOL> x13 = x12 <EOL> x12 = x11 <EOL> x11 = y1 <EOL> y1 = ( ( x21 & MASK2 ) << i15 ) + ( MULT2 * ( x21 >> i16 ) ) <EOL> assert type ( y1 ) == numpy . int32 <EOL> if ( y1 < <NUM_LIT:0> or y1 >= M2 ) : <EOL> y1 -= M2 <EOL> y2 = ( ( x23 & MASK2 ) << i15 ) + ( MULT2 * ( x23 >> i16 ) ) <EOL> assert type ( y2 ) == numpy . int32 <EOL> if ( y2 < <NUM_LIT:0> or y2 >= M2 ) : <EOL> y2 -= M2 <EOL> y2 += x23 <EOL> if ( y2 < <NUM_LIT:0> or y2 >= M2 ) : <EOL> y2 -= M2 <EOL> y2 += y1 <EOL> if ( y2 < <NUM_LIT:0> or y2 >= M2 ) : <EOL> y2 -= M2 <EOL> x23 = x22 <EOL> x22 = x21 <EOL> x21 = y2 <EOL> new_rstate [ ... ] = [ x11 , x12 , x13 , x21 , x22 , x23 ] <EOL> assert new_rstate . dtype == numpy . int32 <EOL> if ( x11 <= x21 ) : <EOL> return ( x11 - x21 + M1 ) * NORM <EOL> else : <EOL> return ( x11 - x21 ) * NORM <EOL> class mrg_uniform_base ( Op ) : <EOL> __props__ = ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def __init__ ( self , output_type , inplace = False ) : <EOL> Op . __init__ ( self ) <EOL> self . output_type = output_type <EOL> self . inplace = inplace <EOL> if inplace : <EOL> self . destroy_map = { <NUM_LIT:0> : [ <NUM_LIT:0> ] } <EOL> self . warned_numpy_version = False <EOL> def __str__ ( self ) : <EOL> if self . inplace : <EOL> s = "<STR_LIT>" <EOL> else : <EOL> s = "<STR_LIT>" <EOL> return self . __class__ . __name__ + "<STR_LIT>" % ( self . output_type , s ) <EOL> def make_node ( self , rstate , size ) : <EOL> return Apply ( self , <EOL> [ rstate , size ] , <EOL> [ rstate . type ( ) , self . output_type ( ) ] ) <EOL> def grad ( self , inputs , ograd ) : <EOL> return [ gradient . grad_undefined ( self , k , inp , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> for k , inp in enumerate ( inputs ) ] <EOL> def R_op ( self , inputs , eval_points ) : <EOL> return [ None for i in eval_points ] <EOL> class mrg_uniform ( mrg_uniform_base ) : <EOL> @ classmethod <EOL> def new ( cls , rstate , ndim , dtype , size ) : <EOL> v_size = as_tensor_variable ( size ) <EOL> if ndim is None : <EOL> ndim = get_vector_length ( v_size ) <EOL> op = cls ( TensorType ( dtype , ( False , ) * ndim ) ) <EOL> return op ( rstate , v_size ) <EOL> def perform ( self , node , inp , out ) : <EOL> rstate , size = inp <EOL> o_rstate , o_sample = out <EOL> n_elements = <NUM_LIT:1> <EOL> for s in size : <EOL> n_elements *= s <EOL> if n_elements > M1 : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> rstate = numpy . asarray ( rstate ) <EOL> if not self . inplace : <EOL> rstate = rstate . copy ( ) <EOL> n_streams , _ = rstate . shape <EOL> rval = numpy . zeros ( n_elements , dtype = self . output_type . dtype ) <EOL> err_orig = numpy . seterr ( over = '<STR_LIT:ignore>' ) <EOL> try : <EOL> for i in xrange ( n_elements ) : <EOL> sample = mrg_next_value ( rstate [ i % n_streams ] , <EOL> rstate [ i % n_streams ] ) <EOL> rval [ i ] = sample <EOL> finally : <EOL> numpy . seterr ( ** err_orig ) <EOL> o_rstate [ <NUM_LIT:0> ] = node . outputs [ <NUM_LIT:0> ] . type . filter ( rstate ) <EOL> o_sample [ <NUM_LIT:0> ] = node . outputs [ <NUM_LIT:1> ] . type . filter ( rval . reshape ( size ) ) <EOL> def c_code ( self , node , name , inp , out , sub ) : <EOL> rstate , size = inp <EOL> assert isinstance ( node . inputs [ <NUM_LIT:0> ] . type , TensorType ) <EOL> o_rstate , o_sample = out <EOL> if self . inplace : <EOL> o_rstate_requirement = ( <EOL> '<STR_LIT>' ) <EOL> else : <EOL> o_rstate_requirement = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> ndim = self . output_type . ndim <EOL> o_type_num = numpy . asarray ( <NUM_LIT:0> , dtype = self . output_type . dtype ) . dtype . num <EOL> fail = sub [ '<STR_LIT>' ] <EOL> if self . output_type . dtype == '<STR_LIT>' : <EOL> otype = '<STR_LIT:float>' <EOL> NORM = '<STR_LIT>' <EOL> else : <EOL> otype = '<STR_LIT>' <EOL> NORM = '<STR_LIT>' <EOL> return """<STR_LIT>""" % locals ( ) <EOL> def c_code_cache_version ( self ) : <EOL> return ( <NUM_LIT:8> , ) <EOL> class GPU_mrg_uniform ( mrg_uniform_base , GpuOp ) : <EOL> @ classmethod <EOL> def new ( cls , rstate , ndim , dtype , size ) : <EOL> v_size = as_tensor_variable ( size ) <EOL> if ndim is None : <EOL> ndim = get_vector_length ( v_size ) <EOL> op = cls ( CudaNdarrayType ( ( False , ) * ndim ) ) <EOL> return op ( rstate , v_size ) <EOL> def c_support_code_apply ( self , node , nodename ) : <EOL> if self . output_type . dtype == '<STR_LIT>' : <EOL> otype = '<STR_LIT:float>' <EOL> NORM = '<STR_LIT>' <EOL> else : <EOL> otype = '<STR_LIT>' <EOL> NORM = '<STR_LIT>' <EOL> return """<STR_LIT>""" % locals ( ) <EOL> def c_code ( self , node , nodename , inp , out , sub ) : <EOL> rstate , size = inp <EOL> o_rstate , o_sample = out <EOL> inplace = int ( self . inplace ) <EOL> ndim = self . output_type . ndim <EOL> o_type_num = numpy . asarray ( <NUM_LIT:0> , dtype = self . output_type . dtype ) . dtype . num <EOL> fail = sub [ '<STR_LIT>' ] <EOL> if self . output_type . dtype == '<STR_LIT>' : <EOL> otype = '<STR_LIT:float>' <EOL> else : <EOL> otype = '<STR_LIT>' <EOL> SYNC = "<STR_LIT>" <EOL> return """<STR_LIT>""" % locals ( ) <EOL> def c_code_cache_version ( self ) : <EOL> return ( <NUM_LIT:12> , ) <EOL> class GPUA_mrg_uniform ( GpuKernelBase , mrg_uniform_base ) : <EOL> _f16_ok = True <EOL> def get_params ( self , node ) : <EOL> return node . inputs [ <NUM_LIT:0> ] . type . context <EOL> @ classmethod <EOL> def new ( cls , rstate , ndim , dtype , size ) : <EOL> v_size = as_tensor_variable ( size ) <EOL> if ndim is None : <EOL> ndim = get_vector_length ( v_size ) <EOL> op = cls ( GpuArrayType ( dtype , ( False , ) * ndim ) ) <EOL> return op ( rstate , v_size ) <EOL> def c_headers ( self ) : <EOL> return super ( GPUA_mrg_uniform , self ) . c_headers ( ) + [ '<STR_LIT>' ] <EOL> def gpu_kernels ( self , node , name ) : <EOL> write = write_w ( self . output_type . dtype ) <EOL> if self . output_type . dtype == '<STR_LIT>' : <EOL> otype = '<STR_LIT>' <EOL> mask = '<STR_LIT>' <EOL> NORM = '<STR_LIT>' <EOL> elif self . output_type . dtype == '<STR_LIT>' : <EOL> otype = '<STR_LIT:float>' <EOL> mask = '<STR_LIT>' <EOL> NORM = '<STR_LIT>' <EOL> elif self . output_type . dtype == '<STR_LIT>' : <EOL> otype = '<STR_LIT>' <EOL> mask = '<STR_LIT>' <EOL> NORM = '<STR_LIT>' <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' , <EOL> self . output_type . dtype ) <EOL> code = """<STR_LIT>""" % locals ( ) <EOL> from pygpu import gpuarray <EOL> return [ Kernel ( code = code , name = "<STR_LIT>" , <EOL> params = [ gpuarray . GpuArray , gpuarray . GpuArray , <EOL> '<STR_LIT>' , '<STR_LIT>' ] , <EOL> flags = Kernel . get_flags ( self . output_type . dtype , '<STR_LIT>' ) ) <EOL> ] <EOL> def c_code ( self , node , nodename , inp , out , sub ) : <EOL> rstate , size = inp <EOL> o_rstate , o_sample = out <EOL> inplace = int ( self . inplace ) <EOL> ndim = self . output_type . ndim <EOL> o_type_num = numpy . asarray ( <NUM_LIT:0> , dtype = self . output_type . dtype ) . dtype . num <EOL> fail = sub [ '<STR_LIT>' ] <EOL> ctx = sub [ '<STR_LIT>' ] <EOL> kname = self . gpu_kernels ( node , nodename ) [ <NUM_LIT:0> ] . objvar <EOL> otypecode = str ( self . output_type . typecode ) <EOL> return """<STR_LIT>""" % locals ( ) <EOL> def c_code_cache_version ( self ) : <EOL> return ( <NUM_LIT:11> , ) <EOL> def guess_n_streams ( size , warn = False ) : <EOL> """<STR_LIT>""" <EOL> if ( isinstance ( size , ( tuple , list ) ) and <EOL> all ( [ isinstance ( i , integer_types ) for i in size ] ) ) : <EOL> r = <NUM_LIT:1> <EOL> for s in size : <EOL> r *= s <EOL> if r > <NUM_LIT:6> : <EOL> r = r // <NUM_LIT:6> <EOL> return min ( r , <NUM_LIT> * <NUM_LIT> ) <EOL> else : <EOL> if warn : <EOL> warnings . warn ( <EOL> ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % str ( size ) , <EOL> stacklevel = <NUM_LIT:3> ) <EOL> return <NUM_LIT> * <NUM_LIT> <EOL> class MRG_RandomStreams ( object ) : <EOL> """<STR_LIT>""" <EOL> def updates ( self ) : <EOL> return list ( self . state_updates ) <EOL> def __init__ ( self , seed = <NUM_LIT> , use_cuda = None ) : <EOL> self . state_updates = [ ] <EOL> super ( MRG_RandomStreams , self ) . __init__ ( ) <EOL> self . default_instance_seed = seed <EOL> self . set_rstate ( seed ) <EOL> if use_cuda is None : <EOL> self . use_cuda = theano . sandbox . cuda . cuda_enabled <EOL> else : <EOL> self . use_cuda = use_cuda <EOL> def set_rstate ( self , seed ) : <EOL> if isinstance ( seed , integer_types ) : <EOL> if seed == <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' , seed ) <EOL> elif seed >= M2 : <EOL> raise ValueError ( '<STR_LIT>' % M2 , seed ) <EOL> self . rstate = numpy . asarray ( [ seed ] * <NUM_LIT:6> , dtype = '<STR_LIT>' ) <EOL> elif len ( seed ) == <NUM_LIT:6> : <EOL> if seed [ <NUM_LIT:0> ] == <NUM_LIT:0> and seed [ <NUM_LIT:1> ] == <NUM_LIT:0> and seed [ <NUM_LIT:2> ] == <NUM_LIT:0> : <EOL> raise ValueError ( <EOL> '<STR_LIT>' , seed ) <EOL> if seed [ <NUM_LIT:3> ] == <NUM_LIT:0> and seed [ <NUM_LIT:4> ] == <NUM_LIT:0> and seed [ <NUM_LIT:5> ] == <NUM_LIT:0> : <EOL> raise ValueError ( <EOL> '<STR_LIT>' , seed ) <EOL> if seed [ <NUM_LIT:0> ] >= M1 or seed [ <NUM_LIT:1> ] >= M1 or seed [ <NUM_LIT:2> ] >= M1 : <EOL> raise ValueError ( <EOL> '<STR_LIT>' % M1 , <EOL> seed ) <EOL> if seed [ <NUM_LIT:3> ] >= M2 or seed [ <NUM_LIT:4> ] >= M2 or seed [ <NUM_LIT:5> ] >= M2 : <EOL> raise ValueError ( <EOL> '<STR_LIT>' % M2 , <EOL> seed ) <EOL> self . rstate = numpy . asarray ( seed , dtype = '<STR_LIT>' ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> def seed ( self , seed = None ) : <EOL> """<STR_LIT>""" <EOL> if seed is None : <EOL> seed = self . default_instance_seed <EOL> self . set_rstate ( seed ) <EOL> for old_r , new_r , size , nstreams in self . state_updates : <EOL> if nstreams is None : <EOL> nstreams = self . n_streams ( size ) <EOL> rstates = self . get_substream_rstates ( nstreams , <EOL> new_r . owner . outputs [ <NUM_LIT:1> ] . dtype ) <EOL> assert ( old_r . get_value ( borrow = True , <EOL> return_internal_type = True ) . shape == <EOL> rstates . shape ) <EOL> assert rstates . dtype == old_r . dtype <EOL> old_r . set_value ( rstates , borrow = True ) <EOL> def inc_rstate ( self ) : <EOL> """<STR_LIT>""" <EOL> self . rstate = multMatVect ( self . rstate , A1p134 , M1 , A2p134 , M2 ) <EOL> assert self . rstate . dtype == numpy . int32 <EOL> @ theano . configparser . change_flags ( compute_test_value = '<STR_LIT>' ) <EOL> def get_substream_rstates ( self , n_streams , dtype , inc_rstate = True ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( dtype , str ) <EOL> assert n_streams < <NUM_LIT:2> ** <NUM_LIT> <EOL> assert n_streams > <NUM_LIT:0> <EOL> rval = numpy . zeros ( ( n_streams , <NUM_LIT:6> ) , dtype = '<STR_LIT>' ) <EOL> rval [ <NUM_LIT:0> ] = self . rstate <EOL> if multMatVect . dot_modulo is None : <EOL> multMatVect ( rval [ <NUM_LIT:0> ] , A1p72 , M1 , A2p72 , M2 ) <EOL> f = multMatVect . dot_modulo <EOL> f . input_storage [ <NUM_LIT:0> ] . storage [ <NUM_LIT:0> ] = A1p72 <EOL> f . input_storage [ <NUM_LIT:2> ] . storage [ <NUM_LIT:0> ] = M1 <EOL> f . input_storage [ <NUM_LIT:3> ] . storage [ <NUM_LIT:0> ] = A2p72 <EOL> f . input_storage [ <NUM_LIT:5> ] . storage [ <NUM_LIT:0> ] = M2 <EOL> for i in xrange ( <NUM_LIT:1> , n_streams ) : <EOL> v = rval [ i - <NUM_LIT:1> ] <EOL> f . input_storage [ <NUM_LIT:1> ] . storage [ <NUM_LIT:0> ] = v [ : <NUM_LIT:3> ] <EOL> f . input_storage [ <NUM_LIT:4> ] . storage [ <NUM_LIT:0> ] = v [ <NUM_LIT:3> : ] <EOL> f . fn ( ) <EOL> rval [ i ] = f . output_storage [ <NUM_LIT:0> ] . storage [ <NUM_LIT:0> ] <EOL> if inc_rstate : <EOL> self . inc_rstate ( ) <EOL> if self . use_cuda and dtype == '<STR_LIT>' : <EOL> rval = rval . flatten ( ) <EOL> tmp_float_buf = numpy . frombuffer ( rval . data , dtype = '<STR_LIT>' ) <EOL> assert tmp_float_buf . shape == rval . shape <EOL> assert ( tmp_float_buf . view ( '<STR_LIT>' ) == rval ) . all ( ) <EOL> rval = tmp_float_buf <EOL> return rval <EOL> def n_streams ( self , size ) : <EOL> return guess_n_streams ( size ) <EOL> def pretty_return ( self , node_rstate , new_rstate , sample , size , nstreams ) : <EOL> sample . rstate = node_rstate <EOL> sample . update = ( node_rstate , new_rstate ) <EOL> self . state_updates . append ( ( node_rstate , new_rstate , size , nstreams ) ) <EOL> node_rstate . default_update = new_rstate <EOL> return sample <EOL> def uniform ( self , size , low = <NUM_LIT:0.0> , high = <NUM_LIT:1.0> , ndim = None , dtype = None , <EOL> nstreams = None ) : <EOL> """<STR_LIT>""" <EOL> low = as_tensor_variable ( low ) <EOL> high = as_tensor_variable ( high ) <EOL> if dtype is None : <EOL> dtype = scal . upcast ( config . floatX , low . dtype , high . dtype ) <EOL> low = cast ( low , dtype = dtype ) <EOL> high = cast ( high , dtype = dtype ) <EOL> if isinstance ( size , tuple ) : <EOL> msg = "<STR_LIT>" <EOL> assert all ( [ isinstance ( i , ( numpy . integer , integer_types , Variable ) ) <EOL> for i in size ] ) , msg <EOL> if any ( [ isinstance ( i , ( numpy . integer , integer_types ) ) and i <= <NUM_LIT:0> <EOL> for i in size ] ) : <EOL> raise ValueError ( <EOL> "<STR_LIT>" , <EOL> size ) <EOL> else : <EOL> if not ( isinstance ( size , Variable ) and size . ndim == <NUM_LIT:1> ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" + str ( size ) + <EOL> "<STR_LIT>" + str ( type ( size ) ) ) <EOL> orig_nstreams = nstreams <EOL> if nstreams is None : <EOL> nstreams = self . n_streams ( size ) <EOL> rstates = self . get_substream_rstates ( nstreams , dtype ) <EOL> if self . use_cuda and dtype == '<STR_LIT>' : <EOL> node_rstate = float32_shared_constructor ( rstates ) <EOL> assert isinstance ( node_rstate . type , CudaNdarrayType ) <EOL> u = self . pretty_return ( node_rstate , <EOL> * GPU_mrg_uniform . new ( node_rstate , <EOL> ndim , dtype , size ) , <EOL> size = size , nstreams = orig_nstreams ) <EOL> else : <EOL> node_rstate = shared ( rstates ) <EOL> u = self . pretty_return ( node_rstate , <EOL> * mrg_uniform . new ( node_rstate , <EOL> ndim , dtype , size ) , <EOL> size = size , nstreams = orig_nstreams ) <EOL> node_rstate . tag . is_rng = True <EOL> r = u * ( high - low ) + low <EOL> if u . type . broadcastable != r . type . broadcastable : <EOL> raise NotImplementedError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> assert r . dtype == dtype <EOL> return r <EOL> def binomial ( self , size = None , n = <NUM_LIT:1> , p = <NUM_LIT:0.5> , ndim = None , dtype = '<STR_LIT>' , <EOL> nstreams = None ) : <EOL> if n == <NUM_LIT:1> : <EOL> if dtype == '<STR_LIT>' and self . use_cuda : <EOL> x = self . uniform ( size = size , dtype = dtype , nstreams = nstreams ) <EOL> else : <EOL> x = self . uniform ( size = size , nstreams = nstreams ) <EOL> return cast ( x < p , dtype ) <EOL> else : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def multinomial ( self , size = None , n = <NUM_LIT:1> , pvals = None , ndim = None , dtype = '<STR_LIT>' , <EOL> nstreams = None ) : <EOL> """<STR_LIT>""" <EOL> if pvals is None : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> pvals = as_tensor_variable ( pvals ) <EOL> if size is not None : <EOL> if any ( [ isinstance ( i , integer_types ) and i <= <NUM_LIT:0> for i in size ] ) : <EOL> raise ValueError ( <EOL> "<STR_LIT>" , <EOL> size ) <EOL> if size is not None : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if ndim is not None : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if pvals . ndim == <NUM_LIT:2> : <EOL> size = pvals [ : , <NUM_LIT:0> ] . shape * n <EOL> unis = self . uniform ( size = size , ndim = <NUM_LIT:1> , nstreams = nstreams ) <EOL> op = multinomial . MultinomialFromUniform ( dtype ) <EOL> n_samples = as_tensor_variable ( n ) <EOL> return op ( pvals , unis , n_samples ) <EOL> else : <EOL> raise NotImplementedError ( ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> def multinomial_wo_replacement ( self , size = None , n = <NUM_LIT:1> , pvals = None , <EOL> ndim = None , dtype = '<STR_LIT>' , nstreams = None ) : <EOL> """<STR_LIT>""" <EOL> if pvals is None : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> pvals = as_tensor_variable ( pvals ) <EOL> if size is not None : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if ndim is not None : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if pvals . ndim == <NUM_LIT:2> : <EOL> size = pvals [ : , <NUM_LIT:0> ] . shape * n <EOL> unis = self . uniform ( size = size , ndim = <NUM_LIT:1> , nstreams = nstreams ) <EOL> op = multinomial . MultinomialWOReplacementFromUniform ( dtype ) <EOL> n_samples = as_tensor_variable ( n ) <EOL> return op ( pvals , unis , n_samples ) <EOL> else : <EOL> raise NotImplementedError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def normal ( self , size , avg = <NUM_LIT:0.0> , std = <NUM_LIT:1.0> , ndim = None , <EOL> dtype = None , nstreams = None ) : <EOL> """<STR_LIT>""" <EOL> avg = as_tensor_variable ( avg ) <EOL> std = as_tensor_variable ( std ) <EOL> if dtype is None : <EOL> dtype = scal . upcast ( config . floatX , avg . dtype , std . dtype ) <EOL> avg = cast ( avg , dtype ) <EOL> std = cast ( std , dtype ) <EOL> evened = False <EOL> constant = False <EOL> if ( isinstance ( size , tuple ) and <EOL> all ( [ isinstance ( i , ( numpy . integer , integer_types ) ) for i in size ] ) ) : <EOL> constant = True <EOL> n_samples = numpy . prod ( size , dtype = '<STR_LIT>' ) <EOL> if n_samples % <NUM_LIT:2> == <NUM_LIT:1> : <EOL> n_samples += <NUM_LIT:1> <EOL> evened = True <EOL> else : <EOL> n_samples = prod ( size ) + ( prod ( size ) % <NUM_LIT:2> ) <EOL> flattened = self . uniform ( size = ( n_samples , ) , dtype = dtype , <EOL> nstreams = nstreams ) <EOL> if constant : <EOL> U1 = flattened [ : n_samples // <NUM_LIT:2> ] <EOL> U2 = flattened [ n_samples // <NUM_LIT:2> : ] <EOL> else : <EOL> U1 = flattened [ : prod ( flattened . shape ) // <NUM_LIT:2> ] <EOL> U2 = flattened [ prod ( flattened . shape ) // <NUM_LIT:2> : ] <EOL> sqrt_ln_U1 = sqrt ( - <NUM_LIT> * log ( U1 ) ) <EOL> first_half = sqrt_ln_U1 * cos ( <EOL> numpy . array ( <NUM_LIT> * numpy . pi , dtype = dtype ) * U2 ) <EOL> second_half = sqrt_ln_U1 * sin ( <EOL> numpy . array ( <NUM_LIT> * numpy . pi , dtype = dtype ) * U2 ) <EOL> normal_samples = join ( <NUM_LIT:0> , first_half , second_half ) <EOL> final_samples = None <EOL> if evened : <EOL> final_samples = normal_samples [ : - <NUM_LIT:1> ] <EOL> elif constant : <EOL> final_samples = normal_samples <EOL> else : <EOL> final_samples = normal_samples [ : prod ( size ) ] <EOL> if not size : <EOL> size = tensor . constant ( size , dtype = '<STR_LIT>' ) <EOL> final_samples = final_samples . reshape ( size ) <EOL> final_samples = avg + std * final_samples <EOL> assert final_samples . dtype == dtype <EOL> return final_samples <EOL> @ register_gpua ( '<STR_LIT>' ) <EOL> @ local_optimizer ( [ mrg_uniform ] ) <EOL> def local_gpua_mrg ( node ) : <EOL> if ( type ( node . op ) == mrg_uniform and <EOL> isinstance ( node . inputs [ <NUM_LIT:0> ] . type , GpuArrayType ) ) : <EOL> outs = GPUA_mrg_uniform . new ( node . inputs [ <NUM_LIT:0> ] , <EOL> node . op . output_type . ndim , <EOL> node . op . output_type . dtype , <EOL> node . inputs [ <NUM_LIT:1> ] ) <EOL> return [ outs [ <NUM_LIT:0> ] , host_from_gpua ( outs [ <NUM_LIT:1> ] ) ] <EOL> MRG_RNGs = ( mrg_uniform , GPU_mrg_uniform , GPUA_mrg_uniform ) <EOL> @ local_optimizer ( MRG_RNGs ) <EOL> def mrg_random_make_inplace ( node ) : <EOL> op = node . op <EOL> if isinstance ( op , MRG_RNGs ) and not op . inplace : <EOL> new_op = op . __class__ ( op . output_type , inplace = True ) <EOL> return new_op . make_node ( * node . inputs ) . outputs <EOL> return False <EOL> optdb . register ( '<STR_LIT>' , <EOL> opt . in2out ( mrg_random_make_inplace , ignore_newtrees = True ) , <EOL> <NUM_LIT> , '<STR_LIT>' , '<STR_LIT>' ) </s>
