2022-09-07 05:29:23 +08:00
|
|
|
# TODO: move the GRAPH and DEBUG stuff to here
|
|
|
|
import gc
|
|
|
|
from tinygrad.helpers import prod
|
2022-10-26 04:58:03 +08:00
|
|
|
from tinygrad.lazy import LazyBuffer
|
2024-03-30 12:30:30 +08:00
|
|
|
from tinygrad.buffer import Buffer
|
|
|
|
from tinygrad import Tensor, GlobalCounters
|
2022-09-07 05:29:23 +08:00
|
|
|
|
|
|
|
def print_objects():
|
2023-01-11 11:16:02 +08:00
|
|
|
#gc.collect()
|
2022-09-07 05:29:23 +08:00
|
|
|
tensors = [x for x in gc.get_objects() if isinstance(x, Tensor)]
|
|
|
|
tensor_ram_used = sum([prod(x.shape)*4 for x in tensors])
|
|
|
|
lazybuffers = [x for x in gc.get_objects() if isinstance(x, LazyBuffer)]
|
2024-03-29 10:31:28 +08:00
|
|
|
gpubuffers = [x for x in gc.get_objects() if isinstance(x, Buffer) and hasattr(x, "_buf")]
|
2023-12-21 06:33:21 +08:00
|
|
|
realized_buffers = [x.realized for x in lazybuffers if x.base == x and x.realized]
|
2022-09-07 05:29:23 +08:00
|
|
|
gpubuffers_orphaned = [x for x in gpubuffers if x not in realized_buffers]
|
|
|
|
|
2023-02-11 13:13:29 +08:00
|
|
|
print(f"{len(tensors)} tensors allocated in {tensor_ram_used/1e9:.2f} GB, GPU using {GlobalCounters.mem_used/1e9:.2f} GB")
|
2022-09-07 05:29:23 +08:00
|
|
|
print(f"{len(lazybuffers)} lazybuffers {len(realized_buffers)} realized, {len(gpubuffers)} GPU buffers")
|
|
|
|
print(f"{len(gpubuffers_orphaned)} GPU buffers are orphaned")
|
|
|
|
|
|
|
|
cnt = 0
|
|
|
|
for tb in gpubuffers_orphaned:
|
|
|
|
bb = gc.get_referrers(tb)
|
|
|
|
for b in bb:
|
|
|
|
if b is not gpubuffers and b is not gpubuffers_orphaned:
|
2024-03-29 10:31:28 +08:00
|
|
|
print(tb, "\nreference", type(b), str(b)[0:150])
|
2022-09-07 05:29:23 +08:00
|
|
|
for x in gc.get_referrers(b):
|
2023-02-28 02:39:47 +08:00
|
|
|
print("double reference", str(x)[0:100])
|
|
|
|
print("\n")
|
2022-09-07 05:29:23 +08:00
|
|
|
if cnt == 10:
|
|
|
|
break
|
|
|
|
cnt += 1
|
|
|
|
|
|
|
|
for x in gpubuffers_orphaned:
|
|
|
|
if getattr(x, '_buf', None): del x._buf
|
|
|
|
if getattr(x, '_image', None): del x._image
|
|
|
|
|
2023-01-11 11:16:02 +08:00
|
|
|
return len(gpubuffers_orphaned)
|
|
|
|
|
2022-09-07 05:29:23 +08:00
|
|
|
"""
|
|
|
|
import gc
|
|
|
|
|
|
|
|
def print_ram():
|
2023-02-11 13:13:29 +08:00
|
|
|
print(GlobalCounters.mem_used/1e9, sum([prod(x.shape)*4 for x in gc.get_objects() if isinstance(x, Tensor)])/1e9)
|
2022-09-07 05:29:23 +08:00
|
|
|
img_count = sum([x.is_image() for x in gc.get_objects() if isinstance(x, OpenCLBuffer)])
|
|
|
|
print("img_count", img_count)
|
|
|
|
buffer_bytes = sum([x.cl.size for x in gc.get_objects() if isinstance(x, CLBuffer)])
|
|
|
|
image_bytes = sum([x.cl.row_pitch*x.cl.height for x in gc.get_objects() if isinstance(x, CLImage)])
|
|
|
|
print("buffer bytes", buffer_bytes/1e9, "image bytes", image_bytes/1e9, "sum", (buffer_bytes+image_bytes)/1e9)
|
|
|
|
"""
|