feat(perfmodel): add varlen aiter FA + aten::_flash_attention_forward perf models (Wan 2.2 coverage) - #651
feat(perfmodel): add varlen aiter FA + aten::_flash_attention_forward perf models (Wan 2.2 coverage)#651gphuang wants to merge 18 commits into
Conversation
…tion_forward perf models Closes #290, #590, #650. Sub-issue of #516 (Primus op-group coverage). Adds shape-based perf models for the varlen FlashAttention ops used by diffusion training (Wan 2.x) and sglang/vLLM inference, plus the PyTorch dispatcher-level aten Flash-Attention forward used elsewhere in the training stack: - aiter__fmha_v3_varlen_fwd -> aiter::fmha_v3_varlen_fwd - aiter__fmha_v3_varlen_forward -> aiter::wrapper_fmha_v3_varlen_fwd - aiter__fmha_v3_varlen_bwd -> aiter::fmha_v3_varlen_bwd - aiter__fmha_v3_varlen_backward -> aiter::wrapper_fmha_v3_varlen_bwd - aiter__fmha_v3_bwd -> aiter::fmha_v3_bwd (non-varlen, was #590) - aten___flash_attention_forward -> aten::_flash_attention_forward All inherit from SDPA so that categorize_torch_op routes them to SDPA_fwd / SDPA_bwd. Backward FLOPs use SDPA.flops_bwd_func with flash_impl=True (5/2 * fwd_flops for the square N_Q=N_KV case; exercised by the new test_varlen_bwd_fwd_flops_ratio_is_5_over_2_for_square). Training-context fix for the existing inference-only path: the ``aiter::fmha_v3_varlen_fwd`` mapping in extensions/pseudo_ops_perf_utils.py is removed, so the core SDPA-derived class wins and Wan 2.x training traces (which have no sglang/vLLM annotation) get a real shape-based GFLOPS instead of silently falling back to no_perf_param_details under the InferenceAttention label. The annotation-aware class is kept in extensions/attention_perf_model_extensions.py for any future mapping of a renamed pseudo-op event. Validated on a real Wan 2.2 T2V A14B training trace (Primus, BF16, mbs=1): the ``other`` bucket drops from 7.19 % -> 0.18 % of step time and the misleading ``InferenceAttention`` bucket from 3.92 % -> 0 %, moving 10.93 pp of attention compute into the correct SDPA_fwd / SDPA_bwd buckets with populated GFLOPS for all 4 attention rows in ``ops_summary``. 12 unit tests in tests/test_aiter_fmha_v3_varlen_ops.py using real Wan 2.2 event payloads; full suite (64 pre-existing + 12 new) passes under black.
…f models 12 tests covering mapping registration, SDPA_fwd/SDPA_bwd categorization, param extraction from real Wan 2.2 event payloads, the 5/2 * fwd FLOPs bwd identity, the wrapper-variant tensor offset, and the aiter::fmha_v3_bwd non-varlen layout (closes #590). Includes a regression assertion that aiter::fmha_v3_varlen_fwd now resolves to the core PerfModel.perf_model class rather than the extensions InferenceAttention class.
There was a problem hiding this comment.
Pull request overview
This PR expands TraceLens’ SDPA/FlashAttention performance-model coverage by adding core (shape-based) perf models for AITER varlen FA forward/backward + AITER non-varlen fmha_v3_bwd, and PyTorch’s dispatcher-level aten::_flash_attention_forward. It also removes a pseudo-op extension mapping that previously overrode the core mapping in training traces, improving categorization and GFLOPS reporting for Wan 2.x workloads.
Changes:
- Add new SDPA-derived perf model classes for
aiter::(wrapper_)fmha_v3_varlen_{fwd,bwd},aiter::fmha_v3_bwd, andaten::_flash_attention_forward. - Register new op→perf-model mappings and route new backward op-names into the
SDPA_bwdcategory. - Stop pseudo-op extension mappings from overriding the core mapping for
aiter::fmha_v3_varlen_fwd, and add unit tests covering the new mappings and shape extraction.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
TraceLens/PerfModel/torch_op_mapping.py |
Registers new op mappings and updates SDPA forward/backward categorization for the new AITER/ATen attention ops. |
TraceLens/PerfModel/perf_model.py |
Implements the new SDPA-derived perf model classes and shared varlen argument parsers. |
TraceLens/PerfModel/extensions/pseudo_ops_perf_utils.py |
Prevents the extension mapping from overriding the core mapping for aiter::fmha_v3_varlen_fwd. |
tests/test_aiter_fmha_v3_varlen_ops.py |
Adds unit tests validating mapping registration, categorization, and FLOPs extraction for the new ops. |
Comments suppressed due to low confidence (1)
TraceLens/PerfModel/perf_model.py:3362
- The varlen backward FLOPs calculation uses only
max_seqlen_{q,kv}once and does not account fornum_seqs_q/num_seqs_kvwhen multiple sequences are present. This will under-estimate bwd FLOPs for packed multi-sequence varlen attention. Suggest matchingflash_attention_varlen_backward.flops()(accumulate for max seqlen + estimate remaining sequences) soaiter::fmha_v3_varlen_bwdbehaves consistently with the existing flash-attn varlen perf model.
def flops(self):
return self.flops_bwd_func(
self.B,
self.max_seqlen_q,
self.H_Q,
self.max_seqlen_kv,
self.H_KV,
self.d_h_qk,
self.d_h_v,
self.param_details["causal"],
self.param_details["flash_impl"],
)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…t review) Addresses the Copilot review on PR #651: the four aiter varlen classes used only ``max_seqlen_{q,kv}`` once and ignored ``num_seqs_q`` / ``num_seqs_kv``, undercounting FLOPs when multiple sequences are packed into the (T, H, D) varlen tensors (cu_seqlens length > 2). Mirrors the established ``flash_attention_varlen_forward.flops()`` / ``flash_attention_varlen_backward.flops()`` lower-bound accumulator: one max-length sequence plus ``num_seqs - 1`` equal-length sequences of average size ``(N - max_seqlen) / (num_seqs - 1)``. Applied to all four classes (direct fwd, direct bwd, wrapper fwd, wrapper bwd). Adds 2 unit tests for the multi-seq case (cu_seqlens = [0, 100, 200], 2-sequence packing): the fwd test verifies the accumulator hits the exact expected FLOPs; the bwd test verifies bwd / fwd = 5/2 still holds with multi-seq packing. No effect on the single-seq Wan 2.2 trace from #157: ``num_seqs_q`` remains 1 there, the additive branch is skipped, and aggregate category percentages (other 0.18 %, SDPA_fwd 4.10 %, SDPA_bwd 6.83 %) are unchanged. 14 / 14 tests in test_aiter_fmha_v3_varlen_ops.py pass; full attention test suite 78 / 78 under black.
Update the affected perf-report regression CSVs after PR #651 routes newly modeled attention ops (notably aten::_flash_attention_forward and Wan AITER attention rows) into SDPA_fwd / SDPA_bwd instead of other. The reference refresh is limited to the seven traces that failed CI. `tests/test_perf_report_regression.py` now passes locally.
Update the H100-vs-MI300 compare-report reference CSVs after PR #651 moves aten::_flash_attention_forward from other into SDPA_fwd. `tests/test_compare_perf_reports.py tests/test_perf_report_regression.py` passes locally (18 tests).
Document why aiter::fmha_v3_varlen_fwd is not in pseudo_ops_perf_utils on the annotation-aware extension class, with a short cross-reference at the mapping site, per PR review.
Resolve conflicts from main's registry-based op categorization refactor. Source: - TraceLens/PerfModel/torch_op_mapping.py: replace this branch's legacy if/elif categorize_torch_op body with main's _categorize_torch_op_from_registry(row, OP_CATEGORY_REGISTRY). The new aiter varlen / aten flash-attention op->perf-model mappings added on this branch already feed build_op_category_registry, so the old sdpa_bwd_names list is obsolete. - TraceLens/PerfModel/perf_model.py: declare category = "SDPA_bwd" on the new backward SDPA classes (aiter__fmha_v3_bwd, aiter__fmha_v3_varlen_bwd, aiter__fmha_v3_varlen_backward). main's registry derives categories from the class .category attribute instead of name lists, so without this override these backward ops would inherit SDPA_fwd from the SDPA base. Reference CSVs (11 ops_summary / ops_unique_args): - Combine main's serialization (deterministic list ['X'] category format, PR #683) with this branch's recategorization. The only semantic change across all conflicts is aten::_flash_attention_forward moving from other -> SDPA_fwd, so main's rows are kept verbatim and only that op's category cell is updated. References were NOT regenerated locally because this environment's pandas serializes empty cells differently (",None," vs ",,"), which would corrupt every reference relative to CI. Validation: test_aiter_fmha_v3_varlen_ops, categorization-registry, pseudo-op, and primus categorization suites pass; black clean.
The torch_compile_triton trace reference CSVs were missed by the earlier ref refresh on this branch. After PR #651 routes aten::_flash_attention_forward from `other` into SDPA_fwd, five sheets for this trace now disagree with the stale references (kernel_summary, ops_summary, ops_summary_by_category, ops_unique_args category cells, plus a new SDPA_fwd row). The only change is the intended other -> SDPA_fwd reroute; no empty-cell serialization noise. tests/test_perf_report_regression.py and tests/test_compare_perf_reports.py pass locally (19 passed).
|
@devalshah-amd @devalshahamd Could you approve this PR? Thanks! |
Resolve conflict in pseudo_ops_perf_utils.py: keep PR #651's training-path fix (do not map aiter::fmha_v3_varlen_fwd here so the core SDPA perf model wins) and take main's new inference attention mappings (aiter::mha_batch_prefill, sglang_profiler::attention_paged_attention_ragged). Focused attention/categorization suites pass (111 tests).
Add explicit Read the Docs configuration and a lightweight Sphinx entrypoint so PR docs checks can build successfully without relying on implicit project defaults.
|
SDPA.get_simulation_time requires dtype from perf model to calculate origami metrics. |
…ields Keep the annotation-aware aiter varlen extension mapping active while falling back to the core SDPA model for unannotated training rows, and add dtype_A_B to the new varlen/aten flash parsers so SDPA simulation/origami metrics have precision metadata. Also remove unrelated docs setup files from this PR scope.
|
Addressed the dtype metadata note in 4df85aa. Added
For each parser this is now Validation: |
Summary
Closes #650
Also closes #290 and #590
Adds shape-based SDPA perf models for AITER varlen FlashAttention fwd/bwd,
aiter::fmha_v3_bwd, andaten::_flash_attention_forward. Also stops the training path from being overridden by the inference-only pseudo-op mapping, so Wan 2.x attention rows report underSDPA_fwd/SDPA_bwdwith GFLOPS.Changes
num_seqs_q/num_seqs_kv.othertoSDPA_fwd/SDPA_bwd.Test plan
pytest tests/test_aiter_fmha_v3_varlen_ops.pypytest tests/test_compare_perf_reports.py tests/test_perf_report_regression.py(18 passed)