-
Notifications
You must be signed in to change notification settings - Fork 3k
Add prefix cache flag to exo bench #1888
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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 |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| import contextlib | ||
| import time | ||
| from dataclasses import dataclass, field | ||
| from typing import Callable, cast | ||
| from typing import Callable, Literal, cast | ||
|
|
||
| import mlx.core as mx | ||
| from mlx_lm.generate import ( | ||
|
|
@@ -74,14 +74,14 @@ class _EngineTask: | |
| all_prompt_tokens: mx.array | ||
| prefix_hit_length: int | ||
| matched_index: int | None | ||
| cache_snapshots: list[CacheSnapshot] | None | ||
| detokenizer: StreamingDetokenizer | ||
| on_generation_token: Callable[[], None] | None = None | ||
| generated_text_parts: list[str] = field(default_factory=list) | ||
| potential_stop_sequence_text: str = "" | ||
| completion_tokens: int = 0 | ||
| generation_start_time: float = 0.0 | ||
| prefill_tps: float = 0.0 | ||
| prefix_cache_hit: Literal["none", "partial", "exact"] = "none" | ||
| media_regions: list[MediaRegion] = field(default_factory=list) | ||
| first_gen_token_time: float | None = None | ||
| last_gen_token_time: float | None = None | ||
|
|
@@ -155,11 +155,16 @@ def submit( | |
|
|
||
| prefix_hit_length = 0 | ||
| matched_index: int | None = None | ||
| is_exact_hit = False | ||
| prompt_tokens = all_prompt_tokens | ||
|
|
||
| if self.kv_prefix_cache is not None and not is_bench: | ||
| cache, remaining_tokens, matched_index = self.kv_prefix_cache.get_kv_cache( | ||
| self.model, all_prompt_tokens, media_regions=media_regions | ||
| if self.kv_prefix_cache is not None and ( | ||
| not is_bench or task_params.use_prefix_cache | ||
| ): | ||
| cache, remaining_tokens, matched_index, is_exact_hit = ( | ||
| self.kv_prefix_cache.get_kv_cache( | ||
| self.model, all_prompt_tokens, media_regions=media_regions | ||
| ) | ||
| ) | ||
| prefix_hit_length = len(all_prompt_tokens) - len(remaining_tokens) | ||
| if prefix_hit_length > 0: | ||
|
|
@@ -168,8 +173,6 @@ def submit( | |
| f"cached ({100 * prefix_hit_length / len(all_prompt_tokens):.1f}%)" | ||
| ) | ||
| prompt_tokens = remaining_tokens | ||
| else: | ||
|
Collaborator
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. We don't need to do this twice |
||
| cache = make_kv_cache(self.model) | ||
| else: | ||
| cache = make_kv_cache(self.model) | ||
|
|
||
|
|
@@ -208,6 +211,15 @@ def submit( | |
| distributed_prompt_progress_callback, | ||
| ) | ||
|
|
||
| prefix_cache_hit: Literal["none", "partial", "exact"] = "none" | ||
| if matched_index is not None and prefix_hit_length > 0: | ||
| assert self.kv_prefix_cache is not None | ||
| if is_exact_hit: | ||
| prefix_cache_hit = "exact" | ||
| _prefill_tps = self.kv_prefix_cache.prefill_tps[matched_index] | ||
| else: | ||
| prefix_cache_hit = "partial" | ||
|
|
||
| # We need to clamp rotating kv caches to max size so that mlx lm's _merge_caches behaves | ||
| for c in cache: | ||
| if ( | ||
|
|
@@ -221,7 +233,7 @@ def submit( | |
| c.values = c._trim(trim_size, c.values) | ||
| c._idx = c.max_size | ||
|
|
||
| if not is_bench: | ||
| if not is_bench or task_params.use_prefix_cache: | ||
| min_prefix_hit_length = max( | ||
| 1000, system_prompt_token_count(task_params, self.tokenizer) | ||
| ) | ||
|
|
@@ -233,6 +245,7 @@ def submit( | |
| matched_index, | ||
| min_prefix_hit_length, | ||
| media_regions, | ||
| prefill_tps=_prefill_tps, | ||
| ) | ||
|
|
||
| last_tokens = prompt_tokens[-2:] | ||
|
|
@@ -268,11 +281,11 @@ def submit( | |
| all_prompt_tokens=all_prompt_tokens, | ||
| prefix_hit_length=prefix_hit_length, | ||
| matched_index=matched_index, | ||
| cache_snapshots=cache_snapshots or None, | ||
| detokenizer=self.tokenizer.detokenizer, | ||
| on_generation_token=on_generation_token, | ||
| generation_start_time=time.perf_counter(), | ||
| prefill_tps=_prefill_tps, | ||
| prefix_cache_hit=prefix_cache_hit, | ||
| media_regions=media_regions, | ||
| ) | ||
|
|
||
|
|
@@ -383,6 +396,7 @@ def step(self) -> list[tuple[int, GenerationResponse]]: | |
| prompt_tokens=len(state.all_prompt_tokens), | ||
| generation_tokens=state.completion_tokens, | ||
| peak_memory_usage=Memory.from_gb(mx.get_peak_memory() / 1e9), | ||
| prefix_cache_hit=state.prefix_cache_hit, | ||
| ) | ||
| total_prompt_tokens = len(state.all_prompt_tokens) | ||
| usage = Usage( | ||
|
|
@@ -449,6 +463,7 @@ def _save_prefix_cache( | |
| matched_index: int | None, | ||
| min_prefix_hit_length: int = 1000, | ||
| media_regions: list[MediaRegion] | None = None, | ||
| prefill_tps: float = 0.0, | ||
| ) -> None: | ||
| if self.kv_prefix_cache is None: | ||
| return | ||
|
|
@@ -470,13 +485,15 @@ def _save_prefix_cache( | |
| cache_snapshots, | ||
| restore_pos=prefix_hit_length, | ||
| media_regions=media_regions, | ||
| prefill_tps=prefill_tps, | ||
| ) | ||
| else: | ||
| self.kv_prefix_cache.add_kv_cache( | ||
| all_prompt_tokens, | ||
| cache, | ||
| cache_snapshots, | ||
| media_regions=media_regions, | ||
| prefill_tps=prefill_tps, | ||
| ) | ||
| except Exception: | ||
| logger.warning("Failed to save prefix cache", exc_info=True) | ||
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.
was this just unused?