-
Notifications
You must be signed in to change notification settings - Fork 17.9k
SYCL: reduce allocation overhead during flash attention #22732
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
sanmai
wants to merge
6
commits into
ggml-org:master
Choose a base branch
from
sanmai:fa-overhead-sycl
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.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ad72f6a
SYCL: reduce allocation overhead during flash attention
sanmai 7844365
tidy up whitespace
sanmai f6b9bbb
add a note about the flag
sanmai af4f8e7
move ggml_sycl_fattn_* into fattn-buffers.hpp
sanmai 5f76f21
refactor implementation into fattn-buffers.cpp
sanmai 9dd6687
move new_fattn_kv_buffers back into ggml-sycl.cpp
sanmai 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
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,56 @@ | ||
| // | ||
| // MIT license | ||
| // Copyright (C) 2025 Intel Corporation | ||
| // SPDX-License-Identifier: MIT | ||
| // | ||
|
|
||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
|
|
||
| #include "common.hpp" | ||
|
|
||
| sycl::half * ggml_sycl_fattn_kv_buffers::kv_buffer::ensure_half(size_t n_elems) { | ||
| const size_t need_bytes = n_elems * sizeof(sycl::half); | ||
|
|
||
| if (capacity >= need_bytes) { | ||
| return ptr; | ||
| } | ||
|
|
||
| if (ptr) { | ||
| SYCL_CHECK(CHECK_TRY_ERROR(qptr->wait())); | ||
| SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(ptr, *qptr))); | ||
| ptr = nullptr; | ||
| capacity = 0; | ||
| } | ||
|
|
||
| size_t cap = 0; | ||
| while (cap < need_bytes) { | ||
| cap += CHUNK_SIZE; | ||
| } | ||
|
|
||
| void * dev_ptr; | ||
| SYCL_CHECK( | ||
| CHECK_TRY_ERROR(dev_ptr = sycl::malloc_device( | ||
| cap, *qptr))); | ||
|
|
||
| if (!dev_ptr) { | ||
| GGML_LOG_ERROR("%s: can't allocate %lu Bytes of memory on device\n", __func__, cap); | ||
| GGML_ABORT("fattn buffer alloc failed"); | ||
| } | ||
|
|
||
| ptr = static_cast<sycl::half *>(dev_ptr); | ||
| capacity = cap; | ||
| return ptr; | ||
| } | ||
|
|
||
| ggml_sycl_fattn_kv_buffers::kv_buffer::~kv_buffer() { | ||
| #ifdef DEBUG_SYCL_POOL | ||
| GGML_LOG_INFO("ggml_sycl_fattn_kv_buffer[%d]: %.2f MiB\n", device, capacity / 1024.0 / 1024.0); | ||
| #endif | ||
| if (ptr) { | ||
| SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(ptr, *qptr))); | ||
| } | ||
| } | ||
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,63 @@ | ||
| // | ||
| // MIT license | ||
| // Copyright (C) 2025 Intel Corporation | ||
| // SPDX-License-Identifier: MIT | ||
| // | ||
|
|
||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
|
|
||
| #ifndef GGML_SYCL_FATTN_BUFFERS_HPP | ||
| #define GGML_SYCL_FATTN_BUFFERS_HPP | ||
|
|
||
| #include <sycl/sycl.hpp> | ||
|
|
||
| typedef sycl::queue *queue_ptr; | ||
|
|
||
| struct ggml_sycl_fattn_kv_buffers { | ||
| // buffers grow in chunks of this size | ||
| static constexpr size_t CHUNK_SIZE = 16ull << 20; // 16 MiB | ||
|
|
||
| struct kv_buffer { | ||
| kv_buffer(queue_ptr qptr_, int device_) : qptr(qptr_), device(device_) {} | ||
| ~kv_buffer(); | ||
|
|
||
| kv_buffer(const kv_buffer &) = delete; | ||
| kv_buffer & operator=(const kv_buffer &) = delete; | ||
|
|
||
| sycl::half * ensure_half(size_t n_elems); | ||
|
|
||
| private: | ||
| sycl::half * ptr = nullptr; | ||
| size_t capacity = 0; | ||
| queue_ptr qptr = nullptr; | ||
| [[maybe_unused]] int device = 0; | ||
| }; | ||
|
|
||
| kv_buffer K; | ||
| kv_buffer V; | ||
|
|
||
| ggml_sycl_fattn_kv_buffers(queue_ptr qptr, int device) : K(qptr, device), V(qptr, device) {} | ||
|
|
||
| ggml_sycl_fattn_kv_buffers(const ggml_sycl_fattn_kv_buffers &) = delete; | ||
| ggml_sycl_fattn_kv_buffers & operator=(const ggml_sycl_fattn_kv_buffers &) = delete; | ||
| }; | ||
|
|
||
| /** | ||
| * Imitates `ggml_sycl_pool_alloc` to keep the code calling alloc unchanged. | ||
| */ | ||
| struct ggml_sycl_fattn_alloc { | ||
| ggml_sycl_fattn_kv_buffers::kv_buffer & buf; | ||
| sycl::half * ptr = nullptr; | ||
|
|
||
| explicit ggml_sycl_fattn_alloc(ggml_sycl_fattn_kv_buffers::kv_buffer & buf_) : buf(buf_) {} | ||
|
|
||
| sycl::half * alloc(size_t n_elems) { | ||
| ptr = buf.ensure_half(n_elems); | ||
| return ptr; | ||
| } | ||
| }; | ||
| #endif |
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 |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| #include "common.hpp" | ||
| #include "convert.hpp" | ||
| #include "vecdotq.hpp" | ||
| #include "fattn-buffers.hpp" | ||
|
|
||
| #include "ggml.h" | ||
|
|
||
|
|
@@ -918,12 +919,13 @@ void launch_fattn( | |
| GGML_ASSERT(!mask || mask->type == GGML_TYPE_F16); | ||
|
|
||
| ggml_sycl_pool & pool = ctx.pool(); | ||
| ggml_sycl_fattn_kv_buffers & fbuf = ctx.fattn_buffers(); | ||
| dpct::queue_ptr main_stream = ctx.stream(); | ||
| const int id = ggml_sycl_get_device(); | ||
| const int nsm = ggml_sycl_info().devices[id].nsm; | ||
|
|
||
| ggml_sycl_pool_alloc<sycl::half> K_f16(pool); | ||
| ggml_sycl_pool_alloc<sycl::half> V_f16(pool); | ||
| ggml_sycl_fattn_alloc K_f16(fbuf.K); | ||
| ggml_sycl_fattn_alloc V_f16(fbuf.V); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I considered adding a no-op template to make it look more uniform: |
||
| ggml_sycl_pool_alloc<int> KV_max(pool); | ||
| ggml_sycl_pool_alloc<float> dst_tmp(pool); | ||
| ggml_sycl_pool_alloc<sycl::float2> dst_tmp_meta(pool); | ||
|
|
||
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.