Skip to content

Support SDPA & Flash Attention 2 for LayoutLMv3 #35467

Open

Description

@stancld

Feature request

Support SDPA & Flash Attention 2 for LayoutLMv3.

Motivation

Speed up document transformer training and inference.

Your contribution

I can add it.

Activity

  1. changed the title [-]Support Flash Attention 2 for LayoutLMv3[/-] [+]Support SDPA & Flash Attention 2 for LayoutLMv3[/+] on Dec 31, 2024
  2. YashPCHFL commented on Mar 2, 2025

    @YashPCHFL

    ++ This will drastically improve the speed of LayoutLmv3.
    I see the PR is already raised

  3. ArthurZucker commented on Oct 20, 2026

    @ArthurZucker
    Collaborator

    Still open: src/transformers/models/layoutlmv3/modeling_layoutlmv3.py doesn't use the attention interface yet (earlier attempts #35469, #41801, #42225 were closed). The path is to port its self-attention to ALL_ATTENTION_FUNCTIONS as BERT does (https://huggingface.co/docs/transformers/main/en/attention_interface); note LayoutLMv3 adds 1D/2D relative position biases to the scores, so SDPA can take them as an additive mask, while flash-attention would need to be disabled when those biases are used.

  4. added
    Good Second IssueIssues that are more difficult to do than "Good First" issues - give it a try if you want!
    on Nov 9, 2026
  5. Kash6 commented on Nov 11, 2026

    @Kash6
    Contributor

    @ArthurZucker @qgallouedec
    Is anyone working on this? I can try something.

    Plan: move LayoutLMv3SelfAttention onto the attention interface like BERT, and pass the two learned relative position biases as position_bias, the way T5 and UDOP do. The CogView softmax stays in the eager path. SDPA only. Flash attention can't take an additive bias, so I'd skip it and mark test_sdpa_can_dispatch_on_flash as skipped with a reason, like UDOP.

    I tried it locally on an RTX 5080. Eager output is bit for bit the same as main at the real head size (64), SDPA matches it to about 1e-6 in fp32, and the LayoutLMv3 tests, including slow ones, pass. With random weights and base-sized shapes SDPA was 1.2x to 1.5x faster for inference and 1.3x to 1.85x for training, with the same or lower peak memory. That is one GPU only.

    Two questions before I open a PR:

    This makes SDPA the default for LayoutLMv3, so output_attentions=True returns empty attentions unless you load with attn_implementation="eager" (same as BERT). Is that ok here?
    PP-DocLayoutV2 inherits these classes through modular, so its generated file changes by a few lines. Fine to include in the same PR?

    I used an AI coding assistant for the investigation. I will write the PR myself and run the tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Feature requestRequest for a new featureGood Second IssueIssues that are more difficult to do than "Good First" issues - give it a try if you want!

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions