def test_integer_sampling_cuda(): # Basic MappedConvolution layer layer = MappedConvolution(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size).double().cuda() # Run a forward and backward pass output, forward_time, backward_time, gradcheck_res = utils.mapped_conv_test( layer, weight=params.weights_unit(in_channels, out_channels), input=params.input_4x5().repeat(bs, in_channels, 1, 1), sample_map=params.sample_map0(), cuda=True) # Manually computed correct result correct_output = 2 + in_channels * torch.tensor( [[30, 25, 31, 39, 33], [49, 40, 40, 54, 43], [46, 35, 47, 26, 33], [50, 36, 27, 40, 45]]).double().cuda() # Assert gradient check has passed assert gradcheck_res # Assert outputs match testing.assert_allclose(output, correct_output)
def test_integer_sampling_cpu(): # Basic MappedConvolution layer layer = MappedConvolution(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size).double() w = params.weights_unit(in_channels, out_channels) in_4x5 = params.input_4x5() m = params.sample_map0() ''' m (the sample map) samples from in_4x5 (the input) as: in_4x5[0, 0, m[0, 0, 0, 1].long(), m[0, 0, 0, 0].long()] i.e. input[batch, channel, y, x] where y, x = sample_map's last dim coords and the sample_maps 3rd dim corresponds to the neighboring samples for each of the kernel's center points >>> in_4x5[0, 0, m[0,0,0,1].long(), m[0,0,0,0].long()] tensor(11., dtype=torch.float64) >>> in_4x5[0, 0, m[0,0,1,1].long(), m[0,0,1,0].long()] tensor(8., dtype=torch.float64) >>> in_4x5[0, 0, m[0,0,2,1].long(), m[0,0,2,0].long()] tensor(2., dtype=torch.float64) >>> in_4x5[0, 0, m[0,1,2,1].long(), m[0,1,2,0].long()] tensor(0., dtype=torch.float64) ''' # Run a forward and backward pass output, forward_time, backward_time, gradcheck_res = utils.mapped_conv_test( layer, weight=w, input=in_4x5.repeat(bs, in_channels, 1, 1), sample_map=m, cuda=False) # Manually computed correct result correct_output = 2 + in_channels * torch.tensor( [[30, 25, 31, 39, 33], [49, 40, 40, 54, 43], [46, 35, 47, 26, 33], [50, 36, 27, 40, 45]]).double() # Assert gradient check has passed assert gradcheck_res # Assert outputs match testing.assert_allclose(output, correct_output)
def test_max_pool_integer_sampling_cpu(): # Basic MaxPool layer layer = MappedMaxPool(kernel_size=kernel_size).double() # Run a forward and backward pass output, forward_time, backward_time, gradcheck_res = utils.mapped_pool_test( layer, input=params.input_4x5().repeat(bs, 1, 1, 1), sample_map=params.sample_map0(), cuda=False) # Manually computed correct result correct_output = torch.tensor([[11, 19, 14, 18, 13], [18, 17, 19, 18, 19], [19, 12, 19, 10, 17], [18, 19, 10, 17, 17]]).double() # Assert gradient check has passed assert gradcheck_res # Assert outputs match testing.assert_allclose(output, correct_output)
def test_avg_pool_integer_sampling_cuda(): # Basic MappedTransposedConvolution layer layer = MappedAvgPool(kernel_size=kernel_size).double().cuda() # Run a forward and backward pass output, forward_time, backward_time, gradcheck_res = utils.mapped_pool_test( layer, input=params.input_4x5().repeat(bs, 1, 1, 1), sample_map=params.sample_map0(), cuda=True) # Manually computed correct result correct_output = torch.tensor( [[7.5, 6.25, 7.75, 9.75, 8.25], [12.25, 10., 10., 13.5, 10.75], [11.5, 8.75, 11.75, 6.5, 8.25], [12.5, 9., 6.75, 10., 11.25] ], ).double().cuda() # Assert gradient check has passed assert gradcheck_res # Assert outputs match testing.assert_allclose(output, correct_output)
def test_layer_weights_cpu(): # Basic MappedConvolution layer layer = MappedConvolution(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size).double() # Run a forward and backward pass output, forward_time, backward_time, gradcheck_res = utils.mapped_conv_test( layer, weight=params.weights_0_25(in_channels, out_channels), input=params.input_ones().repeat(bs, in_channels, 1, 1), sample_map=params.sample_map0(), cuda=False) # Manually computed correct result correct_output = 2 + params.in_channels * 2.5 * torch.ones(1, 1, 4, 5).double() # Assert gradient check has passed assert gradcheck_res # Assert outputs match testing.assert_allclose(output, correct_output)