Metadata-Version: 2.4
Name: pakp-engine
Version: 0.1.0
Summary: PAKP: Predictive Asymmetric KV-Paging via Centered Bounding-Box Lookahead for Sub-6GB Edge GPUs
Author: Akhil M
License: MIT License
        
        Copyright (c) 2026 Akhil M
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/andromechanic/pakp-engine
Project-URL: Repository, https://github.com/andromechanic/pakp-engine
Project-URL: Paper, https://doi.org/10.5281/zenodo.23915486
Keywords: llm,kv-cache,cuda,pytorch,edge-ai,paged-attention,inference
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: torch>=2.1.0
Requires-Dist: transformers>=4.42.0
Requires-Dist: accelerate>=0.30.0
Dynamic: license-file

# PAKP: Predictive Asymmetric KV-Paging for Edge GPUs

[![PyTorch](https://img.shields.io/badge/PyTorch-2.1%2B-EE4C2C.svg)]([https://pytorch.org/](https://pytorch.org/))
[![CUDA](https://img.shields.io/badge/CUDA-12.x-76B900.svg)](https://developer.nvidia.com/cuda-toolkit)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)

**PAKP (`pakp-engine`)** is a two-tier asymmetric KV-cache inference engine and research framework that breaks the **6 GB VRAM wall** on consumer GPUs (e.g., **NVIDIA RTX 4050 Laptop GPU**). By pairing **32 GB Page-Locked Host RAM (`cudaMallocHost`)** with **Anisotropy-Corrected Bounding-Box Lookahead** and **Dual-Stream Asynchronous DMA Prefetching**, PAKP hides **90.5% of PCIe Gen4 latency** behind GPU compute while retaining **100% passkey recall**.

---

## Visual Benchmarks (ASUS TUF F15 — RTX 4050 6GB + 32GB Host RAM)

<p align="center">
  <img src="assets/latency_breakdown.png" width="48%" alt="Latency Breakdown" />
  <img src="assets/vram_scaling.png" width="48%" alt="VRAM Scaling" />
</p>

---

## Core Technical Innovations

1. **Two-Tier Asymmetric Memory Topology:** Retains only Attention Sinks ($S_{\text{sink}}=4$), a sliding Local Window ($S_{\text{win}}=256$), and a compact Subspace Index inside GPU VRAM, offloading historical 64-token KV pages into 32 GB Pinned Host RAM.
2. **Centered Subspace Bounding-Box Index:** Removes key-space anisotropy by subtracting the per-layer global Key mean $\bar{K}_L$ and storing channel-wise extrema $(K_{\min}^{(p)}, K_{\max}^{(p)})$ alongside page centroids $\mu_p$, preventing single-token passkeys from being diluted across 64-token pages.
3. **RoPE-Aligned Cross-Layer Lookahead:** Exploits residual stream continuity ($\cos(h_L, h_{L+1}) = 0.9842$) to project $\hat{Q}_{L+1}^{\text{RoPE}}$ at Layer $L$, overlapping PCIe Gen4 DMA transfers (`prefetch_stream`) with Layer $L$ GPU compute (`compute_stream`).

---

## Quickstart

```python
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from pakp import enable_pakp

model_id = "Qwen/Qwen2.5-1.5B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id, dtype=torch.float16, device_map="cuda")

# Attach PAKP Two-Tier Asymmetric Paging in one line
pakp_cache, hooks = enable_pakp(model, page_size=64, top_k=4, sink_size=4, window_size=256)
```

---

## Reproducing the Paper Benchmarks

Run the complete empirical suite on your local CUDA GPU:

```bash
# 1. Measure Host-to-Device PCIe Gen4 Bandwidth (Pageable vs Pinned DMA)
python benchmarks/pcie_telemetry.py

# 2. Run 28-Layer Dual-Stream DMA Overlap Benchmark (32,768 tokens)
python benchmarks/dma_overlap_bench.py

# 3. Run Live HuggingFace LLM Passkey Ablation Suite (Qwen2.5-1.5B-Instruct)
python benchmarks/live_llm_ablation.py

# 4. Generate High-Resolution Figures (PNG & PDF for LaTeX Paper)
python benchmarks/generate_plots.py
```

---

## Empirical Telemetry Summary

### Table 1: 32,768-Token Dual-Stream DMA Overlap (RTX 4050 6GB)
| Execution Strategy | Per-Token Latency | Throughput | PCIe Overhead Hidden |
| :--- | :---: | :---: | :---: |
| Naive Full KV Offload (3.50 GB over PCIe) | `321.26 ms` | `3.1 tok/s` | `0.0%` |
| Synchronous Top-4 Paging (No Lookahead) | `31.60 ms` | `31.6 tok/s` | `0.0%` |
| **PAKP Dual-Stream Lookahead (Ours)** | **`25.54 ms`** | **`39.2 tok/s`** | **`90.5%`** |
| *Ideal In-VRAM Upper Bound (Zero PCIe)* | *`24.90 ms`* | *`40.2 tok/s`* | *—* |

### Table 2: Live LLM Passkey Retrieval Ablation (`Qwen2.5-1.5B-Instruct`)
| Method | Evicted Page Hit | Active VRAM | Speed | Generated Output |
| :--- | :---: | :---: | :---: | :--- |
| 1. Sliding Window (`Sinks + Win`) | `False` | `3.61 GB` | `46.6 tok/s` | `"123456"` *(Amnesia)* |
| 2. Uncentered Mean Paging | `False` | `3.64 GB` | `26.8 tok/s` | *Misses needle page* |
| **3. PAKP Centered BBox (Ours)** | **`True`** | **`3.64 GB`** | **`27.1 tok/s`** | **`"7392-OMEGA."` (100% Recall)** |

---

## Academic Paper
The full LaTeX source for the accompanying research paper is located in [`paper/main.tex`](paper/main.tex) and [`paper/references.bib`](paper/references.bib).

```bibtex
@article{muralidas2026pakp,
  title={PAKP: Predictive Asymmetric KV-Paging via Centered Bounding-Box Lookahead for Sub-6GB Edge GPUs},
  author={Muralidas, Akhil},
  year={2026}
}
```

## License
Released under the [MIT License](LICENSE).
