Skip to content

Support for Large attn_bias via Sparse Tensors or On‑The‑Fly Construction (seq_len ≈ 12 288) #11928

Closed

Description

@maciek-wisniewski

I’m working with a Transformer model that routinely processes sequences up to 12 288 tokens.
For attention bias I currently create a dense attn_bias of shape 12 288 × 12 288.

Right now, I have problems with memory for my multi-head att, because of that large tensors.
I could create smaller blocks on fly from sparse attn_bias tensor, but I am not sure if xformers supports this type of processing.

I would be grateful for any help. Maybe there are other packages which could help my solve that problem?

Maciek

Activity

  1. a-r-r-o-w commented on Jul 15, 2025

    @a-r-r-o-w
    Contributor

    Does each query attend randomly to a selected set of keys, or does each query attend to keys in some kind of block/causal manner? (By random, I mean a non-common pattern where a query can look arbitrarily at some token positions but not with any specific pattern)

    If it's a causal pattern, you can look into flash-attention or pytorch cudnn attention, which support causal=True as an argument.

    If it's a causal-similarish pattern where each query attends to a prefix of keys, the fastest available implementation is flash-attention. You don't need to materialize the mask and can just specify cumulative sequence lengths.

    If it's a block-followed-by-causal pattern, flash-attention/xformers should be able to support it too.

    If it's an arbitrary pattern, you can look into flex attention

  2. locked and limited conversation to collaborators on Jul 15, 2025
  3. converted this issue into a discussion #11933 on Jul 15, 2025
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions