#NEWLINE# def test_conv_layer( fargs_tests , device_id ) : #NEWLINE# #INDENT# dtype = np . float32 #NEWLINE# ng = NervanaGPU ( stochastic_round = False , bench = True , device_id = device_id ) #NEWLINE# ( N , C , K ) = fargs_tests [ 0 ] #NEWLINE# ( D , H , W ) = fargs_tests [ 1 ] #NEWLINE# ( T , R , S ) = fargs_tests [ 2 ] #NEWLINE# ( padding_d , padding_h , padding_w ) = fargs_tests [ 3 ] #NEWLINE# ( strides_d , strides_h , strides_w ) = fargs_tests [ 4 ] #NEWLINE# conv_ng = ng . conv_layer ( dtype , N , C , K , D , H , W , T , R , S , padding_d , padding_h , padding_w , strides_d , strides_h , strides_w ) #NEWLINE# nc = NervanaCPU ( ) #NEWLINE# conv_nc = nc . conv_layer ( dtype , N , C , K , D , H , W , T , R , S , padding_d , padding_h , padding_w , strides_d , strides_h , strides_w ) #NEWLINE# assert ( conv_nc . dimI == conv_ng . dimI ) #NEWLINE# assert ( conv_nc . dimF == conv_ng . dimF ) #NEWLINE# assert ( conv_nc . dimO == conv_ng . dimO ) #NEWLINE# assert ( conv_nc . M == conv_ng . M ) #NEWLINE# dimI = conv_ng . dimI #NEWLINE# dimF = conv_ng . dimF #NEWLINE# dimO = conv_ng . dimO #NEWLINE# cpuI = np . random . uniform ( ( - 0.8 ) , 0.8 , slicable ( dimI , 1 ) ) . astype ( np . float32 ) #NEWLINE# cpuF = np . random . uniform ( 0.0 , 0.3 , slicable ( dimF ) ) . astype ( np . float32 ) #NEWLINE# cpuE = np . random . uniform ( ( - 0.2 ) , 0.2 , dimO ) . astype ( np . float32 ) #NEWLINE# cpuI [ ( - 1 ) , : ] = 0.0 #NEWLINE# beI = cpuI [ : ( - 1 ) , : ] . reshape ( dimI ) #NEWLINE# beF = cpuF . reshape ( dimF ) #NEWLINE# beE = cpuE #NEWLINE# start_gpu = default_timer ( ) #NEWLINE# ( ngO , ngB , ngU ) = run_backend_conv ( ng , conv_ng , beI , beF , beE , dtype ) #NEWLINE# end_gpu = default_timer ( ) #NEWLINE# start_cpu = default_timer ( ) #NEWLINE# ( ncO , ncB , ncU ) = run_backend_conv ( nc , conv_nc , beI , beF , beE , dtype ) #NEWLINE# end_cpu = default_timer ( ) #NEWLINE# print ( ( 'gputime: %s, cputime %s' % ( ( end_gpu - start_gpu ) , ( end_cpu - start_cpu ) ) ) ) #NEWLINE# cpuO = np . zeros ( dimO , dtype = dtype ) #NEWLINE# cpuB = np . zeros ( slicable ( dimI , 1 ) , dtype = dtype ) #NEWLINE# cpuU = np . zeros ( slicable ( dimF ) , dtype = dtype ) #NEWLINE# ( D , H , W ) = conv_nc . DHW #NEWLINE# ( T , R , S ) = conv_nc . TRS #NEWLINE# ( M , P , Q ) = conv_nc . MPQ #NEWLINE# ( pad_d , pad_h , pad_w ) = conv_nc . padding #NEWLINE# ( str_d , str_h , str_w ) = conv_nc . strides #NEWLINE# for m in range ( M ) : #NEWLINE# #INDENT# mt = ( ( m * str_d ) - pad_d ) #NEWLINE# for p in range ( P ) : #NEWLINE# #INDENT# pr = ( ( p * str_h ) - pad_h ) #NEWLINE# for q in range ( Q ) : #NEWLINE# #INDENT# qs = ( ( q * str_w ) - pad_w ) #NEWLINE# idx = pixel_indices ( conv_nc , mt , pr , qs ) #NEWLINE# cpuO [ : , m , p , q , : ] = np . dot ( cpuF . T , cpuI [ idx , : ] ) #NEWLINE# cpuB [ idx , : ] += np . dot ( cpuF , cpuE [ : , m , p , q , : ] ) #NEWLINE# cpuU += np . dot ( cpuI [ idx , : ] , cpuE [ : , m , p , q , : ] . T ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# for ( op , ngA , ncA , cpuA , w ) in ( ( 'fprop' , ngO , ncO , cpuO , Q ) , ( 'bprop' , ngB , ncB . reshape ( dimI ) , cpuB [ : ( - 1 ) , : ] . reshape ( dimI ) , H ) , ( 'update' , ngU , ncU . reshape ( dimF ) , cpuU . reshape ( dimF ) , S ) ) : #NEWLINE# #INDENT# print ( op ) #NEWLINE# ncAnp = ncA . get ( ) . astype ( np . float32 ) #NEWLINE# ngAnp = ngA . get ( ) . astype ( np . float32 ) #NEWLINE# ncdif = ( cpuA - ncAnp ) #NEWLINE# ngdif = ( cpuA - ngAnp ) #NEWLINE# maxval = abs ( cpuA ) . max ( ) #NEWLINE# ncmaxdif = abs ( ncdif ) . max ( ) #NEWLINE# ngmaxdif = abs ( ngdif ) . max ( ) #NEWLINE# ncRatio = ( ncmaxdif / maxval ) #NEWLINE# ngRatio = ( ngmaxdif / maxval ) #NEWLINE# assert ( ncRatio < 1e-05 ) #NEWLINE# assert ( ngRatio < 1e-05 ) #NEWLINE# assert allclose_with_out ( ncA . get ( ) , cpuA , rtol = 0 , atol = 0.0001 ) #NEWLINE# assert allclose_with_out ( ngA . get ( ) , cpuA , rtol = 0 , atol = 0.001 ) #NEWLINE# #UNINDENT# del ng #NEWLINE# del nc