Skip to main content

eyestech-mla

PyPI Version EyesTech Systems Research Python Versions PyTorch 2.0+ License: MIT

eyestech-mla is a production-grade, standalone PyTorch reference kernel and memory scaling benchmark suite for DeepSeek Multi-Head Latent Attention (MLA). It demonstrates Query Absorption, Decoupled Rotary Position Embedding (RoPE), and Zero-Decompression KV Cache Streaming for high-throughput LLM inference up to 128k context windows.

📖 Canonical Systems Audit & Mathematical Proof:
For the complete mathematical proof, memory bandwidth derivations, and production serving economics (vLLM / SGLang), read the flagship investigation:
👉 DeepSeek MLA Architecture: How It Cuts KV Cache by 93% published by EyesTech Systems Lab.


⚡ Key Architectural Highlights

In autoregressive large language model serving, the primary hardware bottleneck at 32k–128k sequence lengths is High Bandwidth Memory (HBM) exhaustion, not raw compute FLOPs.

Attention Mechanism Stored Scalars / Token / Layer KV Cache @ 128k Context (BS=8, FP16) Hardware Feasibility Wall
Frontier 128-Head MHA 32,768 3,840 GB ❌ Exceeds multi-node GPU clusters
Standard 32-Head MHA 8,192 960 GB ❌ Requires 12x 80GB H100 GPUs
Llama-3 8-Head GQA 2,048 240 GB ⚠️ Requires 3x 80GB H100 GPUs
DeepSeek MLA (Absorbed) 576 67.5 GB Fits on a single 80GB H100 GPU (-93%)

How DeepSeek MLA Achieves 92.97% Memory Compression:

  1. Low-Rank Latent Compression ($c_t^{KV}$): Projects multi-head Key and Value tensors into a compact 512-dimensional shared latent subspace ($d_c = 512$).
  2. Decoupled Rotary Position Embedding ($k_t^R$): Employs a dedicated 64-dimensional uncompressed RoPE key vector ($d_h^R = 64$), preserving exact positional distance semantics without inflating latent memory ($512 + 64 = 576$ scalars per token).
  3. Inference Query Absorption ($q_{\text{absorbed}} = q \cdot W_{UK}$): Leverages associative matrix multiplication during autoregressive decoding. The up-projection matrices are pre-folded into the active Query tensors, allowing attention dot-products directly against compressed latent states with zero decompression overhead.

🚀 Installation

Install via pip:

pip install eyestech-mla

Or install from source:

git clone https://github.com/abhishek2512mishra/deepseek-mla-kvcache.git
cd deepseek-mla-kvcache
pip install .

💻 Quickstart & Code Examples

1. Autoregressive MLA Decoding with Query Absorption

import torch
from eyestech_mla import MultiHeadLatentAttentionDecode

device = "cuda" if torch.cuda.is_available() else "cpu"

# Instantiate DeepSeek MLA kernel matching V2/V3 configurations
mla = MultiHeadLatentAttentionDecode(
    d_model=5120,
    n_heads=128,
    d_head=128,
    d_latent=512,
    d_rope=64
).to(device)

# Simulate 128k context stream at position 1,024
batch_size = 2
seq_len = 1024
h_t = torch.randn(batch_size, 1, 5120, device=device)
cache_latent = torch.randn(batch_size, seq_len, 512, device=device)
cache_rope = torch.randn(batch_size, seq_len, 64, device=device)

# Execute single-token decode step
output, new_latent, new_rope = mla.forward_decode(
    h_t,
    current_pos=seq_len,
    kv_cache_latent=cache_latent,
    kv_cache_rope=cache_rope
)

print(f"Token Output Tensor: {output.shape}")          # [2, 1, 5120]
print(f"Updated Latent Cache: {new_latent.shape}")      # [2, 1025, 512]
print(f"Updated RoPE Cache:   {new_rope.shape}")        # [2, 1025, 64]
print(f"Stored Scalars/Token: {new_latent.shape[-1] + new_rope.shape[-1]}")  # 576

2. KV Cache Scaling Benchmark (CLI)

Run the automated scaling benchmark directly from your terminal:

eyestech-mla-benchmark

3. Programmatic Memory Benchmark API

from eyestech_mla import compute_kv_cache_bytes, format_bytes

# Compute KV cache for 128k context, 60 layers, batch size 8 in FP16
bytes_mla = compute_kv_cache_bytes(
    n_layers=60,
    n_kv_heads=128,
    d_head=128,
    seq_len=131072,
    batch_size=8,
    bytes_per_elem=2,
    is_mla=True,
    d_latent=512,
    d_rope=64
)

bytes_mha = compute_kv_cache_bytes(
    n_layers=60,
    n_kv_heads=128,
    d_head=128,
    seq_len=131072,
    batch_size=8,
    bytes_per_elem=2,
    is_mla=False
)

print(f"DeepSeek MLA Footprint: {format_bytes(bytes_mla)}")  # 67.50 GB
print(f"Frontier MHA Footprint: {format_bytes(bytes_mha)}")  # 3840.00 GB

📊 Benchmark Telemetry (60 Layers, Batch Size = 8, FP16)

========================================================================================
Architecture               | Scalars/Tok | 4k (4096)  | 16k (16384) | 32k (32768) | 64k (65536) | 128k (131072)
----------------------------------------------------------------------------------------
Standard MHA (32-head)     | 8192        | 30.00 GB   | 120.00 GB   | 240.00 GB   | 480.00 GB   | 960.00 GB   
Frontier MHA (128-head)    | 32768       | 120.00 GB  | 480.00 GB   | 960.00 GB   | 1920.00 GB  | 3840.00 GB  
Llama-3 GQA (8-head)       | 2048        | 7.50 GB    | 30.00 GB    | 60.00 GB    | 120.00 GB   | 240.00 GB   
DeepSeek MLA (Absorbed)    | 576         | 2.11 GB    | 8.44 GB     | 16.88 GB    | 33.75 GB    | 67.50 GB    
========================================================================================

📚 Citation & Attribution

If you use this benchmark harness, reference implementation, or mathematical formalization in academic research, benchmark audits, or engineering systems, please cite:

@misc{fischer2026deepseekmla,
  author = {Fischer, Klaus and Ranganathan, Devika},
  title = {DeepSeek MLA Architecture: How It Cuts KV Cache by 93%},
  howpublished = {\url{https://eyestech.in/deepseek-mla-architecture-kv-cache-math/}},
  journal = {EyesTech Systems Research},
  year = {2026},
  note = {EyesTech Systems Lab Hardware Audit Series}
}

⚖️ License

Distributed under the MIT License. Maintained by EyesTech Systems Lab.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

eyestech_mla-1.0.0.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

eyestech_mla-1.0.0-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file eyestech_mla-1.0.0.tar.gz.

File metadata

  • Download URL: eyestech_mla-1.0.0.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.7

File hashes

Hashes for eyestech_mla-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a5ce01222f4f907b3e343059d24a143fcdeac25bac9e4a3b466b7ecd802cdfac
MD5 87a802805a3e3ad86cbcada864c0a808
BLAKE2b-256 cd2a497d22168a9b499959f65e15a445464adbbdbe00cc3d056f873c39c3ff37

See more details on using hashes here.

File details

Details for the file eyestech_mla-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: eyestech_mla-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.7

File hashes

Hashes for eyestech_mla-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a523cbbafb94fbc860afa56e5887ce1cde863730ba253b598524959950be77b0
MD5 af7334344fcc256a634ca78b90f41404
BLAKE2b-256 02a199dda56d2bc54c93098e90297540e85d7b33c8acebd6a3034d63edf63803

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page