introspection.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # TODO: move the GRAPH and DEBUG stuff to here
  2. import gc
  3. from tinygrad.helpers import prod
  4. from tinygrad.lazy import LazyBuffer
  5. from tinygrad.device import Buffer
  6. from tinygrad import Tensor, GlobalCounters
  7. def print_objects():
  8. #gc.collect()
  9. tensors = [x for x in gc.get_objects() if isinstance(x, Tensor)]
  10. tensor_ram_used = sum([prod(x.shape)*4 for x in tensors])
  11. lazybuffers = [x for x in gc.get_objects() if isinstance(x, LazyBuffer)]
  12. gpubuffers = [x for x in gc.get_objects() if isinstance(x, Buffer) and hasattr(x, "_buf")]
  13. realized_buffers = [x.realized for x in lazybuffers if x.base == x and x.realized]
  14. gpubuffers_orphaned = [x for x in gpubuffers if x not in realized_buffers]
  15. print(f"{len(tensors)} tensors allocated in {tensor_ram_used/1e9:.2f} GB, GPU using {GlobalCounters.mem_used/1e9:.2f} GB")
  16. print(f"{len(lazybuffers)} lazybuffers {len(realized_buffers)} realized, {len(gpubuffers)} GPU buffers")
  17. print(f"{len(gpubuffers_orphaned)} GPU buffers are orphaned")
  18. cnt = 0
  19. for tb in gpubuffers_orphaned:
  20. bb = gc.get_referrers(tb)
  21. for b in bb:
  22. if b is not gpubuffers and b is not gpubuffers_orphaned:
  23. print(tb, "\nreference", type(b), str(b)[0:150])
  24. for x in gc.get_referrers(b):
  25. print("double reference", str(x)[0:100])
  26. print("\n")
  27. if cnt == 10:
  28. break
  29. cnt += 1
  30. for x in gpubuffers_orphaned:
  31. if getattr(x, '_buf', None): del x._buf
  32. if getattr(x, '_image', None): del x._image
  33. return len(gpubuffers_orphaned)
  34. """
  35. import gc
  36. def print_ram():
  37. print(GlobalCounters.mem_used/1e9, sum([prod(x.shape)*4 for x in gc.get_objects() if isinstance(x, Tensor)])/1e9)
  38. img_count = sum([x.is_image() for x in gc.get_objects() if isinstance(x, OpenCLBuffer)])
  39. print("img_count", img_count)
  40. buffer_bytes = sum([x.cl.size for x in gc.get_objects() if isinstance(x, CLBuffer)])
  41. image_bytes = sum([x.cl.row_pitch*x.cl.height for x in gc.get_objects() if isinstance(x, CLImage)])
  42. print("buffer bytes", buffer_bytes/1e9, "image bytes", image_bytes/1e9, "sum", (buffer_bytes+image_bytes)/1e9)
  43. """