Remove redundant inner scope so that SPDA count once#1056
Merged
Conversation
Signed-off-by: Parth Ashwin Jain <parthash@amd.com>
amd-callumm
approved these changes
Jul 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Purpose
The attention profiling scopes added in #996 cause the ViT
TORCH_SDPAbackend 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-backenddiffers, ViT shapeH=16 D=64 KV_H=16):Root cause
Every ViT backend is wrapped once at the encoder layer in
MMEncoderAttention._forward_{sdpa,fa,triton}viacreate_attention_profiler_scope(...).For FLASH_ATTN and TRITON_ATTN, the inner scope lives inside the
direct_register_custom_opop body (flash_attn_maxseqlen_wrapper,triton_attn_wrapper), sokey_averages()records one event per op.For TORCH_SDPA, the inner scope was placed in the
apply_sdpahelper, which is a plain function called by thetorch_sdpa_wrappercustom op. Itsrecord_functionis recorded as a separate nested event with the same scope name as the encoder-layer scope (for ViT self-attentionq_len == kv_len, soB/Q/S/H/D/KV_Hall 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_sdpaencoder-layer scope, matching how FLASH_ATTN/TRITON_ATTN are counted. This also fixes per-segment shape mislabeling whenapply_sdpais called once percu_seqlenschunk.Test Result