<s> from __future__ import absolute_import , print_function , division <EOL> import os . path <EOL> from theano import Apply , config , Op <EOL> from theano . compile import optdb <EOL> from theano . gof import LocalOptGroup <EOL> from theano . tensor . basic import as_tensor_variable <EOL> from theano . tensor . opt import in2out <EOL> from . basic_ops import as_gpuarray_variable , infer_context_name <EOL> from . opt_util import inplace_allocempty <EOL> try : <EOL> import pygpu <EOL> from pygpu import blas <EOL> except ImportError as e : <EOL> pass <EOL> class BlasOp ( Op ) : <EOL> def c_headers ( self ) : <EOL> return [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def c_header_dirs ( self ) : <EOL> return [ pygpu . get_include ( ) , os . path . dirname ( __file__ ) ] <EOL> def c_init_code ( self ) : <EOL> return [ '<STR_LIT>' ] <EOL> class GpuGemv ( BlasOp ) : <EOL> """<STR_LIT>""" <EOL> __props__ = ( '<STR_LIT>' , ) <EOL> def __init__ ( self , inplace = False ) : <EOL> self . inplace = inplace <EOL> if self . inplace : <EOL> self . destroy_map = { <NUM_LIT:0> : [ <NUM_LIT:0> ] } <EOL> def make_node ( self , y , alpha , A , x , beta ) : <EOL> ctx_name = infer_context_name ( y , A , x ) <EOL> A = as_gpuarray_variable ( A , ctx_name ) <EOL> x = as_gpuarray_variable ( x , ctx_name ) <EOL> y = as_gpuarray_variable ( y , ctx_name ) <EOL> alpha = as_tensor_variable ( alpha ) <EOL> beta = as_tensor_variable ( beta ) <EOL> assert alpha . ndim == <NUM_LIT:0> <EOL> assert beta . ndim == <NUM_LIT:0> <EOL> assert A . ndim == <NUM_LIT:2> <EOL> assert x . ndim == <NUM_LIT:1> <EOL> assert y . ndim == <NUM_LIT:1> <EOL> assert A . dtype == x . dtype == y . dtype <EOL> return Apply ( self , [ y , alpha , A , x , beta ] , [ y . type ( ) ] ) <EOL> def perform ( self , node , inputs , out_storage ) : <EOL> y , alpha , A , x , beta = inputs <EOL> inplace = self . inplace <EOL> if inplace and y . strides [ <NUM_LIT:0> ] < <NUM_LIT:0> : <EOL> inplace = False <EOL> out_storage [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] = blas . gemv ( alpha , A , x , beta , y , <EOL> overwrite_y = inplace ) <EOL> def c_code ( self , node , name , inp , out , sub ) : <EOL> vars = dict ( out = out [ <NUM_LIT:0> ] , y = inp [ <NUM_LIT:0> ] , alpha = inp [ <NUM_LIT:1> ] , A = inp [ <NUM_LIT:2> ] , x = inp [ <NUM_LIT:3> ] , <EOL> beta = inp [ <NUM_LIT:4> ] , fail = sub [ '<STR_LIT>' ] , name = name ) <EOL> if self . inplace : <EOL> code = """<STR_LIT>""" % vars <EOL> else : <EOL> code = """<STR_LIT>""" % vars <EOL> code += """<STR_LIT>""" % vars <EOL> if config . gpuarray . sync : <EOL> code += """<STR_LIT>""" % vars <EOL> return code <EOL> def c_code_cache_version ( self ) : <EOL> return ( <NUM_LIT:4> , ) <EOL> gpugemv_no_inplace = GpuGemv ( inplace = False ) <EOL> gpugemv_inplace = GpuGemv ( inplace = True ) <EOL> class GpuGemm ( BlasOp ) : <EOL> """<STR_LIT>""" <EOL> __props__ = ( '<STR_LIT>' , ) <EOL> _f16_ok = True <EOL> def __init__ ( self , inplace = False ) : <EOL> self . inplace = inplace <EOL> if self . inplace : <EOL> self . destroy_map = { <NUM_LIT:0> : [ <NUM_LIT:0> ] } <EOL> def make_node ( self , C , alpha , A , B , beta ) : <EOL> ctx_name = infer_context_name ( C , A , B ) <EOL> A = as_gpuarray_variable ( A , ctx_name ) <EOL> B = as_gpuarray_variable ( B , ctx_name ) <EOL> C = as_gpuarray_variable ( C , ctx_name ) <EOL> alpha = as_tensor_variable ( alpha ) <EOL> beta = as_tensor_variable ( beta ) <EOL> assert alpha . ndim == <NUM_LIT:0> <EOL> assert beta . ndim == <NUM_LIT:0> <EOL> assert A . ndim == <NUM_LIT:2> <EOL> assert B . ndim == <NUM_LIT:2> <EOL> assert C . ndim == <NUM_LIT:2> <EOL> assert A . dtype == B . dtype == C . dtype <EOL> return Apply ( self , [ C , alpha , A , B , beta ] , [ C . type ( ) ] ) <EOL> def perform ( self , node , inputs , outputs ) : <EOL> C , alpha , A , B , beta = inputs <EOL> inplace = self . inplace <EOL> if inplace and not C . flags . forc : <EOL> inplace = False <EOL> outputs [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] = blas . gemm ( alpha , A , B , beta , C , <EOL> overwrite_c = inplace ) <EOL> def c_code ( self , node , name , inp , out , sub ) : <EOL> vars = dict ( out = out [ <NUM_LIT:0> ] , C = inp [ <NUM_LIT:0> ] , alpha = inp [ <NUM_LIT:1> ] , A = inp [ <NUM_LIT:2> ] , B = inp [ <NUM_LIT:3> ] , <EOL> beta = inp [ <NUM_LIT:4> ] , fail = sub [ '<STR_LIT>' ] , name = name ) <EOL> if self . inplace : <EOL> code = """<STR_LIT>""" % vars <EOL> else : <EOL> code = """<STR_LIT>""" % vars <EOL> code += """<STR_LIT>""" % vars <EOL> if config . gpuarray . sync : <EOL> code += """<STR_LIT>""" % vars <EOL> return code <EOL> def c_code_cache_version ( self ) : <EOL> return ( <NUM_LIT:5> , ) <EOL> gpugemm_no_inplace = GpuGemm ( inplace = False ) <EOL> gpugemm_inplace = GpuGemm ( inplace = True ) <EOL> class GpuGer ( BlasOp ) : <EOL> """<STR_LIT>""" <EOL> __props__ = ( '<STR_LIT>' , ) <EOL> def __init__ ( self , inplace = False ) : <EOL> self . inplace = inplace <EOL> if self . inplace : <EOL> self . destroy_map = { <NUM_LIT:0> : [ <NUM_LIT:0> ] } <EOL> def make_node ( self , A , alpha , x , y ) : <EOL> ctx_name = infer_context_name ( A , x , y ) <EOL> A = as_gpuarray_variable ( A , ctx_name ) <EOL> x = as_gpuarray_variable ( x , ctx_name ) <EOL> y = as_gpuarray_variable ( y , ctx_name ) <EOL> alpha = as_tensor_variable ( alpha ) <EOL> assert alpha . ndim == <NUM_LIT:0> <EOL> assert A . ndim == <NUM_LIT:2> <EOL> assert x . ndim == <NUM_LIT:1> <EOL> assert y . ndim == <NUM_LIT:1> <EOL> assert A . dtype == x . dtype == y . dtype <EOL> return Apply ( self , [ A , alpha , x , y ] , [ A . type ( ) ] ) <EOL> def perform ( self , node , inp , out ) : <EOL> A , alpha , x , y = inp <EOL> inplace = self . inplace <EOL> if inplace and not A . flags . forc : <EOL> inplace = False <EOL> out [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] = blas . ger ( alpha , x , y , A , <EOL> overwrite_a = inplace ) <EOL> def c_code ( self , node , name , inp , out , sub ) : <EOL> vars = dict ( out = out [ <NUM_LIT:0> ] , A = inp [ <NUM_LIT:0> ] , alpha = inp [ <NUM_LIT:1> ] , x = inp [ <NUM_LIT:2> ] , y = inp [ <NUM_LIT:3> ] , <EOL> fail = sub [ '<STR_LIT>' ] , name = name ) <EOL> if self . inplace : <EOL> code = """<STR_LIT>""" % vars <EOL> else : <EOL> code = """<STR_LIT>""" % vars <EOL> code += """<STR_LIT>""" % vars <EOL> if config . gpuarray . sync : <EOL> code += """<STR_LIT>""" % vars <EOL> return code <EOL> def c_code_cache_version ( self ) : <EOL> return ( <NUM_LIT:3> , ) <EOL> gpuger_no_inplace = GpuGer ( inplace = False ) <EOL> gpuger_inplace = GpuGer ( inplace = True ) <EOL> class GpuDot22 ( BlasOp ) : <EOL> """<STR_LIT>""" <EOL> __props__ = ( ) <EOL> def make_node ( self , x , y ) : <EOL> ctx_name = infer_context_name ( x , y ) <EOL> x = as_gpuarray_variable ( x , ctx_name ) <EOL> y = as_gpuarray_variable ( y , ctx_name ) <EOL> assert x . ndim == <NUM_LIT:2> <EOL> assert y . ndim == <NUM_LIT:2> <EOL> assert x . dtype == y . dtype <EOL> otype = x . type . clone ( <EOL> broadcastable = ( x . type . broadcastable [ <NUM_LIT:0> ] , y . type . broadcastable [ <NUM_LIT:1> ] ) ) <EOL> return Apply ( self , [ x , y ] , [ otype ( ) ] ) <EOL> def perform ( self , node , inputs , outputs ) : <EOL> x , y = inputs <EOL> out = pygpu . empty ( ( x . shape [ <NUM_LIT:0> ] , y . shape [ <NUM_LIT:1> ] ) , dtype = x . dtype , <EOL> context = x . context ) <EOL> outputs [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] = blas . gemm ( <NUM_LIT:1.> , x , y , <NUM_LIT:0.> , out , <EOL> overwrite_c = True ) <EOL> def c_code ( self , node , name , inputs , outputs , sub ) : <EOL> dtype = node . inputs [ <NUM_LIT:0> ] . dtype <EOL> typecode = pygpu . gpuarray . dtype_to_typecode ( dtype ) <EOL> vars = dict ( A = inputs [ <NUM_LIT:0> ] , B = inputs [ <NUM_LIT:1> ] , dtype = dtype , out = outputs [ <NUM_LIT:0> ] , <EOL> typecode = typecode , <EOL> fail = sub [ '<STR_LIT>' ] , name = name ) <EOL> code = """<STR_LIT>""" % vars <EOL> if config . gpuarray . sync : <EOL> code += """<STR_LIT>""" % vars <EOL> return code <EOL> def c_code_cache_version ( self ) : <EOL> return ( <NUM_LIT:4> , ) <EOL> gpu_dot22 = GpuDot22 ( ) <EOL> class GpuGemmBatch ( BlasOp ) : <EOL> __props__ = ( '<STR_LIT>' , ) <EOL> def __init__ ( self , inplace = False ) : <EOL> self . inplace = inplace <EOL> if self . inplace : <EOL> self . destroy_map = { <NUM_LIT:0> : [ <NUM_LIT:0> ] } <EOL> def make_node ( self , C , alpha , A , B , beta ) : <EOL> ctx_name = infer_context_name ( C , A , B ) <EOL> A = as_gpuarray_variable ( A , ctx_name ) <EOL> B = as_gpuarray_variable ( B , ctx_name ) <EOL> C = as_gpuarray_variable ( C , ctx_name ) <EOL> alpha = as_tensor_variable ( alpha ) <EOL> beta = as_tensor_variable ( beta ) <EOL> assert alpha . ndim == <NUM_LIT:0> <EOL> assert beta . ndim == <NUM_LIT:0> <EOL> assert A . ndim == <NUM_LIT:3> <EOL> assert B . ndim == <NUM_LIT:3> <EOL> assert C . ndim == <NUM_LIT:3> <EOL> assert A . dtype == B . dtype == C . dtype <EOL> return Apply ( self , [ C , alpha , A , B , beta ] , [ C . type ( ) ] ) <EOL> def c_headers ( self ) : <EOL> return super ( GpuGemmBatch , self ) . c_headers ( ) + [ '<STR_LIT>' ] <EOL> def c_code ( self , node , name , inp , out , sub ) : <EOL> vars = dict ( out = out [ <NUM_LIT:0> ] , C = inp [ <NUM_LIT:0> ] , alpha = inp [ <NUM_LIT:1> ] , A = inp [ <NUM_LIT:2> ] , B = inp [ <NUM_LIT:3> ] , <EOL> beta = inp [ <NUM_LIT:4> ] , fail = sub [ '<STR_LIT>' ] , name = name ) <EOL> code = """<STR_LIT>""" <EOL> if self . inplace : <EOL> code += """<STR_LIT>""" % vars <EOL> else : <EOL> code += """<STR_LIT>""" % vars <EOL> code += """<STR_LIT>""" % vars <EOL> if config . gpuarray . sync : <EOL> code += """<STR_LIT>""" % vars <EOL> return code <EOL> def c_code_cache_version ( self ) : <EOL> return ( <NUM_LIT:1> , ) <EOL> gpugemmbatch_no_inplace = GpuGemmBatch ( inplace = False ) <EOL> gpugemmbatch_inplace = GpuGemmBatch ( inplace = True ) <EOL> @ inplace_allocempty ( GpuGemv , <NUM_LIT:0> ) <EOL> def local_inplace_gpuagemv ( node , inputs ) : <EOL> return [ gpugemv_inplace ( * inputs ) ] <EOL> @ inplace_allocempty ( GpuGemm , <NUM_LIT:0> ) <EOL> def local_inplace_gpuagemm ( node , inputs ) : <EOL> return [ gpugemm_inplace ( * inputs ) ] <EOL> @ inplace_allocempty ( GpuGer , <NUM_LIT:0> ) <EOL> def local_inplace_gpuager ( node , inputs ) : <EOL> return [ gpuger_inplace ( * inputs ) ] <EOL> gpuablas_opt_inplace = in2out ( LocalOptGroup ( local_inplace_gpuagemv , <EOL> local_inplace_gpuagemm , <EOL> local_inplace_gpuager ) , <EOL> name = '<STR_LIT>' ) <EOL> optdb . register ( '<STR_LIT>' , <EOL> gpuablas_opt_inplace , <EOL> <NUM_LIT> , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) </s>
