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.

Metadata

Release files for eyestech-mla 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for eyestech-mla 1.0.0
File Size Uploaded Provenance Actions
eyestech_mla-1.0.0.tar.gz 8.9 kB Without provenance Details for eyestech_mla-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for eyestech-mla 1.0.0
File Interpreter ABI Platform Actions
eyestech_mla-1.0.0-py3-none-any.whl Python 3 none any Details for eyestech_mla-1.0.0-py3-none-any.whl

Total release size: 18.6 kB

Release files / eyestech_mla-1.0.0.tar.gz

Download URL eyestech_mla-1.0.0.tar.gz
Size 8.9 kB
Tags Source
SHA-256 checksum
How to use checksums
a5ce01222f4f907b3e343059d24a143fcdeac25bac9e4a3b466b7ecd802cdfac
BLAKE2b-256 checksum
How to use checksums
cd2a497d22168a9b499959f65e15a445464adbbdbe00cc3d056f873c39c3ff37
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.7

Release files / eyestech_mla-1.0.0-py3-none-any.whl

Download URL eyestech_mla-1.0.0-py3-none-any.whl
Size 9.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a523cbbafb94fbc860afa56e5887ce1cde863730ba253b598524959950be77b0
BLAKE2b-256 checksum
How to use checksums
02a199dda56d2bc54c93098e90297540e85d7b33c8acebd6a3034d63edf63803
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.7

Security for eyestech-mla 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

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