LPCompact

KV cache
compression.

As an LLM reads a longer history, its attention KV cache grows. LPCompact uses a learned selector to keep a subset of those entries so the same model can continue from a smaller snapshot.

Qwen3.8-27B · 128K input8×
8 GiB to 1 GiB of attention KV tensor storage
state = state.compact()

What compaction changes.

A model-specific selector scores spans of attention entries. The compacted snapshot retains selected keys and values at their original positions. Its continuation clock, recurrent state, and convolution state are preserved.

KV cache quantization

Quantization stores each key and value using fewer bits. LPCompact reduces the number of retained attention positions. The 8× result measures attention tensors specifically. Total GPU memory also includes model weights, working caches, and other allocations.

Text summarisation

Text summarisation creates a shorter prompt that a model can read again. LPCompact works directly on a captured snapshot tied to the model and runtime that created it. Retrieval quality depends on the workload and which attention entries are retained.

Get started

Compact a snapshot in Python.

Set up the BF16 backend and use a UTF-8 history.txt with at least 65,536 model tokens to exercise the default compaction policy for Qwen3.5-4B. The example prints the compaction measurements and continues from the smaller state.

Compact a history · Python
from pathlib import Path
import latentport as lp

history = Path("history.txt").read_text(encoding="utf-8")

with lp.load_model("Qwen/Qwen3.5-4B", backend="bf16") as model:
    state = model.prefill(history).compact()
    print(state.compaction)
    with model.session(state) as session:
        print(session.ask("What should we do next?").text)

Leave room within the model’s context limit for the follow-up and generated tokens.

Models and backends.

LPCompact supports selected Qwen and Gemma checkpoints. The runtime pairs the snapshot with a selector for its base model. BF16, NF4, and GGUF support and quality evaluations depend on the checkpoint and backend.