Skip to main content

KV Cache Visualizer - Attention sink and eviction policy visualization for LLMs

Project description

SinkVis

KV Cache Visualizer — A Python library for understanding attention sinks and KV cache eviction policies in large language models.

Installation

# Basic installation
pip install .

# With HuggingFace transformers support
pip install ".[transformers]"

# With development dependencies
pip install ".[dev]"

# Everything
pip install ".[all]"

From GitHub

pip install git+https://github.com/yourusername/SinkVis.git#subdirectory=py-package/sinkvis

Quick Start

from transformers import GPT2LMHeadModel, GPT2Tokenizer
from sinkvis import SinkVis

# Load model
model = GPT2LMHeadModel.from_pretrained("gpt2")
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")

# Capture attention patterns
with SinkVis(model, tokenizer) as sv:
    inputs = tokenizer("Hello world", return_tensors="pt")
    outputs = model(**inputs, output_attentions=True)
    
    # Get attention data (layer -1 = last layer, head 0)
    attention_data = sv.get_attention_data(layer=-1, head=0)
    
    # Simulate eviction policy
    keep_mask = sv.simulate_policy("streaming_llm", budget=20, sink_count=4)

Features

  • Attention Capture: Hook into any PyTorch transformer model to capture attention patterns
  • Sink Detection: Automatically identify attention sink tokens
  • Heavy Hitter Detection: Find semantically important tokens with high attention
  • Eviction Simulation: Compare policies (LRU, Sliding Window, StreamingLLM, H2O)
  • Memory Profiling: Track GPU memory usage

Eviction Policies

Policy Description
full No eviction (baseline)
lru Least Recently Used
sliding_window Keep only recent tokens
streaming_llm Preserve sinks + sliding window
h2o Heavy-Hitter Oracle

API Reference

SinkVis

class SinkVis:
    def __init__(self, model: nn.Module, tokenizer=None): ...
    def get_attention_data(self, layer: int = -1, head: int = 0) -> List[List[float]]: ...
    def simulate_policy(self, policy: str, budget: int, sink_count: int = 4) -> List[bool]: ...
    def get_memory_stats(self) -> dict: ...
    def analyze_attention(self, tokens: List[str] = None) -> AttentionFrame: ...

Simulation Functions

from sinkvis.simulation import (
    simulate_lru,
    simulate_sliding_window,
    simulate_streaming_llm,
    simulate_h2o,
)

# All return a boolean mask indicating which tokens to keep
mask = simulate_streaming_llm(seq_len=100, budget=20, sink_count=4)

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sinkvis-1.0.0.tar.gz (23.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sinkvis-1.0.0-py3-none-any.whl (26.7 kB view details)

Uploaded Python 3

File details

Details for the file sinkvis-1.0.0.tar.gz.

File metadata

  • Download URL: sinkvis-1.0.0.tar.gz
  • Upload date:
  • Size: 23.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sinkvis-1.0.0.tar.gz
Algorithm Hash digest
SHA256 22f35b4487847237e3a92b27068b2f7f995cde1e4eecaebd6fddb7c8502a1252
MD5 4c22f9bf6264a9040762749329c1aba0
BLAKE2b-256 66942fb75eb9c539eeaea6ad537199ef6611006507d075d1ee85381b6bbb3984

See more details on using hashes here.

Provenance

The following attestation bundles were made for sinkvis-1.0.0.tar.gz:

Publisher: publish.yml on zeinalii/SinkVis

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sinkvis-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: sinkvis-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 26.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sinkvis-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e53c5b19d9df2b2ff40db8e7f62a04cf0bbce6205ed942750d0a1b39b11b9269
MD5 6e9a24f70e07ed31d89c2910b50dcbcf
BLAKE2b-256 767f82e900892526caf6cc01c7577a3e95e48e0ef5cf30bb68ba3a42defa3b66

See more details on using hashes here.

Provenance

The following attestation bundles were made for sinkvis-1.0.0-py3-none-any.whl:

Publisher: publish.yml on zeinalii/SinkVis

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page