setitem isinstance cleanup [pr] (#8932)

This commit is contained in:
chenyu
2025-02-06 11:44:57 -05:00
committed by GitHub
parent 81e241150a
commit 00d72a5144
+2 -3
View File
@@ -1095,7 +1095,6 @@ class Tensor(SimpleMathTrait):
def _getitem(self, indices, v: Optional[Tensor] = None) -> Tensor:
# wrap single index into a list
if (isinstance(indices, list) and all_int(indices)) or not isinstance(indices, (tuple, list)): indices = [indices]
# turn scalar Tensors into const val for int indexing if possible
x, indices = self, list(indices)
# filter ellipsis and fill with slice(None) or fill rest of indices with slice(None)
@@ -1228,8 +1227,8 @@ class Tensor(SimpleMathTrait):
return
# NOTE: check that setitem target is valid first
if not unwrap(self.lazydata.st).contiguous: raise RuntimeError("setitem target needs to be contiguous")
if not isinstance(v, (Tensor, float, int, bool)): raise TypeError(f"can't set a {type(v).__name__} to a Tensor")
if not isinstance(v, Tensor): v = Tensor(v, device=self.device, dtype=self.dtype)
if isinstance(v, get_args(ConstType)): v = Tensor(v, device=self.device, dtype=self.dtype)
if not isinstance(v, Tensor): raise TypeError(f"can't set a {type(v).__name__} to a Tensor")
if self.requires_grad or v.requires_grad: raise NotImplementedError("setitem with requires_grad is not supported")
res = self.realize()._getitem(indices, v)