Skip to content

Instantly share code, notes, and snippets.

@brisvag
Created September 14, 2023 15:50
Show Gist options
  • Save brisvag/b46f21df0519ee7c14c37b8f77a4ac4f to your computer and use it in GitHub Desktop.
Save brisvag/b46f21df0519ee7c14c37b8f77a4ac4f to your computer and use it in GitHub Desktop.
Print info about allocated tensors in outer scopes
import gc
import inspect
import torch
def _print_tensors(depth=2):
"""Print info about tensor variables locally and nonlocally."""
print('='*80)
for obj in gc.get_objects():
frame = inspect.currentframe()
frames = []
for i in range(depth):
frame = frame.f_back
frames.append(frame)
try:
if torch.is_tensor(obj) or (hasattr(obj, 'data') and torch.is_tensor(obj.data)):
for gobj, gobjid in globals().items():
if gobjid is obj and gobj not in ('obj', 'gobjid'):
print('GLOBAL:\t', gobj, obj.size(), obj.data_ptr())
for frame in frames:
fname = frame.f_code.co_name
locs = frame.f_locals
for gobj, gobjid in locs.items():
if gobjid is obj and gobj not in ('obj', 'gobjid'):
print(f'{fname}():\t', gobj, obj.size(), obj.data_ptr())
except:
pass
print('='*80)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment