Skip to content

Add customized static cache implementation #4476

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
15 changes: 14 additions & 1 deletion examples/models/phi-3-mini/eager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

from transformers import AutoTokenizer, Phi3ForCausalLM

from .static_cache import ETStaticCache

end_of_text_token = 32000


Expand All @@ -40,7 +42,18 @@ def _generate_token(args, model, prompt_tokens):
def _generate_token_with_kv_cache(args, model, prompt_tokens):
print("Generating tokens:", end="", flush=True)

result = model.forward(input_ids=prompt_tokens, use_cache=True, return_dict=True)
result = model.forward(
input_ids=prompt_tokens,
use_cache=True,
return_dict=True,
past_key_values=ETStaticCache(
model.config,
prompt_tokens.shape[0],
args.seq_len + prompt_tokens.shape[-1],
device=model.device,
dtype=model.dtype,
),
)

current_token = torch.argmax(result.logits[:, -1, :], dim=-1).item()
current_key_value = result.past_key_values
Expand Down
36 changes: 36 additions & 0 deletions examples/models/phi-3-mini/static_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
from typing import Optional

import torch
from transformers import PretrainedConfig, StaticCache


class ETStaticCache(StaticCache):

def __init__(
self,
config: PretrainedConfig,
max_batch_size: int,
max_cache_len: int,
device,
dtype=torch.float32,
) -> None:
super().__init__(
config=config,
max_batch_size=max_batch_size,
max_cache_len=max_cache_len,
device=device,
dtype=dtype,
)

def get_seq_length(self, layer_idx: Optional[int] = 0) -> int:
return (self.key_cache[layer_idx][0, 0].any(dim=-1)).sum().item()

def get_usable_length(
self, new_seq_length: int, layer_idx: Optional[int] = 0
) -> int:
return self.get_seq_length(layer_idx)
Loading