|
| 1 | +--- |
| 2 | +name: debug-ep-placement |
| 3 | +description: Debug ONNX Runtime node placement and CUDA kernel dispatch for LLM/VLM models — which nodes fall back to CPU, where Memcpy nodes appear, and which attention/gemm kernel a contrib op actually dispatches to. Use when a CUDA EP run is slower than expected, outputs differ across machines, or you need to confirm which kernel path executed. |
| 4 | +--- |
| 5 | + |
| 6 | +# Debugging EP Placement and CUDA Kernel Dispatch |
| 7 | + |
| 8 | +Two distinct questions, two toolsets: |
| 9 | + |
| 10 | +1. **Graph level** — which nodes run on `CUDAExecutionProvider` vs fall back |
| 11 | + to CPU, and where `MemcpyToHost`/`MemcpyFromHost` transfer nodes got |
| 12 | + inserted at the boundary. |
| 13 | +2. **Kernel level** — which backend an LLM contrib op (Attention, |
| 14 | + MultiHeadAttention, GroupQueryAttention) actually dispatched to: flash |
| 15 | + attention, memory-efficient (cutlass), cuDNN flash, TRT fused, lean, or the |
| 16 | + unfused fallback. |
| 17 | + |
| 18 | +## Step 1 — Node placement (graph level) |
| 19 | + |
| 20 | +Verbose session logging prints a "Node placements" section listing every node |
| 21 | +grouped by EP (`onnxruntime/core/framework/session_state.cc`): |
| 22 | + |
| 23 | +```python |
| 24 | +import onnxruntime as ort |
| 25 | +so = ort.SessionOptions() |
| 26 | +so.log_severity_level = 0 # VERBOSE |
| 27 | +so.optimized_model_filepath = "/tmp/optimized.onnx" # inspect in Netron |
| 28 | +sess = ort.InferenceSession(model, so, |
| 29 | + providers=["CUDAExecutionProvider", "CPUExecutionProvider"]) |
| 30 | +``` |
| 31 | + |
| 32 | +Read the placements for: |
| 33 | + |
| 34 | +- Compute nodes on `CPUExecutionProvider` — each one forces a device sync + |
| 35 | + transfer. For LLM decode loops this is usually the whole regression. |
| 36 | +- `MemcpyToHost` / `MemcpyFromHost` nodes — inserted automatically at EP |
| 37 | + boundaries; their count and position show exactly where data bounces. |
| 38 | + |
| 39 | +A profile gives the same answer quantitatively: every `Node` event carries |
| 40 | +`args.provider`, and the [ort-profile-analysis skill](../ort-profile-analysis/SKILL.md) |
| 41 | +prints a per-provider table plus Memcpy share. |
| 42 | + |
| 43 | +Common causes of CPU fallback for transformer models: |
| 44 | + |
| 45 | +- No CUDA kernel for the op/dtype combination — check `docs/OperatorKernels.md` |
| 46 | + (CUDA column) for the op and opset actually in the graph. |
| 47 | +- Shape/attribute constraints a kernel doesn't support (e.g. int64 compute, |
| 48 | + dynamic axes a fused op rejects). |
| 49 | +- The graph wasn't fused: run the transformer optimizer first |
| 50 | + (`python -m onnxruntime.transformers.optimizer --model_type <t> ...` or the |
| 51 | + fusion logic in `onnxruntime/python/tools/transformers/`) so Attention/ |
| 52 | + SkipLayerNorm/MatMulNBits contrib ops exist for CUDA to claim. |
| 53 | + |
| 54 | +## Step 2 — Attention kernel dispatch (kernel level) |
| 55 | + |
| 56 | +Attention backend selection is centralized in |
| 57 | +`onnxruntime/contrib_ops/cuda/bert/attention_kernel_options.cc`, driven by the |
| 58 | +CUDA provider option `sdpa_kernel` and the `ORT_*` env vars declared in |
| 59 | +`onnxruntime/contrib_ops/cpu/bert/attention_common.h`. |
| 60 | + |
| 61 | +To see what actually ran: |
| 62 | + |
| 63 | +```bash |
| 64 | +export ORT_ENABLE_ATTENTION_KERNEL_DEBUG_INFO=1 # prints chosen kernel per op |
| 65 | +``` |
| 66 | + |
| 67 | +To pin or ablate a backend (bisection of a perf/precision difference): |
| 68 | + |
| 69 | +```bash |
| 70 | +export ORT_DISABLE_FLASH_ATTENTION=1 |
| 71 | +export ORT_DISABLE_MEMORY_EFFICIENT_ATTENTION=1 |
| 72 | +export ORT_ENABLE_CUDNN_FLASH_ATTENTION=1 |
| 73 | +export ORT_ENABLE_LEAN_ATTENTION=1 |
| 74 | +# thresholds that silently change dispatch with sequence length: |
| 75 | +export ORT_MIN_SEQ_LEN_FLASH_ATTENTION_PACKED_QKV=<n> |
| 76 | +export ORT_MIN_SEQ_LEN_EFFICIENT_ATTENTION_FP32=<n> |
| 77 | +``` |
| 78 | + |
| 79 | +Remember dispatch also depends on SM architecture, dtype, head size, and |
| 80 | +past/present KV layout — a result that differs between two GPUs is often just |
| 81 | +two different backends. Always record the resolved kernel (debug-info output) |
| 82 | +next to any benchmark number. |
| 83 | + |
| 84 | +## Step 3 — Confirm with a profile |
| 85 | + |
| 86 | +Capture a profile (see [generate-profile](../generate-profile/SKILL.md)); with |
| 87 | +a `--enable_cuda_profiling` build the `Kernel`-category events name the actual |
| 88 | +CUDA kernels (e.g. `flash_fwd_kernel` vs `fmha_cutlassF_*`), which is ground |
| 89 | +truth when the debug-info flag isn't available in the installed wheel. |
| 90 | +`nsys profile` + `onnxruntime/test/python/transformers/parse_nsys.py` is the |
| 91 | +lower-overhead alternative for kernel-level timing. |
| 92 | + |
| 93 | +## Step 4 — Node input/output dumping (debug builds) |
| 94 | + |
| 95 | +For numerical divergence (not placement), build with |
| 96 | +`--cmake_extra_defines onnxruntime_DEBUG_NODE_INPUTS_OUTPUTS=1` and use the |
| 97 | +`ORT_DEBUG_NODE_IO_*` env vars |
| 98 | +(`onnxruntime/core/framework/debug_node_inputs_outputs_utils.h`): |
| 99 | +`ORT_DEBUG_NODE_IO_DUMP_NODE_PLACEMENT=1`, `ORT_DEBUG_NODE_IO_DUMP_OUTPUT_DATA=1`, |
| 100 | +`ORT_DEBUG_NODE_IO_NAME_FILTER=<substr>` to bisect the first diverging node |
| 101 | +between two configurations. |
| 102 | + |
| 103 | +## Quick Reference |
| 104 | + |
| 105 | +| Question | Tool | |
| 106 | +|---|---| |
| 107 | +| Which nodes fell back to CPU? | `log_severity_level=0` → "Node placements"; profile per-provider table | |
| 108 | +| Where do device↔host copies happen? | `MemcpyToHost/FromHost` nodes in optimized model / profile | |
| 109 | +| Which attention backend ran? | `ORT_ENABLE_ATTENTION_KERNEL_DEBUG_INFO=1`; Kernel events in profile | |
| 110 | +| Force a specific backend | `ORT_DISABLE_*` / `ORT_ENABLE_*` env vars, `sdpa_kernel` provider option | |
| 111 | +| First numerically diverging node | debug build + `ORT_DEBUG_NODE_IO_*` | |
0 commit comments