<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , print_function , division <EOL> from itertools import chain <EOL> import numpy <EOL> import theano <EOL> from six . moves import xrange <EOL> from theano . compat import izip <EOL> from theano . gof import Op , Apply , local_optimizer , EquilibriumDB <EOL> from theano . gof . utils import hash_from_dict <EOL> from theano . sandbox . cuda import GpuElemwise , CudaNdarrayType , GpuOp <EOL> from theano . sandbox . cuda . basic_ops import ( as_cuda_ndarray_variable , <EOL> gpu_contiguous ) <EOL> from theano . sandbox . cuda . opt import gpu_seqopt <EOL> import pycuda <EOL> from pycuda . compiler import SourceModule <EOL> import pycuda . gpuarray <EOL> from . import pycuda_init <EOL> if not pycuda_init . pycuda_available : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def _replace_npy_types ( c_arg ) : <EOL> c_arg = c_arg . replace ( '<STR_LIT>' , '<STR_LIT:float>' ) <EOL> c_arg = c_arg . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> c_arg = c_arg . replace ( '<STR_LIT>' , '<STR_LIT:int>' ) <EOL> c_arg = c_arg . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> c_arg = c_arg . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> c_arg = c_arg . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> c_arg = c_arg . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> c_arg = c_arg . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return c_arg <EOL> def theano_parse_c_arg ( c_arg ) : <EOL> c_arg = _replace_npy_types ( c_arg ) <EOL> return pycuda . tools . parse_c_arg ( c_arg ) <EOL> """<STR_LIT>""" <EOL> class PycudaElemwiseSourceModuleOp ( GpuOp ) : <EOL> nin = property ( lambda self : self . scalar_op . nin ) <EOL> nout = property ( lambda self : self . scalar_op . nout ) <EOL> def __init__ ( self , scalar_op , inplace_pattern = None , name = None ) : <EOL> if inplace_pattern is None : <EOL> inplace_pattern = { } <EOL> self . name = name <EOL> self . scalar_op = scalar_op <EOL> self . inplace_pattern = inplace_pattern <EOL> def __str__ ( self ) : <EOL> if self . name is None : <EOL> if self . inplace_pattern : <EOL> items = list ( self . inplace_pattern . items ( ) ) <EOL> items . sort ( ) <EOL> return self . __class__ . __name__ + "<STR_LIT>" % ( self . scalar_op , <EOL> str ( items ) ) <EOL> else : <EOL> return self . __class__ . __name__ + "<STR_LIT>" % ( self . scalar_op ) <EOL> else : <EOL> return self . name <EOL> def __eq__ ( self , other ) : <EOL> return ( type ( self ) == type ( other ) and <EOL> self . scalar_op == other . scalar_op and <EOL> self . inplace_pattern == other . inplace_pattern ) <EOL> def __hash__ ( self ) : <EOL> return ( hash ( type ( self ) ) ^ hash ( self . scalar_op ) ^ <EOL> hash_from_dict ( self . inplace_pattern ) ) <EOL> def make_node ( self , * inputs ) : <EOL> _inputs = [ gpu_contiguous ( as_cuda_ndarray_variable ( i ) ) for i in inputs ] <EOL> if self . nin > <NUM_LIT:0> and len ( _inputs ) != self . nin : <EOL> raise TypeError ( '<STR_LIT>' , ( self . nin , len ( _inputs ) ) ) <EOL> for i in _inputs [ <NUM_LIT:1> : ] : <EOL> if i . type . ndim != inputs [ <NUM_LIT:0> ] . type . ndim : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if any ( [ any ( i . type . broadcastable ) for i in inputs ] ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> assert len ( inputs ) == <NUM_LIT:2> <EOL> otype = CudaNdarrayType ( broadcastable = [ False ] * _inputs [ <NUM_LIT:0> ] . type . ndim ) <EOL> assert self . nout == <NUM_LIT:1> <EOL> fct_name = "<STR_LIT>" % str ( self . scalar_op ) <EOL> out_node = Apply ( self , _inputs , [ otype ( ) for o in xrange ( self . nout ) ] ) <EOL> in_name = [ "<STR_LIT:i>" + str ( id ) for id in range ( len ( inputs ) ) ] <EOL> out_name = [ "<STR_LIT:o>" + str ( id ) for id in range ( self . nout ) ] <EOL> c_code = self . scalar_op . c_code ( out_node , "<STR_LIT>" , <EOL> tuple ( [ n + "<STR_LIT>" for n in in_name ] ) , <EOL> tuple ( n + "<STR_LIT>" for n in out_name ) , { } ) <EOL> c_code_param = "<STR_LIT:U+002CU+0020>" . join ( <EOL> [ _replace_npy_types ( var . type . dtype_specs ( ) [ <NUM_LIT:1> ] ) + "<STR_LIT>" + name <EOL> for var , name in chain ( izip ( inputs , in_name ) , <EOL> izip ( out_node . outputs , out_name ) ) ] + <EOL> [ "<STR_LIT>" ] ) <EOL> mod = SourceModule ( """<STR_LIT>""" % ( fct_name , c_code_param , c_code ) ) <EOL> self . pycuda_fct = mod . get_function ( fct_name ) <EOL> return out_node <EOL> def perform ( self , node , inputs , out ) : <EOL> z , = out <EOL> if ( z [ <NUM_LIT:0> ] is None or <EOL> z [ <NUM_LIT:0> ] . shape != inputs [ <NUM_LIT:0> ] . shape or <EOL> not z [ <NUM_LIT:0> ] . is_c_contiguous ( ) ) : <EOL> z [ <NUM_LIT:0> ] = theano . sandbox . cuda . CudaNdarray . zeros ( inputs [ <NUM_LIT:0> ] . shape ) <EOL> if inputs [ <NUM_LIT:0> ] . shape != inputs [ <NUM_LIT:1> ] . shape : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if inputs [ <NUM_LIT:0> ] . size > <NUM_LIT> : <EOL> grid = ( int ( numpy . ceil ( inputs [ <NUM_LIT:0> ] . size / <NUM_LIT> ) ) , <NUM_LIT:1> ) <EOL> block = ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> else : <EOL> grid = ( <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> block = ( inputs [ <NUM_LIT:0> ] . shape [ <NUM_LIT:0> ] , inputs [ <NUM_LIT:0> ] . shape [ <NUM_LIT:1> ] , <NUM_LIT:1> ) <EOL> self . pycuda_fct ( inputs [ <NUM_LIT:0> ] , inputs [ <NUM_LIT:1> ] , z [ <NUM_LIT:0> ] , <EOL> numpy . intc ( inputs [ <NUM_LIT:1> ] . size ) , block = block , grid = grid ) <EOL> class PycudaElemwiseSourceModuleMakeThunkOp ( Op ) : <EOL> nin = property ( lambda self : self . scalar_op . nin ) <EOL> nout = property ( lambda self : self . scalar_op . nout ) <EOL> __props__ = ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def __init__ ( self , scalar_op , inplace_pattern = None , name = None ) : <EOL> if inplace_pattern is None : <EOL> inplace_pattern = { } <EOL> self . name = name <EOL> self . scalar_op = scalar_op <EOL> self . inplace_pattern = inplace_pattern <EOL> def __hash__ ( self ) : <EOL> return hash ( ( type ( self ) , hash ( self . scalar_op ) , <EOL> hash_from_dict ( self . inplace_pattern ) ) ) <EOL> def __str__ ( self ) : <EOL> if self . name is None : <EOL> if self . inplace_pattern : <EOL> items = list ( self . inplace_pattern . items ( ) ) <EOL> items . sort ( ) <EOL> return self . __class__ . __name__ + "<STR_LIT>" % ( self . scalar_op , <EOL> str ( items ) ) <EOL> else : <EOL> return self . __class__ . __name__ + "<STR_LIT>" % ( self . scalar_op ) <EOL> else : <EOL> return self . name <EOL> def make_node ( self , * inputs ) : <EOL> assert self . nout == <NUM_LIT:1> <EOL> assert len ( inputs ) == <NUM_LIT:2> <EOL> _inputs = [ gpu_contiguous ( as_cuda_ndarray_variable ( i ) ) for i in inputs ] <EOL> if self . nin > <NUM_LIT:0> and len ( _inputs ) != self . nin : <EOL> raise TypeError ( '<STR_LIT>' , ( self . nin , len ( _inputs ) ) ) <EOL> for i in _inputs [ <NUM_LIT:1> : ] : <EOL> if i . type . ndim != inputs [ <NUM_LIT:0> ] . type . ndim : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if any ( [ any ( i . type . broadcastable ) for i in inputs ] ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> otype = CudaNdarrayType ( broadcastable = [ False ] * _inputs [ <NUM_LIT:0> ] . type . ndim ) <EOL> out_node = Apply ( self , _inputs , [ otype ( ) for o in xrange ( self . nout ) ] ) <EOL> return out_node <EOL> def make_thunk ( self , node , storage_map , _ , _2 ) : <EOL> fct_name = "<STR_LIT>" % str ( self . scalar_op ) <EOL> in_name = [ "<STR_LIT:i>" + str ( id ) for id in range ( len ( node . inputs ) ) ] <EOL> out_name = [ "<STR_LIT:o>" + str ( id ) for id in range ( self . nout ) ] <EOL> c_code = self . scalar_op . c_code ( node , "<STR_LIT>" , <EOL> tuple ( [ n + "<STR_LIT>" for n in in_name ] ) , <EOL> tuple ( n + "<STR_LIT>" for n in out_name ) , { } ) <EOL> c_code_param = "<STR_LIT:U+002CU+0020>" . join ( <EOL> [ _replace_npy_types ( var . type . dtype_specs ( ) [ <NUM_LIT:1> ] ) + "<STR_LIT>" + name <EOL> for var , name in chain ( izip ( node . inputs , in_name ) , <EOL> izip ( node . outputs , out_name ) ) ] + <EOL> [ "<STR_LIT>" ] ) <EOL> mod = SourceModule ( """<STR_LIT>""" % ( fct_name , c_code_param , c_code ) ) <EOL> pycuda_fct = mod . get_function ( fct_name ) <EOL> inputs = [ storage_map [ v ] for v in node . inputs ] <EOL> outputs = [ storage_map [ v ] for v in node . outputs ] <EOL> def thunk ( ) : <EOL> z = outputs [ <NUM_LIT:0> ] <EOL> if ( z [ <NUM_LIT:0> ] is None or <EOL> z [ <NUM_LIT:0> ] . shape != inputs [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . shape or <EOL> not z [ <NUM_LIT:0> ] . is_c_contiguous ( ) ) : <EOL> z [ <NUM_LIT:0> ] = theano . sandbox . cuda . CudaNdarray . zeros ( <EOL> inputs [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . shape ) <EOL> if inputs [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . shape != inputs [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] . shape : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if inputs [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . size > <NUM_LIT> : <EOL> grid = ( int ( numpy . ceil ( inputs [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . size / <NUM_LIT> ) ) , <NUM_LIT:1> ) <EOL> block = ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> else : <EOL> grid = ( <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> block = ( inputs [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . shape [ <NUM_LIT:0> ] , inputs [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . shape [ <NUM_LIT:1> ] , <NUM_LIT:1> ) <EOL> pycuda_fct ( inputs [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , inputs [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] , z [ <NUM_LIT:0> ] , <EOL> numpy . intc ( inputs [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] . size ) , block = block , <EOL> grid = grid ) <EOL> thunk . inputs = inputs <EOL> thunk . outputs = outputs <EOL> thunk . lazy = False <EOL> return thunk <EOL> pycuda_optimizer = EquilibriumDB ( ) <EOL> gpu_seqopt . register ( "<STR_LIT>" , pycuda_optimizer , <NUM_LIT> , "<STR_LIT>" ) <EOL> @ local_optimizer ( [ GpuElemwise ] ) <EOL> def local_pycuda_gpu_elemwise ( node ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( node . op , GpuElemwise ) : <EOL> if ( not any ( [ any ( i . type . broadcastable ) for i in node . inputs ] ) and <EOL> all ( [ i . ndim <= <NUM_LIT:2> for i in node . inputs ] ) ) : <EOL> new_op = PycudaElemwiseSourceModuleOp ( node . op . scalar_op , <EOL> node . op . inplace_pattern ) ( <EOL> * node . inputs ) <EOL> return [ new_op ] <EOL> pycuda_optimizer . register ( "<STR_LIT>" , <EOL> local_pycuda_gpu_elemwise ) <EOL> """<STR_LIT>""" </s>
