# tonbistudio/turboquant-pytorch architecture

> Architecture diagram of the GitHub repository tonbistudio/turboquant-pytorch, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tonbistudio/turboquant-pytorch
- Repository: https://github.com/tonbistudio/turboquant-pytorch

## Overview

TurboQuant is a PyTorch library for compressing LLM key/value caches, with a legacy QJL-based V2 and a recommended MSE-only V3. The main workflow is to quantize KV tensors, retain recent tokens at fp16 when configured, and reconstruct tensors for use by a model; separate tools assess attention accuracy and generated text. The graph preserves the documented algorithm and evaluation branches. Some README-described modules are unsampled, so their links are shown only where documentation supports them.

Main entry points: [`turboquant/compressors.py`](https://github.com/tonbistudio/turboquant-pytorch/blob/master/turboquant/compressors.py) (QJL key compressor), [`turboquant/generation_test_v2.py`](https://github.com/tonbistudio/turboquant-pytorch/blob/master/turboquant/generation_test_v2.py) (Generation evaluation), [`turboquant/compressors_v3.py`](https://github.com/tonbistudio/turboquant-pytorch/blob/master/turboquant/compressors_v3.py) (V3 compressor), [`turboquant/validate_v3.py`](https://github.com/tonbistudio/turboquant-pytorch/blob/master/turboquant/validate_v3.py) (V2/V3 attention validation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_v3["V3 KV compression"]
  node_cache_adapter["V3 cache adapter"]
  node_v3_api["V3 compressor<br/>[compressors_v3.py]"]
  node_mse["MSE compressor<br/>[compressors_v3.py]"]
end

subgraph group_legacy["V2 quantization"]
  node_v2_key["QJL key compressor<br/>[compressors.py]"]
  node_v2_value["MSE value compressor<br/>[compressors.py]"]
end

subgraph group_core["Quantization core"]
  node_quantizers["Vector quantizers<br/>[turboquant.py]"]
  node_codebook["Lloyd-Max codebook<br/>[lloyd_max.py]"]
end

subgraph group_eval["Evaluation workflows"]
  node_generation_eval["Generation evaluation"]
  node_attention_v2["V2 attention validation<br/>[validate.py]"]
  node_attention_v3["V2/V3 attention validation<br/>[validate_v3.py]"]
  node_synthetic_tests["Synthetic algorithm tests<br/>[test_turboquant.py]"]
  node_original_generation["V3 generation test<br/>[generation_test.py]"]
  node_original_validation["V2 validation<br/>[validate.py]"]
end

node_engineer(("Library user"))
node_model["Transformers model"]
node_tokenizer["Tokenizer"]

node_engineer -->|"compresses KV"| node_v3_api
node_v3_api -->|"uses compressors"| node_mse
node_mse -->|"uses codebook"| node_codebook
node_cache_adapter -->|"compresses cache"| node_v3_api
node_cache_adapter -->|"supplies cache"| node_model
node_generation_eval -->|"builds prompts"| node_tokenizer
node_generation_eval -->|"evaluates generation"| node_model
node_engineer -->|"uses legacy V2"| node_v2_key
node_v2_key -->|"pairs with"| node_v2_value
node_v2_key -->|"evaluated by"| node_attention_v2
node_v2_key -->|"evaluated by"| node_attention_v3
node_v2_value -->|"evaluated by"| node_attention_v3
node_quantizers -->|"uses codebook"| node_codebook
node_synthetic_tests -->|"tests algorithm"| node_quantizers
node_engineer -->|"runs generation test"| node_generation_eval
node_engineer -->|"runs comparison"| node_attention_v3
node_engineer -->|"runs V2 validation"| node_original_validation
node_engineer -->|"runs synthetic tests"| node_synthetic_tests
node_engineer -->|"runs generation test"| node_original_generation

click node_cache_adapter "https://github.com/tonbistudio/turboquant-pytorch/blob/master/turboquant/generation_test_v2.py"
click node_v3_api "https://github.com/tonbistudio/turboquant-pytorch/blob/master/turboquant/compressors_v3.py"
click node_mse "https://github.com/tonbistudio/turboquant-pytorch/blob/master/turboquant/compressors_v3.py"
click node_v2_key "https://github.com/tonbistudio/turboquant-pytorch/blob/master/turboquant/compressors.py"
click node_v2_value "https://github.com/tonbistudio/turboquant-pytorch/blob/master/turboquant/compressors.py"
click node_quantizers "https://github.com/tonbistudio/turboquant-pytorch/blob/master/turboquant/turboquant.py"
click node_codebook "https://github.com/tonbistudio/turboquant-pytorch/blob/master/turboquant/lloyd_max.py"
click node_generation_eval "https://github.com/tonbistudio/turboquant-pytorch/blob/master/turboquant/generation_test_v2.py"
click node_attention_v2 "https://github.com/tonbistudio/turboquant-pytorch/blob/master/turboquant/validate.py"
click node_attention_v3 "https://github.com/tonbistudio/turboquant-pytorch/blob/master/turboquant/validate_v3.py"
click node_synthetic_tests "https://github.com/tonbistudio/turboquant-pytorch/blob/master/turboquant/test_turboquant.py"
click node_original_generation "https://github.com/tonbistudio/turboquant-pytorch/blob/master/turboquant/generation_test.py"
click node_original_validation "https://github.com/tonbistudio/turboquant-pytorch/blob/master/turboquant/validate.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cache_adapter,node_v3_api,node_mse,node_engineer toneBlue
class node_v2_key,node_v2_value toneAmber
class node_quantizers,node_codebook toneMint
class node_generation_eval,node_attention_v2,node_attention_v3,node_synthetic_tests,node_original_generation,node_original_validation toneRose
class node_model,node_tokenizer toneIndigo
```

## Components

### V3 KV compression

- [V3 cache adapter](https://github.com/tonbistudio/turboquant-pytorch/blob/master/turboquant/generation_test_v2.py): `turboquant/generation_test_v2.py`
- [V3 compressor](https://github.com/tonbistudio/turboquant-pytorch/blob/master/turboquant/compressors_v3.py): `turboquant/compressors_v3.py`
- [MSE compressor](https://github.com/tonbistudio/turboquant-pytorch/blob/master/turboquant/compressors_v3.py): `turboquant/compressors_v3.py`

### V2 quantization

- [QJL key compressor](https://github.com/tonbistudio/turboquant-pytorch/blob/master/turboquant/compressors.py): `turboquant/compressors.py`
- [MSE value compressor](https://github.com/tonbistudio/turboquant-pytorch/blob/master/turboquant/compressors.py): `turboquant/compressors.py`

### Quantization core

- [Vector quantizers](https://github.com/tonbistudio/turboquant-pytorch/blob/master/turboquant/turboquant.py): `turboquant/turboquant.py`
- [Lloyd-Max codebook](https://github.com/tonbistudio/turboquant-pytorch/blob/master/turboquant/lloyd_max.py): `turboquant/lloyd_max.py`

### Evaluation workflows

- [Generation evaluation](https://github.com/tonbistudio/turboquant-pytorch/blob/master/turboquant/generation_test_v2.py): `turboquant/generation_test_v2.py`
- [V2 attention validation](https://github.com/tonbistudio/turboquant-pytorch/blob/master/turboquant/validate.py): `turboquant/validate.py`
- [V2/V3 attention validation](https://github.com/tonbistudio/turboquant-pytorch/blob/master/turboquant/validate_v3.py): `turboquant/validate_v3.py`
- [Synthetic algorithm tests](https://github.com/tonbistudio/turboquant-pytorch/blob/master/turboquant/test_turboquant.py): `turboquant/test_turboquant.py`
- [V3 generation test](https://github.com/tonbistudio/turboquant-pytorch/blob/master/turboquant/generation_test.py): `turboquant/generation_test.py`
- [V2 validation](https://github.com/tonbistudio/turboquant-pytorch/blob/master/turboquant/validate.py): `turboquant/validate.py`

### Other components

- **Library user**
- **Transformers model**
- **Tokenizer**

## Connections

- Library user → V3 compressor: compresses KV (evidence: `README.md`)
- V3 compressor → MSE compressor: uses compressors (evidence: `README.md`)
- MSE compressor → Lloyd-Max codebook: uses codebook (evidence: `turboquant/compressors_v3.py`)
- V3 cache adapter → V3 compressor: compresses cache (evidence: `turboquant/generation_test_v2.py`)
- V3 cache adapter → Transformers model: supplies cache (evidence: `README.md`)
- Generation evaluation → Tokenizer: builds prompts (evidence: `turboquant/generation_test_v2.py`)
- Generation evaluation → Transformers model: evaluates generation (evidence: `turboquant/generation_test_v2.py`)
- Library user → QJL key compressor: uses legacy V2 (evidence: `README.md`)
- QJL key compressor → MSE value compressor: pairs with (evidence: `README.md`)
- QJL key compressor → V2 attention validation: evaluated by (evidence: `turboquant/validate.py`)
- QJL key compressor → V2/V3 attention validation: evaluated by (evidence: `turboquant/validate_v3.py`)
- MSE value compressor → V2/V3 attention validation: evaluated by (evidence: `turboquant/validate_v3.py`)
- Vector quantizers → Lloyd-Max codebook: uses codebook (evidence: `turboquant/turboquant.py`)
- Synthetic algorithm tests → Vector quantizers: tests algorithm (evidence: `README.md`)
- Library user → Generation evaluation: runs generation test (evidence: `README.md`)
- Library user → V2/V3 attention validation: runs comparison (evidence: `README.md`)
- Library user → V2 validation: runs V2 validation (evidence: `README.md`)
- Library user → Synthetic algorithm tests: runs synthetic tests (evidence: `README.md`)
- Library user → V3 generation test: runs generation test (evidence: `README.md`)
