torch_mem_profile.py 778 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. __author__ = "Christian Heider Nielsen"
  4. __doc__ = r"""
  5. Created on 31-10-2020
  6. """
  7. from pathlib import Path
  8. import torch
  9. from pytorch_memlab import LineProfiler
  10. from warg import ensure_existence
  11. if __name__ == "__main__":
  12. def inner() -> None:
  13. """
  14. :rtype: None
  15. """
  16. torch.nn.Linear(100, 100).cuda()
  17. def outer() -> None:
  18. """
  19. :rtype: None
  20. """
  21. linear = torch.nn.Linear(100, 100).cuda()
  22. linear2 = torch.nn.Linear(100, 100).cuda()
  23. inner()
  24. with LineProfiler(outer, inner) as prof:
  25. outer()
  26. with open(ensure_existence(Path("exclude")) / "test.html", "w") as f:
  27. f.write(prof.display()._repr_html_())