catalyst icon indicating copy to clipboard operation
catalyst copied to clipboard

Add support for debugging PennyLane (Python) and Catalyst (C++) simultaneously

Open mlxd opened this issue 7 months ago • 5 comments

Context: Debugging multi-language programming environments can be a challenge due to the intermingling and swapping between all participating languages. In the case of PennyLane and Catalyst, this is a mix of Python and C++, through our own libraries all the way through LLVM and back. Interactive debugging of a multi-language environment can require some custom setup to ensure the environment allows for useful interactive data inspection and validation.

VSCode provides support for mixed-mode debugging where a single editor can support (through the Python and C++ debugging extensions) interactive breakpointing, stepping, and inspection of the running program, where the view can switch between the Python debugger and the C++ debugger, once running. This PR adds preliminary support for this mixed-mode debugging, allowing the user to set breakpoints in their PennyLane program their Catalyst (+/- LLVM) internals, and step all the way through to investigate all aforementioned stages.

Some notes:

  • Use of this functionality requires building Catalyst with debug symbols. This can be achieved via make all BUILD_TYPE="RelWithDebInfo", and may generated approximately 80GB of libraries and binaries. Added internal (BUILD_TYPE) and external (BUILD_TYPE_EXT) configuration for build-flags to restrict debug symbols to our libs and bins only.
  • Launching the C++ debugger requires attaching to a running process. This requires sudo privileges.
  • The spawned compiler subprocess requires receipt of a SIGCONT to continue executing after the C++ debugger has attached.
  • ~~Setting the active qml.qjit(debug_compiler=True) option will not enable the support unless the Python process is called from a debugger (pdb, debugby, etc)~~ This restriction has been removed.

Description of the Change: As above.

Benefits: Enables debugging of mixed mode (Python + C++/LLVM) programs directly through VSCode.

Possible Drawbacks:

Related GitHub Issues:

mlxd avatar May 02 '25 20:05 mlxd

Use of this functionality requires building Catalyst with debug symbols. This can be achieved via make all BUILD_TYPE="RelWithDebInfo", and may generated approximately 80GB of libraries and binaries.

To mitigate one could avoid building llvm itself with debug symbols, the catalyst code base itself is not that bad in debug mode 🤔

dime10 avatar May 02 '25 22:05 dime10

Use of this functionality requires building Catalyst with debug symbols. This can be achieved via make all BUILD_TYPE="RelWithDebInfo", and may generated approximately 80GB of libraries and binaries.

To mitigate one could avoid building llvm itself with debug symbols, the catalyst code base itself is not that bad in debug mode 🤔

This is a good point; I may try to restrict the debug symbols to the Catalyst additions and see what savings we get.

mlxd avatar May 02 '25 23:05 mlxd

Use of this functionality requires building Catalyst with debug symbols. This can be achieved via make all BUILD_TYPE="RelWithDebInfo", and may generated approximately 80GB of libraries and binaries.

To mitigate one could avoid building llvm itself with debug symbols, the catalyst code base itself is not that bad in debug mode 🤔

This is a good point; I may try to restrict the debug symbols to the Catalyst additions and see what savings we get.

This works, so extended the Makefile to offer BUILD_TYPE for our stuff, and BUILD_TYPE_EXT for all external packages. that way we can control both as needed.

mlxd avatar May 30 '25 19:05 mlxd

Codecov Report

:x: Patch coverage is 65.00000% with 7 lines in your changes missing coverage. Please review. :white_check_mark: Project coverage is 97.38%. Comparing base (0faff0b) to head (d61cb1f). :warning: Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
frontend/catalyst/compiler.py 60.00% 4 Missing and 2 partials :warning:
frontend/catalyst/debug/debugger.py 66.66% 1 Missing :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1712      +/-   ##
==========================================
- Coverage   97.44%   97.38%   -0.06%     
==========================================
  Files          90       91       +1     
  Lines       10399    10414      +15     
  Branches      962      965       +3     
==========================================
+ Hits        10133    10142       +9     
- Misses        212      217       +5     
- Partials       54       55       +1     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov[bot] avatar May 30 '25 22:05 codecov[bot]

Coming back to this, I would love to see this merged, as it's quite useful 🤩

Another thing that would be really useful is being able to debug the runtime as well, since this is limited to the compiler. For instance if errors appear in the runtime, the debugger will only stop on the line that calls the ctypes-loaded library function.

dime10 avatar Jul 17 '25 18:07 dime10