<s> from contextlib import contextmanager <EOL> import numpy as np <EOL> from warnings import warn <EOL> from numba . six import raise_from <EOL> class FakeShape ( tuple ) : <EOL> '''<STR_LIT>''' <EOL> def __getitem__ ( self , k ) : <EOL> if k < <NUM_LIT:0> : <EOL> raise IndexError ( '<STR_LIT>' ) <EOL> return super ( FakeShape , self ) . __getitem__ ( k ) <EOL> class FakeCUDAArray ( object ) : <EOL> '''<STR_LIT>''' <EOL> __cuda_ndarray__ = True <EOL> def __init__ ( self , ary ) : <EOL> self . _ary = ary <EOL> def __getattr__ ( self , attrname ) : <EOL> try : <EOL> attr = getattr ( self . _ary , attrname ) <EOL> return attr <EOL> except AttributeError as e : <EOL> raise_from ( AttributeError ( "<STR_LIT>" <EOL> % attrname ) , e ) <EOL> def __getitem__ ( self , idx ) : <EOL> item = self . _ary . __getitem__ ( idx ) <EOL> if isinstance ( item , np . ndarray ) : <EOL> return FakeCUDAArray ( item ) <EOL> return item <EOL> def __setitem__ ( self , idx , val ) : <EOL> return self . _ary . __setitem__ ( idx , val ) <EOL> def copy_to_host ( self , ary = None , stream = <NUM_LIT:0> ) : <EOL> if ary is None : <EOL> ary = np . empty_like ( self . _ary ) <EOL> try : <EOL> np . copyto ( ary , self . _ary ) <EOL> except AttributeError : <EOL> ary [ : ] = self . _ary <EOL> return ary <EOL> def copy_to_device ( self , ary , stream = <NUM_LIT:0> ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> np . copyto ( self . _ary , ary ) <EOL> except AttributeError : <EOL> self . _ary [ : ] = ary <EOL> def to_host ( self ) : <EOL> warn ( '<STR_LIT>' ) <EOL> @ property <EOL> def shape ( self ) : <EOL> return FakeShape ( self . _ary . shape ) <EOL> def ravel ( self , * args , ** kwargs ) : <EOL> return FakeCUDAArray ( self . _ary . ravel ( * args , ** kwargs ) ) <EOL> def reshape ( self , * args , ** kwargs ) : <EOL> return FakeCUDAArray ( self . _ary . reshape ( * args , ** kwargs ) ) <EOL> def is_c_contiguous ( self ) : <EOL> return self . _ary . flags . c_contiguous <EOL> def is_f_contiguous ( self ) : <EOL> return self . _ary . flags . f_contiguous <EOL> def __str__ ( self ) : <EOL> return str ( self . _ary ) <EOL> def __repr__ ( self ) : <EOL> return repr ( self . _ary ) <EOL> def __len__ ( self ) : <EOL> return len ( self . _ary ) <EOL> errmsg_contiguous_buffer = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def sentry_contiguous ( ary ) : <EOL> if not ary . flags [ '<STR_LIT>' ] and not ary . flags [ '<STR_LIT>' ] : <EOL> if ary . strides [ <NUM_LIT:0> ] == <NUM_LIT:0> : <EOL> return sentry_contiguous ( ary [ <NUM_LIT:0> ] ) <EOL> else : <EOL> raise ValueError ( errmsg_contiguous_buffer ) <EOL> def to_device ( ary , stream = <NUM_LIT:0> , copy = True , to = None ) : <EOL> sentry_contiguous ( ary ) <EOL> return FakeCUDAArray ( ary ) <EOL> @ contextmanager <EOL> def pinned ( arg ) : <EOL> yield <EOL> def pinned_array ( shape , dtype = np . float , strides = None , order = '<STR_LIT:C>' ) : <EOL> return np . ndarray ( shape = shape , strides = strides , dtype = dtype , order = order ) <EOL> def device_array ( * args , ** kwargs ) : <EOL> if '<STR_LIT>' in kwargs : <EOL> kwargs . pop ( '<STR_LIT>' ) <EOL> return FakeCUDAArray ( np . ndarray ( * args , ** kwargs ) ) <EOL> def device_array_like ( ary , stream = <NUM_LIT:0> ) : <EOL> return FakeCUDAArray ( np . empty_like ( ary ) ) <EOL> class devicearray ( object ) : <EOL> @ staticmethod <EOL> def auto_device ( ary , stream = <NUM_LIT:0> , copy = True ) : <EOL> return to_device ( ary , stream , copy ) , False </s>
