Skip to content

Commit d7c754c

Browse files
authored
failing test for UOp buffer ref count (tinygrad#9563)
* failing test for UOp buffer ref count * lint
1 parent f90001e commit d7c754c

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

test/test_gc.py

+17
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,22 @@ def test_toposort_blocks_gc(self):
7979
print(inspect.getclosurevars(UOp.toposort.fget))
8080
raise AssertionError(f"never gced {[x for x in gc.get_objects() if isinstance(x, Buffer)]}")
8181

82+
def test_buffer_refcount(self):
83+
init = bufs_allocated()
84+
a = Tensor.empty(10)
85+
self.assertEqual(bufs_allocated()-init, 0)
86+
a.realize()
87+
real_buf = a.lazydata.buffer
88+
# after the Tensor UOp is deleted there shouldn't be any references on the Buffer
89+
with self.assertRaises(AssertionError):
90+
self.assertEqual(real_buf.lb_refcount, 1)
91+
self.assertEqual(bufs_allocated()-init, 1)
92+
del a.lazydata
93+
with self.assertRaises(AssertionError):
94+
self.assertEqual(real_buf.lb_refcount, 0)
95+
self.assertEqual(bufs_allocated()-init, 1) # keep the buffer alive
96+
del real_buf
97+
self.assertEqual(bufs_allocated()-init, 0)
98+
8299
if __name__ == '__main__':
83100
unittest.main()

0 commit comments

Comments
 (0)