forked from tinygrad/tinygrad
Formatted test_indexing (#2688)
* added tensor.clone() for more correct cloning behavior * some work and randint issue * formatted * final cleanups * oops, bug fix
This commit is contained in:
+1067
-848
File diff suppressed because it is too large
Load Diff
+3
-2
@@ -666,11 +666,13 @@ class TestOps(unittest.TestCase):
|
||||
|
||||
def test_slice_errors(self):
|
||||
a = Tensor.ones(4, 3)
|
||||
b = Tensor(2)
|
||||
with self.assertRaises(IndexError): a[1, 77, 77, 77] # IndexError: (finds too many indices before the out of bounds)
|
||||
with self.assertRaises(IndexError): a[1, 77] # IndexError: (out of bounds).
|
||||
with self.assertRaises(IndexError): a[1, -77]
|
||||
with self.assertRaises(IndexError): a[..., ...] # IndexError: only single ellipsis
|
||||
with self.assertRaises(ValueError): a[::0, 1] # no 0 strides
|
||||
with self.assertRaises(IndexError): b[:] # slice cannot be applied to a 0-dim tensor
|
||||
|
||||
def test_slice_ellipsis(self):
|
||||
helper_test_op([(3,3,3,3)], lambda x: x[..., 0], lambda x: x[..., 0])
|
||||
@@ -1312,8 +1314,7 @@ class TestOps(unittest.TestCase):
|
||||
# TODO: currently we do not support tensor indexing for list of list tensor
|
||||
# ex: torch.tensor([1,2])[[[[torch.tensor(1)]]]] -> tensor([[2]])
|
||||
# currently we return ValueError: setting an array element with a sequence.
|
||||
# E TypeError: only integer tensors of a single element can be converted to an index
|
||||
|
||||
# TypeError: only integer tensors of a single element can be converted to an index
|
||||
|
||||
def test_gather(self):
|
||||
# indices cannot have gradient
|
||||
|
||||
+3
-2
@@ -313,8 +313,8 @@ class Tensor:
|
||||
# 1. indices normalization and validation
|
||||
# treat internal tuples and lists as Tensors and standardize indices to list type
|
||||
if isinstance(indices, (tuple, list)):
|
||||
if isinstance(indices, list) and all(isinstance(i, int) for i in indices): indices = [Tensor(indices)] # special case <indices: List[int]>, a lil ugly
|
||||
else: indices = [Tensor(list(i)) if isinstance(i, (tuple, list)) else i for i in indices]
|
||||
if isinstance(indices, list) and all(isinstance(i, int) for i in indices): indices = [Tensor(indices, dtype=dtypes.int32, requires_grad=False, device=self.device)] # special case <indices: List[int]>, a lil ugly
|
||||
else: indices = [Tensor(list(i), dtype=dtypes.int32, requires_grad=False, device=self.device) if isinstance(i, (tuple, list)) else i for i in indices]
|
||||
else: indices = [indices]
|
||||
|
||||
# filter ellipsis and fill with slice(None) or fill rest of indices with slice(None)
|
||||
@@ -334,6 +334,7 @@ class Tensor:
|
||||
for dim,i in enumerate(indices_filtered): type_dim[type(i)].append(dim)
|
||||
|
||||
# validation! raise Errors
|
||||
if slice in type_dim and self.ndim == 0: raise IndexError("slice cannot be applied to a 0-dim tensor.")
|
||||
if len(ellipsis_idx) > 1: raise IndexError("an index can only have a single ellipsis ('...')")
|
||||
if float in type_dim: raise IndexError("float type is not valid index")
|
||||
if any(isinstance(i, slice) and i.step == 0 for i in indices): raise ValueError('slice step cannot be 0')
|
||||
|
||||
Reference in New Issue
Block a user