Metadata-Version: 2.4
Name: mate-llm
Version: 0.1.0
Summary: MATE (Memory-Aligned Trellis Engine): CPTC-TQIR trellis quantization, Trellis-QLoRA training, and compressed KV/MoE kernels in Mojo and CUDA
License: Apache-2.0
Project-URL: Homepage, https://github.com/Honkware/mate-llm
Project-URL: Repository, https://github.com/Honkware/mate-llm
Project-URL: Issues, https://github.com/Honkware/mate-llm/issues
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.26
Requires-Dist: torch>=2.3
Requires-Dist: safetensors>=0.4
Provides-Extra: hf
Requires-Dist: transformers>=4.45; extra == "hf"
Requires-Dist: huggingface_hub>=0.25; extra == "hf"
Provides-Extra: dev
Requires-Dist: pytest>=8.0; extra == "dev"
Dynamic: license-file

# MATE: Memory-Aligned Trellis Engine

2-to-4 bit trellis quantization, compressed-KV CUDA/Mojo inference, bare-metal C++20 serving, 3-tier MoE offload, and zero-materialization Trellis-QLoRA fine-tuning for local GPUs and CPUs.

[Quickstart](#quickstart) | [Benchmarks](#benchmarks) | [Support Matrix](#support-matrix) | [Documentation](#documentation)

## Highlights

- **2-4 bit CPTC-TQIR weights:** Hessian-aware sequential GPTAQ LDLQ with GPU curvature-weighted Viterbi, per-head V-O Hadamard folding (`R2`), KL tile-scale distillation, 6-bit trellis / probability-banded `lm_head` export, and universal GGUF v2/v3, EXL3, MLX, and Hugging Face import.
- **Native Blackwell/Ada CUDA + Mojo 1.2 CPU kernels:** Zero-SMEM pure-ALU `trellis_gemv_v3` (`dp4a` + `svh` epilogue + Stream-K), zero-spill `r4kv_decode_attn` with 128-split Blackwell merge, fused `conv1d+GDN` with L2 state persistence, and zero-`libmax.so` Mojo AVX-512 VNNI / AVX2 CPU SIMD decode.
- **4.5x-6.4x compressed KV caches:** Default rotated 4-bit/2-bit `R4KV` (up to 1M-token context in 24.3 GB VRAM), 8D `E8` lattice (`2.0-4.0` bpw), dual-scaled `KVarN`, and 3-bit `IsoQuant`.
- **Dual Python + bare-metal C++20 serving:** Continuous batching (`B=64`), OpenAI `/v1/chat/completions` and Anthropic `/v1/messages` APIs, confidence-gated speculative decoding (`UnifiedSpeculatorRouter`, Suffix/SAM, Native MTP, `DFlash 2`), and standalone zero-Python C++20 `mate-serve` (`mate/csrc/runtime/`).
- **Frontier MoE & Trellis-QLoRA:** 3-tier GPU doorbell MoE offload (VRAM hot pool + pinned RAM victim cache + 4 KiB-aligned NVMe), exact `Qwen3.8-Flash-Next` / `Hy4-Flash` / `Inkling-2-Mini` hybrid runners, and frozen-base Trellis-QLoRA + LoftQ fine-tuning.

## Quickstart

### Install

Requires Python 3.11+ and PyTorch 2.3+. On NVIDIA GPUs with `nvcc` and `ninja`, `mate` JIT-compiles its CUDA extension (`mate/csrc/`) natively with no Mojo dependency. Mojo 1.2+ is optional on CUDA machines and is only needed for CPU SIMD (`mate/libmate.so`).

```bash
pip install "mate-llm[hf]"
```

For local development from source:

```bash
pip install -e ".[hf]"
```

To build the optional standalone bare-metal C++20 server (`mate-serve` + `libmate_runtime.so`):

```bash
cmake -S mate/csrc/runtime -B build/runtime -DCMAKE_BUILD_TYPE=Release
cmake --build build/runtime -j
```

### Run, Serve, or Quantize

```bash
# Run a pre-quantized MATE checkpoint directly from Hugging Face
mate run Honkware/Qwen3-8B-MATE-4b --ctx 32768 --serve

# Check hardware memory fit offline (no weight download)
mate fit Qwen/Qwen3-8B --ctx 32768

# Interactive chat or OpenAI/Anthropic HTTP server
mate chat Qwen/Qwen3-8B --bits 4
mate serve Qwen/Qwen3-8B --bits 4 --max-seq-len 32768 --host 127.0.0.1 --port 8000

# Quantize a Hugging Face model or GGUF checkpoint to .mate.safetensors
mate quantize Qwen/Qwen3-8B -o qwen3-8b-4b.mate.safetensors --bits 4 --depth 10
mate inspect qwen3-8b-4b.mate.safetensors
```

Query the server with any OpenAI or Anthropic client (`OPENAI_BASE_URL=http://127.0.0.1:8000/v1`, `ANTHROPIC_BASE_URL=http://127.0.0.1:8000`):

```bash
curl http://127.0.0.1:8000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{"model":"Qwen/Qwen3-8B","messages":[{"role":"user","content":"Write a C++20 lock-free ring buffer."}]}'
```

### Python API

```python
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from mate.double_hadamard import fold_vo_head_hadamard
from mate.fast_decode import FastDecodeRunner
from mate.hf import quantize_model
from mate._policy import RoleQuantPolicy

model_id = "Qwen/Qwen3-8B"
tok = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id, dtype=torch.bfloat16, device_map="cuda").eval()

fold_vo_head_hadamard(model)
quantize_model(model, policy=RoleQuantPolicy(dense_bits=4, trellis_depth=10), mode="mse")

runner = FastDecodeRunner(model, max_seq_len=32768, kv_mode="r4kv")
prompt = tok.encode("Explain tail-biting trellis quantization in two sentences.", return_tensors="pt").cuda()
out = runner.generate(prompt, max_new_tokens=128)
print(tok.decode(out[0], skip_special_tokens=True))
```

## Pre-Quantized Checkpoints

Ready-to-run `.mate.safetensors` checkpoints are published on Hugging Face under [huggingface.co/Honkware](https://huggingface.co/Honkware):

| Model Family | Variants | Architecture |
|---|---|---|
| `Qwen/Qwen3-8B` | [`4b`](https://huggingface.co/Honkware/Qwen3-8B-MATE-4b), [`3b`](https://huggingface.co/Honkware/Qwen3-8B-MATE-3b), [`2b`](https://huggingface.co/Honkware/Qwen3-8B-MATE-2b) | Dense GQA |
| `meta-llama/Llama-3.1-8B-Instruct` | [`4b`](https://huggingface.co/Honkware/Meta-Llama-3.1-8B-Instruct-MATE-4b), [`3b`](https://huggingface.co/Honkware/Meta-Llama-3.1-8B-Instruct-MATE-3b), [`2b`](https://huggingface.co/Honkware/Meta-Llama-3.1-8B-Instruct-MATE-2b) | Dense GQA |
| `Qwen/Qwen3-14B` | [`4b`](https://huggingface.co/Honkware/Qwen3-14B-MATE-4b), [`3b`](https://huggingface.co/Honkware/Qwen3-14B-MATE-3b), [`2b`](https://huggingface.co/Honkware/Qwen3-14B-MATE-2b) | Dense GQA |
| `google/gemma-4-12B-it` | [`4b`](https://huggingface.co/Honkware/gemma-4-12B-it-MATE-4b), [`3b`](https://huggingface.co/Honkware/gemma-4-12B-it-MATE-3b), [`2b`](https://huggingface.co/Honkware/gemma-4-12B-it-MATE-2b) | Sliding + Global Hybrid |
| `Qwen3.8-27B` | [`4b`](https://huggingface.co/Honkware/Qwen3.8-27B-MATE-4b), [`3b`](https://huggingface.co/Honkware/Qwen3.8-27B-MATE-3b), [`2b`](https://huggingface.co/Honkware/Qwen3.8-27B-MATE-2b) | 64L GDN + Full Attention |
| `Inkling-2-Mini` | [`13GB`](https://huggingface.co/Honkware/Inkling-2-Mini-MATE-13GB), [`8.3GB`](https://huggingface.co/Honkware/Inkling-2-Mini-MATE-8.3GB) | Looped MoE |
| `Qwen3.8-Flash-Next` | [`3b`](https://huggingface.co/Honkware/Qwen3.8-Flash-Next-MATE-3b), [`mixed-45GB`](https://huggingface.co/Honkware/Qwen3.8-Flash-Next-MATE-mixed-45GB) | 512-Expert MoE + Sparse-QSA |
| `Hy4-Flash` | [`3b`](https://huggingface.co/Honkware/Hy4-Flash-MATE-3b) | LatentMoE + MLA + Engram |

## Benchmarks

All numbers below are measured live on single GPUs (`sm_120a` Blackwell PRO 6000, `R4KV` 4-bit KV cache, CUDA Graphs). Full tables covering `2b`/`3b`/`4b`, NVIDIA L4 (`sm_89`), RTX 5090, batched decode (`803 tok/s` at `B=48` on `Qwen3-8B-4b`; `6,728 tok/s` at `B=64` on `Inkling-2-Mini`), 1M-token context, and Mojo CPU SIMD kernels are in [docs/benchmarks.md](docs/benchmarks.md).

### 4-Bit & 3-Bit Dense / Hybrid Quality & Single-Stream Decode (PRO 6000, 96 GB)

Evaluated on `wikitext-2-raw-v1:test` at `seq_len=2048` (100 windows) against BF16 reference:

| Model | Variant | Size | WT-2 PPL (`delta`) | KLD | Top-1 | `d=128` (`tok/s`) | `d=32,768` (`tok/s`) | Peak VRAM (`32K`) |
|---|---|---:|---:|---:|---:|---:|---:|---:|
| `Qwen3-8B` | `CPTC-4b` | 5.67 GiB | **9.581** (+0.11%) | **0.0315** | **93.23%** | **196.2** | **123.6** | 7.36 GB |
| `Qwen3-8B` | `CPTC-3b` | 4.86 GiB | **10.009** (+4.58%) | **0.1119** | **87.29%** | **244.9** | **142.2** | 6.67 GB |
| `Llama-3.1-8B-Instruct` | `CPTC-4b` | 5.33 GiB | **7.378** (+2.23%) | **0.0277** | **93.13%** | **208.4** | **134.2** | 7.25 GB |
| `Qwen3-14B` | `CPTC-4b` | 9.26 GiB | **8.505** (+0.65%) | **0.0231** | **94.08%** | **124.2** | **95.1** | 11.82 GB |
| `gemma-4-12B-it` | `CPTC-4b` | 7.15 GiB | **13.283** (-0.09%) | **0.0682** | **90.97%** | **113.8** | **78.9** | 9.90 GB |
| `Qwen3.8-27B` | `CPTC-4b` | 16.50 GiB | **6.714** (-0.72%) | **0.0245** | **94.25%** | **64.8** | **60.3** | 17.49 GB |
| `Qwen3.8-27B` | `CPTC-3b` | 13.67 GiB | **6.922** (+2.36%) | **0.0662** | **90.03%** | **80.6** | **73.9** | 14.72 GB |

### Frontier & Batched Highlights (PRO 6000, 96 GB)

| Model | Config | Context `d` | Throughput (`tok/s`) | Peak VRAM |
|---|---|---:|---:|---:|
| `Inkling-2-Mini` | `FP8Dense` (`B=64`, `k=7`, Native DSpark) | 640 | **6,728.60** (105.13/user) | 43.14 GB |
| `Inkling-2-Mini` | `BF16Dense` lossless (`B=64`, `k=7`, Native DSpark) | 640 | **6,514.88** (101.79/user) | 43.16 GB |
| `Inkling-2-Mini` | `FP8Dense` (`M=1`, `r4k_r2v` KV, CPU embed offload) | **1,048,576** | **54.00** | **23.04 GB** |
| `Qwen3-8B` | `CPTC-4b` (`B=48`, ragged `R4KV`) | 128 | **803.08** (16.73/user) | **8.73 GB** |

## Support Matrix

### Hardware Backends

| Target | Compute Path | Requirements | Notes |
|---|---|---|---|
| NVIDIA Blackwell (`sm_120a`) | Native CUDA (`trellis_gemv_v3`, `r4kv`, `gdn`, `t2tc`, `nvfp4_moe`) | CUDA 12.8+, `nvcc`, `ninja` | Full DP4A, PDL, Stream-K, 128-split R4KV, L2 GDN persistence |
| NVIDIA Hopper / Ada (`sm_90` / `sm_89`) | Native CUDA (`trellis_gemv_v3`, `r4kv`, `gdn`, `e8_v3`) | CUDA 12.1+, `nvcc`, `ninja` | Full CUDA-Graph decode and compressed KV |
| NVIDIA Ampere (`sm_80` / `sm_86`) | Native CUDA (`trellis_gemv_v3`, `r4kv`, `e8_v3`) | CUDA 12.1+, `nvcc`, `ninja` | DP4A GEMV and compressed KV |
| x86-64 AVX-512 VNNI / AVX2 | Mojo 1.2 `mate/libmate.so` (`IH4-VNNI-BiasHoist`) | Mojo 1.2+ | Zero `libmax.so` runtime dependency |
| Fallback (non-CUDA / CPU) | PyTorch reference | Python 3.11+, PyTorch 2.3+ | Functional fallback for testing and export |

> [!NOTE]
> ROCm, Metal, and Vulkan backends are not supported yet. On CUDA machines, Mojo 1.2 is optional and only loaded lazily when invoking CPU SIMD kernels.

### Capabilities

- **Model families:** Llama / Mistral / Qwen2 / Qwen3 (`FastDecodeRunner`), Qwen3.5 / Qwen3.8 Gated DeltaNet + Full Attention, Gemma 4 heterogeneous sliding/global attention, `Inkling-2-Mini` looped MoE, `Qwen3.8-Flash-Next` (512-expert MoE + Sparse-QSA + Layer-1 Engram), and `Hy4-Flash` (iHC + MLA + Engram + DFly2 drafter hooks).
- **Input formats:** Hugging Face (`BF16/FP16/FP8/NVFP4/MXFP4/AWQ/GPTQ/NF4`), GGUF v2/v3 (all 36 GGML types), EXL3, and MLX.
- **KV backends (`--kv-backend`):** `r4kv` (4b/2b rotated, default on CUDA), `e8` (8D lattice `2.0-4.0` bpw), `kvarn` (`k4v2/k4v4/k2v2`), `isoquant` (3b scalar / HF cache), `fp16`.
- **Speculative modes (`--speculative`):** `auto`, `router` (`UnifiedSpeculatorRouter`), `suffix` / `sam`, `mtp`, `dflash2` / `dspark` / `dspine`, `--draft-model`, `none`.

## Documentation

- [docs/benchmarks.md](docs/benchmarks.md): Full single-GPU-per-table Blackwell PRO 6000, RTX 5090, and Mojo CPU benchmark reports.
- [docs/architecture.md](docs/architecture.md): Repository layout, CPTC-TQIR weight format, CUDA/Mojo kernels, and bare-metal C++20 `mate-serve` runtime.
- [docs/serving.md](docs/serving.md): CLI reference (`mate fit`, `mate run`, `mate serve`, `mate quantize`), OpenAI and Anthropic HTTP endpoints, and speculative decoding controls.

## License

Apache-2.0. See [LICENSE](LICENSE).
