BQuant [5] - Profiling Memory and Performance


Profiling

We run into a lot of cases where we need to optimize some code: it’s slow or consuming excessive memory.

Profiling tools help with this. In this post, we’ll show two tools:

  • cProfile: using %%prun cell magic in Jupyter notebooks.
  • memray: For memory usage
  • line_profiler: Another method to show line-by-line runtime
  • memory_profiler: A simple method to show total memory used by a function

* I would also like to show pystack, but it doesn’t currently work in BQuant. But it’s a great toolkit.

What’s cprofile?

cProfile is a performance profiler for Python. Use it to understand which calls are taking the most time or are being called most often. Using cProfile is straightforward, with the %%prun cell magic in Jupyter notebooks enabling inline profiling reports. This tool is built into Python, making it widely accessible without the need for additional installations.

What’s Memray?

Memray is a great tool for visualizing memory usage of Python processes. Memray only runs on Linux, or certain versions of OSX, but not on Windows.

It happens to also be published by Bloomberg, but it’s independent from BQuant nothing to do with BQuant.

Memray and BQuant

In BQuant, it’s possible to run into problems with very large models and memory consumption. Memray is a handy tool for quickly visualizing where the memory is going.

It’s a very useful tool for understanding and analyzing the memory consumption of large models in BQuant.

memray flamegraph Fig 1: Example of a FlameGraph

bqnt_profiling.ipynb

bqnt_profiling