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

Enable Flash Attention for SD3 MMDiT #2014

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion keras_hub/src/models/stable_diffusion_3/mmdit.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,9 +595,30 @@ def build(self, inputs_shape, context_shape, timestep_embedding_shape):
self.context_block.build(context_shape, timestep_embedding_shape)

def _compute_attention(self, query, key, value):
batch_size = ops.shape(query)[0]

# Use the fast path when `ops.dot_product_attention` and flash attention
# are available.
if (
hasattr(ops, "dot_product_attention")
and hasattr(keras.config, "is_flash_attention_enabled")
and keras.backend.backend() != "tensorflow"
Copy link
Member

Choose a reason for hiding this comment

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

Maybe let's drop the tf part? And just do the same on all backends?

I don't think we want to be in the business of trying to outsmart core Keras. And layers.MultiHeadAttention isn't doing anything like this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure!

I have submitted a PR to core Keras:
keras-team/keras#20615
With that change, the cost time of the tensorflow will be comparable to jax (w/o flash attention)

  • tensorflow: 10.57s
  • jax: 10.61s

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Should we test this somehow?

I’m not sure how to test this since ops.dot_product_attention is intended to be a drop-in replacement.
Should I compare the numeric w/ and w/o ops.dot_product_attention?

Copy link
Member

Choose a reason for hiding this comment

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

Thanks! keras-team/keras#20615, yeah I think that's the way to go.

Hmm, yeah as long as the code is being exercised, probably fine to leave as is. Let's go with this!

):
# `ops.dot_product_attention` is slower than the vanilla
# implementation in the tensorflow backend.
encoded = ops.dot_product_attention(
query,
key,
value,
scale=self._inverse_sqrt_key_dim,
flash_attention=keras.config.is_flash_attention_enabled(),
)
return ops.reshape(
encoded, (batch_size, -1, self.num_heads * self.head_dim)
)

# Ref: jax.nn.dot_product_attention
# https://github.com/jax-ml/jax/blob/db89c245ac66911c98f265a05956fdfa4bc79d83/jax/_src/nn/functions.py#L846
batch_size = ops.shape(query)[0]
logits = ops.einsum("BTNH,BSNH->BNTS", query, key)
logits = ops.multiply(logits, self._inverse_sqrt_key_dim)
probs = self.softmax(logits)
Expand Down
Loading