<s> """<STR_LIT>""" <EOL> import itertools as itt <EOL> import numpy as np <EOL> from neon import NervanaObject <EOL> from neon . layers . layer import Pooling <EOL> from tests . utils import allclose_with_out <EOL> def pytest_generate_tests ( metafunc ) : <EOL> np . random . seed ( <NUM_LIT:1> ) <EOL> if metafunc . config . option . all : <EOL> bsz_rng = [ <NUM_LIT:32> , <NUM_LIT:64> ] <EOL> else : <EOL> bsz_rng = [ <NUM_LIT> ] <EOL> if '<STR_LIT>' in metafunc . fixturenames : <EOL> fargs = [ ] <EOL> if metafunc . config . option . all : <EOL> fs_rng = [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:5> ] <EOL> pad_rng = [ <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> nifm_rng = [ <NUM_LIT:16> , <NUM_LIT:32> ] <EOL> in_sz_rng = [ <NUM_LIT:8> , <NUM_LIT:16> ] <EOL> else : <EOL> fs_rng = [ <NUM_LIT:2> , <NUM_LIT:4> ] <EOL> pad_rng = [ <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> nifm_rng = [ <NUM_LIT:8> ] <EOL> in_sz_rng = [ <NUM_LIT:8> ] <EOL> fargs_ = [ ] <EOL> for fs in fs_rng : <EOL> stride_rng = set ( [ <NUM_LIT:1> , fs / <NUM_LIT:2> , fs ] ) <EOL> fargs_ . append ( itt . product ( fs_rng , nifm_rng , pad_rng , stride_rng , in_sz_rng , bsz_rng ) ) <EOL> fargs = itt . chain ( * fargs_ ) <EOL> metafunc . parametrize ( '<STR_LIT>' , fargs ) <EOL> def ref_pooling ( inp , inp_shape , fshape , padding , strides , be , ncheck = None ) : <EOL> inp_lshape = list ( inp_shape ) <EOL> bsz = inp . shape [ - <NUM_LIT:1> ] <EOL> if ncheck is None : <EOL> check_inds = np . arange ( bsz ) <EOL> elif type ( ncheck ) is int : <EOL> check_inds = np . random . permutation ( bsz ) <EOL> check_inds = check_inds [ <NUM_LIT:0> : ncheck ] <EOL> else : <EOL> check_inds = ncheck <EOL> check_inds = np . sort ( check_inds ) <EOL> inp_lshape . append ( bsz ) <EOL> inpa = inp . get ( ) . reshape ( inp_lshape ) <EOL> outshape = ( inp_lshape [ <NUM_LIT:0> ] , <EOL> be . output_dim ( inp_lshape [ <NUM_LIT:1> ] , fshape [ <NUM_LIT:0> ] , padding , strides [ <NUM_LIT:0> ] , pooling = True ) , <EOL> be . output_dim ( inp_lshape [ <NUM_LIT:2> ] , fshape [ <NUM_LIT:1> ] , padding , strides [ <NUM_LIT:1> ] , pooling = True ) , <EOL> len ( check_inds ) ) <EOL> if padding > <NUM_LIT:0> : <EOL> padded_shape = ( inp_lshape [ <NUM_LIT:0> ] , <EOL> inp_lshape [ <NUM_LIT:1> ] + <NUM_LIT:2> * padding , <EOL> inp_lshape [ <NUM_LIT:2> ] + <NUM_LIT:2> * padding , <EOL> inp_lshape [ - <NUM_LIT:1> ] ) <EOL> inp_pad = np . zeros ( padded_shape ) <EOL> inp_pad [ : , padding : - padding , padding : - padding , : ] = inpa [ : , <NUM_LIT:0> : , <NUM_LIT:0> : , : ] <EOL> else : <EOL> inp_pad = inpa <EOL> out_exp = np . zeros ( outshape ) <EOL> for indC in range ( outshape [ <NUM_LIT:0> ] ) : <EOL> for indh in range ( outshape [ <NUM_LIT:1> ] ) : <EOL> hrng = ( indh * strides [ <NUM_LIT:0> ] , indh * strides [ <NUM_LIT:0> ] + fshape [ <NUM_LIT:0> ] ) <EOL> for indw in range ( outshape [ <NUM_LIT:2> ] ) : <EOL> wrng = ( indw * strides [ <NUM_LIT:1> ] , indw * strides [ <NUM_LIT:1> ] + fshape [ <NUM_LIT:1> ] ) <EOL> for cnt , indb in enumerate ( check_inds ) : <EOL> inp_check = inp_pad [ indC , hrng [ <NUM_LIT:0> ] : hrng [ <NUM_LIT:1> ] , wrng [ <NUM_LIT:0> ] : wrng [ <NUM_LIT:1> ] , indb ] <EOL> out_exp [ indC , indh , indw , cnt ] = np . max ( inp_check ) <EOL> return ( out_exp , check_inds ) <EOL> def test_padding ( backend_default , poolargs ) : <EOL> fshape , nifm , padding , stride , in_sz , batch_size = poolargs <EOL> NervanaObject . be . bsz = batch_size <EOL> inshape = ( nifm , in_sz , in_sz ) <EOL> insize = np . prod ( inshape ) <EOL> neon_layer = Pooling ( fshape = fshape , strides = stride , padding = padding ) <EOL> inp = neon_layer . be . array ( np . random . random ( ( insize , batch_size ) ) ) <EOL> inp . lshape = inshape <EOL> neon_layer . configure ( inshape ) <EOL> neon_layer . prev_layer = True <EOL> neon_layer . allocate ( ) <EOL> neon_layer . set_deltas ( [ neon_layer . be . iobuf ( inshape ) ] ) <EOL> out = neon_layer . fprop ( inp ) . get ( ) <EOL> ncheck = [ <NUM_LIT:0> , batch_size / <NUM_LIT:2> , batch_size - <NUM_LIT:1> ] <EOL> ( out_exp , check_inds ) = ref_pooling ( inp , inp . lshape , <EOL> ( fshape , fshape ) , <EOL> padding , <EOL> ( stride , stride ) , <EOL> neon_layer . be , <EOL> ncheck = ncheck ) <EOL> out_shape = list ( out_exp . shape [ <NUM_LIT:0> : <NUM_LIT:3> ] ) <EOL> out_shape . append ( batch_size ) <EOL> outa = out . reshape ( out_shape ) <EOL> assert allclose_with_out ( out_exp , outa [ : , : , : , check_inds ] , atol = <NUM_LIT:0.0> , rtol = <NUM_LIT:0.0> ) </s>
