#NEWLINE# def test_careduce( ) : #NEWLINE# #INDENT# '\n    test sum pattern 1, 11, 10, 01, 001, 010, 100, 110, 011, 111,\n    0011, 0101, 0111, 1011, 1111\n\n    test sum pattern implemented with reshape:\n    1000, 0100, 0010, 0001, 11111\n\n    others implemented by reshape that are not tested\n    0011,0101,0110,1001,1010,1100\n    1110,1101,1011\n\n    TODO: test with broadcast\n\n    We test with the pre_scalar_op sqr in all cases. This cover all\n    code, with and without it the pre_scalar_op.\n\n    ' #NEWLINE# for ( scalar_op , careduce_op ) in [ ( theano . scalar . mul , tensor . elemwise . CAReduceDtype ) , ( theano . scalar . add , tensor . elemwise . CAReduceDtype ) , ( theano . scalar . maximum , tensor . CAReduce ) , ( theano . scalar . minimum , tensor . CAReduce ) ] : #NEWLINE# #INDENT# for ( shape , pattern ) in [ ( ( 1 , 1 ) , ( 1 , ) ) , ( ( 1 , 0 ) , ( 1 , ) ) , ( ( 0 , 1 ) , ( 1 , ) ) , ( ( 0 , 0 ) , ( 1 , ) ) , ( ( 0 , 0 , 0 ) , ( 1 , 2 ) ) , ( ( 0 , 0 , 0 , 0 ) , ( 1 , 2 , 3 ) ) , ( ( 2 , 1 ) , ( 1 , ) ) , ( ( 1 , 2 ) , ( 1 , ) ) , ( ( 100 , 3 , 1300 ) , [ 1 ] ) , ( ( 0 , ) , [ 0 ] ) , ( ( 5 , ) , [ 0 ] ) , ( ( 0 , 0 ) , [ 0 , 1 ] ) , ( ( 1 , 0 ) , [ 0 , 1 ] ) , ( ( 5 , 4 ) , [ 0 , 1 ] ) , ( ( 33 , 31 ) , [ 0 , 1 ] ) , ( ( 5 , 4 ) , [ 1 ] ) , ( ( 5 , 4 ) , [ 0 ] ) , ( ( 5 , 4 , 3 ) , [ 0 ] ) , ( ( 5 , 4 , 3 ) , [ 1 ] ) , ( ( 5 , 4 , 3 ) , [ 0 , 1 ] ) , ( ( 5 , 4 , 3 ) , [ 2 ] ) , ( ( 5 , 4 , 3 ) , [ 1 , 2 ] ) , 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 2 , 3 , 1100 , 4 , 5 ) , [ 0 , 1 , 2 , 3 , 4 ] ) , ( ( 2 , 3 , 4 , 1100 , 5 ) , [ 0 , 1 , 2 , 3 , 4 ] ) , ( ( 2 , 3 , 4 , 5 , 1100 ) , [ 0 , 1 , 2 , 3 , 4 ] ) ] : #NEWLINE# #INDENT# op = careduce_op ( scalar_op , axis = pattern ) #NEWLINE# pat = tensor_pattern_to_gpu_pattern ( shape , pattern ) #NEWLINE# a = tensor . TensorType ( 'float32' , ( ( False , ) * len ( shape ) ) ) ( ) #NEWLINE# b = op ( ( a * a ) ) #NEWLINE# val = numpy . random . rand ( numpy . prod ( shape ) ) . reshape ( shape ) #NEWLINE# val = theano . _asarray ( val , dtype = 'float32' ) #NEWLINE# f = theano . function ( [ a ] , b , mode = mode_with_gpu ) #NEWLINE# f2 = theano . function ( [ a ] , b , mode = mode_without_gpu ) #NEWLINE# assert ( tcn . GpuCAReduce in [ x . op . __class__ for x in f . maker . fgraph . toposort ( ) ] ) , ( scalar_op , shape , pattern ) #NEWLINE# if ( tcn . GpuElemwise in [ x . op . __class__ for x in f . maker . fgraph . toposort ( ) ] ) : #NEWLINE# #INDENT# assert ( tcn . GpuReshape in [ x . op . __class__ for x in f . maker . fgraph . toposort ( ) ] ) #NEWLINE# #UNINDENT# assert ( op . __class__ in [ x . op . __class__ for x in f2 . maker . fgraph . toposort ( ) ] ) , ( scalar_op , shape , pattern ) #NEWLINE# f_caused_value_error = False #NEWLINE# try : #NEWLINE# #INDENT# f_out = f ( val ) #NEWLINE# #UNINDENT# except ValueError as e : #NEWLINE# #INDENT# exc = e #NEWLINE# f_caused_value_error = True #NEWLINE# #UNINDENT# except NotImplementedError : #NEWLINE# #INDENT# if ( ( numpy . prod ( shape ) == 0 ) and ( getattr ( scalar_op , 'identity' , None ) != 0 ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# raise #NEWLINE# #UNINDENT# f2_caused_value_error = False #NEWLINE# try : #NEWLINE# #INDENT# f2_out = f2 ( val ) #NEWLINE# #UNINDENT# except ValueError as e : #NEWLINE# #INDENT# exc2 = e #NEWLINE# f2_caused_value_error = True #NEWLINE# #UNINDENT# if ( f_caused_value_error != f2_caused_value_error ) : #NEWLINE# #INDENT# if f_caused_value_error : #NEWLINE# #INDENT# print ( 'f caused this value error:' ) #NEWLINE# print ( exc ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# print ( 'f did not raise a value error, but should have' ) #NEWLINE# #UNINDENT# if f2_caused_value_error : #NEWLINE# #INDENT# print ( 'f2 caused this value error:' ) #NEWLINE# print ( exc2 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# print ( 'f should not have raised a value error' ) #NEWLINE# #UNINDENT# print ( 'shape was: ' , shape ) #NEWLINE# print ( 'pattern was: ' , pattern ) #NEWLINE# assert False #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# orig_rtol = theano . tensor . basic . float32_rtol #NEWLINE# theano . tensor . basic . float32_rtol = 2e-05 #NEWLINE# assert _allclose ( f_out , f2_out ) , ( 'shape' , shape , 'pattern' , pattern , scalar_op , sum ( [ shape [ i ] for i in pattern ] ) , f2 ( val ) , f ( val ) , val ) #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# theano . tensor . basic . float32_rtol = orig_rtol #NEWLINE# #UNINDENT# #UNINDENT# for ( shape , pattern ) in [ ( ( 5 , 4 ) , [ 0 , 1 ] ) , ( ( 5 , 4 ) , [ 0 ] ) , ( ( 5 , 4 , 3 ) , [ 0 ] ) , ( ( 5 , 4 , 3 ) , [ 0 , 1 ] ) , ( ( 5 , 4 , 3 ) , [ 2 ] ) , ( ( 5 , 4 , 3 ) , [ 0 , 1 , 2 ] ) , ( ( 5 , 4 , 3 , 2 ) , [ 0 , 1 , 2 , 3 ] ) , ( ( 5 , 4 , 3 , 2 ) , [ 0 , 2 , 3 ] ) , ( ( 128 , 1 , 3 , 3 ) , [ 0 , 1 , 2 , 3 ] ) ] : #NEWLINE# #INDENT# op = careduce_op ( scalar_op , axis = pattern ) #NEWLINE# pat = tensor_pattern_to_gpu_pattern ( shape , pattern ) #NEWLINE# a = tensor . TensorType ( 'float32' , ( ( False , ) * len ( shape ) ) ) ( ) #NEWLINE# dim_pattern = list ( range ( len ( shape ) ) ) #NEWLINE# dim_pattern [ 0 ] = 1 #NEWLINE# dim_pattern [ 1 ] = 0 #NEWLINE# a = a . dimshuffle ( dim_pattern ) #NEWLINE# b = op ( ( a * a ) ) #NEWLINE# val = numpy . random . rand ( numpy . prod ( shape ) ) . reshape ( shape ) #NEWLINE# val = theano . _asarray ( val , dtype = 'float32' ) #NEWLINE# f = theano . function ( [ a ] , b , mode = mode_with_gpu ) #NEWLINE# f2 = theano . function ( [ a ] , b , mode = mode_without_gpu ) #NEWLINE# assert ( tcn . GpuCAReduce in [ x . op . __class__ for x in f . maker . fgraph . toposort ( ) ] ) , ( scalar_op , shape , pattern ) #NEWLINE# assert ( tcn . GpuElemwise not in [ x . op . __class__ for x in f . maker . fgraph . toposort ( ) ] ) #NEWLINE# assert ( op . __class__ in [ x . op . __class__ for x in f2 . maker . fgraph . toposort ( ) ] ) , ( scalar_op , shape , pattern ) #NEWLINE# assert _allclose ( f2 ( val ) , f ( val ) ) , ( 'shape' , shape , 'pattern' , pattern , scalar_op , sum ( [ shape [ i ] for i in pattern ] ) ) #NEWLINE# #UNINDENT# for ( shape , pattern ) in [ ( ( 5 , ) , [ 0 ] ) , ( ( 5 , 4 ) , [ 0 , 1 ] ) , ( ( 5 , 4 ) , [ 0 ] ) , ( ( 5 , 4 , 3 ) , [ 0 ] ) , ( ( 5 , 4 , 3 ) , [ 0 , 1 ] ) , ( ( 5 , 4 , 3 ) , [ 2 ] ) , ( ( 5 , 4 , 3 ) , [ 0 , 1 , 2 ] ) , ( ( 5 , 4 , 3 , 2 ) , [ 0 , 1 , 2 , 3 ] ) , ( ( 5 , 4 , 3 , 2 ) , [ 0 , 2 , 3 ] ) , ( ( 128 , 1 , 3 , 3 ) , [ 0 , 1 , 2 , 3 ] ) ] : #NEWLINE# #INDENT# op = careduce_op ( scalar_op , axis = pattern ) #NEWLINE# pat = tensor_pattern_to_gpu_pattern ( shape , pattern ) #NEWLINE# shape = ( numpy . asarray ( shape ) * 2 ) #NEWLINE# a = tensor . TensorType ( 'float32' , ( ( False , ) * len ( shape ) ) ) ( ) #NEWLINE# a2 = tcn . CudaNdarrayType ( ( ( False , ) * len ( shape ) ) ) ( ) #NEWLINE# b = op ( ( a * a ) ) #NEWLINE# b2 = op ( ( a2 * a2 ) ) #NEWLINE# val = numpy . random . rand ( numpy . prod ( shape ) ) . reshape ( shape ) #NEWLINE# val = theano . _asarray ( val , dtype = 'float32' ) #NEWLINE# val2 = cuda . CudaNdarray ( val ) #NEWLINE# if ( len ( shape ) == 1 ) : #NEWLINE# #INDENT# val = val [ : : 2 ] #NEWLINE# val2 = val2 [ : : 2 ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( len ( shape ) == 2 ) : #NEWLINE# #INDENT# val = val [ : : 2 , : : 2 ] #NEWLINE# val2 = val2 [ : : 2 , : : 2 ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( len ( shape ) == 3 ) : #NEWLINE# #INDENT# val = val [ : : 2 , : : 2 , : : 2 ] #NEWLINE# val2 = val2 [ : : 2 , : : 2 , : : 2 ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( len ( shape ) == 4 ) : #NEWLINE# #INDENT# val = val [ : : 2 , : : 2 , : : 2 , : : 2 ] #NEWLINE# val2 = val2 [ : : 2 , : : 2 , : : 2 , : : 2 ] #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# f = theano . function ( [ a ] , b , mode = mode_without_gpu ) #NEWLINE# f2 = theano . function ( [ a2 ] , b2 , mode = mode_with_gpu ) #NEWLINE# assert ( tcn . GpuCAReduce in [ x . op . __class__ for x in f2 . maker . fgraph . toposort ( ) ] ) , ( scalar_op , shape , pattern ) #NEWLINE# assert ( tcn . GpuElemwise not in [ x . op . __class__ for x in f . maker . fgraph . toposort ( ) ] ) #NEWLINE# assert ( b . __class__ in [ x . op . __class__ for x in f . maker . fgraph . toposort ( ) ] ) , ( scalar_op , shape , pattern ) #NEWLINE# assert _allclose ( f2 ( val2 ) , f ( val ) ) , ( 'shape' , shape , 'pattern' , pattern , sum ( [ shape [ i ] for i in pattern ] ) )