From d6b10ac4be0192d8aedff95b6559837e01031bf2 Mon Sep 17 00:00:00 2001 From: Chen-Yu Yang Date: Mon, 2 Mar 2026 17:18:27 -0500 Subject: [PATCH] test_derived_from_contiguous_before_assign --- test/unit/test_assign.py | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/test/unit/test_assign.py b/test/unit/test_assign.py index b78855ad6e59c..2987e43ca23e4 100644 --- a/test/unit/test_assign.py +++ b/test/unit/test_assign.py @@ -685,21 +685,36 @@ def test_chained_slice_copies(self): def test_swap_slices(self): """Swap two non-overlapping slices - requires reading both before writing.""" - # without .realize() on temps: values not captured before overwriting + # single realize: WAR detection orders contiguous reads before assign writes buf = Tensor([1, 2, 3, 4, 5, 6, 7, 8]).contiguous().realize() - left = buf[0:4].contiguous() # lazy - not captured yet - right = buf[4:8].contiguous() # lazy - not captured yet - buf[0:4].assign(right).realize() # this works - buf[4:8].assign(left).realize() # left now reads from modified buf! - np.testing.assert_equal(buf.numpy(), [5, 6, 7, 8, 5, 6, 7, 8]) # TODO: wrong! should be [5,6,7,8,1,2,3,4] + left = buf[0:4].contiguous() + right = buf[4:8].contiguous() + buf[0:4].assign(right) + buf[4:8].assign(left) + buf.realize() + np.testing.assert_equal(buf.numpy(), [5, 6, 7, 8, 1, 2, 3, 4]) - # with .realize() on temps: values captured before writes + def test_derived_from_contiguous_before_assign(self): + """Tensors derived from a contiguous read should see pre-assign data, same as the contiguous itself.""" buf = Tensor([1, 2, 3, 4, 5, 6, 7, 8]).contiguous().realize() - left = buf[0:4].contiguous().realize() - right = buf[4:8].contiguous().realize() - buf[0:4].assign(right).realize() - buf[4:8].assign(left).realize() - np.testing.assert_equal(buf.numpy(), [5, 6, 7, 8, 1, 2, 3, 4]) + left = buf[0:4].contiguous() + derived = left + 10 + right = buf[4:8].contiguous() + buf[0:4].assign(right) + Tensor.realize(buf, left, derived) + np.testing.assert_equal(left.numpy(), [1, 2, 3, 4]) + np.testing.assert_equal(derived.numpy(), [11, 12, 13, 14]) + + def test_view_of_contiguous_before_assign(self): + """A view of a contiguous read should see the same pre-assign data as the contiguous.""" + buf = Tensor([1, 2, 3, 4, 5, 6, 7, 8]).contiguous().realize() + c = buf[0:4].contiguous() + v = c[0:2] + right = buf[4:8].contiguous() + buf[0:4].assign(right) + Tensor.realize(buf, c, v) + np.testing.assert_equal(c.numpy(), [1, 2, 3, 4]) + np.testing.assert_equal(v.numpy(), [1, 2]) def test_reduction_after_partial_assign(self): """Reduction over buffer after partial assign - must see the assigned values."""