GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - yonseicasl/REAL: REAL: REtrieval-reAsoning and Logic-constructed Attention Behaviors for Long-Context KV Cache Compression

BranchesTags
Open more actions menu

Latest commit

 

History

13 Commits
13 Commits

Folders and files

NameName
Last commit message
Last commit date
Important_Head
Important_Head
 
 
csrc
csrc
 
 
headkv
headkv
 
 
.gitignore
.gitignore
 
 
InfKV_eval.py
InfKV_eval.py
 
 
InfKV_head_base.sh
InfKV_head_base.sh
 
 
InfKV_head_base_beta.sh
InfKV_head_base_beta.sh
 
 
InfKV_run_longbench.py
InfKV_run_longbench.py
 
 
README.md
README.md
 
 
fp_fn_visual_evidence_annotated.pdf
fp_fn_visual_evidence_annotated.pdf
 
 
head_base.sh
head_base.sh
 
 
head_base_babi.sh
head_base_babi.sh
 
 
head_start_llama.sh
head_start_llama.sh
 
 
head_start_mistral.sh
head_start_mistral.sh
 
 
metrics.py
metrics.py
 
 
needle_base.sh
needle_base.sh
 
 
needle_llama3.sh
needle_llama3.sh
 
 
needle_mistral.sh
needle_mistral.sh
 
 
radial_bar_chart.pdf
radial_bar_chart.pdf
 
 
requirements.txt
requirements.txt
 
 
run_longbench.py
run_longbench.py
 
 
run_needle_in_haystack.py
run_needle_in_haystack.py
 
 
strategy_comparison.pdf
strategy_comparison.pdf
 
 
test.sh
test.sh
 
 

Repository files navigation

REAL: REtrieval-reAsoning and Logic-constructed Attention Behaviors for Long-Context KV Cache Compression

The growing sequence length of large language models poses significant challenges for key-value caches. Existing state-of-the-art cache eviction methods primarily analyze the inference behavior of attention heads in successful retrieval-reasoning cases, often overlooking diverse behaviors in failure cases, such as bias and distraction. This oversight limits the potential to leverage heterogeneous head behaviors for improved eviction performance. Inspired by the confusion matrix, we introduce an Attention Behavior Matrix to comprehensively analyze attention head behaviors in both success and failure scenarios. By maximizing the signal-to-noise ratio — strengthening valid reasoning pathways in success cases while inhibiting noise from bias and distraction in failure cases — we propose REtrieval-reAsoning and Logic-constructed (REAL). REAL is the first KV cache eviction method that leverages multi-behavior analysis.

Table of Contents

  • Updated Timeline
  • Note
  • Environment Setup
  • Download Model Weights and Datasets
  • Workflow

Updated Timeline

😊 [2026.4.15] Source Code Commitment.


Note

⚠️ The data/ directory and model weights are not included in this repository due to file size limits. Please follow the instructions below to download them.


Environment Setup

1. Clone this repository

git clone https://github.com/yonseicasl/REAL.git
cd REAL

2. Install dependencies

pip install -r requirements.txt

3. Build the CUDA extension

cd csrc
make
cd ..

Download Model Weights and Datasets

1. Download Model Weights

Item Source Size
Meta-Llama-3-8B-Instruct HuggingFace ~16 GB
Mistral-7B-Instruct-v0.2 HuggingFace ~14 GB

Meta-Llama-3-8B-Instruct

huggingface-cli download meta-llama/Meta-Llama-3-8B-Instruct \
    --local-dir ./Meta-Llama-3-8B-Instruct

Or using Python:

from huggingface_hub import snapshot_download
snapshot_download(repo_id="meta-llama/Meta-Llama-3-8B-Instruct",
                  local_dir="./Meta-Llama-3-8B-Instruct")

Mistral-7B-Instruct-v0.2

huggingface-cli download mistralai/Mistral-7B-Instruct-v0.2 \
    --local-dir ./Mistral-7B-Instruct-v0.2

Or using Python:

from huggingface_hub import snapshot_download
snapshot_download(repo_id="mistralai/Mistral-7B-Instruct-v0.2",
                  local_dir="./Mistral-7B-Instruct-v0.2")

2. Download Datasets

Item Source Size
LongBench HuggingFace ~500 MB
LongBench v2 HuggingFace ~200 MB

LongBench

mkdir -p data/LongBench
python3 - <<'EOF'
from datasets import load_dataset
import json, os

tasks = [
    "narrativeqa", "qasper", "multifieldqa_en", "multifieldqa_zh",
    "hotpotqa", "2wikimqa", "musique", "dureader", "gov_report",
    "qmsum", "multi_news", "vcsum", "trec", "triviaqa", "samsum",
    "lsht", "passage_count", "passage_retrieval_en", "passage_retrieval_zh",
    "lcc", "repobench-p"
]
for task in tasks:
    ds = load_dataset("THUDM/LongBench", task, split="test")
    with open(f"data/LongBench/{task}.jsonl", "w") as f:
        for item in ds:
            f.write(json.dumps(item, ensure_ascii=False) + "\n")
    print(f"Downloaded: {task}")
EOF

LongBench v2

mkdir -p data/longbenchv2
python3 - <<'EOF'
from datasets import load_dataset
import json

ds = load_dataset("THUDM/LongBench-v2", split="train")
# split by category
from collections import defaultdict
buckets = defaultdict(list)
for item in ds:
    buckets[item["domain"]].append(item)
for domain, items in buckets.items():
    fname = domain.lower().replace(" ", "_") + ".jsonl"
    with open(f"data/longbenchv2/{fname}", "w") as f:
        for item in items:
            f.write(json.dumps(item, ensure_ascii=False) + "\n")
    print(f"Downloaded: {fname}")
EOF

Workflow

1. Compute INFsc

python structure_head_InfScore.py --model meta-llama/Meta-Llama-3-8B-Instruct --max_len 2048 --depths 15

2. Allocate KV budget

 ./head_base.sh 0 ReasonKV 0.1 flash_attention_2 meta-llama/Meta-Llama-3-8B-Instruct reason 1.351 0.98
 ./InfKV_head_base.sh 0 InfKV 128 flash_attention_2 meta-llama/Meta-Llama-3-8B-Instruct 1.5 1

3. Inference

./head_base.sh 0 ReasonKV 0.05 flash_attention_2 meta-llama/Meta-Llama-3-8B-Instruct reason 1 1

4. Evaluate

python InfKV_eval.py \
    --results_dir results_long_bench_reason_base0.1_beta1.351_temp0.98 \
    --model Meta-Llama-3-8B-Instruct \
    --capacity_ratio 0.1
python eval.py \
    --results_dir HeadKV/results/results_long_bench_reason_base128_beta1.351_temp0.98 \
    --model meta-llama-3-8b-instruct \
    --capacity 128

About

REAL: REtrieval-reAsoning and Logic-constructed Attention Behaviors for Long-Context KV Cache Compression

Topics

attention-headsattention-mechanismbudgetcache-evictioncompressionevictioninferencekv-cachellmlong-contextlongbenchmachine-learningreasoningretrievaltransformers

Resources

Readme
Activity
Custom properties

Stars

1 star

Watchers

0 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.