2020-12-07 02:34:40 +08:00
|
|
|
#!/usr/bin/env python
|
2020-12-09 06:18:45 +08:00
|
|
|
import gc
|
2020-12-07 02:34:40 +08:00
|
|
|
import unittest
|
2023-02-27 22:53:18 +08:00
|
|
|
import numpy as np
|
2024-09-26 08:26:31 +08:00
|
|
|
from tinygrad.device import Buffer
|
|
|
|
from tinygrad.engine.realize import run_schedule
|
2021-01-01 22:19:03 +08:00
|
|
|
from tinygrad.tensor import Tensor
|
2020-12-07 02:34:40 +08:00
|
|
|
|
2020-12-09 06:18:45 +08:00
|
|
|
def tensors_allocated():
|
2020-12-09 18:52:28 +08:00
|
|
|
return sum([isinstance(x, Tensor) for x in gc.get_objects()])
|
2020-12-18 06:37:31 +08:00
|
|
|
|
2024-09-26 08:26:31 +08:00
|
|
|
def bufs_allocated():
|
|
|
|
return sum([isinstance(x, Buffer) for x in gc.get_objects()])
|
|
|
|
|
2020-12-07 02:34:40 +08:00
|
|
|
class TestGC(unittest.TestCase):
|
2020-12-09 06:18:45 +08:00
|
|
|
|
2020-12-07 02:34:40 +08:00
|
|
|
def test_gc(self):
|
2024-09-24 16:07:44 +08:00
|
|
|
Tensor.manual_seed(0)
|
2024-03-19 04:47:07 +08:00
|
|
|
a = Tensor.rand(4, 4, requires_grad=True)
|
2022-09-21 23:16:02 +08:00
|
|
|
b = Tensor.zeros(4, 4, requires_grad=True)
|
2020-12-07 02:34:40 +08:00
|
|
|
(a*b).mean().backward()
|
2024-08-18 04:44:34 +08:00
|
|
|
assert (tensors_allocated() > 0)
|
2020-12-07 02:34:40 +08:00
|
|
|
del a,b
|
2024-10-07 02:46:58 +08:00
|
|
|
assert (tensors_allocated() == 2) # one for Tensor._device_rng_counters, and one for Tensor._device_seeds
|
|
|
|
Tensor.manual_seed(0)
|
2020-12-07 02:34:40 +08:00
|
|
|
|
|
|
|
def test_gc_complex(self):
|
2024-09-24 16:07:44 +08:00
|
|
|
Tensor.manual_seed(0)
|
2023-02-27 22:53:18 +08:00
|
|
|
a = Tensor(np.zeros((4, 4), dtype=np.float32), requires_grad=True)
|
2024-03-19 04:47:07 +08:00
|
|
|
b = Tensor.rand(4, 4, requires_grad=True)
|
2024-08-18 04:44:34 +08:00
|
|
|
assert (tensors_allocated() == 5)
|
2024-10-07 02:46:58 +08:00
|
|
|
(a*b).mean().backward()
|
|
|
|
assert (tensors_allocated() == 6)
|
2020-12-07 02:34:40 +08:00
|
|
|
del b
|
2024-10-07 02:46:58 +08:00
|
|
|
assert (tensors_allocated() == 4)
|
2023-02-27 22:53:18 +08:00
|
|
|
b = Tensor(np.zeros((4, 4), dtype=np.float32), requires_grad=True)
|
2020-12-09 06:18:45 +08:00
|
|
|
print(tensors_allocated())
|
2020-12-07 02:34:40 +08:00
|
|
|
(a*b).mean().backward()
|
2020-12-09 06:18:45 +08:00
|
|
|
print(tensors_allocated())
|
2024-10-07 02:46:58 +08:00
|
|
|
assert (tensors_allocated() == 6)
|
2020-12-07 02:34:40 +08:00
|
|
|
del b
|
2024-10-07 02:46:58 +08:00
|
|
|
assert (tensors_allocated() == 4)
|
|
|
|
Tensor.manual_seed(0)
|
2020-12-07 02:34:40 +08:00
|
|
|
|
2024-09-26 08:26:31 +08:00
|
|
|
def test_schedule_gc(self):
|
|
|
|
init = bufs_allocated()
|
|
|
|
x = Tensor.ones(256).contiguous().realize()
|
|
|
|
y = Tensor.ones(5, 5).contiguous()
|
|
|
|
y.schedule()
|
|
|
|
del x
|
|
|
|
del y
|
|
|
|
self.assertEqual(bufs_allocated()-init, 0)
|
|
|
|
|
|
|
|
def test_schedule_gc_with_inputs(self):
|
|
|
|
init = bufs_allocated()
|
|
|
|
x = Tensor.ones(256).contiguous().realize()
|
|
|
|
y = x+Tensor.ones(256).contiguous()
|
|
|
|
ys = y.schedule()
|
|
|
|
del x
|
|
|
|
run_schedule(ys)
|
|
|
|
np.testing.assert_equal(y.numpy(), np.full((256,), 2))
|
|
|
|
self.assertEqual(bufs_allocated()-init, 1)
|
|
|
|
del y
|
|
|
|
self.assertEqual(bufs_allocated()-init, 0)
|
|
|
|
|
2020-12-07 02:34:40 +08:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|