A very specific and excellent question!
ONNX Runtime (ORT) is a high-performance inference engine for ONNX models, and getting debug symbols for the Python version can be super helpful for debugging issues, especially when you're working on complex model deployments.
Here's how you can obtain the debug symbols for the ONNX Runtime Python package (aka onnxruntime):
Method 1: Install the onnxruntime-debug package (Recommended)
The ONNX Runtime team publishes a separate package called onnxruntime-debug that contains the debug symbols. You can install it using pip:
pip install onnxruntime-debug
This package is built with debug symbols enabled, and it's synchronized with the regular onnxruntime package. Meaning, if you're using onnxruntime version 1.12.0, you should install onnxruntime-debug version 1.12.0 as well.
After installation, you'll find the debug symbols in the following location:
import onnxruntime
import os
debug_symbols_path = os.path.join(os.path.dirname(onnxruntime.__file__), 'onnxruntime.cpython-*.so.dSYM') # macOS/Linux
# or
debug_symbols_path = os.path.join(os.path.dirname(onnxruntime.__file__), 'onnxruntime.pyd.pdb') # Windows
print(debug_symbols_path)
Method 2: Build ONNX Runtime from source with debug symbols
If you're comfortable building C++ code and want the latest-and-greatest (or a specific commit), you can build ONNX Runtime from source with debug symbols enabled.
Here's a condensed guide:
- Clone the ONNX Runtime repository:
git clone https://github.com/microsoft/onnxruntime.git
cd onnxruntime
- Checkout the desired branch/tag (e.g.,
rel-1.12.0):
git checkout rel-1.12.0
-
Install build dependencies:
Follow the instructions for your OS: Windows, Linux, or macOS.
-
Configure the build with debug symbols:
In the tools/ci_build/build.py script, add the following flags:
- For Linux/macOS:
--cmake_extra_defines onnxruntime_DEBUG_SYMBOLS=ON
- For Windows:
--cmake_extra_defines onnxruntime_DEBUG_INFORMATION_FORMAT=ProgramDatabase
Example (Linux/macOS):
./build.sh --config Debug --build_shared_lib --cmake_extra_defines onnxruntime_DEBUG_SYMBOLS=ON
Example (Windows):
build.bat --config Debug --build_shared_lib --cmake_extra_defines onnxruntime_DEBUG_INFORMATION_FORMAT=ProgramDatabase
- Build and install the Python package:
cd build/Linux/Debug # or build/Windows/Debug
pip install -e python
The resulting onnxruntime Python package will contain debug symbols.
Method 3: Download pre-built debug binaries (only for older versions)
Microsoft used to publish debug symbol packages (e.g., onnxruntime-debug-1.8.0.zip) on the ONNX Runtime releases page. However, this practice was discontinued around version 1.9.0.
If you're stuck with an older version (<= 1.8.x), you can try downloading the corresponding debug symbol archive from the releases page.
Using the debug symbols
Once you've obtained the debug symbols using any of the methods above, you can:
- Attach a debugger (e.g.,
gdb, lldb, or Visual Studio) to the Python process.
- Load the symbols: Point your debugger to the
onnxruntime.cpython-*.so.dSYM (or onnxruntime.pyd.pdb on Windows) file.
- Set breakpoints in the ONNX Runtime C++ code (e.g., in
onnxruntime/core/session/inference_session.cc).
- Run your Python code with the
onnxruntime package imported.
Now, when the execution hits a breakpoint, you'll see the C++ source code, variables, and call stacks, making it much easier to debug issues!
Happy debugging!