Skip to content

fix: optimize attention decomposition - #4448

Open
fs-eire wants to merge 3 commits into
pytorch:mainfrom
fs-eire:attention-arange-decomposition-fixes
Open

fix: optimize attention decomposition#4448
fs-eire wants to merge 3 commits into
pytorch:mainfrom
fs-eire:attention-arange-decomposition-fixes

Conversation

@fs-eire

@fs-eire fs-eire commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Description

Align attention decomposition behavior with the ONNX graph.

This PR does the following:

  • Restrict attention-mask Select choices to scalars while preserving mask polarity and -inf semantics.
  • Preserve static arange operations as TensorRT LINSPACE Fill layers.
  • Add regression coverage for attention masks and static arange conversion.

With this change, Torch-TensorRT is able to generate optimized engine with a pattern that an attention using a causal mask AND a padding mask, when decompose_attention=True.

Fixes # (issue)

Type of change

  • Bug fix (non-breaking change which fixes an issue)

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 Jul 30, 2026
@github-actions github-actions Bot added component: tests Issues re: Tests component: lowering Issues re: The lowering / preprocessing passes component: conversion Issues re: Conversion stage component: core Issues re: The core compiler component: converters Issues re: Specific op converters component: api [Python] Issues re: Python API component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels Jul 30, 2026
@github-actions
github-actions Bot requested a review from lanluo-nvidia July 30, 2026 07:06
@narendasan
narendasan requested review from micwill755 and zewenli98 and removed request for lanluo-nvidia July 31, 2026 19:11
assert attn_mask is None, "attn_mask must be None when is_causal=True"
attn_bias = torch.zeros((L, S), dtype=query.dtype, device=device)
temp_mask = torch.ones((L, S), dtype=torch.bool, device=device).tril(diagonal=0)
attn_bias = attn_bias.masked_fill(temp_mask.logical_not(), float("-inf"))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

For the bool attn_mask path we switched to scalar where(0 / -inf), but is_causal still uses masked_fill. Should the causal path use the same scalar where pattern?

Comment thread py/torch_tensorrt/dynamo/conversion/impl/arange.py
@lanluo-nvidia lanluo-nvidia added this to the v2.14.0 milestone Aug 18, 2026
Restore the original -inf value and invalid-mask polarity while retaining scalar Select inputs required for compact TensorRT MHA masks.
@fs-eire
fs-eire force-pushed the attention-arange-decomposition-fixes branch from 7a7c41a to ca98121 Compare August 19, 2026 00:04
@lanluo-nvidia lanluo-nvidia modified the milestones: v2.14.0, v2.15.0 Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla signed component: api [Python] Issues re: Python API component: conversion Issues re: Conversion stage component: converters Issues re: Specific op converters 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants