Skip to content

Cast/normalize attn_bias in scaled_dot_product_efficient_attention - #4636

Open
apbose wants to merge 2 commits into
mainfrom
abose/fix-sdpa-efficient-attention-mask-cast
Open

Cast/normalize attn_bias in scaled_dot_product_efficient_attention#4636
apbose wants to merge 2 commits into
mainfrom
abose/fix-sdpa-efficient-attention-mask-cast

Conversation

@apbose

@apbose apbose commented Aug 28, 2026

Copy link
Copy Markdown
Collaborator

Description

attn_bias was assigned straight to attention_layer.mask with no dtype cast or rank normalization, unlike scaled_dot_product_attention() which already does this for attn_mask. TRT's AttentionInput layer rejects non-Float/Half/BFloat16/Bool masks (e.g. int32 padding masks), leaving a broken output tensor that crashes whatever converter reads it next.

Fixes the root cause of #4496.

Type of change

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

Checklist:

  • I have performed a self-review of my own code
  • I have added tests to verify my fix or my feature

attn_bias was assigned straight to attention_layer.mask with no dtype
cast or rank normalization, unlike the sibling
scaled_dot_product_attention() converter which already does this for
attn_mask. When PyTorch's SDPA dispatcher routes through the efficient
backend with an int32 padding mask (e.g. BERT bfloat16), TRT's
AttentionInput layer rejected the mask (must be Float/Half/BFloat16/
Bool), producing a broken output tensor whose shape then crashes
whatever converter reads it next (seen as permute's issue #4496 and
today as matmul.py's "Could not get tensor shape").

Apply the same BOOL-check/cast/rank-normalize treatment attn_mask
already gets, for both the causal+attn_bias (additive bias combined
via elementwise add) and plain attn_bias branches.
@meta-cla meta-cla Bot added the cla signed label Aug 28, 2026
@github-actions github-actions Bot added 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 Aug 28, 2026
@github-actions
github-actions Bot requested a review from zewenli98 August 28, 2026 18:07
@github-actions github-actions Bot added the component: tests Issues re: Tests label Aug 28, 2026
@apbose apbose added this to the v2.15.0 milestone Aug 28, 2026
@apbose
apbose force-pushed the abose/fix-sdpa-efficient-attention-mask-cast branch from 537a499 to c2ea7b5 Compare August 28, 2026 23:46

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There are some changes that do not conform to Python style guidelines:

--- /home/runner/work/TensorRT/TensorRT/tests/py/dynamo/conversion/test_attention.py	2026-08-28 23:46:43.316968+00:00
+++ /home/runner/work/TensorRT/TensorRT/tests/py/dynamo/conversion/test_attention.py	2026-08-28 23:47:09.397118+00:00
@@ -126,13 +126,11 @@
        # straight to TensorRT's IAttention layer with no cast, which TRT's
        # AttentionInput layer rejects for anything but Float/Half/BFloat16/
        # Bool matching the attention dtype. int32 (not e.g. float32) is used
        # here since PyTorch's own eager kernel accepts an int32 bias but
        # rejects a mismatched float dtype outright.
-        attn_bias = torch.zeros(
-            (query_shape[0], 1, 1, key_shape[2]), dtype=torch.int32
-        )
+        attn_bias = torch.zeros((query_shape[0], 1, 1, key_shape[2]), dtype=torch.int32)
        inputs.extend([query, key, value, attn_bias])
        self.run_test(
            SDPA(),
            inputs,
            rtol=1e-2,

Covers the case a dtype-mismatched attn_bias (e.g. float32 bias with
float16 query/key/value) is passed to
_scaled_dot_product_efficient_attention, which previously reached
TensorRT's IAttention layer uncast and could be rejected.
@apbose
apbose force-pushed the abose/fix-sdpa-efficient-attention-mask-cast branch from c2ea7b5 to 4077390 Compare August 28, 2026 23:53
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: tests Issues re: Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant