mirror of
https://github.com/tinygrad/tinygrad.git
synced 2026-01-10 07:28:15 -05:00
* fixed pylint, formatted python files iwth cblack on localhost
* Revert "fixed pylint, formatted python files iwth cblack on localhost"
This reverts commit 07e2b88466.
* dedented 4-spaces added linter
Co-authored-by: Iain Wong <iainwong@outlook.com>
47 lines
1.2 KiB
Python
47 lines
1.2 KiB
Python
#!/usr/bin/env python
|
|
import gc
|
|
import unittest
|
|
from tinygrad.tensor import Tensor, GPU, Device
|
|
from .config import ANE
|
|
|
|
def tensors_allocated():
|
|
return sum([isinstance(x, Tensor) for x in gc.get_objects()])
|
|
|
|
class TestGC(unittest.TestCase):
|
|
device = Device.CPU
|
|
|
|
def test_gc(self):
|
|
a = Tensor.zeros(4,4, device=self.device)
|
|
b = Tensor.zeros(4,4, device=self.device)
|
|
(a*b).mean().backward()
|
|
assert(tensors_allocated() > 0)
|
|
del a,b
|
|
assert(tensors_allocated() == 0)
|
|
|
|
def test_gc_complex(self):
|
|
a = Tensor.zeros(4,4, device=self.device)
|
|
b = Tensor.zeros(4,4, device=self.device)
|
|
assert(tensors_allocated() == 2)
|
|
(a*b).mean().backward()
|
|
assert(tensors_allocated() == 4)
|
|
del b
|
|
assert(tensors_allocated() == 2)
|
|
b = Tensor.zeros(4,4, device=self.device)
|
|
print(tensors_allocated())
|
|
(a*b).mean().backward()
|
|
print(tensors_allocated())
|
|
assert(tensors_allocated() == 4)
|
|
del b
|
|
assert(tensors_allocated() == 2)
|
|
|
|
@unittest.skipUnless(GPU, "Requires GPU")
|
|
class TestGCGPU(TestGC):
|
|
device = Device.GPU
|
|
|
|
@unittest.skipUnless(ANE, "Requires ANE")
|
|
class TestGCANE(TestGC):
|
|
device=Device.ANE
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|