-
Notifications
You must be signed in to change notification settings - Fork 13.7k
sycl: flash-attention implementation #16969
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
Open
ye-NX
wants to merge
6
commits into
ggml-org:master
Choose a base branch
from
ye-NX:saf-ye/flash-attn
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+230
−0
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fc0e041
sycl: initialize flash-attention implementation
ye-NX dd1fde5
Update ggml/src/ggml-sycl/flash-attn/flash-attn-sycl.cpp
safranowith 4f52591
Update ggml/src/ggml-sycl/flash-attn/flash-attn-sycl.cpp
safranowith af5b644
Update ggml/src/ggml-sycl/flash-attn/flash-attn-sycl.cpp
ye-NX 8e8fb57
add include in ggml-sycl.cpp
safranowith dcd7ca5
remove unrelated changes
safranowith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| #include "flash-attn-sycl.h" | ||
|
|
||
| #include "kernels/flash-attn-kernel.h" | ||
|
|
||
| #include <cmath> | ||
| #include <cstring> | ||
| #include <limits> | ||
| #include <sycl/sycl.hpp> | ||
|
|
||
| #define FLASH_ATTN_BR_MAX 32 | ||
| #define FLASH_ATTN_BC_MAX 32 | ||
|
|
||
| // Flash Attention: https://arxiv.org/abs/2205.14135 | ||
| void ggml_sycl_op_flash_attn(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { | ||
| const ggml_tensor * Q = dst->src[0]; | ||
| const ggml_tensor * K = dst->src[1]; | ||
| const ggml_tensor * V = dst->src[2]; | ||
| const ggml_tensor * mask = dst->src[3]; | ||
|
|
||
| GGML_ASSERT(Q != nullptr); | ||
| GGML_ASSERT(K != nullptr); | ||
| GGML_ASSERT(V != nullptr); | ||
| GGML_ASSERT(dst != nullptr); | ||
|
|
||
| if (Q->type != GGML_TYPE_F32 || K->type != GGML_TYPE_F32 || V->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32) { | ||
| fprintf(stderr, "[SYCL] FLASH-ATTENTION: tensor type not supported (Q=%d, K=%d, V=%d, dst=%d)\n", Q->type, K->type, V->type, dst->type); | ||
| return; | ||
| } | ||
|
|
||
| const float * Q_d = (const float *) Q->data; | ||
| const float * K_d = (const float *) K->data; | ||
| const float * V_d = (const float *) V->data; | ||
| float * dst_d = (float *) dst->data; | ||
|
|
||
| dpct::queue_ptr stream = ctx.stream(); | ||
|
|
||
| const int64_t d = Q->ne[0]; | ||
| const int64_t N = Q->ne[1]; | ||
|
|
||
| float scale; | ||
| float max_bias; | ||
| float logit_softcap; | ||
|
|
||
| std::memcpy(&scale, (const float *) dst->op_params + 0, sizeof(float)); | ||
| std::memcpy(&max_bias, (const float *) dst->op_params + 1, sizeof(float)); | ||
| std::memcpy(&logit_softcap, (const float *) dst->op_params + 2, sizeof(float)); | ||
|
|
||
| const bool masked = (mask != nullptr); | ||
|
|
||
| const int Br = std::min((int) FLASH_ATTN_BR_MAX, (int) N); | ||
| const int Bc = std::min((int) FLASH_ATTN_BC_MAX, (int) N); | ||
|
|
||
| const int Tr = (N + Br - 1) / Br; | ||
| const int Tc = (N + Bc - 1) / Bc; | ||
|
|
||
| float * l_d = (float *) sycl::malloc_device(N * sizeof(float), *stream); | ||
| float * m_d = (float *) sycl::malloc_device(N * sizeof(float), *stream); | ||
|
|
||
| stream->fill(l_d, 0.0f, N); | ||
| stream->fill(m_d, -std::numeric_limits<float>::infinity(), N); | ||
| stream->fill(dst_d, 0.0f, N * d); | ||
| stream->wait(); | ||
safranowith marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| for (int j = 0; j < Tc; ++j) { | ||
| stream->submit([&](sycl::handler & cgh) { | ||
| cgh.parallel_for(sycl::range<1>(Tr), [=](sycl::id<1> idx) { | ||
| const int i = idx[0]; | ||
| flash_attn_tiled_kernel<FLASH_ATTN_BR_MAX, FLASH_ATTN_BC_MAX>(Q_d, K_d, V_d, dst_d, l_d, m_d, i, j, Br, | ||
| Bc, N, d, masked, scale); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| stream->wait(); | ||
safranowith marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| sycl::free(l_d, *stream); | ||
| sycl::free(m_d, *stream); | ||
| } | ||
|
|
||
| bool ggml_sycl_flash_attn_ext_supported(const ggml_tensor * dst) { | ||
| const ggml_tensor * Q = dst->src[0]; | ||
| const ggml_tensor * K = dst->src[1]; | ||
| const ggml_tensor * V = dst->src[2]; | ||
|
|
||
| if (Q == nullptr || K == nullptr || V == nullptr) { | ||
| return false; | ||
| } | ||
|
|
||
| if (Q->type != GGML_TYPE_F32 || K->type != GGML_TYPE_F32 || V->type != GGML_TYPE_F32) { | ||
| return false; | ||
| } | ||
|
|
||
| if (dst->type != GGML_TYPE_F32) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| #pragma once | ||
|
|
||
| #include "../common.hpp" | ||
|
|
||
| // Flash attention operation for SYCL backend | ||
| // This implements the Flash Attention algorithm optimized for SYCL devices | ||
| void ggml_sycl_op_flash_attn(ggml_backend_sycl_context & ctx, ggml_tensor * dst); | ||
|
|
||
| // Check if flash attention is supported for given tensor | ||
| bool ggml_sycl_flash_attn_ext_supported(const ggml_tensor * dst); |
108 changes: 108 additions & 0 deletions
108
ggml/src/ggml-sycl/flash-attn/kernels/flash-attn-kernel.h
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| #pragma once | ||
|
|
||
| #include <sycl/sycl.hpp> | ||
|
|
||
| template <int Br_MAX = 32, int Bc_MAX = 32> | ||
| inline void flash_attn_tiled_kernel(const float * Q, | ||
| const float * K, | ||
| const float * V, | ||
| float * O, | ||
| float * l, | ||
| float * m, | ||
| const int i_block, | ||
| const int j_block, | ||
| const int Br, | ||
| const int Bc, | ||
| const int N, | ||
| const int d, | ||
| const bool masked, | ||
| const float scale) { | ||
| const int i_start = i_block * Br; | ||
| const int j_start = j_block * Bc; | ||
|
|
||
| float S[Br_MAX][Bc_MAX]; | ||
| float P[Br_MAX][Bc_MAX]; | ||
| float m_local[Br_MAX]; | ||
| float l_local[Br_MAX]; | ||
|
|
||
| for (int qi = 0; qi < Br; ++qi) { | ||
| const int q_row = i_start + qi; | ||
| if (q_row >= N) { | ||
| continue; | ||
| } | ||
|
|
||
| for (int kj = 0; kj < Bc; ++kj) { | ||
| const int k_row = j_start + kj; | ||
| if (k_row >= N) { | ||
| S[qi][kj] = -INFINITY; | ||
| continue; | ||
| } | ||
|
|
||
| if (masked && k_row > q_row) { | ||
| S[qi][kj] = -INFINITY; | ||
| continue; | ||
| } | ||
|
|
||
| float score = 0.0f; | ||
| for (int k = 0; k < d; ++k) { | ||
| score += Q[q_row * d + k] * K[k_row * d + k]; | ||
| } | ||
| S[qi][kj] = score * scale; | ||
| } | ||
| } | ||
|
|
||
| for (int qi = 0; qi < Br; ++qi) { | ||
| const int q_row = i_start + qi; | ||
| if (q_row >= N) { | ||
| continue; | ||
| } | ||
|
|
||
| m_local[qi] = -INFINITY; | ||
| for (int kj = 0; kj < Bc; ++kj) { | ||
| if (j_start + kj < N) { | ||
| m_local[qi] = sycl::fmax(m_local[qi], S[qi][kj]); | ||
| } | ||
| } | ||
|
|
||
| l_local[qi] = 0.0f; | ||
| for (int kj = 0; kj < Bc; ++kj) { | ||
| if (j_start + kj < N && !sycl::isinf(S[qi][kj])) { | ||
| P[qi][kj] = sycl::exp(S[qi][kj] - m_local[qi]); | ||
| l_local[qi] += P[qi][kj]; | ||
| } else { | ||
| P[qi][kj] = 0.0f; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (int qi = 0; qi < Br; ++qi) { | ||
| const int q_row = i_start + qi; | ||
| if (q_row >= N) { | ||
| continue; | ||
| } | ||
|
|
||
| const float m_old = m[q_row]; | ||
| const float m_new = sycl::fmax(m_old, m_local[qi]); | ||
| const float l_old = l[q_row]; | ||
| const float l_new = sycl::exp(m_old - m_new) * l_old + sycl::exp(m_local[qi] - m_new) * l_local[qi]; | ||
|
|
||
| const float correction_old = sycl::exp(m_old - m_new); | ||
| const float correction_new = sycl::exp(m_local[qi] - m_new); | ||
|
|
||
| for (int k = 0; k < d; ++k) { | ||
| float pv = 0.0f; | ||
| for (int kj = 0; kj < Bc; ++kj) { | ||
| const int v_row = j_start + kj; | ||
| if (v_row < N) { | ||
| pv += P[qi][kj] * V[v_row * d + k]; | ||
| } | ||
| } | ||
|
|
||
| const int o_idx = q_row * d + k; | ||
| O[o_idx] = (correction_old * O[o_idx] + correction_new * pv) / l_new; | ||
| } | ||
|
|
||
| l[q_row] = l_new; | ||
| m[q_row] = m_new; | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.