Skip to content
Closed
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
7 changes: 5 additions & 2 deletions kvpress/presses/key_rerotation_press.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: Copyright (c) 1993-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-FileCopyrightText: Copyright (c) 1993-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make a bulk rewrite to update the whole repo?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can do that before the next release

# SPDX-License-Identifier: Apache-2.0


Expand Down Expand Up @@ -70,8 +70,10 @@ def _rerotate_cos_sin(x, inv_freq, selected_positions):
Cosine and sine embeddings, each of shape
``(bsz, num_key_value_heads, n_kept, d)``, matching ``dtype``/``device`` of ``x``.
"""
device = x.device
selected_positions = selected_positions.to(device=device)
inv_freq = inv_freq.to(device=device)
bsz, num_key_value_heads, n_kept = selected_positions.shape
device = selected_positions.device
device_type = x.device.type
dtype = x.dtype
# Original positional indices
Expand Down Expand Up @@ -119,6 +121,7 @@ def rerotate_keys(
The rerotated keys tensor of shape
``(bsz, num_heads, n_kept, d)``.
"""
indices = indices.to(device=keys.device)
new_cos, new_sin = KeyRerotationPress._rerotate_cos_sin(keys, module.rotary_emb.inv_freq, indices)
indices = indices.unsqueeze(-1).expand(-1, -1, -1, module.head_dim)
keys = keys.gather(2, indices).contiguous()
Expand Down
Loading