-
Notifications
You must be signed in to change notification settings - Fork 200
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
Prefix caching improvements #758
Merged
ilya-lavrenov
merged 12 commits into
openvinotoolkit:master
from
popovaan:prefix_caching_improvements
Aug 14, 2024
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c97e69c
Applied review comments.
popovaan d869221
Minor correction.
popovaan 7f3e4f6
Optimized hash computation.
popovaan 73de969
Moved restoring of blocks to the requests creation method.
popovaan 7e6e2e5
Merge remote-tracking branch 'upstream/master' into prefix_caching_im…
popovaan f9fe637
Added weak_ptr to sequence group in Sequence.
popovaan 9fd7dd3
Merge remote-tracking branch 'upstream/master' into prefix_caching_im…
popovaan 9fcf2eb
Code format.
popovaan adfd7e6
Code format.
popovaan acd443b
Tests cmake fix.
popovaan 9f0bed2
Minor correction.
popovaan 006b144
Removed enable_prefix_caching flag from Sequence, changed conditions.
popovaan 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 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 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 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 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,60 @@ | ||
// Copyright (C) 2023-2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include <string_view> | ||
#include "sequence_group.hpp" | ||
|
||
namespace ov { | ||
namespace genai { | ||
size_t Sequence::_make_hash(size_t content_length) { | ||
auto sequence_group = get_sequence_group_ptr(); | ||
auto block_size = sequence_group->get_block_size(); | ||
size_t block_start_idx = content_length - (content_length % block_size); | ||
if (block_start_idx == content_length) { | ||
block_start_idx -= block_size; | ||
} | ||
|
||
// hash of current block depends on prefix hashes | ||
std::vector<int64_t> content; | ||
size_t prefix_hashes_needed_count = block_start_idx / block_size; | ||
OPENVINO_ASSERT(prefix_hashes_needed_count <= m_prefix_hashes.size()); | ||
content.insert(content.end(), m_prefix_hashes.begin(), m_prefix_hashes.begin() + prefix_hashes_needed_count); | ||
|
||
// get tokens corresponding to current block | ||
const auto prompt_ids = sequence_group->get_prompt_ids(); | ||
OPENVINO_ASSERT(content_length <= prompt_ids.size() + m_generated_ids.size()); | ||
if (block_start_idx < prompt_ids.size()) { | ||
content.insert(content.end(), prompt_ids.begin() + block_start_idx, prompt_ids.begin() + std::min(prompt_ids.size(), content_length)); | ||
} | ||
if (content_length > prompt_ids.size()) { | ||
size_t start = block_start_idx < prompt_ids.size() ? 0 : block_start_idx - prompt_ids.size(); | ||
content.insert(content.end(), m_generated_ids.begin() + start, m_generated_ids.begin() + content_length - prompt_ids.size()); | ||
} | ||
const char* data = reinterpret_cast<const char*>(content.data()); | ||
std::size_t size = content.size() * sizeof(content[0]); | ||
return std::hash<std::string_view>{}(std::string_view(data, size)); | ||
} | ||
|
||
// Each KV block can be uniquely identified by | ||
// the tokens within the block and the tokens in the prefix before the block. | ||
// hash(prefix tokens + block tokens) <--> KV Block | ||
size_t Sequence::get_hash(size_t content_length) { | ||
|
||
auto sequence_group = get_sequence_group_ptr(); | ||
OPENVINO_ASSERT(sequence_group, "Hash computation requires setting of sequence_group ptr."); | ||
auto content_len = content_length == 0 ? sequence_group->get_context_len() : content_length; | ||
auto block_size = sequence_group->get_block_size(); | ||
size_t cur_content = block_size * (m_prefix_hashes.size() + 1); | ||
while (cur_content <= content_len) | ||
{ | ||
m_prefix_hashes.push_back(_make_hash(cur_content)); | ||
cur_content += block_size; | ||
} | ||
if (content_len % block_size == 0) { | ||
return m_prefix_hashes[content_len / block_size - 1]; | ||
} | ||
|
||
return _make_hash(content_len); | ||
} | ||
} // namespace genai | ||
} // namespace ov |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need to add an assert that content_length corresponds to last uncomputed block? E.g. if we have 1 block with has, but content_length is 3x of block_size.
E.g. block_start_idx / block_size == m_prefix_hashes.size()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a case when
block_start_idx / block_size < m_prefix_hashes.size()
.When we restore blocks of prompt first we check hash of full block and it is saved in
m_prefix_hashes
, then if we couldn't find hash of full block incashed_blocks
we check hashes of partially completed content of this block. Socontent_length
in this case is less thanm_prefix_hashes.size() * block_size
.So I added assert
block_start_idx / block_size <= m_prefix_hashes.size()
.