> ## Documentation Index
> Fetch the complete documentation index at: https://docs.priorlabs.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Faster Inference with KV Cache

> Get a quick introduction into faster inference with fit_with_cache

<div className="cookbook-meta">
  {/* cookbook-authors:start process_markdown.py */}

  <div className="cookbook-authors">
    <div className="cookbook-author-bar">
      <span className="cookbook-author-by">By</span>
      <span className="cookbook-author-list"><span className="cookbook-author-entry"><span className="cookbook-author-name">Prior Labs</span><span className="cookbook-author-links"><a href="https://www.linkedin.com/company/prior-labs" className="cookbook-author-icon-link" aria-label="LinkedIn" target="_blank" rel="noopener noreferrer"><svg className="cookbook-author-icon" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="M20.447 20.452h-3.554v-5.569c0-1.328-.027-3.037-1.852-3.037-1.853 0-2.136 1.445-2.136 2.939v5.667H9.351V9h3.414v1.561h.046c.477-.9 1.637-1.85 3.37-1.85 3.601 0 4.267 2.37 4.267 5.455v6.286zM5.337 7.433a2.062 2.062 0 1 1 0-4.124 2.062 2.062 0 0 1 0 4.124zM7.119 20.452H3.555V9h3.564v11.452zM22.225 0H1.771C.792 0 0 .774 0 1.729v20.542C0 23.227.792 24 1.771 24h20.451C23.2 24 24 23.227 24 22.271V1.729C24 .774 23.2 0 22.222 0h.003z" /></svg></a><a href="https://twitter.com/prior_labs" className="cookbook-author-icon-link" aria-label="X" target="_blank" rel="noopener noreferrer"><svg className="cookbook-author-icon" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="M18.244 2.25h3.308l-7.227 8.26 8.502 11.24H16.17l-5.214-6.817L4.99 21.75H1.68l7.73-8.835L1.254 2.25H8.08l4.713 6.231zm-1.161 17.52h1.833L7.084 4.126H5.117z" /></svg></a></span></span></span>
    </div>
  </div>

  {/* cookbook-authors:end process_markdown.py */}

  {/* cookbook-colab:start process_markdown.py */}

  <div className="cookbook-colab">
    <a href="https://colab.research.google.com/github/PriorLabs/tabpfn-cookbook/blob/main/notebooks/faster_performance_with_cache.ipynb" className="cookbook-colab-button" target="_blank" rel="noopener noreferrer">
      <svg className="cookbook-colab-icon" viewBox="0 0 24 24" aria-hidden="true" focusable="false">
        <path fill="#F9AB00" d="M16.9414 4.9757a7.033 7.033 0 0 0-4.9308 2.0646 7.033 7.033 0 0 0-.1232 9.8068l2.395-2.395a3.6455 3.6455 0 0 1 5.1497-5.1478l2.397-2.3989a7.033 7.033 0 0 0-4.8877-1.9297zM7.07 4.9855a7.033 7.033 0 0 0-4.8878 1.9316l2.3911 2.3911a3.6434 3.6434 0 0 1 5.0227.1271l1.7341-2.9737-.0997-.0802A7.033 7.033 0 0 0 7.07 4.9855zm15.0093 2.1721l-2.3892 2.3911a3.6455 3.6455 0 0 1-5.1497 5.1497l-2.4067 2.4068a7.0362 7.0362 0 0 0 9.9456-9.9476zM1.932 7.1674a7.033 7.033 0 0 0-.002 9.6816l2.397-2.397a3.6434 3.6434 0 0 1-.004-4.8916zm7.664 7.4235c-1.38 1.3816-3.5863 1.411-5.0168.1134l-2.397 2.395c2.4693 2.3328 6.263 2.5753 9.0072.5455l.1368-.1115z" />
      </svg>

      <span className="cookbook-colab-label">Open in Colab</span>
    </a>
  </div>

  {/* cookbook-colab:end process_markdown.py */}
</div>

*Build the model's internal representation once at fit time, reuse it at predict.*

By default TabPFN recomputes its representation of the training set on every call to `predict`. The `fit_with_cache` mode does that work once, during `fit`, and caches the result, so the first prediction is noticeably faster. This short notebook benchmarks the two modes side by side on the same data.

## Setup

*Installing TabPFN and scikit-learn.*

```python theme={null}
!pip install tabpfn scikit-learn
```

## Imports

*The classifier, a dataset, and timing utilities.*

```python theme={null}
import time
import os

from google.colab import userdata
from sklearn.datasets import make_classification
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split

from tabpfn import TabPFNClassifier
```

## Authenticating with a Token

*Setting `TABPFN_TOKEN` so the weights can be downloaded.*

```python theme={null}
os.environ["TABPFN_TOKEN"] = userdata.get('TABPFN_TOKEN')
```

## Data and Split

*A synthetic 5,000-row classification set; a small test slice keeps prediction quick.*

```python theme={null}
X, y = make_classification(n_samples=5000, n_features=20, random_state=42, n_classes=2)

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.05, random_state=42, stratify=y
)
```

## The Benchmark Function

*Timing fit and predict.*

`benchmark_tabpfn` fits, times the first prediction. The first prediction is where the cache pays off.

```python theme={null}
def benchmark_tabpfn(clf: TabPFNClassifier, name: str, n_predict_calls = 1) -> None:
    t0 = time.perf_counter()
    clf.fit(X_train, y_train)
    t_fit = time.perf_counter() - t0

    t1 = time.perf_counter()
    for _ in range(n_predict_calls):
        preds = clf.predict(X_test)
    t_pred = time.perf_counter() - t1

    print(
        f"[{name}] fit: {t_fit:.4f}s | predict: {t_pred:.4f}s"
    )
```

## With and Without the Cache

*Comparing default mode against `fit_with_cache`.*

The baseline recomputes the training representation at predict time. With `fit_with_cache`, that work happens during `fit`, so the first prediction is faster.

```python theme={null}
# Baseline: no cache
clf_no_cache = (
    TabPFNClassifier()
)
benchmark_tabpfn(clf_no_cache, "no_cache")

# With KV cache: cache is built during `fit`, so first predict is faster
clf_kv = TabPFNClassifier(fit_mode="fit_with_cache")
benchmark_tabpfn(clf_kv, "kv_cache")
```

```console theme={null}
[no_cache] fit: 0.6197s | predict: 2.3743s
[kv_cache] fit: 2.8213s | predict: 0.4184s
```

## The Importance of Cache with Repeated `predict` Calls

```python theme={null}
N_PREDICT_CALLS = 10

# Baseline: no cache
clf_no_cache = (
    TabPFNClassifier()
)
benchmark_tabpfn(clf_no_cache, "no_cache", n_predict_calls = N_PREDICT_CALLS)

# With cache: the multiple predict calls are easily handled
clf_kv = TabPFNClassifier(fit_mode="fit_with_cache")
benchmark_tabpfn(clf_kv, "kv_cache", n_predict_calls = N_PREDICT_CALLS)
```

```console theme={null}
[no_cache] fit: 0.6057s | predict: 23.4870s
[kv_cache] fit: 2.8484s | predict: 4.4342s
```
