Skip to content

fix dynamo lowering fold shared kv attention - #4943

Merged
lanluo-nvidia merged 2 commits into
mainfrom
lluo/fix_decomp_test_failure
Oct 29, 2026
Merged

lanluo-nvidia merged 2 commits into
mainfrom
lluo/fix_decomp_test_failure

Conversation

@lanluo-nvidia

Copy link
Copy Markdown
Collaborator

Description

#4921 introduced the following test failure:
https://github.com/pytorch/TensorRT/actions/runs/40760002261/job/123120801180

Fixes # (issue)

Type of change

Please delete options that are not relevant and/or add your own.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • My code follows the style guidelines of this project (You can use the linters)
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas and hacks
  • I have made corresponding changes to the documentation
  • I have added tests to verify my fix or my feature
  • New and existing unit tests pass locally with my changes
  • I have added the relevant labels to my PR in so that relevant reviewers are notified

@meta-cla meta-cla Bot added the cla signed label Oct 29, 2026
@github-actions github-actions Bot added documentation Improvements or additions to documentation component: tests Issues re: Tests component: lowering Issues re: The lowering / preprocessing passes component: core Issues re: The core compiler component: api [Python] Issues re: Python API component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels Oct 29, 2026
@github-actions
github-actions Bot requested a review from zewenli98 October 29, 2026 17:17

@shoumikhin shoumikhin 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.

The pass fix works: on torch 2.15 the backend now logs "Folded 2" where it found nothing before. Two of the new test cases fail on torch 2.16 though, see inline. Tested on A100.

decompositions = get_decompositions()
# Exercise the decomposed form even on PyTorch versions that preserve matmul.
decompositions.update(
torch._decomp.get_decompositions([torch.ops.aten.matmul.default])

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.

This lookup returns an empty dict. matmul has no entry in the decomposition table, so the update adds nothing.

On torch 2.15 that does not matter, because AOT tracing already turns matmul into bmm. On the 2.16 nightly that main uses, aot_export_joint_simple keeps aten.matmul. So both joint_export cases fail with 0 != 2 on the bmm count, before the pass runs. The dynamo-lowering job on this PR already fails on exactly these two cases.

This works on both versions:

from torch._export.utils import _get_decomp_for_cia

decompositions[torch.ops.aten.matmul.default] = _get_decomp_for_cia(
    torch.ops.aten.matmul.default
)

With it, all four new cases pass with your fix and fail without it, on torch 2.15 and 2.16.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Agreed—the lookup was empty. Replaced it with _get_decomp_for_cia(aten.matmul.default) so the joint-export tests explicitly exercise the bmm form.”

torch.testing.assert_close(folded(*inputs), Module()(*inputs))
decompositions = get_decompositions()
decompositions.update(
torch._decomp.get_decompositions([torch.ops.aten.matmul.default])

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.

Same no-op lookup as above. Here it can simply be removed: make_fx already splits matmul into bmm on both versions.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Removed the no-op lookup; make_fx uses the existing decomposition table directly.

rewritten, and ``scaled_dot_product_attention`` also needs
``decompose_attention=True``. The setting has no effect under ``torch.compile``
with ``use_distributed_mode_trace``.
``decompose_attention=True``. Both preserved matmuls and the reshape/``bmm``

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.

This reads as if every torch.compile graph is covered now. One case is still not: scaled_dot_product_attention(..., enable_gqa=True) with one KV head and decompose_attention=True. In the bmm form the KV head is repeated by the matmul's own broadcasting expand, and _unflatten_bmm_operand skips any expand that changes the shape. So nothing folds on torch 2.15 with plain torch.compile, or on 2.16 with use_distributed_mode_trace. The output is still correct. Only the speedup is missing.

The simple fix is to say here that the bmm form is matched for repeat_kv style graphs. Matching a broadcast from a single KV head would also work, but that can be a follow-up. I don't think this needs to block the merge.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Narrowed the wording to repeat_kv style graphs in the guide and API docstrings, and documented the remaining enable_gqa=True broadcasting limitation. Leaving that optimization for a follow-up.”

@shoumikhin shoumikhin 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.

Thanks, all three points are addressed. Looks good to me.

@lanluo-nvidia lanluo-nvidia added this to the v2.15.0 milestone Oct 29, 2026
@lanluo-nvidia
lanluo-nvidia merged commit b2fa3ee into main Oct 29, 2026
191 of 210 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci: full cla signed component: api [Python] Issues re: Python API component: core Issues re: The core compiler component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths component: lowering Issues re: The lowering / preprocessing passes component: tests Issues re: Tests documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants