.. meta:: :description: ROCm Systems Profiler Python profiling documentation and reference :keywords: rocprof-sys, rocprofiler-systems, Omnitrace, ROCm, Python, profiling Python, profiler, tracking, visualization, tool, Instinct, accelerator, AMD **************************************************** Profiling Python scripts **************************************************** `ROCm Systems Profiler `_ supports profiling Python code at the source level and the script level. Python support is enabled via the ``ROCPROFSYS_USE_PYTHON`` and the ``ROCPROFSYS_PYTHON_VERSIONS=".`` CMake options. Alternatively, to build multiple Python versions, use ``ROCPROFSYS_PYTHON_VERSIONS=".;[.]"``, and ``ROCPROFSYS_PYTHON_ROOT_DIRS="/path/to/version;[/path/to/version]"`` instead of ``ROCPROFSYS_PYTHON_VERSION``. When building multiple Python versions, the length of the ``ROCPROFSYS_PYTHON_VERSIONS`` and ``ROCPROFSYS_PYTHON_ROOT_DIRS`` lists must be the same size. .. note:: When using ROCm Systems Profiler with Python programs, the Python interpreter major and minor version (e.g. 3.7) must match the interpreter major and minor version used when compiling the Python bindings. When building ROCm Systems Profiler, the shared object file ``libpyrocprofsys.----.so`` is generated where ``IMPL`` is the Python implementation, ``VERSION`` is the major and minor version, ``ARCH`` is the architecture, ``OS`` is the operating system, and ``ABI`` is the application binary interface, for example, ``libpyrocprofsys.cpython-38-x86_64-linux-gnu.so``. .. note:: Direct Perfetto output (using ``--trace`` or ``ROCPROFSYS_USE_TRACE=ON``) has limited support for Artificial Intelligence (AI) and Machine Learning (ML) workloads. Data from child threads is not captured. Instead, use ROCPD (``ROCPROFSYS_USE_ROCPD=ON``) as the output type. For more information, see the :ref:`rocprof_sys_rocpd_output` section. Getting started ======================================== The ROCm Systems Profiler Python package is installed in ``lib/pythonX.Y/site-packages/rocprofsys``. To ensure the Python interpreter can find the ROCm Systems Profiler package, add this path to the ``PYTHONPATH`` environment variable, as in the following example: .. code-block:: shell export PYTHONPATH=/opt/rocprofiler-systems/lib/python3.8/site-packages:${PYTHONPATH} Both the ``share/rocprofiler-systems/setup-env.sh`` script and the module file in ``share/modulefiles/rocprofiler-systems`` automatically handle the prefixing of the ``PYTHONPATH`` environment variable. .. note:: Profiling PyTorch and other AI workloads might fail because it is unable to find the libraries in the default linker path. As a workaround, you need to explicitly add the library path to ``LD_LIBRARY_PATH``. For example, when using PyTorch with Python 3.10, add the following to the environment: .. code-block:: shell export LD_LIBRARY_PATH=:/opt/venv/lib/python3.10/site-packages/torch/lib:$LD_LIBRARY_PATH Running ROCm Systems Profiler on a Python script ================================================ ROCm Systems Profiler provides an ``rocprof-sys-python`` helper bash script which ensures ``PYTHONPATH`` is properly set and the correct Python interpreter is used. This means the following commands are effectively equivalent: .. code-block:: shell rocprof-sys-python --help and .. code-block:: shell export PYTHONPATH=/opt/rocprofiler-systems/lib/python3.8/site-packages:${PYTHONPATH} python3.8 -m rocprofsys --help .. note:: ``rocprof-sys-python`` and ``python -m rocprofsys`` use the same command-line syntax as the other ``rocprof-sys`` executables (``rocprof-sys-python --