Change:
Arguably, indexing a Tensor by a LongTensor with no dimension should return a LongTensor with no dimension. Consider the following case: This breaks indexing by indices when the list of indices is empty, which can be hard to detect. In my use-case, I use such a masking to select rows of a tensor corresponding to "valid" entries in a vector; I had not considered that it would not work when there are no valid entries.
Thanks! I'll take a look.

Discussion:
def test_broadcast_batched_matmul(self): TestTorch._test_broadcast_batched_matmul(self, lambda t: t.cuda()) <ins> def test_index(self): TestTorch._test_index(self, lambda t: t.cuda()) </ins> def test_advancedindex(self): TestTorch._test_advancedindex(self, lambda t: t.cuda())

sequence.add_(start - 1) return sequence.resize_(*size) <del> def test_index(self): reference = self._consecutive((3, 3, 3)) </del> <ins> @staticmethod def _test_index(self, conv_fn): def consec(size, start=1): sequence = torch.ones(int(torch.Tensor(size).prod(0)[0])).cumsum(0) sequence.add_(start - 1) return sequence.view(*size) reference = conv_fn(consec((3, 3, 3))) </ins> # empty tensor indexing <del> self.assertEqual(reference[torch.LongTensor()], reference.new()) </del> <ins> self.assertEqual(reference[conv_fn(torch.LongTensor())], reference.new()) </ins> <del> self.assertEqual(reference[0], self._consecutive((3, 3)), 0) self.assertEqual(reference[1], self._consecutive((3, 3), 10), 0) self.assertEqual(reference[2], self._consecutive((3, 3), 19), 0) self.assertEqual(reference[0, 1], self._consecutive((3,), 4), 0) self.assertEqual(reference[0:2], self._consecutive((2, 3, 3)), 0) </del> <ins> self.assertEqual(reference[0], consec((3, 3)), 0) self.assertEqual(reference[1], consec((3, 3), 10), 0) self.assertEqual(reference[2], consec((3, 3), 19), 0) self.assertEqual(reference[0, 1], consec((3,), 4), 0) self.assertEqual(reference[0:2], consec((2, 3, 3)), 0) </ins> self.assertEqual(reference[2, 2, 2], 27, 0) <del> self.assertEqual(reference[:], self._consecutive((3, 3, 3)), 0) </del> <ins> self.assertEqual(reference[:], consec((3, 3, 3)), 0) </ins> # indexing with Ellipsis self.assertEqual(reference[..., 2], torch.Tensor([[3, 6, 9],

self.assertEqual(reference[2, 2, 2, ...], 27, 0) self.assertEqual(reference[...], reference, 0) <del> reference_5d = self._consecutive((3, 3, 3, 3, 3)) </del> <ins> reference_5d = conv_fn(consec((3, 3, 3, 3, 3))) </ins> self.assertEqual(reference_5d[..., 1, 0], reference_5d[:, :, :, 1, 0], 0) self.assertEqual(reference_5d[2, ..., 1, 0], reference_5d[2, :, :, 1, 0], 0) self.assertEqual(reference_5d[2, 1, 0, ..., 1], reference_5d[2, 1, 0, :, 1], 0) self.assertEqual(reference_5d[...], reference_5d, 0) # LongTensor indexing <del> reference = self._consecutive((5, 5, 5)) idx = torch.LongTensor([2, 4]) </del> <ins> reference = conv_fn(consec((5, 5, 5))) idx = conv_fn(torch.LongTensor([2, 4])) </ins> self.assertEqual(reference[idx], torch.stack([reference[2], reference[4]])) # TODO: enable one indexing is implemented like in numpy # self.assertEqual(reference[2, idx], torch.stack([reference[2, 2], reference[2, 4]]))

self.assertEqual(reference[None, 2:5, None, None], reference.unsqueeze(0)[:, 2:5].unsqueeze(2).unsqueeze(2)) # indexing with step <del> reference = self._consecutive((10, 10, 10)) </del> <ins> reference = consec((10, 10, 10)) </ins> self.assertEqual(reference[1:5:2], torch.stack([reference[1], reference[3]], 0)) self.assertEqual(reference[1:6:2], torch.stack([reference[1], reference[3], reference[5]], 0)) self.assertEqual(reference[1:9:4], torch.stack([reference[1], reference[5]], 0))

torch.stack([reference[:, 2, 1], reference[:, 2, 3], reference[:, 2, 5]], 1)) lst = [list(range(i, i + 10)) for i in range(0, 100, 10)] <del> tensor = torch.DoubleTensor(lst) </del> <ins> tensor = conv_fn(torch.DoubleTensor(lst)) </ins> for _i in range(100): idx1_start = random.randrange(10) idx1_end = idx1_start + random.randrange(1, 10 - idx1_start + 1)

self.assertRaises(TypeError, lambda: reference[0.0, ..., 0.0:2.0]) self.assertRaises(TypeError, lambda: reference[0.0, :, 0.0]) <ins> def test_index(self): self._test_index(self, lambda x: x) </ins> @staticmethod def _test_advancedindex(self, conv_fn): # Tests for Integer Array Indexing, Part I - Purely integer array