Skip to content

feat(perfmodel): add varlen aiter FA + aten::_flash_attention_forward perf models (Wan 2.2 coverage) - #651

Open
gphuang wants to merge 18 commits into
mainfrom
feat/perfmodel/wan22-attention-coverage
Open

feat(perfmodel): add varlen aiter FA + aten::_flash_attention_forward perf models (Wan 2.2 coverage)#651
gphuang wants to merge 18 commits into
mainfrom
feat/perfmodel/wan22-attention-coverage

Conversation

@gphuang

@gphuang gphuang commented May 19, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes #650
Also closes #290 and #590

Adds shape-based SDPA perf models for AITER varlen FlashAttention fwd/bwd, aiter::fmha_v3_bwd, and aten::_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 under SDPA_fwd / SDPA_bwd with GFLOPS.

Changes

  • Register 6 new attention op names in the core perf-model mapping.
  • Add packed-varlen FLOPs handling via num_seqs_q / num_seqs_kv.
  • Keep the pseudo-op extension class, but prevent it from overriding the core training mapping.
  • Add 14 regression tests using real Wan 2.2 event payloads.
  • Refresh affected perf-report and compare-report CSV references for rows that now move from other to SDPA_fwd / SDPA_bwd.

Test plan

  • pytest tests/test_aiter_fmha_v3_varlen_ops.py
  • Focused attention / pseudo-op / Primus categorization tests pass locally
  • pytest tests/test_compare_perf_reports.py tests/test_perf_report_regression.py (18 passed)

gphuang added 2 commits May 19, 2026 10:27
…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.
Copilot AI review requested due to automatic review settings May 19, 2026 15:28
@gphuang
gphuang requested review from a team, ajassani, devalshahamd and gabeweisz as code owners May 19, 2026 15:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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, and aten::_flash_attention_forward.
  • Register new op→perf-model mappings and route new backward op-names into the SDPA_bwd category.
  • 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 for num_seqs_q/num_seqs_kv when multiple sequences are present. This will under-estimate bwd FLOPs for packed multi-sequence varlen attention. Suggest matching flash_attention_varlen_backward.flops() (accumulate for max seqlen + estimate remaining sequences) so aiter::fmha_v3_varlen_bwd behaves 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.

Comment thread TraceLens/PerfModel/perf_model.py
…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.
gphuang added 2 commits May 25, 2026 01:49
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).
Comment thread TraceLens/PerfModel/extensions/pseudo_ops_perf_utils.py Outdated
gphuang added 2 commits May 26, 2026 21:11
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.
@gphuang
gphuang requested a review from olehtika May 26, 2026 18:20
@gphuang gphuang self-assigned this May 29, 2026
gphuang added 2 commits June 4, 2026 11:25
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).
@gphuang

gphuang commented Jun 4, 2026

Copy link
Copy Markdown
Contributor Author

@devalshah-amd @devalshahamd Could you approve this PR? Thanks!

gphuang and others added 3 commits June 12, 2026 02:46
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.
@kyle-hoffmeyer

Copy link
Copy Markdown
Collaborator

SDPA.get_simulation_time requires dtype from perf model to calculate origami metrics. _parse_aiter_fmha_v3_varlen_fwd_args, _parse_aiter_fmha_v3_varlen_bwd_args, aten__flash_attention_forward.get_param_details need to all return "dtype_A_B" as well, which would be event["args"]["Input type"][q_idx], event["args"]["Input type"][k_idx] for these perf models (data type of Q and K matrices)

Comment thread TraceLens/PerfModel/perf_model.py
Comment thread TraceLens/PerfModel/torch_op_mapping.py
Comment thread docs/conf.py Outdated
gphuang added 2 commits July 9, 2026 15:23
…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.
@gphuang

gphuang commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the dtype metadata note in 4df85aa.

Added dtype_A_B extraction to:

  • _parse_aiter_fmha_v3_varlen_fwd_args
  • _parse_aiter_fmha_v3_varlen_bwd_args
  • aten___flash_attention_forward.get_param_details

For each parser this is now (Input type[q_idx], Input type[k_idx]), so SDPA simulation/origami has Q/K precision metadata available.

Validation: pytest tests/test_aiter_fmha_v3_varlen_ops.py (15 passed) and black --check on touched files.

@gphuang
gphuang requested a review from devalshahamd July 9, 2026 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

6 participants