<s> """<STR_LIT>""" <EOL> import warnings <EOL> from theano . gof import Apply <EOL> from theano . sandbox . cuda import CudaNdarrayType <EOL> from theano . sandbox . cuda . basic_ops import as_cuda_ndarray_variable <EOL> from theano . sandbox . cuda . basic_ops import gpu_contiguous <EOL> from theano . sandbox . cuda import GpuOp <EOL> from theano . tensor import get_scalar_constant_value , NotScalarConstantError <EOL> from pylearn2 . sandbox . cuda_convnet . base_acts import UnimplementedError <EOL> from pylearn2 . sandbox . cuda_convnet . convnet_compile import convnet_available <EOL> from pylearn2 . sandbox . cuda_convnet . convnet_compile import cuda_convnet_loc <EOL> from pylearn2 . sandbox . cuda_convnet . shared_code import this_dir <EOL> import pylearn2 . sandbox . cuda_convnet . pthreads <EOL> from theano import config <EOL> def max_pool_c01b ( c01b , pool_shape , pool_stride , image_shape = None , start = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> assert pool_shape [ <NUM_LIT:0> ] == pool_shape [ <NUM_LIT:1> ] <EOL> assert pool_shape [ <NUM_LIT:0> ] > <NUM_LIT:0> <EOL> assert pool_stride [ <NUM_LIT:0> ] > <NUM_LIT:0> <EOL> assert pool_stride [ <NUM_LIT:0> ] <= pool_shape [ <NUM_LIT:0> ] <EOL> if pool_stride [ <NUM_LIT:0> ] != pool_stride [ <NUM_LIT:1> ] : <EOL> raise ValueError ( "<STR_LIT>" + str ( pool_stride ) ) <EOL> if image_shape is not None : <EOL> warnings . warn ( "<STR_LIT>" ) <EOL> op = MaxPool ( pool_shape [ <NUM_LIT:0> ] , pool_stride [ <NUM_LIT:0> ] , start ) <EOL> c01b = gpu_contiguous ( c01b ) <EOL> return op ( c01b ) <EOL> class MaxPool ( GpuOp ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ds , stride , start = <NUM_LIT:0> , outputs = <NUM_LIT:0> ) : <EOL> self . ds = ds <EOL> self . stride = stride <EOL> self . start = start <EOL> self . copy_non_contiguous = <NUM_LIT:0> <EOL> assert stride > <NUM_LIT:0> and stride <= ds , ( stride , ds ) <EOL> assert ds > <NUM_LIT:0> , ds <EOL> def __eq__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> return ( type ( self ) == type ( other ) and <EOL> self . ds == other . ds and <EOL> self . stride == other . stride and <EOL> self . start == other . start ) <EOL> def __hash__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( hash ( type ( self ) ) ^ hash ( self . ds ) ^ <EOL> hash ( self . stride ) ^ hash ( self . start ) ) <EOL> def c_header_dirs ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ this_dir , config . pthreads . inc_dir ] if config . pthreads . inc_dir else [ this_dir ] <EOL> def c_headers ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def c_lib_dirs ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ cuda_convnet_loc , config . pthreads . lib_dir ] if config . pthreads . lib_dir else [ cuda_convnet_loc ] <EOL> def c_libraries ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ '<STR_LIT>' , config . pthreads . lib ] if config . pthreads . lib else [ '<STR_LIT>' ] <EOL> def c_code_cache_version ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( <NUM_LIT:1> , ) <EOL> def _argument_contiguity_check ( self , arg_name ) : <EOL> """<STR_LIT>""" <EOL> return """<STR_LIT>""" % { <EOL> '<STR_LIT:class>' : self . __class__ . __name__ , <EOL> '<STR_LIT>' : arg_name , <EOL> '<STR_LIT>' : self . __class__ . __name__ . upper ( ) , <EOL> } <EOL> def make_node ( self , images ) : <EOL> """<STR_LIT>""" <EOL> images = as_cuda_ndarray_variable ( images ) <EOL> assert images . ndim == <NUM_LIT:4> <EOL> channels_broadcastable = images . type . broadcastable [ <NUM_LIT:0> ] <EOL> batch_broadcastable = images . type . broadcastable [ <NUM_LIT:3> ] <EOL> rows_broadcastable = False <EOL> cols_broadcastable = False <EOL> targets_broadcastable = ( channels_broadcastable , rows_broadcastable , <EOL> cols_broadcastable , batch_broadcastable ) <EOL> targets_type = CudaNdarrayType ( broadcastable = targets_broadcastable ) <EOL> targets = targets_type ( ) <EOL> return Apply ( self , [ images ] , [ targets ] ) <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> """<STR_LIT>""" <EOL> images , = inputs <EOL> targets , = outputs <EOL> fail = sub [ '<STR_LIT>' ] <EOL> num_braces = <NUM_LIT:0> <EOL> if self . copy_non_contiguous : <EOL> raise UnimplementedError ( ) <EOL> else : <EOL> basic_setup = "<STR_LIT>" <EOL> setup_nv_images = self . _argument_contiguity_check ( "<STR_LIT>" ) + """<STR_LIT>""" <EOL> num_braces += <NUM_LIT:1> <EOL> setup_nv_targets = """<STR_LIT>""" <EOL> num_braces += <NUM_LIT:1> <EOL> do_pool = """<STR_LIT>""" <EOL> braces = '<STR_LIT:}>' * num_braces <EOL> rval = ( basic_setup + <EOL> setup_nv_images + <EOL> setup_nv_targets + <EOL> do_pool + <EOL> braces ) <EOL> start = self . start <EOL> stride = self . stride <EOL> ds = self . ds <EOL> rval = rval % locals ( ) <EOL> return rval <EOL> def R_op ( self , inp , evals ) : <EOL> """<STR_LIT>""" <EOL> x , = inp <EOL> ev , = evals <EOL> if ev is not None : <EOL> ev = gpu_contiguous ( ev ) <EOL> return [ MaxPoolRop ( self . ds , self . stride , self . start ) ( x , ev ) ] <EOL> else : <EOL> return [ None ] <EOL> def grad ( self , inp , grads ) : <EOL> """<STR_LIT>""" <EOL> x , = inp <EOL> gz , = grads <EOL> gz = gpu_contiguous ( gz ) <EOL> maxout = self ( x ) <EOL> return [ MaxPoolGrad ( self . ds , self . stride , self . start ) ( x , maxout , gz ) ] <EOL> def make_thunk ( self , node , storage_map , compute_map , no_recycling ) : <EOL> """<STR_LIT>""" <EOL> if not convnet_available ( ) : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> return super ( MaxPool , self ) . make_thunk ( <EOL> node , storage_map , compute_map , no_recycling ) <EOL> class MaxPoolRop ( GpuOp ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ds , stride , start = <NUM_LIT:0> , outputs = <NUM_LIT:0> ) : <EOL> self . ds = ds <EOL> self . stride = stride <EOL> self . start = start <EOL> self . copy_non_contiguous = <NUM_LIT:0> <EOL> assert stride > <NUM_LIT:0> and stride <= ds , ( stride , ds ) <EOL> assert ds > <NUM_LIT:0> , ds <EOL> def __eq__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> return ( type ( self ) == type ( other ) and <EOL> self . ds == other . ds and <EOL> self . stride == other . stride and <EOL> self . start == other . start ) <EOL> def __hash__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( hash ( type ( self ) ) ^ hash ( self . ds ) ^ <EOL> hash ( self . stride ) ^ hash ( self . start ) ) <EOL> def c_header_dirs ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ this_dir ] <EOL> def c_headers ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def c_lib_dirs ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ cuda_convnet_loc ] <EOL> def c_libraries ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ '<STR_LIT>' ] <EOL> def c_code_cache_version ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( <NUM_LIT:1> , ) <EOL> def _argument_contiguity_check ( self , arg_name ) : <EOL> """<STR_LIT>""" <EOL> return """<STR_LIT>""" % { <EOL> '<STR_LIT:class>' : self . __class__ . __name__ , <EOL> '<STR_LIT>' : arg_name , <EOL> '<STR_LIT>' : self . __class__ . __name__ . upper ( ) , <EOL> } <EOL> def make_node ( self , images , evals ) : <EOL> """<STR_LIT>""" <EOL> images = as_cuda_ndarray_variable ( images ) <EOL> evals = as_cuda_ndarray_variable ( evals ) <EOL> assert images . ndim == <NUM_LIT:4> <EOL> assert evals . ndim == <NUM_LIT:4> <EOL> channels_broadcastable = images . type . broadcastable [ <NUM_LIT:0> ] <EOL> batch_broadcastable = images . type . broadcastable [ <NUM_LIT:3> ] <EOL> rows_broadcastable = False <EOL> cols_broadcastable = False <EOL> targets_broadcastable = ( channels_broadcastable , rows_broadcastable , <EOL> cols_broadcastable , batch_broadcastable ) <EOL> targets_type = CudaNdarrayType ( broadcastable = targets_broadcastable ) <EOL> targets = targets_type ( ) <EOL> return Apply ( self , [ images , evals ] , [ targets ] ) <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> """<STR_LIT>""" <EOL> images , evals = inputs <EOL> targets , = outputs <EOL> fail = sub [ '<STR_LIT>' ] <EOL> num_braces = <NUM_LIT:0> <EOL> if self . copy_non_contiguous : <EOL> raise UnimplementedError ( ) <EOL> else : <EOL> basic_setup = "<STR_LIT>" <EOL> setup_nv_images = self . _argument_contiguity_check ( "<STR_LIT>" ) + """<STR_LIT>""" <EOL> num_braces += <NUM_LIT:1> <EOL> setup_nv_targets = """<STR_LIT>""" <EOL> num_braces += <NUM_LIT:1> <EOL> do_pool = """<STR_LIT>""" <EOL> braces = '<STR_LIT:}>' * num_braces <EOL> rval = ( basic_setup + <EOL> setup_nv_images + <EOL> setup_nv_targets + <EOL> do_pool + <EOL> braces ) <EOL> start = self . start <EOL> stride = self . stride <EOL> ds = self . ds <EOL> rval = rval % locals ( ) <EOL> return rval <EOL> def make_thunk ( self , node , storage_map , compute_map , no_recycling ) : <EOL> """<STR_LIT>""" <EOL> if not convnet_available ( ) : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> return super ( MaxPoolRop , self ) . make_thunk ( <EOL> node , storage_map , storage_map , no_recycling ) <EOL> class MaxPoolGrad ( GpuOp ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ds , stride , start ) : <EOL> self . ds = ds <EOL> self . stride = stride <EOL> self . start = start <EOL> self . copy_non_contiguous = <NUM_LIT:0> <EOL> assert stride > <NUM_LIT:0> and stride <= ds , ( stride , ds ) <EOL> assert ds > <NUM_LIT:0> , ds <EOL> def __eq__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> return ( type ( self ) == type ( other ) and <EOL> self . ds == other . ds and <EOL> self . stride == other . stride and <EOL> self . start == other . start ) <EOL> def __hash__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( hash ( type ( self ) ) ^ hash ( self . ds ) ^ <EOL> hash ( self . stride ) ^ hash ( self . start ) ) <EOL> def c_header_dirs ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ this_dir , config . pthreads . inc_dir ] if config . pthreads . inc_dir else [ this_dir ] <EOL> def c_headers ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def c_lib_dirs ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ cuda_convnet_loc , config . pthreads . lib_dir ] if config . pthreads . lib_dir else [ cuda_convnet_loc ] <EOL> def c_libraries ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ '<STR_LIT>' , config . pthreads . lib ] if config . pthreads . lib else [ '<STR_LIT>' ] <EOL> def c_code_cache_version ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( <NUM_LIT:1> , ) <EOL> def _argument_contiguity_check ( self , arg_name ) : <EOL> return """<STR_LIT>""" % { <EOL> '<STR_LIT:class>' : self . __class__ . __name__ , <EOL> '<STR_LIT>' : arg_name , <EOL> '<STR_LIT>' : self . __class__ . __name__ . upper ( ) , <EOL> } <EOL> def make_node ( self , images , maxout , gz ) : <EOL> """<STR_LIT>""" <EOL> images = as_cuda_ndarray_variable ( images ) <EOL> maxout = as_cuda_ndarray_variable ( maxout ) <EOL> gz = as_cuda_ndarray_variable ( gz ) <EOL> assert images . ndim == <NUM_LIT:4> <EOL> assert maxout . ndim == <NUM_LIT:4> <EOL> assert gz . ndim == <NUM_LIT:4> <EOL> try : <EOL> nb_channel = int ( get_scalar_constant_value ( images . shape [ <NUM_LIT:0> ] ) ) <EOL> assert nb_channel % <NUM_LIT:16> == <NUM_LIT:0> <EOL> except NotScalarConstantError : <EOL> pass <EOL> return Apply ( self , [ images , maxout , gz ] , [ images . type ( ) ] ) <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> """<STR_LIT>""" <EOL> images , maxout , gz = inputs <EOL> targets , = outputs <EOL> fail = sub [ '<STR_LIT>' ] <EOL> num_braces = <NUM_LIT:0> <EOL> if self . copy_non_contiguous : <EOL> raise UnimplementedError ( ) <EOL> else : <EOL> basic_setup = "<STR_LIT>" <EOL> setup_nv_images = self . _argument_contiguity_check ( "<STR_LIT>" ) + """<STR_LIT>""" <EOL> num_braces += <NUM_LIT:1> <EOL> setup_nv_maxout = self . _argument_contiguity_check ( "<STR_LIT>" ) + """<STR_LIT>""" <EOL> num_braces += <NUM_LIT:1> <EOL> setup_nv_gz = self . _argument_contiguity_check ( "<STR_LIT>" ) + """<STR_LIT>""" <EOL> num_braces += <NUM_LIT:1> <EOL> setup_nv_targets = """<STR_LIT>""" <EOL> num_braces += <NUM_LIT:1> <EOL> undo_pool = """<STR_LIT>""" <EOL> braces = '<STR_LIT:}>' * num_braces <EOL> rval = ( basic_setup + <EOL> setup_nv_images + <EOL> setup_nv_maxout + <EOL> setup_nv_gz + <EOL> setup_nv_targets + <EOL> undo_pool + <EOL> braces ) <EOL> start = self . start <EOL> stride = self . stride <EOL> ds = self . ds <EOL> rval = rval % locals ( ) <EOL> return rval <EOL> def make_thunk ( self , node , storage_map , compute_map , no_recycling ) : <EOL> """<STR_LIT>""" <EOL> if not convnet_available ( ) : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> return super ( MaxPoolGrad , self ) . make_thunk ( <EOL> node , storage_map , compute_map , no_recycling ) </s>
