File size: 8,736 Bytes
b786614
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
"""
core.py — The main ProactiveCache user-facing API.

Three lines to make any HuggingFace model O(n):

    model = ProactiveCache.apply(model, budget=512)
    ProactiveCache.profile(model, tokenizer, corpus="wikitext")
    output = model.generate(input_ids, max_new_tokens=500)
"""

from __future__ import annotations
import os
import pickle
import torch
from typing import Optional, Union, List, Dict
from functools import wraps

from .profiler import profile_model as _profile_model
from .prototypes import build_prototypes, save_prototypes, load_prototypes
from .eviction import evict
from .utils import get_device, get_seq_len


class ProactiveCache:
    """
    Make any HuggingFace transformer O(n) with proactive KV cache eviction.

    Proactive Cache profiles attention head patterns offline (once per model),
    then uses the resulting prototype centroids to score token importance at
    inference time — without any query lookups. This makes each decode step
    O(budget) instead of O(n), achieving up to 3× generation speedup at
    2048-token contexts on LLaMA-3.1 8B.

    Quickstart:
        from proactive_cache import ProactiveCache
        from transformers import AutoModelForCausalLM, AutoTokenizer

        model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.1-8B")
        tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-8B")

        # Apply O(n) eviction (one line)
        model = ProactiveCache.apply(model, budget=512)

        # Profile once on calibration data (saves to disk)
        ProactiveCache.profile(model, tokenizer, corpus="wikitext")

        # All inference is now O(n)
        output = model.generate(input_ids, max_new_tokens=500)

    Licensed under the GNU Affero General Public License v3 (AGPLv3). See LICENSE.
    """

    @classmethod
    def apply(
        cls,
        model,
        budget: int = 256,
        prototype_path: Optional[str] = None,
    ):
        """
        Wrap a HuggingFace model's ``generate()`` with O(n) KV eviction.

        After calling ``apply()``, the model's ``generate()`` method will:
          1. Run the standard prefill pass.
          2. Score token importance using prototypes (or sink+recency if no
             prototypes are loaded yet).
          3. Prune the KV cache to ``budget`` tokens.
          4. Continue auto-regressive decoding against the pruned cache —
             each step now attends to B tokens instead of n, achieving O(n)
             overall complexity.

        Args:
            model:          Any HuggingFace ``AutoModelForCausalLM`` instance.
            budget:         Fixed KV cache size kept after eviction (tokens).
            prototype_path: Path to prototypes .pkl. If None, will search for
                            a ``proactive_cache_prototypes.pkl`` in the current
                            working directory.

        Returns:
            The same model object, with ``generate()`` patched in-place.
        """
        # Resolve prototype path
        if prototype_path is None:
            prototype_path = "proactive_cache_prototypes.pkl"

        prototypes = None
        if os.path.exists(prototype_path):
            prototypes = load_prototypes(prototype_path)
        else:
            print(
                f"[ProactiveCache] No prototypes at '{prototype_path}'. "
                "Using attention-sink + recency fallback. "
                "Run ProactiveCache.profile() for best results."
            )

        # Store config on the model for introspection
        model._proactive_config = {
            "budget": budget,
            "prototype_path": prototype_path,
            "active": True,
        }
        model._proactive_prototypes = prototypes

        # Patch generate()
        original_generate = model.generate.__func__ if hasattr(model.generate, "__func__") \
            else model.generate

        @wraps(original_generate)
        def proactive_generate(input_ids=None, **kwargs):
            return cls._wrapped_generate(
                model=model,
                original_generate=original_generate,
                input_ids=input_ids,
                budget=budget,
                prototypes=model._proactive_prototypes,
                **kwargs,
            )

        # Bind the patched generate
        import types
        model.generate = types.MethodType(
            lambda self, *args, **kw: proactive_generate(*args, **kw),
            model,
        )

        print(
            f"[ProactiveCache] Applied to {type(model).__name__} | "
            f"budget={budget} | "
            f"prototypes={'loaded' if prototypes else 'none (fallback)'}"
        )
        return model

    @classmethod
    def profile(
        cls,
        model,
        tokenizer,
        corpus: Union[str, List[str]] = "wikitext",
        num_docs: int = 50,
        seq_len: int = 512,
        n_clusters: int = 4,
        save_path: Optional[str] = "proactive_cache_prototypes.pkl",
    ) -> Dict:
        """
        Profile a model's attention patterns and build the prototype library.

        Run this once per model. The resulting .pkl file can be shared and
        reused for the same model family across different hardware.

        Args:
            model:      HuggingFace CausalLM model (same one passed to apply()).
            tokenizer:  Corresponding tokenizer.
            corpus:     Dataset name ("wikitext", "pg19") or list of strings.
            num_docs:   Number of calibration documents (default 50).
            seq_len:    Sequence length for profiling (default 512).
            n_clusters: KMeans clusters per (layer, head) pair (default 4).
            save_path:  Where to save the .pkl prototype file.

        Returns:
            prototypes: The built prototype library dict.
        """
        print(f"[ProactiveCache] Starting profiling: corpus={corpus}, "
              f"num_docs={num_docs}, seq_len={seq_len}")

        patterns = _profile_model(model, tokenizer, corpus, num_docs, seq_len)
        prototypes = build_prototypes(patterns, n_clusters=n_clusters, max_seq_len=seq_len)

        if save_path:
            save_prototypes(prototypes, save_path)

        # Update live model if already patched
        if hasattr(model, "_proactive_prototypes"):
            model._proactive_prototypes = prototypes
            print("[ProactiveCache] Live model updated with new prototypes.")

        return prototypes

    @classmethod
    def remove(cls, model):
        """Remove the ProactiveCache patch from a model, restoring original generate()."""
        if not hasattr(model, "_proactive_config"):
            print("[ProactiveCache] Model is not patched.")
            return model

        # Restore original generate via re-loading the class method
        model.generate = type(model).generate.__get__(model, type(model))
        del model._proactive_config
        del model._proactive_prototypes
        print("[ProactiveCache] Patch removed. Model restored to original generate().")
        return model

    # ── Internal ──────────────────────────────────────────────────────────────

    @staticmethod
    def _wrapped_generate(
        model,
        original_generate,
        input_ids,
        budget: int,
        prototypes: Optional[Dict],
        **kwargs,
    ):
        """
        Internal: patched generate() that injects KV eviction after prefill.

        Strategy:
          1. Run prefill forward pass to build the full KV cache.
          2. Evict the cache down to `budget` tokens.
          3. Feed only the last input token + pruned cache to generate(),
             which continues auto-regressive decoding from that state.
        """
        device = get_device(model)
        seq_len = input_ids.shape[1]

        # If context fits in budget, skip eviction entirely
        if seq_len <= budget:
            return original_generate(model, input_ids, **kwargs)

        # 1. Prefill
        with torch.no_grad():
            out = model(input_ids, use_cache=True)
            past_kv = out.past_key_values

        # 2. Evict
        past_kv = evict(past_kv, budget, prototypes, seq_len, device)

        # 3. Continue generation from pruned KV cache
        #    Pass only the last token as "prompt" — the KV cache holds the rest
        last_token = input_ids[:, -1:]
        max_new_tokens = kwargs.pop("max_new_tokens", 100)

        return original_generate(
            model,
            last_token,
            past_key_values=past_kv,
            max_new_tokens=max_new_tokens,
            **kwargs,
        )