-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
84 additions
and
3 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
shortfin/python/shortfin_apps/llm/components/kvcache/base_attention_cache.py
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,80 @@ | ||
# Copyright 2024 Advanced Micro Devices, Inc. | ||
# | ||
# Licensed 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 | ||
|
||
""" | ||
Base class for kv caches. | ||
""" | ||
|
||
from typing import List | ||
from attention_paging import PageInfo | ||
import math | ||
|
||
|
||
|
||
class BasePagedAttentionCache: | ||
""" | ||
Manages lifecycle of pages (using PageInfo as handles). | ||
Page States: | ||
Caching - Page can be read by multiple threads | ||
- Also maintains a reference count | ||
Writing - Page is being modified by a single owner thread | ||
Transitions: | ||
Caching -> Writing: When acquiring an unreferenced LRU leaf page for writing | ||
Writing -> Caching: When writing is complete and page is released | ||
Thread Safety: | ||
- Multiple readers allowed in ReadableCaching state | ||
- Single writer exclusive access in Writing state | ||
- Reference counting prevents eviction of in-use pages | ||
""" | ||
def __init__(self, page_pool, tokens_per_page): | ||
self.page_pool = page_pool | ||
self.tokens_per_page = tokens_per_page | ||
|
||
|
||
def acquire_pages_for_tokens(self, tokens: List[int]) -> tuple[List[PageInfo], int)]: | ||
""" | ||
Given a list of tokens, return a list of pages and a start position to continue generation from. | ||
In the base implementation, this will just allocate all new pages, but in shared-kv implementations, we will fetch cached pages if applicable. | ||
The pages are returned in order. | ||
No token at idx < n_cached_token should be written to. TODO: consider enforcing this. | ||
""" | ||
pages_needed = math.ceil(len(tokens) / self.tokens_per_page) | ||
pages = self.page_pool.acquire_free_pages(pages_needed) | ||
|
||
n_cached_tokens = 0 | ||
|
||
return pages, n_cached_tokens | ||
|
||
def publish_pages(self, tokens, pages) -> None: | ||
""" | ||
Given a list of tokens and pages containing KV corresponding to these tokens, make these pages available to other requests. | ||
Associates the tokens with the pages, and mark them as done writing. | ||
It is assumed that hereafter, the calling request will not modify these pages, at least not the positions [0:len(tokens)]. | ||
""" | ||
|
||
pass # the base implementation doesn't cache unfinished requests. | ||
|
||
def release_pages(self, tokens, pages): | ||
""" | ||
Decrement reference count for these pages. When reference count is zero, they will be elegible for eviction. | ||
""" | ||
# in the base implementation, the pages can be owned by 1 request max, so they can be instantly release | ||
self.page_pool.release_pages(pages) | ||
|
||
|
||
|
||
|
||
|
||
|
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