some setitem tests (#16209)

This commit is contained in:
chenyu
2026-05-14 22:36:25 -04:00
committed by GitHub
parent 891a1ae7c2
commit a75c14f010
2 changed files with 13 additions and 0 deletions
+7
View File
@@ -344,6 +344,13 @@ class TestWithGrad(unittest.TestCase):
with self.assertRaises(RuntimeError):
z[:2] = Tensor([0.0, 0.0])
def test_setitem_mutates_buffer(self):
x = Tensor([1.0, 2.0, 3.0, 4.0]).realize()
y = x * 2.0
x[0] = 99.0
# TODO: either raise or match eager
np.testing.assert_allclose(y.numpy(), [198.0, 4.0, 6.0, 8.0])
class TestSetitemLoop(unittest.TestCase):
def test_arange(self):
N = 10
+6
View File
@@ -84,6 +84,12 @@ class TestTensorGradient(unittest.TestCase):
np.testing.assert_allclose(src.grad.numpy(), [2.0, 2.0, 2.0, 2.0])
np.testing.assert_allclose(x.grad.numpy(), [2.0, 2.0, 2.0, 2.0])
def test_setitem_on_grad_used_tensor_raises(self):
x = Tensor([1.0, 2.0, 3.0, 4.0], requires_grad=True).realize()
_ = (x * 2.0).sum()
with self.assertRaises(RuntimeError):
x[0] = 99.0
def test_gradient_through_chained_unrealized_setitem(self):
g1 = Tensor.zeros(4).contiguous()
g1[2] = Tensor(1.0)