diff --git a/test/test_gc.py b/test/test_gc.py index a51c7adf18..9e7d540116 100644 --- a/test/test_gc.py +++ b/test/test_gc.py @@ -86,15 +86,28 @@ class TestGC(unittest.TestCase): a.realize() real_buf = a.lazydata.buffer # after the Tensor UOp is deleted there shouldn't be any references on the Buffer - with self.assertRaises(AssertionError): - self.assertEqual(real_buf.lb_refcount, 1) + self.assertEqual(real_buf.lb_refcount, 1) self.assertEqual(bufs_allocated()-init, 1) del a.lazydata - with self.assertRaises(AssertionError): - self.assertEqual(real_buf.lb_refcount, 0) + self.assertEqual(real_buf.lb_refcount, 0) self.assertEqual(bufs_allocated()-init, 1) # keep the buffer alive del real_buf self.assertEqual(bufs_allocated()-init, 0) + def test_assign_refcount(self): + init = bufs_allocated() + a = Tensor.full((4,), 1.).contiguous() + a.realize() + real_buf = a.lazydata.buffer + self.assertEqual(real_buf.lb_refcount, 1) + a.assign(Tensor.full((4,), 2.)) + self.assertIs(a.lazydata.src[0].buffer, real_buf) + # NOTE: this is still 1, we don't count the ASSIGN + self.assertEqual(real_buf.lb_refcount, 1) + a.realize() + del a + self.assertEqual(real_buf.lb_refcount, 0) # no UOps for this Buffer + self.assertEqual(bufs_allocated()-init, 1) # Buffer is alive + if __name__ == '__main__': unittest.main() diff --git a/tinygrad/engine/schedule.py b/tinygrad/engine/schedule.py index ada1aa6d8f..e2d45d7446 100644 --- a/tinygrad/engine/schedule.py +++ b/tinygrad/engine/schedule.py @@ -474,8 +474,6 @@ def create_schedule_with_vars(big_sink:UOp) -> tuple[list[ScheduleItem], dict[Va # TODO: move this to create_kernels k = fix_kernel_ast(u.src[1], var_vals) schedule.append(ScheduleItem(k.arg.ast, tuple(s.buf_uop.buffer for s in k.src), k.arg.metadata)) - # increment the refcount of the target buf (this is required by the JIT and memory planner) TODO: this does not belong here - k.src[0].buffer.ref(1) for x in children.get(u, []): in_degree[x] -= 1 if in_degree[x] == 0: queue.append(x) diff --git a/tinygrad/ops.py b/tinygrad/ops.py index c592e63925..554428f927 100644 --- a/tinygrad/ops.py +++ b/tinygrad/ops.py @@ -535,6 +535,7 @@ class UOp(MathTrait, metaclass=UOpMetaClass): from tinygrad.device import Buffer assert isinstance(self.device, str), f"buffer not supported on multi {self.device}" buffers[self] = ret = Buffer(self.device, self.size, self.dtype if isinstance(self.dtype, ImageDType) else self.dtype.base) + ret.ref(1) return ret @property def realized(self) -> Optional[Buffer]: return self.buffer if self.op is Ops.BUFFER and self.buffer.is_allocated() else None