<s> import time <EOL> import numpy as np <EOL> import theano <EOL> import theano . tensor as T <EOL> from theano . sandbox . cuda . basic_ops import gpu_from_host , host_from_gpu <EOL> from pylearn2 . sandbox . cuda_convnet . filter_acts import FilterActs <EOL> from pylearn2 . sandbox . cuda_convnet . weight_acts import WeightActs <EOL> from pylearn2 . sandbox . cuda_convnet . img_acts import ImageActs <EOL> import cudarray as ca <EOL> def avg_running_time ( fun ) : <EOL> n_iter = <NUM_LIT:20> <EOL> start_time = time . time ( ) <EOL> for _ in range ( n_iter ) : <EOL> fun ( ) <EOL> duration = time . time ( ) - start_time <EOL> return duration / float ( n_iter ) <EOL> def allclose ( a , b ) : <EOL> atol = <NUM_LIT> <EOL> rtol = <NUM_LIT> <EOL> return np . allclose ( a , b , atol = atol , rtol = rtol ) <EOL> def benchmark ( n_imgs , n_channels , img_shape , n_filters , filter_shape , pad ) : <EOL> print ( '<STR_LIT>' <EOL> % ( ( n_imgs , n_channels ) + img_shape ) <EOL> + '<STR_LIT>' <EOL> % ( ( n_filters , ) + filter_shape + ( pad , ) ) ) <EOL> padding = ( pad , pad ) <EOL> strides = ( <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> img_h , img_w = img_shape <EOL> filter_h , filter_w = filter_shape <EOL> convout_h = img_h + <NUM_LIT:2> * pad - filter_h + <NUM_LIT:1> <EOL> convout_w = img_w + <NUM_LIT:2> * pad - filter_w + <NUM_LIT:1> <EOL> imgs_bc01_shape = ( n_imgs , n_channels , img_h , img_w ) <EOL> filters_bc01_shape = ( n_filters , n_channels , filter_h , filter_w ) <EOL> imgs_bc01 = np . random . randn ( n_imgs , n_channels , img_h , img_w ) <EOL> imgs_c01b = np . transpose ( imgs_bc01 , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:0> ) ) <EOL> filters_fc01 = np . random . randn ( n_filters , n_channels , filter_h , filter_w ) <EOL> filters_c01f = np . transpose ( filters_fc01 , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:0> ) ) <EOL> convout_bc01 = np . random . randn ( n_imgs , n_filters , convout_h , convout_w ) <EOL> convout_c01b = np . transpose ( convout_bc01 , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:0> ) ) <EOL> imgs_bc01_t = theano . shared ( imgs_bc01 . astype ( theano . config . floatX ) ) <EOL> imgs_c01b_t = theano . shared ( imgs_c01b . astype ( theano . config . floatX ) ) <EOL> filters_fc01_t = theano . shared ( filters_fc01 . astype ( theano . config . floatX ) ) <EOL> filters_c01f_t = theano . shared ( filters_c01f . astype ( theano . config . floatX ) ) <EOL> convout_bc01_t = theano . shared ( convout_bc01 . astype ( theano . config . floatX ) ) <EOL> convout_c01b_t = theano . shared ( convout_c01b . astype ( theano . config . floatX ) ) <EOL> imgs_bc01_ca = ca . array ( imgs_bc01 ) <EOL> filters_fc01_ca = ca . array ( filters_fc01 ) <EOL> convout_bc01_ca = ca . array ( convout_bc01 ) <EOL> print ( '<STR_LIT>' ) <EOL> convout_cc_op = FilterActs ( stride = <NUM_LIT:1> , partial_sum = <NUM_LIT:4> , pad = pad ) <EOL> convout_cc_expr = convout_cc_op ( imgs_c01b_t , filters_c01f_t ) <EOL> convout_cc_fun = theano . function ( [ ] , convout_cc_expr ) <EOL> convout_cc = convout_cc_fun ( ) <EOL> convout_cc = np . transpose ( convout_cc , ( <NUM_LIT:3> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> def convout_ca_fun ( ) : <EOL> convout = ca . nnet . conv_bc01 ( imgs_bc01_ca , filters_fc01_ca , padding , <EOL> strides ) <EOL> return convout <EOL> convout_ca = np . array ( convout_ca_fun ( ) ) <EOL> print ( '<STR_LIT>' + str ( allclose ( convout_ca , convout_cc ) ) ) <EOL> duration_cc = avg_running_time ( convout_cc_fun ) <EOL> duration_ca = avg_running_time ( convout_ca_fun ) <EOL> print ( '<STR_LIT>' <EOL> % ( duration_cc , duration_ca ) ) <EOL> print ( '<STR_LIT>' % ( duration_cc / duration_ca ) ) <EOL> del convout_cc_op <EOL> del convout_cc_expr <EOL> del convout_cc_fun <EOL> print ( '<STR_LIT>' ) <EOL> dimgs_cc_op = ImageActs ( stride = <NUM_LIT:1> , partial_sum = <NUM_LIT:1> , pad = pad ) <EOL> dimgs_cc_expr = dimgs_cc_op ( convout_c01b_t , filters_c01f_t ) <EOL> dimgs_cc_fun = theano . function ( [ ] , dimgs_cc_expr ) <EOL> dimgs_cc = dimgs_cc_fun ( ) <EOL> dimgs_cc = np . transpose ( dimgs_cc , ( <NUM_LIT:3> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> def dimgs_ca_fun ( ) : <EOL> return ca . nnet . conv_bc01_bprop_imgs ( filters_fc01_ca , convout_bc01_ca , <EOL> img_shape , padding , strides ) <EOL> dimgs_ca = np . array ( dimgs_ca_fun ( ) ) <EOL> print ( '<STR_LIT>' + str ( allclose ( dimgs_ca , dimgs_cc ) ) ) <EOL> duration_cc = avg_running_time ( dimgs_cc_fun ) <EOL> duration_ca = avg_running_time ( dimgs_ca_fun ) <EOL> print ( '<STR_LIT>' <EOL> % ( duration_cc , duration_ca ) ) <EOL> print ( '<STR_LIT>' % ( duration_cc / duration_ca ) ) <EOL> del dimgs_cc_op <EOL> del dimgs_cc_expr <EOL> del dimgs_cc_fun <EOL> dfilters_cc_op = WeightActs ( stride = <NUM_LIT:1> , partial_sum = <NUM_LIT:1> , pad = pad ) <EOL> dfilters_cc_expr = dfilters_cc_op ( imgs_c01b_t , convout_c01b_t , <EOL> T . as_tensor_variable ( filter_shape ) ) <EOL> dfilters_cc_fun = theano . function ( [ ] , dfilters_cc_expr ) <EOL> dfilters_cc = dfilters_cc_fun ( ) [ <NUM_LIT:0> ] <EOL> dfilters_cc = np . transpose ( dfilters_cc , ( <NUM_LIT:3> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> def dfilters_ca_fun ( ) : <EOL> return ca . nnet . conv_bc01_bprop_filters ( imgs_bc01_ca , convout_bc01_ca , <EOL> filter_shape , padding , strides ) <EOL> dfilters_ca = np . array ( dfilters_ca_fun ( ) ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' + str ( allclose ( dfilters_ca , dfilters_cc ) ) ) <EOL> duration_cc = avg_running_time ( dfilters_cc_fun ) <EOL> duration_ca = avg_running_time ( dfilters_ca_fun ) <EOL> print ( '<STR_LIT>' <EOL> % ( duration_cc , duration_ca ) ) <EOL> print ( '<STR_LIT>' % ( duration_cc / duration_ca ) ) <EOL> def run ( ) : <EOL> np . random . seed ( <NUM_LIT:1> ) <EOL> configurations = [ <EOL> ( <NUM_LIT> , <NUM_LIT:3> , ( <NUM_LIT:32> , <NUM_LIT:32> ) , <NUM_LIT> , ( <NUM_LIT:11> , <NUM_LIT:11> ) , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , ( <NUM_LIT:32> , <NUM_LIT:32> ) , <NUM_LIT> , ( <NUM_LIT:7> , <NUM_LIT:7> ) , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , ( <NUM_LIT:16> , <NUM_LIT:16> ) , <NUM_LIT> , ( <NUM_LIT:5> , <NUM_LIT:5> ) , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , ( <NUM_LIT:16> , <NUM_LIT:16> ) , <NUM_LIT> , ( <NUM_LIT:5> , <NUM_LIT:5> ) , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , ( <NUM_LIT:16> , <NUM_LIT:16> ) , <NUM_LIT> , ( <NUM_LIT:3> , <NUM_LIT:3> ) , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT:64> , <NUM_LIT:3> , ( <NUM_LIT> , <NUM_LIT> ) , <NUM_LIT> , ( <NUM_LIT:16> , <NUM_LIT:16> ) , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT:64> , <NUM_LIT> , ( <NUM_LIT:32> , <NUM_LIT:32> ) , <NUM_LIT:64> , ( <NUM_LIT:8> , <NUM_LIT:8> ) , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> , <NUM_LIT:32> , ( <NUM_LIT> , <NUM_LIT> ) , <NUM_LIT:64> , ( <NUM_LIT:6> , <NUM_LIT:6> ) , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , ( <NUM_LIT:16> , <NUM_LIT:16> ) , <NUM_LIT> , ( <NUM_LIT:8> , <NUM_LIT:8> ) , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , ( <NUM_LIT:32> , <NUM_LIT:32> ) , <NUM_LIT> , ( <NUM_LIT:4> , <NUM_LIT:4> ) , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT:5> , <NUM_LIT:3> , ( <NUM_LIT:5> , <NUM_LIT:5> ) , <NUM_LIT:16> , ( <NUM_LIT:3> , <NUM_LIT:3> ) , <NUM_LIT:1> ) , <EOL> ( <NUM_LIT:64> , <NUM_LIT:32> , ( <NUM_LIT:32> , <NUM_LIT:32> ) , <NUM_LIT:32> , ( <NUM_LIT:5> , <NUM_LIT:5> ) , <NUM_LIT:2> ) , <EOL> ( <NUM_LIT:64> , <NUM_LIT:1> , ( <NUM_LIT> , <NUM_LIT> ) , <NUM_LIT:32> , ( <NUM_LIT:7> , <NUM_LIT:7> ) , <NUM_LIT:4> ) , <EOL> ( <NUM_LIT:64> , <NUM_LIT:3> , ( <NUM_LIT:9> , <NUM_LIT:16> ) , <NUM_LIT:32> , ( <NUM_LIT:7> , <NUM_LIT:7> ) , <NUM_LIT:4> ) , <EOL> ( <NUM_LIT> , <NUM_LIT:3> , ( <NUM_LIT:32> , <NUM_LIT:32> ) , <NUM_LIT:64> , ( <NUM_LIT:5> , <NUM_LIT:5> ) , <NUM_LIT:2> ) , <EOL> ( <NUM_LIT> , <NUM_LIT:64> , ( <NUM_LIT:16> , <NUM_LIT:16> ) , <NUM_LIT:64> , ( <NUM_LIT:5> , <NUM_LIT:5> ) , <NUM_LIT:2> ) , <EOL> ( <NUM_LIT> , <NUM_LIT:64> , ( <NUM_LIT:8> , <NUM_LIT:8> ) , <NUM_LIT:64> , ( <NUM_LIT:5> , <NUM_LIT:5> ) , <NUM_LIT:2> ) , <EOL> ] <EOL> for conf in configurations : <EOL> benchmark ( * conf ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> run ( ) </s>
