-
Notifications
You must be signed in to change notification settings - Fork 127
Integrate Q-Filters #54
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
13 commits
Select commit
Hold shift + click to select a range
52b3541
Add epsilon to ExpectedAttentionPress (#47)
SimJeg 7180135
Fix distributed inference (#49)
SimJeg 8565cbb
qfilters_press
alessiodevoto 9e0f6b0
Add DuoAttentionPress (#50)
SimJeg e25b7a6
add ChunkKV
Dominic789654 48270fa
fix style
dbfc374
qfilters_press
alessiodevoto 8fc790e
Add DuoAttentionPress (#50)
SimJeg 850e107
add ChunkKV
Dominic789654 4888dca
fix style
66a0593
Merge branch 'main' into qfilters
NathanGodey 3bb4077
implement discussion suggestions
6c6bc0f
copyright + readme + final fixes
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,58 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 1993-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from contextlib import contextmanager | ||
| from dataclasses import dataclass | ||
|
|
||
| import torch | ||
| from huggingface_hub import PyTorchModelHubMixin, get_collection | ||
|
|
||
| from kvpress.presses.scorer_press import ScorerPress | ||
|
|
||
|
|
||
| class QFilters(torch.nn.Module, PyTorchModelHubMixin): | ||
| def __init__(self, num_layers: int, num_kv_heads: int, kv_head_dim: int): | ||
| super().__init__() | ||
| self.q_filters = torch.nn.Parameter(torch.randn(num_layers, num_kv_heads, kv_head_dim)) | ||
|
|
||
| @classmethod | ||
| def from_pretrained(cls, pretrained_model_name_or_path): | ||
| return super().from_pretrained(pretrained_model_name_or_path) | ||
|
|
||
|
|
||
| @dataclass | ||
| class QFilterPress(ScorerPress): | ||
| """ | ||
| Prune KV pairs with Q-filters | ||
| """ | ||
|
|
||
| def __post_init_from_model__(self, model): | ||
| model_name = model.config.name_or_path.split("/")[-1] | ||
| self.q_filters = self.load_q_filters(model_name) | ||
| self.q_filters = self.q_filters.to(model.dtype) | ||
|
|
||
| @staticmethod | ||
| def load_q_filters(model_name): | ||
| try: | ||
| return QFilters.from_pretrained(f"nthngdy/{model_name}_qfilt").q_filters | ||
| except TypeError: | ||
| raise ValueError( | ||
| f"Could not load Q-filters for {model_name}. Available models: {QFilterPress.available_qfilters()}" | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def available_qfilters(): | ||
| collection = get_collection("nthngdy/q-filters-67a4994dcb302a3d37f3d119", token=False) | ||
| return [x.item_id.split("/")[-1][:-6] for x in collection.items] | ||
|
|
||
| def score(self, module, hidden_states, keys, values, attentions, kwargs): | ||
| q_filter = self.q_filters[module.layer_idx][None, :, None] | ||
| q_filter = q_filter.to(keys.device) | ||
| scores = -(q_filter * keys).sum(dim=-1) | ||
| return scores | ||
|
|
||
| @contextmanager | ||
| def __call__(self, model): | ||
| self.__post_init_from_model__(model) | ||
| with super().__call__(model): | ||
| yield |
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,8 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 1993-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| from kvpress.presses.qfilter_press import QFilterPress | ||
|
|
||
|
|
||
| def test_load_qfilters(): | ||
| for model_name in QFilterPress.available_qfilters(): | ||
| QFilterPress.load_q_filters(model_name) |
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.