================================
Quick Start with Graph Profiling
================================

.. include:: <isonum.txt>

PTI profiles the GPU work that applications run as graphs: Level Zero native graphs, SYCL\ |trade| graphs, and XPU graphs in PyTorch\ |reg|. PTI reports individual, node-level GPU operations through the same ``PTI_VIEW_DEVICE_GPU_*`` views as other GPU work.

On-Demand Profiling
-------------------

With on-demand profiling, you can enable profiling at any time, including after the graph is created or executed. PTI reports every execution of the graph while profiling is enabled. With native graphs on a supported GPU and driver (see `Graph Support`_), this works out of the box, without needing additional PTI API calls or environment variables.

Without on-demand profiling, PTI reports a graph only if profiling was enabled before the graph was created. Call ``ptiViewEnable`` for the ``PTI_VIEW_DEVICE_GPU_*`` views first, then create the graph.

Graph Support
-------------

On-demand profiling requires Level Zero driver version 1.15.39122+12 or newer. To check the version, run ``sycl-ls`` (the value in brackets on each ``level_zero:gpu`` line) or, in PyTorch, print ``torch.xpu.get_device_properties().driver_version``.

.. list-table::
   :header-rows: 1
   :stub-columns: 1
   :widths: 28 24 24 24

   * - Graph or command list
     - Linux\ |reg|
     - Windows
     - Intel\ |reg| Data Center GPU Max Series
   * - Level Zero native graph
     - On demand (1)
     - On demand (1)
     - Not supported
   * - SYCL graph created as a Level Zero native graph
     - On demand (1, 2)
     - On demand (1, 2)
     - Not supported
   * - XPU graph (``torch.xpu.XPUGraph``) in PyTorch 2.14 and newer
     - On demand (1, 2, 3)
     - On demand (1, 2, 3)
     - Uses legacy SYCL graphs (5)
   * - Legacy SYCL graph
     - On demand with environment variables (4)
     - Enable views first (5)
     - Enable views first (5)
   * - Level Zero command list created with ``ZE_COMMAND_LIST_FLAG_ENABLE_COMMAND_VISIT``
     - On demand (6)
     - On demand (6)
     - Not supported

1. Requires Intel\ |reg| Arc\ |trade| B-Series Graphics or newer GPUs. **No environment variables are needed.**
2. Requires the SYCL runtime from Intel\ |reg| oneAPI DPC++/C++ Compiler 2026.1.1 or newer. See `SYCL Graphs`_.
3. PyTorch 2.14 also needs an upgrade of ``intel-pti`` and ``dpcpp-cpp-rt``. See `PyTorch XPU Graphs`_.
4. Set ``NEOReadDebugKeys=1`` and ``ExperimentalFlatCommandListApiRecording=1``. These environment variables do not work on Windows.
5. Call ``ptiViewEnable`` for the ``PTI_VIEW_DEVICE_GPU_*`` views before creating the graph. On-demand profiling is not available.
6. Create the command list with ``zeCommandListCreate`` and the ``ZE_COMMAND_LIST_FLAG_ENABLE_COMMAND_VISIT`` flag. No environment variables are needed.

SYCL Graphs
-----------

A SYCL graph is a Level Zero native graph when it is created with the ``enable_native_recording`` property, and a legacy SYCL graph otherwise. Native recording requires an in-order queue; on other queues, recording throws a ``sycl::exception``:

.. code-block:: c++

   namespace sycl_ext = sycl::ext::oneapi::experimental;

   sycl::queue queue{sycl::property::queue::in_order{}};

   // Level Zero native graph
   sycl_ext::command_graph native_graph{
       queue, {sycl_ext::property::graph::enable_native_recording{}}};

   // Legacy SYCL graph
   sycl_ext::command_graph legacy_graph{queue};

In SYCL and Level Zero applications, enable the views with the ``ptiView`` API as shown in :doc:`quickstart`.

PyTorch XPU Graphs
------------------

PyTorch 2.14 and newer captures XPU graphs (``torch.xpu.XPUGraph`` and ``torch.xpu.graph``) as Level Zero native graphs by default, except on Intel Data Center GPU Max Series, where it uses legacy SYCL graphs.

PyTorch 2.15 and newer needs no upgrade: PyTorch 2.15 installs ``intel-pti`` 1.1.0 and SYCL runtime 2026.1.2, which profile native graphs.

PyTorch 2.14 installs ``intel-pti`` 1.0.1 and SYCL runtime 2026.1.0, which do not profile native graphs. As a workaround, upgrade both in the PyTorch environment:

.. code-block:: bash

   pip install --upgrade "intel-pti==1.2.1" "dpcpp-cpp-rt==2026.1.1"

pip reports dependency conflicts with the versions PyTorch 2.14 pins; the upgrade still succeeds.

Example: Profiling a PyTorch XPU Graph
--------------------------------------

This script captures an XPU graph before the profiler starts, then profiles five replays of it. With on-demand profiling (see `Graph Support`_), the trace shows the graph's kernels for every replay:

.. code-block:: python

   import torch
   from torch.profiler import ProfilerActivity, profile

   x = torch.randn(512, 512, device="xpu")

   # Warm up on a side stream before capturing the graph.
   s = torch.xpu.Stream()
   s.wait_stream(torch.xpu.current_stream())
   with torch.xpu.stream(s):
       y = torch.relu(x @ x) + 1
   torch.xpu.current_stream().wait_stream(s)

   # Capture the graph before the profiler starts.
   g = torch.xpu.XPUGraph()
   with torch.xpu.graph(g):
       y = torch.relu(x @ x) + 1

   # Profile five replays of the graph.
   with profile(activities=[ProfilerActivity.CPU, ProfilerActivity.XPU]) as prof:
       for _ in range(5):
           g.replay()
       torch.xpu.synchronize()

   prof.export_chrome_trace("xpu_graph_trace.json")

Open ``xpu_graph_trace.json`` in Perfetto, as described in :doc:`quickstart_pytorch`. Each replay appears as one ``urEnqueueGraphExp`` call, and the graph's three kernels appear for every replay. The first execution of a graph is slow while profiling is active; when measuring overhead, exclude it (see :doc:`knownissues`).
