Lex4
The sub-4-bit inference engine & .lex4 format for desktop frontier AI.
Run a 320B-parameter frontier MoE (GLM-5.3-Flash) on a single 128 GB NVIDIA DGX Spark at 20–45 tok/s with 262K context, zero swap, and a live per-request fast / balanced / exact NVMe dial.
Why Lex4?
Until now, running a 320B Mixture-of-Experts model locally forced an ugly compromise:
Approach (GLM-5.3-Flash, 320B MoE) |
Hardware Required | Speed (Decode) | Code Quality vs. NVFP4 | Prose Quality vs. NVFP4 | Context / Stability |
|---|---|---|---|---|---|
NVIDIA Official NVFP4 (4.27 bpw, ~172 GB) |
2× DGX Spark (~$9.4K–$12K+) | ~15–26 tok/s | Baseline (0%) |
Baseline (0%) |
2 boxes + 200GbE link |
1-Spark Ultra-Low-Bit (2.01 bpw all-in-RAM) |
1× DGX Spark | Fast | +26.6% worse | +142.3% worse | Severe prose/reasoning loss |
| Naive CPU / SSD Offloading | 1× DGX Spark | ~1–4 tok/s | Near-baseline | Near-baseline | Breaks CUDA graphs, stalls |
Lex4 (3.50 bpw .lex4 + O_DIRECT Pager + SM121a) |
1× DGX Spark (~$4.7K–$6K) | 21–45 tok/s | +0.17%–+1.00% (matches 2-Spark) |
+2.91% (Les Mis) |
262K ctx, ~10 GiB free RAM, 0 swap |
Lex4 hits 98.0%–99.4% of GB10's measured 234.1 GB/s physical DRAM bandwidth ceiling, boots 83.5 GB of experts in 20.1 seconds via double-buffered O_DIRECT QD=4 NVMe streaming (4.16 GB/s), and keeps full Blackwell CUDA graphs intact while paging cold experts asynchronously in the background.
Quick Start (DGX Spark — 2 Commands)
Requires an NVIDIA DGX Spark (128 GB, Blackwell GB10 / SM121a) with Docker + NVIDIA Container Toolkit and 150 GB+ free NVMe space.
1. Install the zero-dependency lex4 CLI
curl -fsSL https://raw.githubusercontent.com/lex4-dev/lex4/main/install.sh | bash
(Or via PyPI: pipx install lex4 / pip install lex4. The host CLI uses Python stdlib only — zero host PyTorch/CUDA packages needed.)
2. Verify your Spark and launch
lex4 doctor
lex4 up --preset balanced
Want a ChatGPT-like browser UI automatically wired on http://localhost:3000?
lex4 up --preset balanced --with-webui
Want to chat right in your terminal?
lex4 chat
What happens on
lex4 up:
- Pulls the pre-compiled
ghcr.io/lex4-dev/lex4:sparkimage (SM121a CUDA/PTX kernels already compiled — zero compile wait).- Downloads
Lex4-dev/GLM-5.3-Flash-LEX4-3.5bpw(~134 GB) and the optional DFlash2 drafter (incoai/GLM-5.3-Flash-DFlash2) on first run.- Starts a single auto-restarting (
unless-stopped) container running vLLM (:8000internal) + the Lex4 Smart Proxy (:8888OpenAI-compatible API).
For a 100% commercial-clean stack (Apache-2.0 engine + NVIDIA Open Model Agreement weights, skipping the CC-BY-NC-ND-4.0 DFlash2 drafter):
lex4 up --preset balanced --no-drafter
Built-In RAM & Context Presets
Pick the footprint that fits how you use your Spark — or pass --ram-gb and --context for any custom split:
| Preset | Resident Experts | Context Window | FP8 KV Cache | Free OS RAM | Best For |
|---|---|---|---|---|---|
balanced (default) |
83 GB (62.7%) |
262,144 (262K) |
8.0 GiB (~347K tok) |
~9.8 GiB | Flagship daily driver — long-context agents, coding, chat |
low-ram |
64 GB (48.0%) |
262,144 (262K) |
8.0 GiB (~347K tok) |
~29–31 GiB | Heavy multitasking, IDEs, browsers, extra Docker containers |
max-quality |
86 GB (64.9%) |
102,400 (102K) |
4.0 GiB (~171K tok) |
~11.4 GiB | Peak warm-cache hit rate when ≤102K context is plenty |
lex4 up --preset balanced # 83 GB experts, 262K context (default)
lex4 up --preset low-ram # 64 GB experts, 262K context (~30 GiB free RAM)
lex4 up --preset max-quality # 86 GB experts, 102K context (highest cache hit rate)
lex4 up --ram-gb 76 --context 262144 # Custom RAM/context with automatic KV math
Live Per-Request Mode Dial (Zero Restarts)
Every request can choose its own spot on the Quality ↔ Speed ↔ NVMe curve on the fly — without reloading weights or restarting the server:
| Mode | Behavior | NVMe Traffic | Typical Speed (DGX Spark) | When to Use |
|---|---|---|---|---|
auto (proxy default) |
Routes quick logs, greetings, short Q&A & context compressions to fast; routes coding, math, tools & long prompts to balanced |
Low | 0.36–0.65s TTFT / 30–39 tok/s |
Everyday agents, Open WebUI, Hermes, Cursor |
fast (/fast <msg>) |
Resident-only rerouting, full CUDA graphs, zero SSD reads | 0.0 GB | 0.36–0.57s TTFT, 25–35 tok/s |
Instant replies, diary logs, background compressions |
balanced (/balanced <msg>) |
Warm adaptive background NVMe paging + full CUDA graphs | Light | 0.60–0.80s TTFT, 28–39 tok/s |
Daily coding, reasoning, multi-turn tool use |
exact (/exact or /ultra) |
Synchronous 12-slot O_DIRECT QD=4 exact miss-fetch waves (100% full-checkpoint parity, target-only eager bypass) |
Heavy | 5.2–9.8 tok/s |
Competition math, hard debugging, verification |
Using the Mode Dial
1. In any chat UI (Open WebUI, Hermes, Telegram, lex4 chat) — prefix your message:
/fast log my weight 185.2 lbs
/balanced write a thread-safe LRU cache in Python
/exact prove that there are infinitely many primes congruent to 3 mod 4
2. Via OpenAI API (http://127.0.0.1:8888/v1/chat/completions):
from openai import OpenAI
client = OpenAI(base_url="http://127.0.0.1:8888/v1", api_key="lex4-local")
resp = client.chat.completions.create(
model="GLM-5.3-Flash",
messages=[{"role": "user", "content": "Solve x^3 - 6x^2 + 11x - 6 = 0"}],
extra_body={"lex4_mode": "exact"}, # "fast" | "balanced" | "exact"
)
print(resp.choices[0].message.content)
Every response includes X-Lex4-Mode and X-Lex4-Mode-Source headers, and live routing telemetry is served at GET http://127.0.0.1:8888/lex4/stats.
Verified Benchmarks (1× NVIDIA DGX Spark, 128 GB)
All numbers below are direct measured runs on a single 128 GB DGX Spark (GLM-5.3-Flash-LEX4-3.5bpw, 133.2 GB MoE on disk, 40-task cold held-out suite across chat, tools, coding, and hard_math with zero prefix cache reuse between modes). Raw JSON artifacts live in benchmarks/results/.
| Preset (Resident RAM / Context) | Mode (Effort) | Chat | Tools | Coding | Cold Hard Math | Overall | Decode Speed | Avg TTFT | NVMe Read | Free RAM |
|---|---|---|---|---|---|---|---|---|---|---|
balanced (83 GB / 262K) |
fast (low) |
8/8 (100%) |
8/8 (100%) |
6/10 (60%) |
4/14 (28.6%) |
26/40 (65.0%) |
25.6 tok/s |
0.54 s |
0.0 GB |
9.9 GiB |
balanced (83 GB / 262K) |
balanced (high) |
8/8 (100%) |
8/8 (100%) |
9/10 (90%) |
7/14 (50.0%) |
32/40 (80.0%) |
28.3 tok/s |
0.76 s |
438 GB |
9.9 GiB |
balanced (83 GB / 262K) |
exact (high) |
— | — | — | 12/14 (85.7%) |
— | 8.6 tok/s |
3.47 s |
5.2 TB |
9.9 GiB |
low-ram (64 GB / 262K) |
fast (low) |
8/8 (100%) |
8/8 (100%) |
3/10 (30%) |
5/14 (35.7%) |
24/40 (60.0%) |
29.6 tok/s |
0.50 s |
0.0 GB |
31.5 GiB |
low-ram (64 GB / 262K) |
balanced (high) |
8/8 (100%) |
8/8 (100%) |
7/10 (70%) |
5/14 (35.7%) |
28/40 (70.0%) |
28.7 tok/s |
0.75 s |
658 GB |
31.5 GiB |
low-ram (64 GB / 262K) |
exact (high) |
— | — | — | 11/14 (78.6%) |
— | 5.2 tok/s |
6.11 s |
8.6 TB |
31.5 GiB |
max-quality (86 GB / 102K) |
fast (low) |
8/8 (100%) |
8/8 (100%) |
6/10 (60%) |
5/14 (35.7%) |
27/40 (67.5%) |
26.2 tok/s |
0.57 s |
0.0 GB |
11.3 GiB |
max-quality (86 GB / 102K) |
balanced (high) |
8/8 (100%) |
8/8 (100%) |
8/10 (80%) |
9/14 (64.3%) |
33/40 (82.5%) |
30.0 tok/s |
0.80 s |
559 GB |
11.4 GiB |
max-quality (86 GB / 102K) |
exact (high) |
— | — | — | 12/14 (85.7%) |
— | 8.6 tok/s |
3.92 s |
5.9 TB |
11.4 GiB |
Key takeaway: even at 64 GB (low-ram) with 31.5 GiB free OS RAM, flipping a single hard math prompt to /exact recovers 11/14 (78.6%, 0 truncations) on demand — a +42.9% jump over balanced on cold prompts!
How Lex4 Works Under the Hood
.lex4Sub-4-Bit Trellis-L14 Format (lex4/engine/format.py,codebooks.py):- Compresses
GLM-5.3-Flash's 328 GB of MoE experts to 133.2 GB (3.50 bpwaverage across 5 Hessian-allocated tiers) with< 1%perplexity delta at full residency (+0.27%code,+0.91%literary prose vs. NVFP4 reference). - Every expert payload is 4096-byte aligned on disk for zero-copy
O_DIRECTNVMe DMA.
- Compresses
- Hand-Tuned Blackwell SM121a Kernels (
lex4/engine/blackwell_mma.py):- Direct PTX
mma.m16n8k32+ zero-spill unified decode GEMV hitting 229–232.6 GB/s out of GB10's 234.1 GB/s physical DRAM ceiling (98.0%–99.4% speed of light), plus a zero-spill cooperative BF16 GEMV kernel for unquantized dense/drafter layers.
- Direct PTX
- Zero-Doorbell
O_DIRECTNVMe Pager (lex4/engine/pager.py,runtime.py):- Allocates RAM per layer via Shannon entropy water-filling (100% resident in unpredictable early layers, dynamically paged in spikier deep layers) and pins the 246 RTAQ termination experts that govern clean
</think>closure at0.00 GBextra RAM. - Uses
O_DIRECTlibc.preadwith QD=4 parallel staging + 12 rolling wave slots (13.12 GB/scold multi-file read — 10.5× faster than buffered I/O, +0 MiB OS swap, and 20.1 s total MoE boot load).
- Allocates RAM per layer via Shannon entropy water-filling (100% resident in unpredictable early layers, dynamically paged in spikier deep layers) and pins the 246 RTAQ termination experts that govern clean
- 2-Gear Adaptive Speculation (
lex4/adapt_gear.py):- Automatically shifts DFlash2 lookahead depth between
d=2(creative chat) andd=7(code, math, tools) with hysteresis, while keeping CUDA graphs intact.
- Automatically shifts DFlash2 lookahead depth between
CLI Reference
lex4 presets # Compare balanced (83GB), low-ram (64GB), max-quality (86GB)
lex4 doctor # Verify GPU, RAM, NVMe free space, Docker, and model cache
lex4 pull # Download .lex4 model weights (pass --no-drafter for commercial-clean)
lex4 up [OPTIONS] # Start single-container server (:8888 API, optional --with-webui :3000)
lex4 status # Show container health, live /lex4/stats routing counts, host RAM/swap
lex4 logs -f # Tail server logs
lex4 chat # Interactive streaming terminal chat with live tok/s & mode badge
lex4 bench # Run a 4-prompt live fast/balanced/exact smoke & speed check
lex4 down # Stop Lex4 server (and Open WebUI if started)
Quantizing Your Own MoE Model (Apache-2.0 Open Core)
Lex4's universal 2-stage cross-layer convex RD encoder (lex4/engine/encoder.py) is fully open-source under Apache-2.0 and auto-parses Hugging Face config.json across GLM, DeepSeek-V3/R1, Qwen3-MoE, Llama-4, Mixtral, and OLMoE:
- Stage 1 (
run_stage1_sensitivity): Single-probe empirical-Bayes Hessian sensitivity pass across all MoE layers. - Global Convex RD (
allocate_global_rd): ExactO(N log N)Pareto bit allocation across alllayers × experts × {gate_up, down}for any target bpw (2.5–3.8 bpw). - Stage 2 (
run_stage2_encode): Trellis-L14 Viterbi + block-LDL quantization with per-row FP16 scale refit and automatic GPU runtime self-verification.
Licensing & Commercial Use
Lex4 is designed to be crystal-clear for both individual builders / researchers and commercial teams:
| Component | License | Commercial Use |
|---|---|---|
Lex4 Engine, Kernels, .lex4 Format, Encoder, CLI & Proxy (this repo) |
Apache-2.0 | Yes |
Lex4-dev/GLM-5.3-Flash-LEX4-3.5bpw (derivative of NVIDIA GLM-5.3-Flash-FP4-Local / zai-org/GLM-5.3-Flash) |
NVIDIA Open Model Agreement | Yes |
Optional incoai/GLM-5.3-Flash-DFlash2 drafter (not redistributed by Lex4; fetched directly from HF) |
CC-BY-NC-ND-4.0 | No (personal / research only — pass --no-drafter for commercial deployments) |
See LICENSE and NOTICE for full details.
Roadmap
- Blackwell SM121a (
DGX Spark) — 98–99.4% DRAM roofline,O_DIRECTQD=4pager, livefast / balanced / exactmode dial, smart auto-routing proxy,lex4CLI & Docker stack - More
.lex4Frontier MoE Checkpoints — DeepSeek, Qwen-MoE, Kimi, Llama-4 - Commercial-Clean Speculative Decoding — native MTP / Apache-2.0 drafter support
- Apple Silicon (Metal / MLX) Backend — bringing
.lex4sub-4-bit MoE + NVMe paging to unified-memory Mac Studio / MacBook Pro - Multi-Spark Tensor & Pipeline Scaling — 600B–1T+ models across 2–4 Sparks
Acknowledgments
Lex4 builds on the work of an extraordinary open-source and research community:
- vLLM (Apache-2.0) — Host serving engine, V1
GPUModelRunner, modularFusedMoEinterface, andvllm.general_pluginsextension system. - eugr (
spark-vllm-b12x) — Community DGX Spark Blackwell container stack andb12xSM121a non-MoE kernels powering our Docker base image. - Z.ai / Zhipu AI (
GLM-5.3-Flash) (MIT License) — Original 320B MoE base model, tokenizer, and chat template. - NVIDIA (
GLM-5.3-Flash-FP4-Local& Model Optimizer) (NVIDIA Open Model Agreement) — Quantization-aware distilled checkpoint and non-MoE FP4 weights. - Inco AI & Z-Lab (
GLM-5.3-Flash-DFlash2) (GitHub,CC-BY-NC-ND-4.0) — Block-diffusion speculative drafter optionally pulled directly from Hugging Face for personal/research acceleration. - Open WebUI — Optional browser chat interface wired via
--with-webui. - Quantization & Systems Research:
- QuIP# & QTIP (Tseng et al., Cornell RelaxML) — Foundational Randomized Hadamard Transform (RHT), LDLQ, E8 lattice, and Trellis-Coded Quantization theory.
- ExLlamaV3 — Pioneered practical GPU trellis quantization (EXL3) for local LLMs.
- RTAQ (
arXiv:2611.37751) — Reasoning-Termination-Aware Quantization, inspiring our 246-expert termination pinning (rtaq_pin_experts.json). - Routes of Reasoning (
arXiv:2611.05919) — Inspiring our opt-in router steering hooks (lex4/engine/think_ctrl.py).
- PyTorch,
safetensors, and Hugging Face — Core runtime, serialization, and model distribution ecosystem.
Metadata
Release files for lex4 0.4.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| lex4-0.4.4.tar.gz | 178.7 kB | With provenance | Details for lex4-0.4.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| lex4-0.4.4-py3-none-any.whl | Python 3 | none | any | Details for lex4-0.4.4-py3-none-any.whl |
Total release size: 340.8 kB
Release files / lex4-0.4.4.tar.gz
| Download URL | lex4-0.4.4.tar.gz |
|---|---|
| Size | 178.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
22cef8bf1d954d87471baff5978b4925808d23d078f3c1305a1d8a4ea85d7ac8
|
|
BLAKE2b-256 checksum How to use checksums |
00a287bb7c76aa75ee289edc6a778776189ac43b2e8893ba338681507967cd17
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 5, 2026.
Transparency logRelease files / lex4-0.4.4-py3-none-any.whl
| Download URL | lex4-0.4.4-py3-none-any.whl |
|---|---|
| Size | 162.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d45e37c448c7d3ddde3b62e0ec73e395e7609bc9f2df0b459013782b5d6dd821
|
|
BLAKE2b-256 checksum How to use checksums |
a24323682a797b7c9fd578643e9bb641ca5f9f1d430b9d41ac74d35f6f317762
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 5, 2026.
Transparency logSecurity for lex4 0.4.4
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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for lex4-0.4.4.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 5, 2026.
Transparency logA 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.