<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> import logging <EOL> from math import ceil <EOL> logger = logging . getLogger ( __name__ ) <EOL> class OpCollection ( object ) : <EOL> """<STR_LIT>""" <EOL> zero_operand_ops = { "<STR_LIT>" , "<STR_LIT>" } <EOL> unary_ops = { "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" } <EOL> binary_ops = { "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" } <EOL> reduction_ops = { "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" } <EOL> float_ops = zero_operand_ops | unary_ops | binary_ops <EOL> ew_ops = float_ops - { '<STR_LIT>' , '<STR_LIT>' } <EOL> class Tensor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> backend , <EOL> shape = None , <EOL> dtype = np . float32 , <EOL> name = None , <EOL> persist_values = True ) : <EOL> self . backend = backend <EOL> self . shape = shape <EOL> self . dtype = dtype <EOL> self . name = name <EOL> self . persist_values = persist_values <EOL> self . _min_dims = <NUM_LIT:2> <EOL> self . base = None <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def __len__ ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def __setitem__ ( self , index , value ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def __getitem__ ( self , index ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def _assign ( self , value ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def set ( self , ary ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def get ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def asnumpyarray ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def take ( self , indices , axis , out = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def fill ( self , value ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def copy ( self , a ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def copy_from ( self , a ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def reshape ( self , * shape ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> @ property <EOL> def T ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def transpose ( self , out = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def hist ( self , tag ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> @ property <EOL> def _original_base ( self ) : <EOL> """<STR_LIT>""" <EOL> original_base = self <EOL> while original_base . base is not None : <EOL> original_base = original_base . base <EOL> return original_base <EOL> def __add__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , self , other ) <EOL> def __sub__ ( self , other ) : <EOL> return OpTreeNode . build ( "<STR_LIT>" , self , other ) <EOL> def __mul__ ( self , other ) : <EOL> return OpTreeNode . build ( "<STR_LIT>" , self , other ) <EOL> def __div__ ( self , other ) : <EOL> return OpTreeNode . build ( "<STR_LIT>" , self , other ) <EOL> def __truediv__ ( self , other ) : <EOL> return OpTreeNode . build ( "<STR_LIT>" , self , other ) <EOL> def __pow__ ( self , other ) : <EOL> return OpTreeNode . build ( "<STR_LIT>" , self , other ) <EOL> def __radd__ ( self , other ) : <EOL> return OpTreeNode . build ( "<STR_LIT>" , other , self ) <EOL> def __rsub__ ( self , other ) : <EOL> return OpTreeNode . build ( "<STR_LIT>" , other , self ) <EOL> def __rmul__ ( self , other ) : <EOL> return OpTreeNode . build ( "<STR_LIT>" , other , self ) <EOL> def __rdiv__ ( self , other ) : <EOL> return OpTreeNode . build ( "<STR_LIT>" , other , self ) <EOL> def __rtruediv__ ( self , other ) : <EOL> return OpTreeNode . build ( "<STR_LIT>" , other , self ) <EOL> def __rpow__ ( self , other ) : <EOL> return OpTreeNode . build ( "<STR_LIT>" , other , self ) <EOL> def __eq__ ( self , other ) : <EOL> return OpTreeNode . build ( "<STR_LIT>" , self , other ) <EOL> def __ne__ ( self , other ) : <EOL> return OpTreeNode . build ( "<STR_LIT>" , self , other ) <EOL> def __lt__ ( self , other ) : <EOL> return OpTreeNode . build ( "<STR_LIT>" , self , other ) <EOL> def __le__ ( self , other ) : <EOL> return OpTreeNode . build ( "<STR_LIT>" , self , other ) <EOL> def __gt__ ( self , other ) : <EOL> return OpTreeNode . build ( "<STR_LIT>" , self , other ) <EOL> def __ge__ ( self , other ) : <EOL> return OpTreeNode . build ( "<STR_LIT>" , self , other ) <EOL> def __abs__ ( self ) : <EOL> return OpTreeNode . build ( "<STR_LIT>" , self , None ) <EOL> def __neg__ ( self ) : <EOL> return OpTreeNode . build ( "<STR_LIT>" , self , None ) <EOL> class Backend ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , rng_seed = None , default_dtype = np . float32 , <EOL> compat_mode = None , deterministic = None ) : <EOL> self . default_dtype = default_dtype <EOL> self . rng_seed = rng_seed <EOL> self . rng = self . gen_rng ( rng_seed ) <EOL> self . bsz = None <EOL> self . _min_dims = <NUM_LIT:2> <EOL> if compat_mode is not None : <EOL> if compat_mode == '<STR_LIT>' : <EOL> self . set_caffe_compat ( ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % compat_mode ) <EOL> else : <EOL> self . compat_mode = None <EOL> if deterministic is not None : <EOL> logger . warning ( '<STR_LIT>' ) <EOL> self . deterministic = self . rng_seed is not None <EOL> def output_dim ( self , X , S , padding , strides , pooling = False ) : <EOL> """<STR_LIT>""" <EOL> if self . check_caffe_compat ( ) and pooling : <EOL> size = int ( ceil ( float ( X - S + <NUM_LIT:2> * padding ) / strides ) ) + <NUM_LIT:1> <EOL> if padding > <NUM_LIT:0> and ( size - <NUM_LIT:1> ) * strides >= X + padding : <EOL> size -= <NUM_LIT:1> <EOL> else : <EOL> size = ( X - S + <NUM_LIT:2> * padding ) / strides + <NUM_LIT:1> <EOL> if pooling and padding >= S : <EOL> raise ValueError ( "<STR_LIT>" % ( padding , S ) ) <EOL> return size <EOL> def set_caffe_compat ( self ) : <EOL> """<STR_LIT>""" <EOL> self . compat_mode = '<STR_LIT>' <EOL> def check_caffe_compat ( self ) : <EOL> return self . compat_mode == '<STR_LIT>' <EOL> def iobuf ( self , dim0 , x = None , dtype = None , name = None , persist_values = True , <EOL> shared = None , parallelism = None ) : <EOL> """<STR_LIT>""" <EOL> if x is not None : <EOL> return x <EOL> if isinstance ( dim0 , tuple ) : <EOL> if ( len ( dim0 ) == <NUM_LIT:2> ) : <EOL> bufshape = ( dim0 [ <NUM_LIT:0> ] , dim0 [ <NUM_LIT:1> ] * self . bsz ) <EOL> else : <EOL> bufshape = ( np . prod ( dim0 ) , self . bsz ) <EOL> else : <EOL> bufshape = ( dim0 , self . bsz ) <EOL> if shared is not None : <EOL> if shared . shape == bufshape : <EOL> return shared <EOL> else : <EOL> return shared . share ( bufshape ) <EOL> else : <EOL> return self . zeros ( bufshape , dtype = dtype , name = name , <EOL> persist_values = persist_values ) <EOL> def shared_iobuf_size ( self , shape , parallelism ) : <EOL> """<STR_LIT>""" <EOL> num_dev = <NUM_LIT:1> if parallelism in ( '<STR_LIT>' , '<STR_LIT>' ) else getattr ( self , '<STR_LIT>' , <NUM_LIT:1> ) <EOL> return num_dev * np . prod ( shape ) <EOL> def distribute_data ( self , tensor , layer_parallelism ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> def revert_tensor ( self , tensor ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def gen_rng ( self , seed = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def rng_get_state ( self , state ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def rng_reset ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def rng_set_state ( self , state ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def execute ( self , node ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def begin ( self , block , identifier ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def end ( self , block , identifier ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def empty ( self , shape , dtype = None , name = None , persist_values = True , <EOL> parallel = False , distributed = False ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def array ( self , ary , dtype = None , name = None , persist_values = True , <EOL> parallel = False , distributed = False ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def zeros ( self , shape , dtype = None , name = None , persist_values = True , <EOL> parallel = False , distributed = False ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def ones ( self , shape , dtype = None , name = None , persist_values = True , <EOL> parallel = False , distributed = False ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def empty_like ( self , other_ary , name = None , persist_values = True ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def zeros_like ( self , other_ary , name = None , persist_values = True ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def dot ( self , a , b , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , b , out = out ) <EOL> def compound_dot ( self , A , B , C , alpha = <NUM_LIT:1.0> , beta = <NUM_LIT:0.0> , relu = False ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def batched_dot ( self , A , B , C , alpha = <NUM_LIT:1.0> , beta = <NUM_LIT:0.0> , relu = False ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def make_binary_mask ( self , out , keepthresh = <NUM_LIT:0.5> ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def add ( self , a , b , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , b , out = out ) <EOL> def subtract ( self , a , b , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , b , out = out ) <EOL> def multiply ( self , a , b , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , b , out = out ) <EOL> def divide ( self , a , b , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , b , out = out ) <EOL> def true_divide ( self , a , b , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , b , out = out ) <EOL> def power ( self , a , b , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , b , out = out ) <EOL> def reciprocal ( self , a , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , <NUM_LIT:1.> , a , out = out ) <EOL> def negative ( self , a , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , None , out = out ) <EOL> def sgn ( self , a , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , None , out = out ) <EOL> def absolute ( self , a , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , None , out = out ) <EOL> def fabs ( self , a , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , None , out = out ) <EOL> def sqrt ( self , a , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , None , out = out ) <EOL> def square ( self , a , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , None , out = out ) <EOL> def exp ( self , a , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , None , out = out ) <EOL> def exp2 ( self , a , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , None , out = out ) <EOL> def safelog ( self , a , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , None , out = out ) <EOL> def log ( self , a , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , None , out = out ) <EOL> def log2 ( self , a , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , None , out = out ) <EOL> def sig ( self , a , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , None , out = out ) <EOL> def sig2 ( self , a , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , None , out = out ) <EOL> def tanh ( self , a , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , None , out = out ) <EOL> def tanh2 ( self , a , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , None , out = out ) <EOL> def finite ( self , a , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , None , out = out ) <EOL> def equal ( self , a , b , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , b , out = out ) <EOL> def not_equal ( self , a , b , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , b , out = out ) <EOL> def less ( self , a , b , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , b , out = out ) <EOL> def less_equal ( self , a , b , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , b , out = out ) <EOL> def greater ( self , a , b , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , b , out = out ) <EOL> def greater_equal ( self , a , b , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , b , out = out ) <EOL> def maximum ( self , a , b , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , b , out = out ) <EOL> def minimum ( self , a , b , out = None ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , b , out = out ) <EOL> def clip ( self , a , a_min , a_max , out = None ) : <EOL> """<STR_LIT>""" <EOL> return self . minimum ( self . maximum ( a , a_min ) , a_max , out = out ) <EOL> def sum ( self , a , axis = None , out = None , keepdims = True ) : <EOL> """<STR_LIT>""" <EOL> if axis is None : <EOL> return OpTreeNode . build ( "<STR_LIT>" , OpTreeNode . build ( "<STR_LIT>" , a , None , axis = <NUM_LIT:0> ) , <EOL> None , axis = <NUM_LIT:1> , out = out ) <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , None , axis = axis , out = out ) <EOL> def max ( self , a , axis = None , out = None , keepdims = True ) : <EOL> """<STR_LIT>""" <EOL> if axis is None : <EOL> return OpTreeNode . build ( "<STR_LIT>" , OpTreeNode . build ( "<STR_LIT>" , a , None , axis = <NUM_LIT:0> ) , <EOL> None , axis = <NUM_LIT:1> , out = out ) <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , None , axis = axis , out = out ) <EOL> def min ( self , a , axis = None , out = None , keepdims = True ) : <EOL> """<STR_LIT>""" <EOL> if axis is None : <EOL> return OpTreeNode . build ( "<STR_LIT>" , OpTreeNode . build ( "<STR_LIT>" , a , None , axis = <NUM_LIT:0> ) , <EOL> None , axis = <NUM_LIT:1> , out = out ) <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , None , axis = axis , out = out ) <EOL> def argmax ( self , a , axis = <NUM_LIT:1> , out = None , keepdims = True ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , None , axis = axis , out = out ) <EOL> def argmin ( self , a , axis = <NUM_LIT:1> , out = None , keepdims = True ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . build ( "<STR_LIT>" , a , None , axis = axis , out = out ) <EOL> def mean ( self , a , axis = None , partial = None , out = None , keepdims = True ) : <EOL> """<STR_LIT>""" <EOL> shape = a . shape <EOL> if axis is None : <EOL> return self . multiply ( self . sum ( a ) , <NUM_LIT:1.0> / ( shape [ <NUM_LIT:0> ] * shape [ <NUM_LIT:1> ] ) , out = out ) <EOL> return self . multiply ( self . sum ( a , axis = axis ) , <NUM_LIT:1.0> / shape [ axis ] , out = out ) <EOL> def var ( self , a , axis = None , partial = None , out = None , keepdims = True ) : <EOL> """<STR_LIT>""" <EOL> if axis is None : <EOL> return self . mean ( self . square ( a - self . mean ( a ) ) , out = out ) <EOL> return self . mean ( self . square ( a - self . mean ( a , axis = axis ) ) , axis = axis , out = out ) <EOL> def std ( self , a , axis = None , partial = None , out = None , keepdims = True ) : <EOL> """<STR_LIT>""" <EOL> return self . sqrt ( self . var ( a , axis = axis , partial = partial , out = out ) ) <EOL> def take ( self , a , indices , axis , out = None ) : <EOL> """<STR_LIT>""" <EOL> return a . take ( indices , axis , out ) <EOL> def onehot ( self , indices , axis , out = None ) : <EOL> """<STR_LIT>""" <EOL> if axis not in ( <NUM_LIT:0> , <NUM_LIT:1> ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return OpTreeNode . build ( "<STR_LIT>" , None , None , idx = indices , axis = axis , out = out ) <EOL> def update_fc_bias ( self , err , out ) : <EOL> """<STR_LIT>""" <EOL> self . ng . sum ( err , axis = <NUM_LIT:1> , out = out ) <EOL> def add_fc_bias ( self , inputs , bias ) : <EOL> """<STR_LIT>""" <EOL> self . ng . add ( inputs , bias , out = inputs ) <EOL> def conv_layer ( self , dtype , <EOL> N , C , K , <EOL> D = <NUM_LIT:1> , H = <NUM_LIT:1> , W = <NUM_LIT:1> , <EOL> T = <NUM_LIT:1> , R = <NUM_LIT:1> , S = <NUM_LIT:1> , <EOL> pad_d = <NUM_LIT:0> , pad_h = <NUM_LIT:0> , pad_w = <NUM_LIT:0> , <EOL> str_d = <NUM_LIT:1> , str_h = <NUM_LIT:1> , str_w = <NUM_LIT:1> , <EOL> relu = False , bsum = False ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def fprop_conv ( self , layer , I , F , O , alpha = <NUM_LIT:1.0> , relu = False , repeat = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def bprop_conv ( self , layer , F , E , grad_I , alpha = <NUM_LIT:1.0> , repeat = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def update_conv ( self , layer , I , E , grad_F , alpha = <NUM_LIT:1.0> , repeat = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def deconv_layer ( self , dtype , <EOL> N , C , K , <EOL> P , Q , <EOL> R = <NUM_LIT:1> , S = <NUM_LIT:1> , <EOL> pad_d = <NUM_LIT:0> , pad_h = <NUM_LIT:0> , pad_w = <NUM_LIT:0> , <EOL> str_d = <NUM_LIT:1> , str_h = <NUM_LIT:1> , str_w = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def pool_layer ( self , dtype , <EOL> op , N , C , <EOL> D = <NUM_LIT:1> , H = <NUM_LIT:1> , W = <NUM_LIT:1> , <EOL> J = <NUM_LIT:1> , T = <NUM_LIT:1> , R = <NUM_LIT:1> , S = <NUM_LIT:1> , <EOL> pad_j = <NUM_LIT:0> , pad_d = <NUM_LIT:0> , pad_h = <NUM_LIT:0> , pad_w = <NUM_LIT:0> , <EOL> str_j = None , str_d = None , str_h = None , str_w = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def fprop_pool ( self , layer , I , O ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def bprop_pool ( self , layer , I , E , grad_I ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def compound_bprop_lut ( self , nin , inputs , error , error_t , dW , pad_idx , alpha = <NUM_LIT:1.0> , beta = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> class OpTreeNode ( tuple ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , * args ) : <EOL> return tuple . __new__ ( cls , args ) <EOL> def __str__ ( self ) : <EOL> s = '<STR_LIT:(>' + str ( self [ <NUM_LIT:0> ] ) <EOL> s += '<STR_LIT:U+002CU+0020>' <EOL> if isinstance ( self [ <NUM_LIT:1> ] , Tensor ) : <EOL> if self [ <NUM_LIT:1> ] . name and self [ <NUM_LIT:1> ] . name is not None : <EOL> s += self [ <NUM_LIT:1> ] . name <EOL> else : <EOL> s += '<STR_LIT>' + hex ( id ( self [ <NUM_LIT:1> ] ) ) <EOL> else : <EOL> s += str ( self [ <NUM_LIT:1> ] ) <EOL> s += '<STR_LIT:U+002CU+0020>' <EOL> if isinstance ( self [ <NUM_LIT:2> ] , Tensor ) : <EOL> if self [ <NUM_LIT:2> ] . name and self [ <NUM_LIT:2> ] . name is not None : <EOL> s += self [ <NUM_LIT:2> ] . name <EOL> else : <EOL> s += '<STR_LIT>' + hex ( id ( self [ <NUM_LIT:2> ] ) ) <EOL> else : <EOL> s += str ( self [ <NUM_LIT:2> ] ) <EOL> s += '<STR_LIT:)>' <EOL> return s <EOL> def __repr__ ( self ) : <EOL> return self . __str__ ( ) <EOL> def key ( self ) : <EOL> """<STR_LIT>""" <EOL> stack = self . traverse ( list ( ) ) <EOL> for i in range ( len ( stack ) ) : <EOL> if type ( stack [ i ] ) is dict : <EOL> if '<STR_LIT>' in stack [ i ] : <EOL> stack [ i ] = ( stack [ i ] [ '<STR_LIT>' ] , stack [ i ] [ '<STR_LIT>' ] ) <EOL> else : <EOL> stack [ i ] = ( stack [ i ] [ '<STR_LIT>' ] ) <EOL> return tuple ( stack ) <EOL> def intrinsic_key_maps ( self ) : <EOL> """<STR_LIT>""" <EOL> stack = self . traverse ( list ( ) ) <EOL> tensor_index = <NUM_LIT:0> <EOL> tensor_index_map = { } <EOL> index_tensor_map = { } <EOL> for i in range ( len ( stack ) ) : <EOL> if type ( stack [ i ] ) is dict : <EOL> if '<STR_LIT>' in stack [ i ] : <EOL> stack [ i ] = ( stack [ i ] [ '<STR_LIT>' ] , stack [ i ] [ '<STR_LIT>' ] ) <EOL> else : <EOL> stack [ i ] = ( stack [ i ] [ '<STR_LIT>' ] ) <EOL> elif isinstance ( stack [ i ] , Tensor ) : <EOL> if stack [ i ] in tensor_index_map : <EOL> stack [ i ] = ( tensor_index_map [ stack [ i ] ] , stack [ i ] . shape ) <EOL> else : <EOL> tensor_index_map [ stack [ i ] ] = tensor_index <EOL> index_tensor_map [ tensor_index ] = stack [ i ] <EOL> stack [ i ] = ( tensor_index , stack [ i ] . shape ) <EOL> tensor_index += <NUM_LIT:1> <EOL> return ( tuple ( stack ) , tensor_index_map , index_tensor_map ) <EOL> @ staticmethod <EOL> def build ( op , a , b , out = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> for arg in ( a , b ) : <EOL> if not isinstance ( arg , ( int , float , Tensor , OpTreeNode , type ( None ) ) ) : <EOL> return NotImplemented <EOL> out_shape = [ <NUM_LIT:1> , <NUM_LIT:1> ] <EOL> if isinstance ( a , ( OpTreeNode , Tensor ) ) : <EOL> a_shape = a . shape <EOL> elif isinstance ( a , ( float , int ) ) : <EOL> a_shape = [ <NUM_LIT:1> , <NUM_LIT:1> ] <EOL> else : <EOL> a_shape = [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> if isinstance ( b , ( OpTreeNode , Tensor ) ) : <EOL> b_shape = b . shape <EOL> elif isinstance ( b , ( float , int ) ) : <EOL> b_shape = [ <NUM_LIT:1> , <NUM_LIT:1> ] <EOL> else : <EOL> b_shape = [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> if len ( a_shape ) == <NUM_LIT:1> : <EOL> a_shape = a_shape + ( <NUM_LIT:1> , ) <EOL> if len ( b_shape ) == <NUM_LIT:1> : <EOL> b_shape = b_shape + ( <NUM_LIT:1> , ) <EOL> if op in OpCollection . ew_ops : <EOL> for i in range ( <NUM_LIT:2> ) : <EOL> out_shape [ i ] = max ( a_shape [ i ] , b_shape [ i ] ) <EOL> elif op in OpCollection . reduction_ops : <EOL> if "<STR_LIT>" in kwargs : <EOL> out_shape = list ( a_shape ) <EOL> out_shape [ kwargs [ "<STR_LIT>" ] ] = <NUM_LIT:1> <EOL> else : <EOL> pass <EOL> elif op == "<STR_LIT>" : <EOL> out_shape = a_shape <EOL> elif op == "<STR_LIT>" : <EOL> assert ( len ( a_shape ) == len ( b_shape ) and len ( b_shape ) == <NUM_LIT:2> and <EOL> a_shape [ <NUM_LIT:1> ] == b_shape [ <NUM_LIT:0> ] ) <EOL> out_shape = ( a_shape [ <NUM_LIT:0> ] , b_shape [ <NUM_LIT:1> ] ) <EOL> elif op == "<STR_LIT>" : <EOL> assert b is None <EOL> out_shape = tuple ( reversed ( a_shape ) ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" % op ) <EOL> out_shape = tuple ( out_shape ) <EOL> op_dict = { "<STR_LIT>" : op , "<STR_LIT>" : out_shape } <EOL> op_dict . update ( kwargs ) <EOL> node = OpTreeNode ( op_dict , a , b ) <EOL> if op == "<STR_LIT>" : <EOL> return node . execute ( ) <EOL> if out is not None : <EOL> return OpTreeNode ( { "<STR_LIT>" : "<STR_LIT>" } , out , node ) . execute ( ) <EOL> return node <EOL> def execute ( self ) : <EOL> """<STR_LIT>""" <EOL> assert ( self [ <NUM_LIT:0> ] [ "<STR_LIT>" ] == "<STR_LIT>" ) <EOL> backend = self [ <NUM_LIT:1> ] . backend <EOL> if isinstance ( backend , Backend ) : <EOL> return backend . execute ( self ) <EOL> else : <EOL> raise NotImplementedError ( ) <EOL> def traverse ( self , stack ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( self [ <NUM_LIT:1> ] , OpTreeNode ) : <EOL> self [ <NUM_LIT:1> ] . traverse ( stack ) <EOL> elif self [ <NUM_LIT:1> ] is not None : <EOL> stack . append ( self [ <NUM_LIT:1> ] ) <EOL> if isinstance ( self [ <NUM_LIT:2> ] , OpTreeNode ) : <EOL> self [ <NUM_LIT:2> ] . traverse ( stack ) <EOL> elif self [ <NUM_LIT:2> ] is not None : <EOL> stack . append ( self [ <NUM_LIT:2> ] ) <EOL> stack . append ( self [ <NUM_LIT:0> ] ) <EOL> return stack <EOL> @ property <EOL> def T ( self ) : <EOL> return OpTreeNode . build ( "<STR_LIT>" , self , None ) <EOL> def transpose ( self , out = None ) : <EOL> """<STR_LIT>""" <EOL> if out : <EOL> return OpTreeNode . build ( "<STR_LIT>" , out , self . T ) <EOL> return self . T <EOL> @ staticmethod <EOL> def optree_to_list ( optree ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( optree , OpTreeNode ) : <EOL> return list ( map ( OpTreeNode . optree_to_list , optree ) ) <EOL> else : <EOL> return optree <EOL> @ staticmethod <EOL> def list_to_optree ( l ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( l , list ) : <EOL> return OpTreeNode ( * map ( OpTreeNode . list_to_optree , l ) ) <EOL> else : <EOL> return l <EOL> @ property <EOL> def shape ( self ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( self , OpTreeNode ) : <EOL> return self [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> if isinstance ( self , Tensor ) : <EOL> return self . shape <EOL> return ( <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> @ staticmethod <EOL> def _pretty_print ( node ) : <EOL> operators = { '<STR_LIT>' : '<STR_LIT:+>' , <EOL> '<STR_LIT>' : '<STR_LIT:->' , <EOL> '<STR_LIT>' : '<STR_LIT:*>' , <EOL> '<STR_LIT>' : '<STR_LIT:/>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> s = '<STR_LIT>' <EOL> if isinstance ( node , Tensor ) : <EOL> if node . name : <EOL> s = node . name <EOL> else : <EOL> s = '<STR_LIT>' + hex ( id ( node ) ) <EOL> elif isinstance ( node , OpTreeNode ) : <EOL> if node [ <NUM_LIT:2> ] : <EOL> s += OpTreeNode . _pretty_print ( node [ <NUM_LIT:1> ] ) + '<STR_LIT:U+0020>' <EOL> if node [ <NUM_LIT:0> ] [ '<STR_LIT>' ] in operators : <EOL> s += operators [ node [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ] <EOL> else : <EOL> s += node [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> s += '<STR_LIT:U+0020>' + OpTreeNode . _pretty_print ( node [ <NUM_LIT:2> ] ) <EOL> else : <EOL> s = node [ <NUM_LIT:0> ] [ '<STR_LIT>' ] + '<STR_LIT:U+0020>' + OpTreeNode . _pretty_print ( node [ <NUM_LIT:1> ] ) <EOL> s = '<STR_LIT:(>' + s + '<STR_LIT:)>' <EOL> else : <EOL> s = str ( node ) <EOL> s = '<STR_LIT:(>' + s + '<STR_LIT:)>' <EOL> return s <EOL> def pp ( self ) : <EOL> """<STR_LIT>""" <EOL> return OpTreeNode . _pretty_print ( self ) <EOL> def asnumpyarray ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . astensor ( ) . get ( ) <EOL> def astensor ( self ) : <EOL> """<STR_LIT>""" <EOL> stack = self . traverse ( list ( ) ) <EOL> be = None <EOL> for s in stack : <EOL> if isinstance ( s , Tensor ) : <EOL> be = s . backend <EOL> break <EOL> if be is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> buf = be . empty ( self . shape ) <EOL> buf [ : ] = self <EOL> return buf <EOL> def __add__ ( self , other ) : <EOL> return self . build ( "<STR_LIT>" , self , other ) <EOL> def __sub__ ( self , other ) : <EOL> return self . build ( "<STR_LIT>" , self , other ) <EOL> def __mul__ ( self , other ) : <EOL> return self . build ( "<STR_LIT>" , self , other ) <EOL> def __div__ ( self , other ) : <EOL> return self . build ( "<STR_LIT>" , self , other ) <EOL> def __truediv__ ( self , other ) : <EOL> return self . build ( "<STR_LIT>" , self , other ) <EOL> def __pow__ ( self , other ) : <EOL> return self . build ( "<STR_LIT>" , self , other ) <EOL> def __radd__ ( self , other ) : <EOL> return self . build ( "<STR_LIT>" , other , self ) <EOL> def __rsub__ ( self , other ) : <EOL> return self . build ( "<STR_LIT>" , other , self ) <EOL> def __rmul__ ( self , other ) : <EOL> return self . build ( "<STR_LIT>" , other , self ) <EOL> def __rdiv__ ( self , other ) : <EOL> return self . build ( "<STR_LIT>" , other , self ) <EOL> def __rtruediv__ ( self , other ) : <EOL> return self . build ( "<STR_LIT>" , other , self ) <EOL> def __rpow__ ( self , other ) : <EOL> return self . build ( "<STR_LIT>" , other , self ) <EOL> def __eq__ ( self , other ) : <EOL> return self . build ( "<STR_LIT>" , self , other ) <EOL> def __ne__ ( self , other ) : <EOL> return self . build ( "<STR_LIT>" , self , other ) <EOL> def __lt__ ( self , other ) : <EOL> return self . build ( "<STR_LIT>" , self , other ) <EOL> def __le__ ( self , other ) : <EOL> return self . build ( "<STR_LIT>" , self , other ) <EOL> def __gt__ ( self , other ) : <EOL> return self . build ( "<STR_LIT>" , self , other ) <EOL> def __ge__ ( self , other ) : <EOL> return self . build ( "<STR_LIT>" , self , other ) <EOL> def __abs__ ( self ) : <EOL> return self . build ( "<STR_LIT>" , self , None ) <EOL> def __neg__ ( self ) : <EOL> return self . build ( "<STR_LIT>" , self , None ) <EOL> class Block ( object ) : <EOL> """<STR_LIT>""" <EOL> epoch , minibatch , fprop , bprop , update = range ( <NUM_LIT:5> ) </s>
