def run_compare_reference_larger_output(cuda, ttype):
     x3 = torch.randn(2, 3, 10, 15).type(ttype)
     xs3 = torch.LongTensor([[4, 5], [8, 6]])
     x1 = x3[0, :, :4, :5].clone().view(1, 3, 4, 5)
     x2 = x3[1, :, :8, :6].clone().view(1, 3, 8, 6)
     if cuda:
         x1 = x1.cuda()
         x2 = x2.cuda()
         x3 = x3.cuda()
         xs3 = xs3.cuda()
     else:
         x1 = x1.cpu()
         x2 = x2.cpu()
         x3 = x3.cpu()
         xs3 = xs3.cpu()
     x1 = x1.requires_grad_()
     x2 = x2.requires_grad_()
     x3 = x3.requires_grad_()
     # Compare forward
     y1 = torch_adaptive_avg_pool2d(x1, output_size=(20, 25))
     y2 = torch_adaptive_avg_pool2d(x2, output_size=(20, 25))
     y3 = adaptive_avgpool_2d(x3, output_sizes=(20, 25), batch_sizes=xs3)
     np.testing.assert_almost_equal(y3.data.cpu().numpy(),
                                    torch.cat([y1, y2]).data.cpu().numpy())
     # Compare backward
     dx1, dx2, = torch.autograd.grad(y1.sum() + y2.sum(), [x1, x2])
     dx3, = torch.autograd.grad(y3.sum(), [x3])
     ref = dx3.clone().zero_()
     ref[0, :, :4, :5] = dx1.data
     ref[1, :, :8, :6] = dx2.data
     np.testing.assert_almost_equal(dx3.data.cpu().numpy(),
                                    ref.data.cpu().numpy())
 def run_base(self, cuda, ttype):
     self.convert(cuda, ttype)
     x = self._x.detach().requires_grad_()
     xs = self._s.detach()
     y = adaptive_avgpool_2d(x, output_sizes=(1, 4), batch_sizes=xs)
     y.backward(self._dy, retain_graph=True)
     np.testing.assert_array_almost_equal(y.data.cpu(), self._expect_y)
     np.testing.assert_array_almost_equal(x.grad.data.cpu(),
                                          self._expect_dx)