Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sub-quadratic attention #1

Open
wants to merge 35 commits into
base: main
Choose a base branch
from
Open

sub-quadratic attention #1

wants to merge 35 commits into from

Conversation

Birch-san
Copy link
Owner

@Birch-san Birch-san commented Dec 26, 2022

Implementation of:

Self-attention Does Not Need O(n^2) Memory:
https://arxiv.org/abs/2112.05682v2

Based on Amin Rezaei's implementation:
https://github.com/AminRezaei0x443/memory-efficient-attention

With:

  • substantial rewrite to optimize it for 3D tensors in [batch * num_heads, tokens, channels_per_head] format
  • use batched matmuls
  • fuse multiplies into matmuls
  • simplifications to utility functions (to make more use of pytorch idioms).
  • typings

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint.

@isamu-isozaki
Copy link

Nice!

…hannels_per_head] in order to make use of batched matmuls. fuse multiply into matmul. breaks bias, mask in exchange for massive speedup.
…ul for SD 2.1. but remove value float32, having established that it works without.
…to prefer fast-path whenever unchunked attention would fit into memory. add kv_chunk_size_min to control the kv_chunk_size=None behaviour, so that sqrt(key_tokens) does not pick too small of a chunk size
…of chunk key size. improve separation of concerns.
…al kv_chunk_size: they can notice when no chunking would happen at all, and use fast-path. note: there's a question of whether that concern belongs *inside* the algorithm. but it'd feel weird for chunked attention to have a no-chunking-at-all branch.
… equivalent fast-path for 1 query chunk, 1 kv chunk is already supported inside
…ything in one chunk, to re-use an existing fast-path.
starts: List[int],
sizes: List[int],
) -> Tensor:
slicing = [slice(start, start + size) for start, size in zip(starts, sizes)]
Copy link
Owner Author

Choose a reason for hiding this comment

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

this attempts to implement jax.lax.dynamic_slice(), but hey is this literally just torch.narrow()?

Copy link

@brkirch brkirch Jan 5, 2023

Choose a reason for hiding this comment

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

Yeah that works also:
brkirch/stable-diffusion-webui@b119815

No notable performance difference that I observed, but it's probably slightly more efficient nonetheless.

scale: float,
) -> AttnChunk:
attn_weights = torch.baddbmm(
torch.empty(1, 1, 1, device=query.device, dtype=query.dtype),
Copy link

Choose a reason for hiding this comment

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

Shouldn't torch.zeros() be used here instead of torch.empty()?

Copy link
Owner Author

Choose a reason for hiding this comment

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

nope; it's actually an unused tensor (because beta=0), so we want whatever's the cheapest thing that passes the parameter validation. unfortunately PyTorch complains if you pass None. bad API design.

Beinsezii added a commit to Beinsezii/diffusers that referenced this pull request Feb 28, 2024
Beinsezii added a commit to Beinsezii/diffusers that referenced this pull request Feb 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants