Skip to content

feat(pytorch): record the SDPA backend and GPU kernels of PyTorch rows - #137

Merged
tvy-amd merged 7 commits into
mainfrom
fm/dnnbench-pytorch-dispatch
Nov 23, 2026
Merged

tvy-amd merged 7 commits into
mainfrom
fm/dnnbench-pytorch-dispatch

Conversation

@tvy-amd

@tvy-amd tvy-amd commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

A PyTorch row did not record what PyTorch ran. Under --pytorch-sdpa-backend default, the PyTorch dispatcher selects Flash, memory-efficient or math SDPA for each graph. Tuning (MIOpen find, TunableOp) can select other kernels. The result file kept only the requested selection, so a slow or wrong PyTorch row was hard to explain.

Change

With the new opt-in option --pytorch-dispatch (config key pytorch_dispatch, recorded as run.config.pytorch_dispatch), each PyTorch plan (ootb and oracle) gets a dispatch object:

"dispatch": {"sdpa_backend": "flash", "fa_library": "aotriton", "kernels": ["attn_fwd"]}
Key Value
sdpa_backend The PyTorch SDPA op that the launch called: flash, efficient, math, cudnn, overrideable. null when no SDPA op ran (no SDPA node, or the tool's own formula, for example the ALiBi backward).
fa_library ROCm builds with Flash only: the preferred library, aotriton or ck. A strict --pytorch-rocm-fa-library is applied as each call applies it.
kernels The distinct GPU kernel names of one launch, in order of first launch.

dispatch is null without --pytorch-dispatch, and on hipDNN, rocKE and AITER rows.

How the record is made (PyTorchCudaExecutor.capture_dispatch, execution/pytorch_dispatch.py):

  1. After the timed loop, the device is synchronized. Input-fill, warmup and cache-flush kernels are then not in the trace.
  2. One launch runs under torch.profiler (CPU and GPU activities) with the inputs that the timed loop primed. The first-call setup (fused-kernel probes, layout copies) does not run again.
  3. The Chrome trace gives the kernels (kernel events only, so no copies or fills) and the SDPA backend (the ATen SDPA cpu_op).
  • The launch is not timed. No timed sample changes.
  • On a reference row, the capture runs before the MATH reference output pass, so it records the timed selection.
  • With --oracle, the tuned child process records its own launch. oracle.dispatch thus shows the kernels after tuning. The child's capture warnings are copied to the row as tuned run: dispatch not recorded: ....
  • A failed capture never fails the row: dispatch (or dispatch.kernels for an empty trace) is null, and the row gets dispatch not recorded: <reason>.
  • KINETO_LOG_LEVEL is set to 6 when it is not set, so the profiler writes no lines on stderr.
  • When a torch.profiler session is already active in the process, the capture does not start a second one: Kineto runs one session at a time, and a nested session stopped the caller's session (seen in test_sdpa_validation_grades_with_repeatable_math_on_gpu). The check covers a session of the same thread (_profiler_enabled()) and a Python session of any thread (the private process-wide flag torch.autograd.profiler._is_profiler_enabled). The row then gets a dispatch not recorded warning.
  • Why opt-in: PyTorch has no public process-wide check. A capture from a worker thread while another thread profiles cancelled that trace, and the process crashed at its export (review feedback; reproduced with CPU torch 2.15). Without the option, no profiler session starts.
  • -v shows dispatch lines (SDPA backend and kernels) in the detail block.

Schema

The keys are additive inside schema 3 (graph_id geometry hash, from #138), so they need no version bump. Readers of schema 2 and 3 keep working. Files written before this change do not have the keys; read them as null. Documented in docs/results-schema.md (run.config.pytorch_dispatch, plan, new dispatch section, row warnings, version-history note), docs/methodology.md (PyTorch dispatch record) and docs/usage.md (option table, "PyTorch dispatch record" section, -v).

Tests

  • tests/unit/execution/test_pytorch_dispatch.py: kernel-list filtering and order, SDPA backend detection for each backend (forward and bound backward), mixed backends, a real CPU torch trace with a forced backend, and the ROCm Flash library rules (process preference, strict library, refused library, CUDA build).
  • Executor: the traced launch reuses the primed inputs, and the device is drained before the trace.
  • Suite runner: a failed capture and an empty trace give a warning and keep the row; the capture runs on the timed selection, not the MATH reference pass; without --pytorch-dispatch no capture runs and dispatch is null.
  • Oracle: oracle.dispatch and its warnings come from the tuned child; the child gets --pytorch-dispatch only when the run has it.
  • Nested session: a capture inside a caller's torch.profiler session refuses and the caller's session keeps recording, for the same thread and for a session of another thread (fails without the guard).
  • Schema 3 golden keys (test_schema_v3.py) include dispatch and run.config.pytorch_dispatch.
  • GPU (gpu marker): test_pytorch_sdpa_row_records_its_backend_and_kernels runs sample_sdpa.json (default, flash, efficient, math) and sample_sdpa_backward.json (default, efficient) with pytorch_dispatch=True. It checks a non-empty, distinct kernel list and that a strict selection is the recorded backend.

Local: pytest tests/unit -m "not gpu": 2148 passed, 17 skipped (no GPU on this host).

Profiler cost on the gfx950 runner (torch 2.16 nightly, ROCm 7.18), measured in a fresh process: the first profiler start takes about 0.8 s, later sessions about 1 ms. CPU-only sessions cost the same. A full gfx950 suite run with this change took 176 s.

AITER's setup.py imports aiter/jit/core.py, which reads the HIP version
from hipconfig on PATH, else from ROCM_HOME, else from ROCM_PATH, else
from /opt/rocm. With ROCm wheel torch, setup_env.py sets ROCM_PATH to the
hipDNN prefix (_rocm_sdk_libraries). That prefix has no bin/hipconfig, no
include/hip/hip_version.h and no .info/version. On a host without
/opt/rocm, the AITER stage stopped with "ROCm version file not found".

Set ROCM_HOME to the toolchain prefix (rocm-sdk-devel in rocm torch mode)
for the AITER build and prebuild, unless ROCM_HOME is already set. Keep
ROCM_PATH unchanged for plugin discovery and the activation script.
rocm-libraries 1423e5c (#14901) collapses the gfx950 dense attention
catalog to one variant per (head_size, dtype, mask, tile). Its descriptors
do not record num_query_heads or num_kv_heads: the kernel reads them as
arguments. catalog_kernels compared the missing fields with the graph's
head counts, so no shipped variant matched and only rocke:auto ran. The
gfx950 test test_rocke_rows_launch_hipdnn_kernels_and_match_pytorch
failed on rocm-libraries develop.

Compare the head counts only when the descriptor records them. Put the
graph's head counts into the spec of each matched variant, as for batch
and sequence lengths, so that rocKE's grid uses them.
@tvy-amd
tvy-amd requested a review from a team as a code owner November 19, 2026 18:53
@tvy-amd
tvy-amd force-pushed the fm/dnnbench-pytorch-dispatch branch 2 times, most recently from e885154 to 1670029 Compare November 19, 2026 20:13
A PyTorch row did not record what PyTorch ran. Under default dispatch,
PyTorch selects Flash, memory-efficient or math SDPA for each graph, and
tuning (MIOpen find, TunableOp) can select other kernels.

Each PyTorch plan now has a `dispatch` object, in `ootb` and in `oracle`:

- `sdpa_backend`: the PyTorch SDPA op that the launch called.
- `fa_library`: the preferred ROCm Flash library (aotriton or ck).
- `kernels`: the distinct GPU kernel names, in order of first launch.

After the timed loop, the executor runs one more launch of the primed timed
path under torch.profiler. The device is drained first, so input-fill,
warmup and flush kernels are not in the trace. The launch is not timed. A
failed capture gives a null value and a row warning; it does not fail the
row. The tuned child records its own launch for `oracle.dispatch`.

The key is additive, so the schema version stays 2. `-v` shows the
dispatch lines.
Kineto runs one profiler session at a time. A dispatch capture inside a
caller's session stopped that session. The capture now refuses, and the
row gets a dispatch warning.
@tvy-amd
tvy-amd force-pushed the fm/dnnbench-pytorch-dispatch branch from 1670029 to f2779aa Compare November 20, 2026 15:45
@tvy-amd
tvy-amd changed the base branch from main to fm/aiter-rocm-version November 20, 2026 15:46
Base automatically changed from fm/aiter-rocm-version to main November 23, 2026 18:09
…ispatch

# Conflicts:
#	tests/unit/reporting/test_schema_v3.py
The capture starts a torch.profiler session. Kineto runs one session per
process, and a second session cancels the first. The same-thread check
did not see a session that another thread started: the worker cancelled
the caller's trace, and the later export could crash the process.

- Add --pytorch-dispatch (off by default) and run.config.pytorch_dispatch.
  Without it, no capture runs and the plan's dispatch stays null.
- Forward the option to the tuned PyTorch child.
- Also check the process-wide flag of the Python profiler, so a Python
  torch.profiler session of another thread blocks the capture.
- Document the option, the limit and the new run.config key.
@tvy-amd
tvy-amd merged commit c0bb8a4 into main Nov 23, 2026
15 of 17 checks passed
@tvy-amd
tvy-amd deleted the fm/dnnbench-pytorch-dispatch branch November 23, 2026 22:14
GarbyGilbertAMD added a commit that referenced this pull request Nov 24, 2026
Bring in the rocm-libraries bump to 28f91cf (#142, #144), which fixes the
MIOpen plugin load with the current ROCm nightly, and the PyTorch SDPA
backend record (#137). The import conflicts in oracle.py and
pytorch_ops/__init__.py keep the imports of both sides.

ALMIOPEN-3178
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants