Skip to content

Remove redundant inner scope so that SPDA count once#1056

Merged
mgehre-amd merged 1 commit into
gfx11from
parthash/fix_vit-sdpa-profiler-double-count
Jul 13, 2026
Merged

Remove redundant inner scope so that SPDA count once#1056
mgehre-amd merged 1 commit into
gfx11from
parthash/fix_vit-sdpa-profiler-double-count

Conversation

@parthash0804

@parthash0804 parthash0804 commented Jul 13, 2026

Copy link
Copy Markdown

Purpose

The attention profiling scopes added in #996 cause the ViT TORCH_SDPA backend to report exactly 2x the number of attention calls (and ~2x "CUDA total") compared to the real number of ops. FLASH_ATTN and TRITON_ATTN report correct counts.

Example (Qwen/Qwen3-VL-4B-Instruct, identical config, only --mm-encoder-attn-backend differs, ViT shape H=16 D=64 KV_H=16):

Backend Calls / shape
FLASH_ATTN 24
TRITON_ATTN 24
TORCH_SDPA 48 ← wrong

Root cause

Every ViT backend is wrapped once at the encoder layer in MMEncoderAttention._forward_{sdpa,fa,triton} via create_attention_profiler_scope(...).

For FLASH_ATTN and TRITON_ATTN, the inner scope lives inside the direct_register_custom_op op body (flash_attn_maxseqlen_wrapper, triton_attn_wrapper), so key_averages() records one event per op.

For TORCH_SDPA, the inner scope was placed in the apply_sdpa helper, which is a plain function called by the torch_sdpa_wrapper custom op. Its record_function is recorded as a separate nested event with the same scope name as the encoder-layer scope (for ViT self-attention q_len == kv_len, so B/Q/S/H/D/KV_H all match). key_averages() aggregates both same-named events → every SDPA op is counted twice, inflating both "Calls" and "CUDA total" by 2x.

This is a measurement/counting artifact only — no extra GPU work is done.

Fix

Remove the redundant profiler scope inside apply_sdpa. The SDPA op is already covered by the _forward_sdpa encoder-layer scope, matching how FLASH_ATTN/TRITON_ATTN are counted. This also fixes per-segment shape mislabeling when apply_sdpa is called once per cu_seqlens chunk.

Test Result

  • FLASH_ATTN / TRITON_ATTN: unchanged (24 calls/shape)
  • TORCH_SDPA: 48 → 24 calls/shape
image

Signed-off-by: Parth Ashwin Jain <parthash@amd.com>

@mgehre-amd mgehre-amd left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@mgehre-amd mgehre-amd merged commit 79bb388 into gfx11 Jul 13, 2026
7 checks passed
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.

3 participants