| import torch |
| import torch.nn as nn |
| import torch.nn.functional as F |
| from typing import List |
| import os |
|
|
| from inference.pipeline.kvcompress.utils import cal_similarity, compute_attention_scores |
|
|
|
|
| class KVCompressor: |
| def __init__( |
| self, |
| kernel_size=7, |
| mix_lambda=0.07, |
| compress_strategy="token", |
| query_granularity="chunk", |
| score_weighting_method="default", |
| power=3, |
| **kwargs, |
| ): |
| self.kernel_size = kernel_size |
| self.mix_lambda = mix_lambda |
| assert compress_strategy in ["token", "frame", "chunk"] |
| assert query_granularity in ["token", "frame", "chunk"] |
| self.compress_strategy = compress_strategy |
| self.query_granularity = query_granularity |
| self.score_weighting_method = score_weighting_method |
| self.power = power |
|
|
| def update_kv( |
| |
| self, |
| key_states, |
| query_states, |
| value_states, |
| clean_chunk_tokens, |
| latent_size_t, |
| latent_size_h, |
| latent_size_w |
| ): |
| if self.query_granularity == "token": |
| |
| query_states = query_states[- 50 : ] |
| elif self.query_granularity == "frame": |
| query_states = query_states[- latent_size_h * latent_size_w : ] |
| elif self.query_granularity == "chunk": |
| pass |
| else: |
| raise ValueError("Invalid query granularity") |
| |
| if self.compress_strategy == "token": |
| return self.update_kv_token( |
| key_states, |
| query_states, |
| value_states, |
| clean_chunk_tokens, |
| each_chunk_tokens=latent_size_t * latent_size_h * latent_size_w, |
| ) |
| elif self.compress_strategy == "frame": |
| return self.update_kv_frame_chunk( |
| key_states, |
| query_states, |
| value_states, |
| clean_chunk_tokens, |
| together_size=latent_size_h * latent_size_w, |
| ) |
| elif self.compress_strategy == "chunk": |
| return self.update_kv_frame_chunk( |
| key_states, |
| query_states, |
| value_states, |
| clean_chunk_tokens, |
| together_size=latent_size_t * latent_size_h * latent_size_w, |
| ) |
| else: |
| raise ValueError("Invalid compress strategy") |
|
|
| def update_kv_token( |
| self, |
| key_states, |
| query_states, |
| value_states, |
| clean_chunk_tokens, |
| each_chunk_tokens, |
| ): |
| each_chunk_tokens = int(each_chunk_tokens) |
| head_dim = query_states.shape[-1] |
| kv_cache_len = key_states.shape[0] |
|
|
| attn_weights = compute_attention_scores(query_states, key_states[:clean_chunk_tokens]) |
| attn_weights_sum = ( |
| nn.functional.softmax( |
| attn_weights[:, :, : clean_chunk_tokens], |
| dim=-1, |
| dtype=torch.float32, |
| ) |
| .mean(dim=-2) |
| .to(query_states.dtype) |
| ) |
|
|
| attn_cache = F.max_pool1d( |
| attn_weights_sum, |
| kernel_size=self.kernel_size, |
| padding=self.kernel_size // 2, |
| stride=1, |
| ).to('cpu') |
|
|
| similarity_cos = cal_similarity(key_states[:clean_chunk_tokens, :, :]).to('cpu') |
|
|
| final_score = attn_cache * self.mix_lambda - similarity_cos * (1 - self.mix_lambda) |
|
|
| |
| min_scores_per_head = final_score.min(dim=-1, keepdim=True).values |
| final_score = final_score - min_scores_per_head |
|
|
| |
| |
| if self.score_weighting_method == "no_weight": |
| print("Using no weighting method") |
| pass |
|
|
| elif self.score_weighting_method == "hard_code": |
| print("Using hard code weighting method") |
| final_score[:, :each_chunk_tokens] -= 1e6 |
| |
| elif self.score_weighting_method == "exponential": |
| print("Using exponential weighting method") |
| seq_len = final_score.shape[1] |
| positions = torch.arange(seq_len, dtype=torch.float32, device=final_score.device) / (seq_len - 1) if seq_len > 1 else torch.zeros(1) |
| decay_rate = 2.0 |
| |
| exponential_values = 1 - torch.exp(-decay_rate * positions) |
| max_value = 1 - torch.exp(torch.tensor(-decay_rate, device=final_score.device)) |
| weights = 0.1 + 0.9 * (exponential_values / max_value) |
| final_score = final_score * weights.unsqueeze(0) |
|
|
| elif self.score_weighting_method == "polynomial": |
| print(f"Using polynomial weighting method, power={self.power}") |
| seq_len = final_score.shape[1] |
| positions = torch.arange(seq_len, dtype=torch.float32, device=final_score.device) / (seq_len - 1) if seq_len > 1 else torch.zeros(1) |
| |
| weights = 0.1 + 0.9 * (positions ** self.power) |
| final_score = final_score * weights.unsqueeze(0) |
| elif self.score_weighting_method == "upper_convex_polynomial": |
| print(f"Using upper convex polynomial weighting method, power={self.power}") |
| seq_len = final_score.shape[1] |
| positions = torch.arange(seq_len, dtype=torch.float32, device=final_score.device) / (seq_len - 1) if seq_len > 1 else torch.zeros(1) |
| max_value = 2.0 |
| |
| weights = max_value * (1 - (1 - positions) ** self.power) |
| final_score = final_score * weights.unsqueeze(0) |
|
|
| elif self.score_weighting_method == "gaussian": |
| print("Using gaussian weighting method") |
| |
| seq_len = final_score.shape[1] |
|
|
| positions = torch.arange(seq_len, dtype=torch.float32, device=final_score.device) |
| sigma = seq_len / 4.0 |
|
|
| gaussian_decay = torch.exp(-0.5 * (positions / sigma) ** 2) |
| min_decay = torch.exp(torch.tensor(-0.5 * ((seq_len - 1) / sigma) ** 2, device=final_score.device)) |
|
|
| |
| weights = 0.1 + 0.9 * ((gaussian_decay - min_decay) / (1.0 - min_decay)) |
| final_score = final_score * weights.unsqueeze(0) |
| else: |
| raise ValueError(f"Unknown score weighting method: {self.score_weighting_method}") |
|
|
| |
| num_to_keep = self.budget |
|
|
| |
| try: |
| indices = final_score.topk(num_to_keep, dim=-1).indices |
| del final_score |
| except RuntimeError: |
| import pdb; pdb.set_trace() |
| indices = indices.unsqueeze(-1).expand(-1, -1, head_dim).permute(1, 0, 2) |
|
|
| indices = indices.to(key_states.device) |
|
|
| |
| k_past_compress = key_states[:clean_chunk_tokens, :, :].gather(dim=0, index=indices) |
| v_past_compress = value_states[:clean_chunk_tokens, :, :].gather(dim=0, index=indices) |
| k_cur = key_states[clean_chunk_tokens :, :, :] |
| v_cur = value_states[clean_chunk_tokens :, :, :] |
|
|
| key_compress = torch.cat([k_past_compress, k_cur], dim=0) |
| value_compress = torch.cat([v_past_compress, v_cur], dim=0) |
|
|
| return key_compress, value_compress, indices |
|
|
| def update_kv_frame_chunk( |
| self, |
| key_states, |
| query_states, |
| value_states, |
| clean_chunk_tokens, |
| together_size, |
| ): |
| head_dim = query_states.shape[-1] |
| kv_cache_len = key_states.shape[0] |
|
|
| |
|
|
| |
| attn_weights = compute_attention_scores(query_states, key_states) |
| attn_weights_sum = ( |
| nn.functional.softmax( |
| attn_weights[:, :, : clean_chunk_tokens], |
| dim=-1, |
| dtype=torch.float32, |
| ) |
| .mean(dim=-2) |
| .to(query_states.dtype) |
| ) |
|
|
| |
| attn_cache = F.max_pool1d( |
| attn_weights_sum, |
| kernel_size=self.kernel_size, |
| padding=self.kernel_size // 2, |
| stride=1, |
| ).to('cpu') |
|
|
| |
| similarity_cos = cal_similarity(key_states[:clean_chunk_tokens, :, :]).to('cpu') |
|
|
| |
| final_score_per_token = attn_cache * self.mix_lambda - similarity_cos * (1 - self.mix_lambda) |
| |
|
|
| |
| |
|
|
| assert clean_chunk_tokens % together_size == 0 |
| num_frames = clean_chunk_tokens // together_size |
|
|
| |
| score_frames = final_score_per_token.view( |
| key_states.shape[1], num_frames, together_size |
| ) |
|
|
| |
| frame_scores = score_frames.mean(dim=-1) |
|
|
| |
| assert self.budget % together_size == 0 |
| num_frames_to_keep = self.budget // together_size |
|
|
| |
| frame_indices = frame_scores.topk(num_frames_to_keep, dim=-1).indices |
| |
| |
|
|
| |
| |
|
|
| |
| token_offsets = torch.arange(together_size, device=key_states.device) |
| frame_indices_expanded = frame_indices.unsqueeze(-1) * together_size |
| token_indices_per_head = frame_indices_expanded + token_offsets |
| token_indices_flat = token_indices_per_head.view(key_states.shape[1], -1) |
| indices_gather = token_indices_flat.permute(1, 0).unsqueeze(-1).expand(-1, -1, head_dim) |
|
|
| |
| k_past_compress = key_states[:clean_chunk_tokens, :, :].gather(dim=0, index=indices_gather) |
| v_past_compress = value_states[:clean_chunk_tokens, :, :].gather(dim=0, index=indices_gather) |
|
|
| |
| k_cur = key_states[clean_chunk_tokens:, :, :] |
| v_cur = value_states[clean_chunk_tokens:, :, :] |
|
|
| key_compress = torch.cat([k_past_compress, k_cur], dim=0) |
| value_compress = torch.cat([v_past_compress, v_cur], dim=0) |
|
|
| return key_compress, value_compress, indices_gather |
|
|
|
|
| def plot_tensor_values(tensor_1d: torch.Tensor, title: str = "Tensor Values", save_path: str = None, |
| xlabel: str = "Position", ylabel: str = "Value", figsize: tuple = (10, 6)): |
| try: |
| import matplotlib.pyplot as plt |
| import numpy as np |
| except ImportError: |
| print("Warning: matplotlib not available, skipping plot") |
| return |
|
|
| |
| if tensor_1d.dim() != 1: |
| raise ValueError(f"Input tensor must be 1D, got {tensor_1d.dim()}D") |
|
|
| |
| values = tensor_1d.detach().cpu().float().numpy() |
| positions = np.arange(len(values)) |
|
|
| |
| plt.figure(figsize=figsize) |
| plt.plot(positions, values, 'b-', linewidth=2, markersize=4, alpha=0.8) |
| plt.xlabel(xlabel, fontsize=12) |
| plt.ylabel(ylabel, fontsize=12) |
| plt.title(title, fontsize=14) |
|
|
| |
| plt.tight_layout() |
|
|
| |
| if save_path is not None: |
| os.makedirs(os.path.dirname(save_path), exist_ok=True) |
| plt.savefig(save_path, dpi=100, bbox_inches='tight') |
| print(f"Plot saved to: {save_path}") |
|
|
| plt.close() |