Attempt to fix #2220: Enable Flash Attention in KV-cache path#2248
Open
woaiwang wants to merge 1 commit into
Open
Attempt to fix #2220: Enable Flash Attention in KV-cache path#2248woaiwang wants to merge 1 commit into
woaiwang wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2220
What I did:
I noticed in #2220 that passing an explicit
attn_maskdisables the PyTorch SDPA Flash Attention fast path. I tried to drop the mask during the decoding phase (q.size(2) == 1) inCausalSelfAttention.scaled_dot_product_attentionto re-enable it.The Issue:
Running
pytest tests/test_model.pyresults inAssertionError: Tensor-likes are not close!for a few tests (e.g.,test_against_gpt_neox_model).I realize that simply dropping the mask causes the query to attend to the uninitialized/padded parts of the
kandvtensors in theKVCacheifinput_pos_maxp1is not aggressively slicing them.Question for reviewers:
Could you guide me on the safest way to slice the KV-cache or formulate the
is_causalflag here so that we can safely drop the explicit mask without reading uninitialized memory? I would love to finish this PR with your guidance!